#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# calculate specific word frequency in text

edited November 2013

Using: Processing 2.1, Mac OSX 10.8.5

Starting point: Daniel Shiffman's example on Analyzing King Lear http://www.learningprocessing.com/examples/chapter-18/example-18-6/

This works from a text file - calculates and displays the instance of each word in the document. What I want is for the program to find one specific word (for example 'considers') and tell me how many times it occurs in the text (only that word).

Here is my code so far. I have played a bit with HashMap but I'm lost on how to apply it properly or if its the correct approach.

``````String[] november;
int counter = 0;

String delimiters = ",.?!;:[]- ";

void setup() {
size (900,200);
String everything = join(rawtext, "");
november = splitTokens(everything, delimiters);
frameRate (1);
}

void draw() {
background (255);

String considers = november[counter];
int total = 0;
for (int i = 0; i <november.length; i ++ ) {
if (considers.equals (november[i])) {
total ++;
}
}

fill (0);
text (considers, 10, 90);
text (total, 10, 110);
stroke (0);
fill (175);
rect (10, 50, total/4, 20);

counter = (counter + 1) % november.length;
}
``````

• edited November 2013 Answer ✓

Untested:

code edited as megwells pointed [removed text(considers)]

``````String[] november;
String keyWord = "considers";
int counter = 0;
int total = 0;

String delimiters = ",.?!;:[]- ";

void setup() {
size (900, 200);
String everything = join(rawtext, " ");
november = splitTokens(everything, delimiters);
noLoop();
}

void draw() {
background (255);

for (int i = 0; i <november.length; i ++ ) {
if (november[i].equals (keyWord)) {
total ++;
}
}

fill (0);
text (total, 10, 110);
stroke (0);
fill (175);
rect (10, 50, total/4, 20);
}
``````
• Thank-you! It works! and is elegant to boot!

(note to others with a similar query: remove the text (considers, 10, 90); line)