Interactive Word Cloud Part 2

As promised, here is a quick walk through of creating a wordcloud automatically using WordCram.

Step 1: Install Processing and WordCram.

The full code I am using to generate wordclouds:

// This code was prepared using examples from wordcram.org
import wordcram.*;
import wordcram.text.*;
import java.awt.Shape;

size(1100, 900);

// Each Word object has its word, and its weight.  You can use whatever
// numbers you like for their weights, and they can be in any order.
Word name = new Word("Campbell",40); // 
name.setAngle(0);
name.setFont(createFont("Chancery URW",13));
Word[] wordArray = new Word[] {
     name,
     new Word("Passion", 20),
     new Word("Volunteering", 30),
     new Word("Canadian Coast Guard", 10),
     new Word("University of Cambridge", 15),
     new Word("Carleton University", 16),
     new Word("Computer Engineering", 7),
     new Word("Technology Policy", 7),
     new Word("Modeling", 8),
     new Word("Wine making", 5),
     new Word("Embedded Controllers", 5),
     new Word("Keyboards", 2),
     new Word("Baking",6),
     new Word("Wine Making", 4),
     new Word("Hobby Coding", 5),
     new Word("Analytics (Operations Research)",5),
     new Word("Consulting", 6),
     new Word("Public Policy", 5),
     new Word("Health Policy Development",3),
     new Word("Tanzania",3),
     new Word("Cycling", 5),
     new Word("Vintage Electronics",3),
     new Word("Leadership",15),
     new Word("Teaching", 20),
     new Word("Task Automation", 15),
     new Word("Open Hardware",7),
     new Word("Linux", 15),
     new Word("Emacs", 12),
     new Word("Frugal",15),
     new Word("Easygoing", 10),
     new Word("Engaging", 15),
     new Word("Mathematics", 7),
     new Word("Cambridge McKinsey Risk Prize",6),
     new Word("Recognized",5),
     new Word("Taiwan",3),
     new Word("Medical First Responder",8),
     new Word("Programming Languages", 15),
     new Word("Visualization",12)
  };

// Pass in the sketch (the variable "this"), so WordCram can draw to it.
try{
WordCram wordcram = new WordCram(this)

// Pass in the words to draw.
  .fromWords(wordArray)
  .withColors(color(20,93,133), color(0,166,242), color(80,87,89),color(173,176,179),color(217,14,0))
  .withAngler(Anglers.horiz())
  .withPlacer(Placers.centerClump())
  .toSvg("background.svg",1100,900)
  ;

wordcram.sizedByWeight(20,80);
wordcram.maxAttemptsToPlaceWord(5000);
// Now we've created our WordCram, we can draw it:
wordcram.drawAll();
for (Word word : wordcram.getSkippedWords()) {
     println(word.word + " skipped because " + word.wasSkippedBecause());
   }
}
catch (java.io.FileNotFoundException x) {
  println(x);
}

I drew heavily on the examples from WordCram for learning how to use their library. This code is based off of code I used about a year ago to do a report cover so I am not sure exactly which examples I drew from.

Going through the major items:

Line 6: The size of the image to be generated.
Lines 10-12: I create a ‘Word’ object for my name, most of the other words are created in an array below but I wanted my name to have a different font (line 12 — if you get an error it could be that you don’t have this font. Either comment it out or just replace it with a font you have).
Lines 13-52: The words I want in my cloud. The numbers correspond to a weighting that determines their relative size.
Lines 56-64: Creating the ‘WordCram’ object. This specifies the word array, an array of colors to use (in rgb), an ‘Angler’ (in this case making all words horizontal, a ‘Placer’ (in this case clumping the words in the centre) and a specifier indicating that I want to export to svg.
Line 66: Sets the minimum and maximum sizes for the words.
Lines 67-73: Actually drawing the wordcloud. It will print a message in the processing log if it is unable to place a word. If words aren’t being placed, either lower your word size or increase the size of your cloud.

When you run the sketch, it will save an svg in the current directory with the filename set on line 63. It may also pop up a blank canvas on your screen, you can close this.

If you don’t like the cloud it generates, you can run it again and will get a different cloud. Otherwise play with the settings, full documentation can be found at the WordCram JavaDoc site.

Here is an example of the output of the code above:
Example WordCloud

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>