so i found this great code that parses through a text file and counts the amount of times all the words show up.
i'm trying to modify the code so that it allows me to just get the value of certains words.
like
happy:1
sad:4
so that i can extract those int values as variables.
here's the code
String lines[];
String list[];
void setup() {
lines = loadStrings("http://api.wefeelfine.org:8080/ShowFeelings?display=text&returnfields=feeling,conditions&limit=50");
println("there are" + lines.length + "lines");
Words words = new Words();
for (int i=0; i < lines.length; i++) {
String list[] = splitTokens(lines[i]);
for (int j=0; j < list.length; j++) {
words.addWord(list[j]);
}
}
words.printWords();
}
class Words {
Hashtable ht=new Hashtable();
void addWord(String txt) {
Word word;
if (ht.containsKey(txt)) {
word = (Word)ht.get(txt);
word.count++;
} else {
word = new Word(txt);
}
ht.put(txt,word);
}
void printWords() {
for (Enumeration e = ht.keys() ; e.hasMoreElements()
{
String name = e.nextElement().toString();
Word word = (Word)ht.get(name);
println(word.txt + ":" + word.count);
}
}
}
class Word {
public String txt;
public int count = 1;
Word(String txt) {
this.txt = txt;
}
}