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