How to sort the words by their frequency

a 夏天 提交于 2019-12-11 07:14:53

问题


I take an input text file, convert it to an array, sort the array, and then get the frequencies of each word. I can't figure out how to sort them according to their frequencies, from highest to lowest, without importing lots of things (which is what I am trying to do):

//find frequencies
    int count = 0;
    List<String> list = new ArrayList<>();
    for(String s:words){
        if(!list.contains(s)){
            list.add(s);
        }
    }
    for(int i=0;i<list.size();i++){
        for(int j=0;j<words.length;j++){
            if(list.get(i).equals(words[j])){
                count++;
            }
        }

        System.out.println(list.get(i) + "\t" + count);
        count=0;
    }

This returns the words with their frequencies in an unsorted order, for example:

the 3
with 7
he 8

etc.

I want this to be sorted like:

he 8
with 7
the 3

回答1:


I implemented it like so,

private static class Tuple implements Comparable<Tuple> {
    private int count;
    private String word;

    public Tuple(int count, String word) {
        this.count = count;
        this.word = word;
    }

    @Override
    public int compareTo(Tuple o) {
        return new Integer(this.count).compareTo(o.count);
    }
    public String toString() {
        return word + " " + count;
    }
}

public static void main(String[] args) {
    String[] words = { "the", "he", "he", "he", "he", "he", "he", "he",
            "he", "the", "the", "with", "with", "with", "with", "with",
            "with", "with" };
    // find frequencies
    Arrays.sort(words);
    Map<String, Integer> map = new HashMap<String, Integer>();
    for (String s : words) {
        if (map.containsKey(s)) {
            map.put(s, map.get(s) + 1);
        } else {
            map.put(s, 1);
        }
    }
    List<Tuple> al = new ArrayList<Tuple>();
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        al.add(new Tuple(entry.getValue(), entry.getKey()));
    }
    Collections.sort(al);
    System.out.println(al);
}

Output is,

[the 3, with 7, he 8]



回答2:


I would suggest using a small helper class:

class WordFreq implements Comparable<WordFreq> {
   final String word;
   int freq;
   @Override public int compareTo(WordFreq that) {
     return Integer.compare(this.freq, that.freq);
   }
}

Build an array of instances of this class, one for each word, then sort the array using Arrays.sort.




回答3:


You should create an object of type Word that holds the word's String value and its frequency.

Then you can implement compareTo or use a Comparator and call Collections.sort() on your list of type Word




回答4:


Use a Map<String, Integer> instead to store both your String as key and the frequency as value, with initial value of 1. If the word already exists, just update the value by increasing it by 1. Then, convert this map into a Map<Integer, List<String>> (or a Guava Multimap) and use the Integer values as keys and the String keys to store them as values.



来源:https://stackoverflow.com/questions/22515889/how-to-sort-the-words-by-their-frequency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!