Sorting an array list of words based on frequency and finding its frequency rank

607 views Asked by At

I'm given a text file to read through. I can find the words in the text and file and their frequencies. I store this data in an ArrayList<HashEntry> where HashEntry consists of a key with the word and a value with the frequency. I sorted this ArrayList in decreasing frequency order using Collections.sort.

So I end up with something like this:

"a" 3, "a" 3, "a" 3, "now" 2, "days" 2, "everbody" 2, "wanna" 2, "they" 2,   
"they" 2, "their" 2, "just" 2, "now" 2, "days" 2, "talk" 1

I need to be able to produce something that will tell me the rank of occurrences. For example, "a" would have rank 1 because it appears the most. Everything that appears twice would have rank 2. "talk" would have rank 9 because there were 8 words that had a higher rank.

How would I go about doing this? I'm having several problems with what I've tried so far because there are duplicates in the ArrayList and the duplicates aren't necessarily sequential. I tried putting the data into a hash table by iterating through the ArrayList, where the key is the word, but I'm not sure how I could actually calculate the rank for the word.

for (int i = 0; i < ranked.size(); i++) { //ranked is my array list
            rankedht.put(ranked.get(i).getKey(),1); //rankedht is a hash table
        }

Doing that will just get me a hash table with the words and frequencies, but then that loses sort.

Thanks

0

There are 0 answers