reworking of IntHash and HashMap examples

This commit is contained in:
Daniel Shiffman
2013-03-15 16:15:23 -04:00
parent d420268c25
commit c05cef2917
7 changed files with 7786 additions and 13612 deletions
@@ -7,12 +7,19 @@
* be used for this, however, this example uses the IntHash
* class offered by Processing's data package for simplicity
* and added functionality.
*
* This example uses the IntHash to perform a simple concordance
* http://en.wikipedia.org/wiki/Concordance_(publishing)
*
*/
// The next line is needed if running in JavaScript Mode with Processing.js
/* @pjs font="Georgia.ttf"; */
IntHash concordance; // HashMap object
// An IntHash pairs Strings with integers
IntHash concordance;
// The raw array of words in
String[] tokens;
int counter = 0;
@@ -23,8 +30,8 @@ void setup() {
// Load file and chop it up
String[] lines = loadStrings("dracula.txt");
String allText = join(lines, " ");
tokens = splitTokens(allText, " ,.?!:;[]-");
String allText = join(lines, " ").toLowerCase();
tokens = splitTokens(allText, " ,.?!:;[]-\"");
// Create the font
textFont(createFont("Georgia", 24));
@@ -35,13 +42,15 @@ void draw() {
fill(255);
// Look at words one at a time
String s = tokens[counter];
counter = (counter + 1) % tokens.length;
concordance.increment(s);
if (counter < tokens.length) {
String s = tokens[counter];
counter++;
concordance.increment(s);
}
// x and y will be used to locate each word
float x = 0;
float y = 100;
float y = 48;
concordance.sortValues();
@@ -54,7 +63,7 @@ void draw() {
// Only display words that appear 3 times
if (count > 3) {
// The size is the count
int fsize = constrain(count, 0, 100);
int fsize = constrain(count, 0, 48);
textSize(fsize);
text(word, x, y);
// Move along the x-axis
@@ -64,7 +73,7 @@ void draw() {
// If x gets to the end, move y
if (x > width) {
x = 0;
y += 100;
y += 48;
// If y gets to the end, we're done
if (y > height) {
break;
File diff suppressed because it is too large Load Diff