繁体   English   中英

Word频率计数器问题与逻辑Java

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

您可以使用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));
        }
}

您使事情变得过于复杂。

您只需要几行:

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;
}

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

  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 

我认为要运行得更快,您应该使用从列表排序开始的另一种算法:

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

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

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;
}

我将这样进行:

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,您可以轻松地增加频率计数器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM