Hi,
I've been doing some on and off Processing stuff over the last year and thoroughly enjoyed it. Fantastic program. But this one has got me stumped.
I wanted to make an array of all the words used in a passage of text, but with no duplicates. So if "hello" was used in the first sentence and in the third, it would only be recorded from the first sentence and ignored the second time. A bit like making a dictionary. Eventually I would like to rank them to find the most popular word.
I'm not sure how you would approach this but i'm using an array of all words in the passage, plus another array to store each word and check if it hasn't appeared before, and then looping through the arrays using for loops.
It works fine using an array of words i write in the program. But if i load the text from an external source it seems to add the word regardless of whether it has appeared before. The program runs fine and there are no errors.
below is the code and it loads the text from gutenberg.org, but shortens the array to only 50 words so you don't have to wait too long for the results! you can comment out those lines in the setup() part and uncomment the words //= {"hello"... to try it with an internal array.
Any help is greatfully received.
Code:
String delimiters = " ,.?!;:[]";
String[] words;// = {"hello", "my", "name", "is", "david", "and", "david", "is", "my", "name" };
String[] checks = new String[0];
int counter = 1509;
String url = "http://www.gutenberg.org/dirs/etext97/1ws3310.txt";
int c=0;
void setup() {
String[] rawText = loadStrings(url);
String joinedText = join(rawText, "" );
words = splitTokens(joinedText, delimiters);
words = subset(words, 1509, 50);
checks = append(checks, words[0] );
frameRate(5);
}
void draw() {
println("running " + c + ", " + words.length + ", " + checks.length);
String theWord = words[c];
println(theWord);
println("///");
for(int j=0; j<checks.length; j++) {
String theCheck = checks[j];
println(theCheck);
if(theCheck == theWord) {
println("break");
break;
} else if((theCheck != theWord) & (j== checks.length-1)) {
println(theWord + ", " + theCheck);
checks = append(checks, theWord);
}
}
c++;
if(c == words.length) {
println("//////");
println(checks);
println(words.length);
println(checks.length);
noLoop();
}
}