Q
从一组文档
0
A
回答
0
是更好地为您手动选择那些300个字(它不是这么多,是一个时间) - 编写的代码在Python 3
import os
files = os.listdir()
topWords = ["word1", "word2.... etc"]
wordsCount = 0
for file in files:
file_opened = open(file, "r")
lines = file_opened.read().split("\n")
for word in topWords:
if word in lines and wordsCount < 301:
print("I found %s" %word)
wordsCount += 1
#Check Again wordsCount to close first repetitive instruction
if wordsCount == 300:
break
-1
import os
import operator
from collections import defaultdict
files = os.listdir()
topWords = ["word1", "word2.... etc"]
wordsCount = 0
words = defaultdict(lambda: 0)
for file in files:
open_file = open(file, "r")
for line in open_file.readlines():
raw_words = line.split()
for word in raw_words:
words[word] += 1
sorted_words = sorted(words.items(), key=operator.itemgetter(1))
现在就顶300从排序的话,他们是你想要的话。
0
最简单有效的方法申请最重要的词的TF-IDF实现。如果您有停用词,您可以在应用此代码之前过滤停用词。希望这对你有用。
import java.util.List;
/**
* Class to calculate TfIdf of term.
* @author Mubin Shrestha
*/
public class TfIdf {
/**
* Calculates the tf of term termToCheck
* @param totalterms : Array of all the words under processing document
* @param termToCheck : term of which tf is to be calculated.
* @return tf(term frequency) of term termToCheck
*/
public double tfCalculator(String[] totalterms, String termToCheck) {
double count = 0; //to count the overall occurrence of the term termToCheck
for (String s : totalterms) {
if (s.equalsIgnoreCase(termToCheck)) {
count++;
}
}
return count/totalterms.length;
}
/**
* Calculates idf of term termToCheck
* @param allTerms : all the terms of all the documents
* @param termToCheck
* @return idf(inverse document frequency) score
*/
public double idfCalculator(List allTerms, String termToCheck) {
double count = 0;
for (String[] ss : allTerms) {
for (String s : ss) {
if (s.equalsIgnoreCase(termToCheck)) {
count++;
break;
}
}
}
return 1 + Math.log(allTerms.size()/count);
}
}
+0
谢谢@shiv。但是我已经实现了Tf-Idf,并且我使用Lucene来实现(为了更快的处理)。问题是Tf-Idf为每个文档提供“重要条款”,而不是整套文档。 – Vijender
相关问题
- 1. Applescript归档一组文件
- 2. 从一组文档中找到最相似的文档(最近的邻居)
- 3. 将组合框从Word文档复制到另一个Word文档使用vba
- 4. 从另一个文档(),xslt
- 5. 从一个字节数组创建一个XPS文档
- 6. 如何从文档数组中删除子文档<condition>?
- 7. 从mongodb中删除文档数组中的完整文档
- 8. 从属于子文档数组的子文档中提取
- 9. 将子文档添加到同一文档中的数组中
- 10. 签名PDF文档从字节数组
- 11. C#:从bson文档检索数组值
- 12. 从类型数组中添加一项到PouchDB文档属性
- 13. 从文档目录加载图像到一个数组
- 14. 无法从Javascript删除文档中的一组节点
- 15. 一个文档内替换数组值
- 16. JAXBException,解组一个XML文档
- 17. 通过一组对象查找文档
- 18. JVCL组件文档
- 19. 如何从另一个文档的子集制作新文档?
- 20. Arangodb从文档
- 21. 从文档
- 22. 从DocBook文档
- 23. 在一个数组中组织一个PHP Zip归档文件
- 24. 从SharePoint 2010文档库检索文档
- 25. 从文档库打印多文档?
- 26. 从SharePoint文档库打开文档
- 27. VB将宏从一个文档复制到另一个文档的宏
- 28. PHP DOMDocument将节点从一个文档移动到另一个文档
- 29. PHP将xml节点从一个文档复制到另一个文档
- 30. 复制合并从VB.net中的一个word文档到另一个word文档
谢谢@Awaish,但我也试过这个。这种方法的结果很差,因为重要的术语只出现一次或两次。如果我尝试根据频率对Tf-idf术语进行排序和选择,会出现许多常见和不相关的术语。 – Vijender