標籤:list() top 度量 輸入關鍵詞 縮小 creat cut info turn
關鍵詞句和文本集每篇文章相關度計算:假設語料庫中有幾萬篇文章,每篇文章的長度不一,你任意輸入關鍵詞或句子,通過代碼以tf-idf值為準檢索出來相似性高的文章。
TF-IDF是一種統計方法,用以評估一字詞對於一個檔案集或一個語料庫中的其中一份檔案的重要程度。字詞的重要性隨著它在檔案中出現的次數成正比增加,但同時會隨著它在語料庫中出現的頻率成反比下降。TF-IDF加權的各種形式常被搜尋引擎應用,作為檔案與使用者查詢之間相關程度的度量或評級。
TFIDF的主要思想是:如果某個詞或短語在一篇文章中出現的頻率TF高,並且在其他文章中很少出現,則認為此詞或者短語具有很好的類別區分能力,適合用來分類。
TFIDF實際上是:TF * IDF。TF-IDF值與該詞的出現頻率成正比,與在整個語料庫中的出現次數成反比。
TF詞頻(Term Frequency),IDF逆向檔案頻率(Inverse Document Frequency)。
詞頻 (TF)= 某個詞在文章中出現的總次數/文章的總詞數,TF表示詞條在文檔d中出現的頻率。
逆文檔頻率(IDF) = log(詞料庫的文檔總數/包含該詞的文檔數+1),為了避免分母為0,所以在分母上加1.。IDF的主要思想是:如果包含該詞的文檔數越少,也就是log裡面分母越小,IDF越大,則說明該詞具有很好的類別區分能力。
停用詞大致分為兩類。一類是人類語言中包含的功能詞,這些功能詞極其普遍,與其他詞相比,功能詞沒有什麼實際含義,比如‘the‘、‘is‘、‘at‘、‘which‘、‘on‘等。但是對於搜尋引擎來說,當所要搜尋的短語包含功能詞,特別是像‘The Who‘、‘The The‘或‘Take The‘等複合名詞時,停用詞的使用就會導致問題。另一類詞包括詞彙詞,比如‘want‘等,這些詞應用十分廣泛,但是對這樣的詞搜尋引擎無法保證能夠給出真正相關的搜尋結果,難以協助縮小搜尋範圍,同時還會降低搜尋的效率,所以通常會把這些詞從問題中移去,從而提高搜尋效能。
該語料庫每行是一篇文章,每篇文章前面是題目,後面是摘要。包含兩萬多篇文章,僅做測試用。
停用詞表和語料庫見百度雲連結:連結:https://pan.baidu.com/s/1wNNUd0Pe20HFLAyuNcwDrg 密碼:367d
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Tue Jul 31 10:57:03 2018 4 5 @author: Lenovo 6 """ 7 import jieba 8 import math 9 import numpy as np10 11 filename = ‘句子相似性/title.txt‘#語料庫12 filename2 = ‘句子相似性/stopwords.txt‘#停用詞表,使用的哈工大停用詞表13 14 def stopwordslist():#擷取停用詞表15 stopwords = [line.strip() for line in open(filename2, encoding=‘UTF-8‘).readlines()]16 return stopwords17 18 stop_list = stopwordslist()19 def get_dic_input(str):20 dic = {}21 22 cut = jieba.cut(str)23 list_word = (‘,‘.join(cut)).split(‘,‘)24 25 for key in list_word:#去除輸入停用詞26 if key in stop_list:27 list_word.remove(key)28 29 length_input = len(list_word)30 31 for key in list_word:32 dic[key] = 033 34 return dic, length_input35 36 def get_tf_idf(filename):37 s = input("請輸入要檢索的關鍵詞句:")38 39 dic_input_idf, length_input = get_dic_input(s)40 f = open(filename, ‘r‘, encoding=‘utf-8‘)41 list_tf = []42 list_idf = []43 word_vector1 = np.zeros(length_input)44 word_vector2 = np.zeros(length_input)45 46 lines = f.readlines()47 length_essay = len(lines)48 f.close()49 50 for key in dic_input_idf:#計算出每個詞的idf值依次儲存在list_idf中51 for line in lines:52 if key in line.split():53 dic_input_idf[key] += 154 list_idf.append(math.log(length_essay / (dic_input_idf[key]+1)))55 56 for i in range(length_input):#將idf值儲存在矩陣向量中57 word_vector1[i] = list_idf.pop()58 59 for line in lines:#依次計算每個詞在每行的tf值依次儲存在list_tf中60 length = len(line.split())61 dic_input_tf, length_input= get_dic_input(s)62 63 for key in line.split():64 if key in stop_list:#去除文章中停用詞65 length -= 166 if key in dic_input_tf:67 dic_input_tf[key] += 168 for key in dic_input_tf:69 tf = dic_input_tf[key] / length70 list_tf.append(tf)71 72 for i in range(length_input):#將每行tf值儲存在矩陣向量中73 word_vector2[i] = list_tf.pop()74 75 #print(word_vector2)76 #print(word_vector1)77 tf_idf = float(np.sum(word_vector2 * word_vector1))78 if tf_idf > 0.3:#篩選出相似性高的文章79 print("tf_idf值:", tf_idf)80 print("文章:", line)81 82 get_tf_idf(filename)
輸入:人工智慧發展趨勢
輸出如下:輸出tf-idf值大於0.3的,從兩萬多篇中檢索出12篇
TF-IDF演算法--關鍵詞句和文本集中每篇文章相關度計算