简体   繁体   English

字数统计功能可对txt文件中的字进行计数

[英]Word count function that counts words in a txt file

I'm very new to java here so please bear with me. 我对Java很陌生,所以请多多包涵。

I'm currently trying to create code that does the following: 我目前正在尝试创建执行以下操作的代码:

  1. Add code to your processFile function that counts the number of times each word appears in the file. 将代码添加到您的processFile函数中,以计算每个单词在文件中出现的次数。

  2. Add code to your processFile function that loops through your HashMap to find the most frequent word. 将代码添加到您的processFile函数中, processFile函数循环遍历HashMap以查找最常用的单词。 After your loop, the variable added for bonus requirement #1 should contain the value for the most frequent word. 循环后,为奖金要求#1添加的变量应包含最常用单词的值。

So far I've come up with this and was wondering if anyone could please help me progress further. 到目前为止,我已经提出了这个建议,并且想知道是否有人可以帮助我进一步发展。

        Map<String, Integer> freq = new Hashmap<String, Integer>();
        FileInputStream fi = new FileInputStream("readwords,txt");
        Scanner input = new Scanner(fi);
        while (input.hasNext()) {
        String word = input.next().toLowerCase();
        Integer f = freq.get(word);
            if (f == null) {
                freq.put(word,1);
            }
            else { 
                freq.put(word,f+1);
            }
        }

Thank you 谢谢

Your syntax is close, but you've mixed String declaration styles, your generic type is missing a > and your variable names are inconsistent. 您的语法接近,但是您混合使用了String声明样式,您的泛型类型缺少一个>并且您的变量名称不一致。 I think you wanted something like, 我想你想要类似的东西,

Map<String, Integer> map = new HashMap<>();
File file = new File("readwords.txt");  
try (Scanner input = new Scanner(file)) {
    while (input.hasNext()) {
        String word = input.next().toLowerCase();
        Integer f = map.get(word);
        if (f == null) {
            map.put(word, 1);
        } else {
            map.put(word, f + 1);
        }
    }
} catch (FileNotFoundException fnfe) {
    fnfe.printStackTrace();
}

For counting the words and for getting most frequently used word you can try this: 要计算单词数并获取最常用的单词,您可以尝试以下操作:

   public void processFile() throws Exception {
        Map<String, Integer> freq = new HashMap<>();
        FileInputStream fi = new FileInputStream("readwords.txt");
        String mostFreqWord = null;
        Integer highestFreq = 0;
        Scanner input = new Scanner(fi);
        while (input.hasNext()) {
            String word = input.next().toLowerCase();
            Integer f = freq.get(word) == null ? 1 : freq.get(word) + 1;

            freq.put(word, f);

            if(f > highestFreq) {
                mostFreqWord = word;  // set most frequent word
                highestFreq = f;      // frequency of most frequent word
            }

        }

        System.out.println("Word :" + mostFreqWord
                + " is the most frequent word with frequency:" + highestFreq);
    }

Since I have modified the code you already posted, here is the explanation of modification that I did (I assume that you already know what your original code was doing). 因为我已经修改了您已经发布的代码,所以这里是我所做的修改的说明(我假设您已经知道您的原始代码在做什么)。

Inside loop, below line checks if word word has encountered first time in loop, if yes then sets it's frequency as 1 otherwise it increments frequency for that word. 在循环内部,下面的行检查单词word是否在循环中第一次遇到,如果是,则将其频率设置为1,否则将增加该单词的频率。

Integer f = freq.get(word) == null ? 1 : freq.get(word) + 1;

Then it sets latest frequency for the word: freq.put(word, f); 然后,它设置单词的最新频率: freq.put(word, f);

Statement if(f > highestFreq) checks if the highest frequency is still highest, if not then updates highestFreq and mostFreqWord words. 语句if(f > highestFreq)检查最高频率是否仍然最高,如果不是,则更新highestFreqmostFreqWord字。

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

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