資源簡(jiǎn)介
包含課題的python源碼,實(shí)驗(yàn)報(bào)告以及測(cè)試數(shù)據(jù)。
對(duì)于給定文本庫(kù),用戶提交檢索關(guān)鍵詞(例如: NBA, basket, ball),在文本庫(kù)中查詢與檢索關(guān)鍵詞最相關(guān)的 k 個(gè)文本(例如 k=5),并根據(jù)文本與檢索關(guān)鍵詞的相關(guān)度,對(duì)這 k 個(gè)文本進(jìn)行排序,將排序后的結(jié)果返回給用戶。
使用TF-IDF權(quán)值衡量關(guān)鍵詞對(duì)于某篇文章的重要性,從而根據(jù)關(guān)鍵詞挑選出相關(guān)性較高的文本。首先程序加載文本庫(kù),并對(duì)數(shù)據(jù)進(jìn)行處理,用戶輸入一個(gè)或多個(gè)關(guān)鍵詞,分別輸出前五的各檢索關(guān)鍵詞的文本排序序列。

代碼片段和文件信息
import?math
import?os
import?re
from?nltk.corpus?import?stopwords
def?loadDataSet(path):
????“““
????讀取文本庫(kù)中的文本內(nèi)容以字典形式輸出
????:param?path:?文本庫(kù)地址
????:return:?文本庫(kù)字典{文本名1:文本內(nèi)容1,文本名2:文本內(nèi)容2...}
????“““
????#?將文件夾內(nèi)的文本全部導(dǎo)入程序
????files?=?os.listdir(path)??#?得到文件夾下的所有文件名稱
????all_docu_dic?=?{}??#?接收文檔名和文檔內(nèi)容的詞典
????for?file?in?files:??#?遍歷文件夾
????????if?not?os.path.isdir(file):??#?判斷是否是文件夾,不是文件夾才打開
????????????f?=?open(path?+?“/“?+?file?encoding=‘UTF-8-sig‘)??#?打開文件
????????????iter_f?=?iter(f)??#?創(chuàng)建迭代器
????????????strr?=?““
????????????for?line?in?iter_f:??#?遍歷文件,一行行遍歷,讀取文本
????????????????strr?=?strr?+?line
????????????all_docu_dic[file]?=?strr.strip(‘.‘)???#?去除末尾的符號(hào).
????print(“文件庫(kù):“)
????print(all_docu_dic)
????return?all_docu_dic
def?dealDataSet(all_docu_dic):
????“““
????處理文件庫(kù)字典的數(shù)據(jù)
????:param?all_docu_dic:文本庫(kù)字典{文本名1:文本內(nèi)容1,文本名2:文本內(nèi)容2...}
????:return:?1.all_words_set?文本庫(kù)的詞庫(kù){word1word2...}
?????????????2.words_num_dic?文本詞數(shù)字典{txt1:{word1:num1word2:num2}...}
????“““
????all_words?=?[]
????all_docu_cut?=?{}??#?分完詞后的dic(dic嵌套list)
????stop_words?=?stopwords.words(‘english‘)????#?原始停用詞庫(kù)
????#?#停用詞的擴(kuò)展
????#?print(len(stop_words))
????#?extra_words?=?[‘?‘]#新增的停用詞
????#?stop_words.extend(extra_words)#最后停用詞
????#?print(len(stop_words))
????#?計(jì)算所有文檔總詞庫(kù)和分隔后的詞庫(kù)
????for?filename?content?in?all_docu_dic.items():
????????cut?=?re.split(“[!??‘.)(+-=。:]“?content)??#?分詞
????????new_cut?=?[w?for?w?in?cut?if?w?not?in?stop_words?if?w]??#?去除停用詞,并且去除split后產(chǎn)生的空字符
????????all_docu_cut[filename]?=?new_cut??#?鍵為文本名,值為分詞完成的list
????????all_words.extend(new_cut)
????all_words_set?=?set(all_words)??#?轉(zhuǎn)化為集合形式
????#?計(jì)算各文本中的詞數(shù)
????words_num_dic?=?{}
????for?filename?cut?in?all_docu_cut.items():
????????words_num_dic[filename]?=?dict.fromkeys(all_docu_cut[filename]?0)
????????for?word?in?cut:
????????????words_num_dic[filename][word]?+=?1
????#?print(“詞庫(kù):“)
????#?print(all_words_set)
????print(“文件分詞庫(kù):“)
????print(all_docu_cut)
????return?all_words_set?words_num_dic?????#?返回詞庫(kù)和文檔詞數(shù)字典
def?computeTF(in_word?words_num_dic):
????“““
????計(jì)算單詞in_word在每篇文檔的TF
????:param?in_word:?單詞
????:param?words_num_dic:?文本詞數(shù)字典{txt1:{word1:num1word2:num2}...}
????:return:?tfDict:?單詞in_word在所有文本中的tf值字典?{文件名1:tf1文件名2:tf2...}
????“““
????allcount_dic?=?{}???#?各文檔的總詞數(shù)
????tfDict?=?{}?????#?in_word的tf字典
????#?計(jì)算每篇文檔總詞數(shù)
????for?filename?num?in?words_num_dic.items():
????????count?=?0
????????for?value?in?num.values():
????????????count?+=?value
????????allcount_dic[filename]?=?count
????#?計(jì)算tf
????for?filename?num?in?words_num_dic.items():
????????if?in_word?in?num.keys():
????????????tfDict[filename]?=?num[in_word]?/?allcount_dic[filename]
????return?tfDict
def?computeIDF(in_word?words_num_dic):
????“““
????計(jì)算in_word的idf值
????:param?in_word:?單詞
????:param?words_num_dic:?文本詞數(shù)字典{txt1:{word1:num1word2:num2}...}
????:return:?單詞in_word在整個(gè)文本庫(kù)中的idf值
????“““
????docu_count?=?len(words_num_dic)????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????6737??2020-06-18?18:42??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\B4.py
?????文件??????????5??2020-06-16?11:32??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\data\1.txt
?????文件?????????16??2020-06-14?20:55??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\data\2.txt
?????文件?????????11??2020-06-14?20:46??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\data\3.txt
?????文件?????????23??2020-06-16?11:33??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\data\4.txt
?????文件?????????17??2020-06-16?11:33??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\data\5.txt
?????文件?????????29??2020-06-16?11:34??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\data\6.txt
?????文件??????????5??2020-06-14?20:46??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\data\7.txt
?????文件??????????3??2020-06-14?20:56??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\data\8.txt
?????文件????????121??2020-06-18?19:31??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\readme.txt
?????文件?????402836??2020-06-18?18:47??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\基于關(guān)鍵詞的文本排序檢索系統(tǒng)課題報(bào)告.pdf
?????目錄??????????0??2020-06-18?19:32??基于關(guān)鍵詞的文本排序檢索系統(tǒng)\data
?????目錄??????????0??2020-06-18?19:32??基于關(guān)鍵詞的文本排序檢索系統(tǒng)
-----------?---------??----------?-----??----
???????????????409803????????????????????13
評(píng)論
共有 條評(píng)論