Python 使用tf-idf算法計(jì)算文檔關(guān)鍵字權(quán)重并生成詞云的方法
Python 使用tf-idf算法計(jì)算文檔關(guān)鍵字權(quán)重,并生成詞云

1. 根據(jù)tf-idf計(jì)算一個(gè)文檔的關(guān)鍵詞或者短語:
代碼如下:
注意需要安裝pip install sklean;
from re import split
from jieba.posseg import dt
from sklearn.feature_extraction.text import TfidfVectorizer
from collections import Counter
from time import time
import jieba
#pip install sklean
FLAGS = set('a an b f i j l n nr nrfg nrt ns nt nz s t v vi vn z eng'.split())
def cut(text):
for sentence in split('[^a-zA-Z0-9\u4e00-\u9fa5]+', text.strip()):
for w in dt.cut(sentence):
if len(w.word) > 2 and w.flag in FLAGS:
yield w.word
class TFIDF:
def __init__(self, idf):
self.idf = idf
@classmethod
def train(cls, texts):
model = TfidfVectorizer(tokenizer=cut)
model.fit(texts)
idf = {w: model.idf_[i] for w, i in model.vocabulary_.items()}
return cls(idf)
def get_idf(self, word):
return self.idf.get(word, max(self.idf.values()))
def extract(self, text, top_n=10):
counter = Counter()
for w in cut(text):
counter[w] += self.get_idf(w)
#return [i[0:2] for i in counter.most_common(top_n)]
return [i[0] for i in counter.most_common(top_n)]
if __name__ == '__main__':
t0 = time()
with open('./nlp-homework.txt', encoding='utf-8')as f:
_texts = f.read().strip().split('\n')
# print(_texts)
tfidf = TFIDF.train(_texts)
# print(_texts)
for _text in _texts:
seq_list=jieba.cut(_text,cut_all=True) #全模式
# seq_list=jieba.cut(_text,cut_all=False) #精確模式
# seq_list=jieba.cut_for_search(_text,) #搜索引擎模式
# print(list(seq_list))
print(tfidf.extract(_text))
with open('./resultciyun.txt','a+', encoding='utf-8') as g:
for i in tfidf.extract(_text):
g.write(str(i) + " ")
print(time() - t0)
2. 生成詞云:
代碼如下:
- 注意需要安裝
pip install wordcloud; - 以及為了保證中文字體正常顯示,需要下載
SimSun.ttf字體,并且將這個(gè)字體包也放在和程序相同的目錄下;
from wordcloud import WordCloud
filename = "resultciyun.txt"
with open(filename) as f:
resultciyun = f.read()
wordcloud = WordCloud(font_path="simsun.ttf").generate(resultciyun)
# %pylab inline
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()3 最后詞云的圖片

總結(jié)
最后的最后
由本人水平所限,難免有錯(cuò)誤以及不足之處, 屏幕前的靚仔靚女們 如有發(fā)現(xiàn),懇請指出!
到此這篇關(guān)于Python 使用tf-idf算法計(jì)算文檔關(guān)鍵字權(quán)重,并生成詞云的文章就介紹到這了,更多相關(guān)Python tf-idf算法關(guān)鍵字權(quán)重并生成詞云內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python datetime 如何處理時(shí)區(qū)信息
這篇文章主要介紹了Python datetime 如何處理時(shí)區(qū)信息,幫助大家更好的用python 處理時(shí)間,感興趣的朋友可以了解下。2020-09-09
基于Python實(shí)現(xiàn)多圖繪制系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)一個(gè)簡單的多圖繪制系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
celery4+django2定時(shí)任務(wù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了celery4+django2定時(shí)任務(wù)的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
TENSORFLOW變量作用域(VARIABLE SCOPE)
這篇文章主要介紹了TENSORFLOW變量作用域(VARIABLE SCOPE),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
python實(shí)現(xiàn)飛船游戲的縱向移動(dòng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)飛船游戲的縱向移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04

