Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Function WordCloud can take such options as WordOrientation and ColorFunction. It is not obvious how those word-stylings can depend on words themselves, for example semantic meaning of words or their linguistic (structural) properties. Is this possible at all?


NOTE: Originally posted on Wolfram Community. I am following @Mr.Wizard recommendation to make it a self-Q&A

share|improve this question
up vote 10 down vote accepted

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

share|improve this answer
1  
This is really cool! Thank you for sharing. Please excuse me while I go play with this now :-) – MarcoB Feb 27 at 22:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.