ファイルの各行の単語の頻度を知りたいです。ファイル内のすべての単語に対してこれを行いたいです。JavaでBufferedReaderとFileReaderを使用しています。
1 に答える
2
次の 2 点をお勧めします。
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 */
于 2011-02-18T16:10:41.790 に答える