# Finding wordcount/frequency with a given text file

**URL:** https://discourse.processing.org/t/finding-wordcount-frequency-with-a-given-text-file/16178
**Category:** Coding Questions
**Tags:** homework
**Created:** [December 6, 2019, 1:59pm UTC](https://discourse.processing.org/t/finding-wordcount-frequency-with-a-given-text-file/16178 "2019-12-06T13:59:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bc15](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bc15](https://discourse.processing.org/u/bc15)
#### Post date: [December 6, 2019, 1:59pm UTC](https://discourse.processing.org/t/finding-wordcount-frequency-with-a-given-text-file/16178/1 "2019-12-06T13:59:07Z")

</div>

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 ☹

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.

```auto
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);
  }
}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 6, 2019, 2:02pm UTC](https://discourse.processing.org/t/finding-wordcount-frequency-with-a-given-text-file/16178/2 "2019-12-06T14:02:15Z")

</div>

please format your code posting by pasting it into the

```auto
</> 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,

> **[IntDict / Reference](https://processing.org/reference/IntDict.html)**
>
> A simple class to use a String as a lookup for an int value. String "keys" are associated with integer values.

---

<div class="post-metadata">

### Author: ![bc15](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bc15](https://discourse.processing.org/u/bc15)
#### Post date: [December 6, 2019, 2:09pm UTC](https://discourse.processing.org/t/finding-wordcount-frequency-with-a-given-text-file/16178/3 "2019-12-06T14:09:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [December 6, 2019, 7:27pm UTC](https://discourse.processing.org/t/finding-wordcount-frequency-with-a-given-text-file/16178/4 "2019-12-06T19:27:46Z")

</div>

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?
