java progem查找单词的分布数组

发布于 2024-10-18 02:30:20 字数 77 浏览 5 评论 0原文

我想找到文件每一行中某个单词的频率。我想对文件中的每个单词执行此操作。我在java中使用BufferedReader和FileReader。

I want to find the frequency of a word in each line of a file. I want to do this for every word in the file. I'm using BufferedReader and FileReader in java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-10-25 02:30:20

我建议两件事:

1)更好地分解你的问题。
您是否想找出每行中单词的频率?或者文件中单词的频率。例如,如果您的输入是:

test test dog cat dog dog cat
test cat dog dog cat test

您想要:

test: 2, 1
dog: 3, 2
cat: 2, 2

或者您想要

test: 3
dog: 5
cat: 4

2) 这是您需要的工具

Map<String,Integer> wordCounts; // store your word counts in your class

/**
 * countWord- a method to signify counting a word
 * @param word the word just counted
 */   
public void countWord(String word) {
    int counts = 0; // assume it does not exist yet
    if(wordCounts.containsKey(word)) { // but if it does, get that count
        counts = wordCounts.get(word);
    } /* end if(contains(word))*/
    wordCounts.put(word,counts + 1); // tell the map to put one more than there was
} /* end countWord */

I recommend two things:

1) break down your question better.
Are you trying to find the frequency of words in each line? Or the frequency of words in the file. As in, if your input was :

test test dog cat dog dog cat
test cat dog dog cat test

Would you want:

test: 2, 1
dog: 3, 2
cat: 2, 2

Or would you want

test: 3
dog: 5
cat: 4

2) here's the tools you need

Map<String,Integer> wordCounts; // store your word counts in your class

/**
 * countWord- a method to signify counting a word
 * @param word the word just counted
 */   
public void countWord(String word) {
    int counts = 0; // assume it does not exist yet
    if(wordCounts.containsKey(word)) { // but if it does, get that count
        counts = wordCounts.get(word);
    } /* end if(contains(word))*/
    wordCounts.put(word,counts + 1); // tell the map to put one more than there was
} /* end countWord */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文