8
$\begingroup$

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

$\endgroup$
10
$\begingroup$

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]

http://community.wolfram.com//c/portal/getImageAttachment?filename=2016-02-24_16-46-42.png&userId=11733

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]

http://community.wolfram.com//c/portal/getImageAttachment?filename=dfgerg54678hgfd.png&userId=11733

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]

http://community.wolfram.com//c/portal/getImageAttachment?filename=sdwefagdcvbgfshj768tqewdsa.png&userId=11733

$\endgroup$
  • 1
    $\begingroup$ This is really cool! Thank you for sharing. Please excuse me while I go play with this now :-) $\endgroup$ – MarcoB Feb 27 '16 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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