Message Boards Message Boards


WordCloud: orientation & color as word-functions

Posted 4 years ago
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["", "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:

    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

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

    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
or Discard

Group Abstract Group Abstract