Skip to content

Instantly share code, notes, and snippets.

@danbernier
Last active November 16, 2015 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danbernier/6be904ca0bc3e0d79498 to your computer and use it in GitHub Desktop.
Save danbernier/6be904ca0bc3e0d79498 to your computer and use it in GitHub Desktop.
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("http://nytimes.com");
wordCram.drawAll();
}
void draw() {}
void wordsCounted(Word[] words) {
for (Word word : words) {
word.setProperty("rawCount", word.weight);
}
}
void mouseClicked() {
Word w = wordCram.getWordAt(mouseX, mouseY);
print(w);
println(" Raw count: " + w.getProperty("rawCount"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment