python TF-IDF算法實(shí)現(xiàn)文本關(guān)鍵詞提取
TF(Term Frequency)詞頻,在文章中出現(xiàn)次數(shù)最多的詞,然而文章中出現(xiàn)次數(shù)較多的詞并不一定就是關(guān)鍵詞,比如常見的對(duì)文章本身并沒有多大意義的停用詞。所以我們需要一個(gè)重要性調(diào)整系數(shù)來衡量一個(gè)詞是不是常見詞。該權(quán)重為IDF(Inverse Document Frequency)逆文檔頻率,它的大小與一個(gè)詞的常見程度成反比。在我們得到詞頻(TF)和逆文檔頻率(IDF)以后,將兩個(gè)值相乘,即可得到一個(gè)詞的TF-IDF值,某個(gè)詞對(duì)文章的重要性越高,其TF-IDF值就越大,所以排在最前面的幾個(gè)詞就是文章的關(guān)鍵詞。
TF-IDF算法的優(yōu)點(diǎn)是簡(jiǎn)單快速,結(jié)果比較符合實(shí)際情況,但是單純以“詞頻”衡量一個(gè)詞的重要性,不夠全面,有時(shí)候重要的詞可能出現(xiàn)的次數(shù)并不多,而且這種算法無法體現(xiàn)詞的位置信息,出現(xiàn)位置靠前的詞和出現(xiàn)位置靠后的詞,都被視為同樣重要,是不合理的。
TF-IDF算法步驟:
(1)、計(jì)算詞頻:
詞頻 = 某個(gè)詞在文章中出現(xiàn)的次數(shù)
考慮到文章有長(zhǎng)短之分,考慮到不同文章之間的比較,將詞頻進(jìn)行標(biāo)準(zhǔn)化
詞頻 = 某個(gè)詞在文章中出現(xiàn)的次數(shù)/文章的總詞數(shù)
詞頻 = 某個(gè)詞在文章中出現(xiàn)的次數(shù)/該文出現(xiàn)次數(shù)最多的詞出現(xiàn)的次數(shù)
(2)、計(jì)算逆文檔頻率
需要一個(gè)語(yǔ)料庫(kù)(corpus)來模擬語(yǔ)言的使用環(huán)境。
逆文檔頻率 = log(語(yǔ)料庫(kù)的文檔總數(shù)/(包含該詞的文檔數(shù) + 1))
(3)、計(jì)算TF-IDF
TF-IDF = 詞頻(TF)* 逆文檔頻率(IDF)
詳細(xì)代碼如下:
#!/usr/bin/env python #-*- coding:utf-8 -*- ''' 計(jì)算文檔的TF-IDF ''' import codecs import os import math import shutil #讀取文本文件 def readtxt(path): with codecs.open(path,"r",encoding="utf-8") as f: content = f.read().strip() return content #統(tǒng)計(jì)詞頻 def count_word(content): word_dic ={} words_list = content.split("/") del_word = ["\r\n","/s"," ","/n"] for word in words_list: if word not in del_word: if word in word_dic: word_dic[word] = word_dic[word]+1 else: word_dic[word] = 1 return word_dic #遍歷文件夾 def funfolder(path): filesArray = [] for root,dirs,files in os.walk(path): for file in files: each_file = str(root+"http://"+file) filesArray.append(each_file) return filesArray #計(jì)算TF-IDF def count_tfidf(word_dic,words_dic,files_Array): word_idf={} word_tfidf = {} num_files = len(files_Array) for word in word_dic: for words in words_dic: if word in words: if word in word_idf: word_idf[word] = word_idf[word] + 1 else: word_idf[word] = 1 for key,value in word_dic.items(): if key !=" ": word_tfidf[key] = value * math.log(num_files/(word_idf[key]+1)) #降序排序 values_list = sorted(word_tfidf.items(),key = lambda item:item[1],reverse=True) return values_list #新建文件夾 def buildfolder(path): if os.path.exists(path): shutil.rmtree(path) os.makedirs(path) print("成功創(chuàng)建文件夾!") #寫入文件 def out_file(path,content_list): with codecs.open(path,"a",encoding="utf-8") as f: for content in content_list: f.write(str(content[0]) + ":" + str(content[1])+"\r\n") print("well done!") def main(): #遍歷文件夾 folder_path = r"分詞結(jié)果" files_array = funfolder(folder_path) #生成語(yǔ)料庫(kù) files_dic = [] for file_path in files_array: file = readtxt(file_path) word_dic = count_word(file) files_dic.append(word_dic) #新建文件夾 new_folder = r"tfidf計(jì)算結(jié)果" buildfolder(new_folder) #計(jì)算tf-idf,并將結(jié)果存入txt i=0 for file in files_dic: tf_idf = count_tfidf(file,files_dic,files_array) files_path = files_array[i].split("http://") #print(files_path) outfile_name = files_path[1] #print(outfile_name) out_path = r"%s//%s_tfidf.txt"%(new_folder,outfile_name) out_file(out_path,tf_idf) i=i+1 if __name__ == '__main__': main()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python使用any判斷一個(gè)對(duì)象是否為空的方法
這篇文章主要介紹了python使用any判斷一個(gè)對(duì)象是否為空的方法,并給出了改進(jìn)的方法供大家對(duì)比參考,具有一定的借鑒價(jià)值,需要的朋友可以參考下2014-11-11Python實(shí)現(xiàn)向好友發(fā)送微信消息優(yōu)化篇
利用python可以實(shí)現(xiàn)微信消息發(fā)送功能,怎么實(shí)現(xiàn)呢?你肯定會(huì)想著很復(fù)雜,但是python的好處就是很多人已經(jīng)把接口打包做好了,只需要調(diào)用即可,今天通過本文給大家分享使用?Python?實(shí)現(xiàn)微信消息發(fā)送的思路代碼,一起看看吧2022-06-06Python實(shí)現(xiàn)檢測(cè)文件的MD5值來查找重復(fù)文件案例
這篇文章主要介紹了Python實(shí)現(xiàn)檢測(cè)文件的MD5值來查找重復(fù)文件案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03