0

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

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”,但如果这样做,我会遇到其他问题,例如重复添加单词。我的逻辑在这里吗?这个项目会有更好的方法吗?提前致谢!

4

6 回答 6

2

你可以使用类的频率方法来做到这一点Collections

这是一个示例:

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));
        }
}
于 2013-01-12T22:37:19.440 回答
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;
}
于 2013-01-12T22:52:28.053 回答
0

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

  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
    
于 2013-01-12T22:36:32.153 回答
0

我认为要运行得更快,您应该使用另一种算法,首先对列表进行排序:

  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 列表。

于 2013-01-12T22:38:09.953 回答
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;
}
于 2013-01-12T22:38:46.430 回答
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,您可以轻松地增加频率计数器。

于 2013-01-12T22:51:31.840 回答