I have a decent idea on how to visualize it with a box plot as it was practiced during class, but I’m having trouble calculating the frequency of each words by modifying the given code
- Read in the text file.
- Use the provided code to loop through all the text and store each different word in the array.
- Modify the code to count the frequency of each word.
- Draw a boxplot with the words and their frequency.
- Challenge: Use a different text file, consider using project gutenberg.
- Challenge: Create a visualization where the size of each word corresponds to its frequency.
void setup() {
size(500, 500);
WordCount wc = new WordCount();
String[] data = loadStrings("gettysburg-address.txt");
for (int i = 0; i < data.length; i++) {
String[] row = splitTokens(data[i]);
for (int j = 0; j < row.length; j++) {
String s = row[j];
wc.add(new Word(s));
}
}
wc.display();
}
class Word {
String s;
int freq;
Word(String s) {
freq = 1;
this.s = s.toLowerCase().replaceAll("\\W", "");
}
String toString() {
return s + ":" + freq;
}
} class WordCount {
Word[] words = new Word[0];
// Return the index of word in the array,
// return -1 if it does not exist int find(String word) { for (int i = 0; i < words.length; i++) { if (words[i].s.equals(word)) { return i; } } return -1; } //Add s to the word count
void add(Word w) {
if (find(w.s) == -1) {
words = (Word[])append(words, w);
}
}
void display() {
println(words);
}
}