我正在尝试计算文档频率(即每个单词出现在多少个文档中),例如:
Doc1:这款手机是有史以来最伟大的手机。
Doc2:您的电话号码是多少。
结果:
this 1
phone 2
is 1
the 1
ever 1
what's 1
your 1
number 1
我有以下 Java 代码
HashMap<String, String> wordDoc = new HashMap<String, String>();
HashMap<String, Integer> countDfIndex = new HashMap<String, Integer>();
if (!wordDoc.containsKey(word)) {
wordDoc.put(word,docno);
countDfIndex.put(word, 1);
}
if (wordDoc.get(word)!=null) {
if(!wordDoc.containsValue(docno)) {
wordDoc.put(word,docno);
countDfIndex.put(word, countDfIndex.get(word)+1);
}
}
我没有得到正确的结果,请帮忙!!
最佳答案
我假设您正在尝试计算包含相应单词的文档数量,而不是出现的总数。
如果是这样:
Map<String, Integer> countDfIndex = new HashMap<String, Integer>();
for (... document : documents) {
Set<String> alreadyAdded = new HashSet<String>(); // new empty set for each document
...
if (!alreadyAdded.contains(word)) {
if (!countDfIndex.containsKey(word) {
countDfIndex.put(word, 1);
} else {
countDfIndex.put(word, countDfIndex.get(word) + 1);
}
alreadyAdded.add(word); // don't add the word anymore if found again in the document
}
}
关于java - 使用 HashMap 计算文档频率 [Java],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36399399/