Message Boards Message Boards

GROUPS:

WordCloud: orientation & color as word-functions

Posted 4 years ago
3003 Views
|
1 Reply
|
10 Total Likes
|

WordCloud a recent function can do some neat tricks beyond the obvious. For example:

  • Wrappers such as Style and Rotate passed to individual words will be preserved in the WordCloud
  • These wrappers can be functions of words, for example their semantics or linguistics

Let's consider a list of top ranked baby names in 2015:

page = Import["http://www.babycenter.com/popularBabyNames.htm?year=2015", "Data"];

data = Flatten[{{#2, 1./#1}, {#3, 1./#1}} & @@@Cases[page, {_Integer, ___String}, Infinity][[All, 1 ;; 3]], 1]

enter image description here

The simple code below will color and orient words differently depending on the gender deduced by machine learning function Classify:

WordCloud[{
    If[Classify["NameGender", #] === "Male",
     Rotate[Style[#1, Brown], Pi/4],
     Rotate[Style[#1, Pink], -Pi/4]], #2} & @@@ data]

enter image description here

Quite impressive! But let's hook orientation and color to different variables. Color will still be gender, semantics, but let's link orientation with, say, length of the name. The longer the name the steeper the slope between $0$ and $2 \pi$. We need to find the range of lengths of words:

rng = StringLength /@ data[[All, 1]] // MinMax
(*{5,11}*)

and now with slight modification of code we get some beautiful and meaningful flow:

WordCloud[{
    If[Classify["NameGender", #1] === "Male",
     Rotate[Style[#1, Red], Pi/2 Rescale[StringLength[#1], rng]],
     Rotate[Style[#1, Blue], Pi/2 Rescale[StringLength[#1], rng]]], #2} & @@@ data]

enter image description here

I didn't know you could customize it so much! very cool!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract