逻辑java的字频率计数器问题

时间:2013-01-12 22:24:12

标签: java

我正在建立一个基本的词频计数器。代码如下:

public static List<Frequency> computeWordFrequencies(List<String> words) 
{
    List<Frequency> list_of_frequency = new ArrayList<Frequency>();
    List<String> list_of_words = words;
    int j = 0;
    for(int i=0; i<list_of_words.size(); i++)
    {

        String current_word = list_of_words.get(i);
        boolean added = false;
        if(list_of_frequency.size() == 0)
        {
            list_of_frequency.add(new Frequency(current_word, 1));
            System.out.println("added " + current_word);
        }
        else
        {

            System.out.println("Current word: " + current_word);
            System.out.println("Current Frequency: " + list_of_frequency.get(j).getText());
            if(list_of_frequency.contains(current_word))
            {
                list_of_frequency.get(j).incrementFrequency();
                System.out.println("found... incremented " + list_of_frequency.get(j).getText() + " frequency");
                added = true;
            }
            else
            {
                list_of_frequency.add(new Frequency(current_word, 1));
                System.out.println("added " + current_word);
                added = true;
            }
        }
    }
}

我得到的输出是:

added I
Current word: am
Current Frequency: I
added am
Current word: very
Current Frequency: I
added very
Current word: good
Current Frequency: I
added good
Current word: at
Current Frequency: I
added at
Current word: being
Current Frequency: I
added being
Current word: good
Current Frequency: I
added good
Total item count: 7
Unique item count: 7
I:1
am:1
very:1
good:1
at:1
being:1
good:1

所以我需要一个for循环来循环遍历“list_of_frequency”但如果我这样做,我会遇到其他问题,例如重复添加单词。我的逻辑是否正确,是否会有更好的方式来进行这个项目? 提前谢谢!

6 个答案:

答案 0 :(得分:2)

您可以使用Collections

frequency方法执行此操作

这是一个示例:

public void wordFreq(){
String text = "hello bye hello a bb a bye hello";

        List<String> list = Arrays.asList(text.split(" "));

        Set<String> uniqueWords = new HashSet<String> (list);
        for (String word : uniqueWords) {
            System.out.println(word + ": " + Collections.frequency(list, word));
        }
}

答案 1 :(得分:1)

你的事情过于复杂。

你只需几行:

public static Map<String, Integer> getFrequencies(List<String> words) {
    Map<String, Integer> freq = new HashMap<String, Integer>();
    for (String word : words) {
        Integer i = freq.get(word);
        freq.put(word, i == null ? 1 : i + 1);
    }
    return freq;
}

答案 2 :(得分:0)

在其他部分中添加此代码。 你应该做的是

  1. 在循环中检查单词是否已在列表中
  2. 如果第1部分为真,则只增加其频率
  3. 否则将其放入频率为1的列表中

    for(j = 0; j < list_of_frequency.size; j++)
       if(list_of_frequency.get(i).getText().equals(current_word))
          list_of_frequency.get(i).frequency++; // increment frequency 
                                  //if word is already encountered before
    

答案 3 :(得分:0)

我认为要跑得更快,你应该使用另一种算法,首先是对List进行排序:

  1) sort your list of string (cf. java.util.Collections.sort())
  2) in pseudo code :
 iterate your sorted list
 current_word = word of current iteration
 if it's a new word (! current_word.equals( oldWord) )
 counter = 1
 if (current_word.equals( oldWord)) {
    counter++
     store current_word in variable oldWord 
 }
 when the word change create your Frequency(oldWord, counter) and add to the list of frequencies

因此,您不需要每次检查频率列表,只需为一个单词插入一次,就会更快。

由于列表list_of_frequency的所有条目都是唯一的单词,因此您也可以使用Set而不是list_of_frequency的列表。

答案 4 :(得分:0)

用此替换您的方法。在分析数据时使用地图可以获得更好的性能。

public static List<Frequency> computeWordFrequencies(List<String> words) {
    Map<String, Integer> counts = new HashMap<String, Integer>();
    for(String word : words) {
        Integer current = counts.get(word);
        if(current != null) {
            counts.put(word, current+1);
        }
        else counts.put(word, 1);
    }

    // Then, if you really need that list of Frequency
    List<Frequency> list_of_frequency = new ArrayList<Frequency>();

    for(String s : counts.keySet()) {
        list_of_frequency.add(new Frequency(s, counts.get(s)));
    }

    return list_of_frequency;
}

答案 5 :(得分:0)

我会这样做:

List<String> words = Arrays.asList("foo", "bar", "qux", "foo");

Map<String, AtomicInteger> frequencyMap = new HashMap<String, AtomicInteger>();
for (String word : words)
{
    AtomicInteger freq = frequencyMap.get(word);
    if (freq == null) {
        frequencyMap.put(word, new AtomicInteger(1));
    }
    else
    {
        freq.incrementAndGet();
    }
}

for (String word : frequencyMap.keySet())
{
    System.out.println(word + " :" + frequencyMap.get(word));
}

使用AtomicInteger,您可以轻松增加频率计数器。