Skip to content

Instantly share code, notes, and snippets.

Last active November 16, 2015 14:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
WordCram Processing sketch example for getting raw word count. Total hack!
import wordcram.*;
This takes advantage of two nice WordCram features, and one accident of implementation:
- Feature 1: you can set and get any properties you want on your Words
- Feature 2: you can implement `void wordsCounted(Word[] words)` in your sketch,
and it'll be called after the words are counted up
WordCram normalizes the Word weights, so the most-frequent Word has a weight of 1, and
the least-frequent has near-zero. The accident-of-implementation is that `wordsCounted` is
called BEFORE the weights are normalized: at that point, each Word's current weight is
actually its raw count. So set that as a property, and pull it out later!
What this really reveals is that each Word should know its own raw count. That can easily
be made into a method on Word. But here's a hack to avoid the wait.
WordCram wordCram;
void setup() {
size(900, 500);
wordCram = new WordCram(this).fromWebPage("");
void draw() {}
void wordsCounted(Word[] words) {
for (Word word : words) {
word.setProperty("rawCount", word.weight);
void mouseClicked() {
Word w = wordCram.getWordAt(mouseX, mouseY);
println(" Raw count: " + w.getProperty("rawCount"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment