Finding wordcount/frequency with a given text file

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 :frowning:

  1. Read in the text file.
  2. Use the provided code to loop through all the text and store each different word in the array.
  3. Modify the code to count the frequency of each word.
  4. Draw a boxplot with the words and their frequency.
  5. Challenge: Use a different text file, consider using project gutenberg.
  6. 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);
  }
}
1 Like

please format your code posting by pasting it into the

</> code button

of the editor header menu ( context name: Preformatted text )
it looks like
```
type or paste code here
```

also can use the ``` manually above and below your code.

thank you.


also a topic title as

Help With Processing assignment

will not increase the chance to get support.
and think about if thousand people per month check this forum and
see that title???

there is no real question in the title and the whole post


there is a very special type of array to do most of that
job automatically,

1 Like

intDict is what i automatically thought to use but the assignment specifically asks to accomplish without using it, which was my reason for struggling.

You already have that freq variable in your word class, and i don‘t think you use it, other than to Print it.

Why not increment that every time you add that word to your wordcount?

Also, why is your find() method commented out?

1 Like