Python基于詞頻排序?qū)崿F(xiàn)快速挖掘關(guān)鍵詞
一、所有的代碼
這是所有的代碼
from collections import defaultdict
import jieba.posseg as jp
with open('keyword.txt','r',encoding='utf-8') as file:
keyword_list = file.read().split('\n')
not_flag = set(['w','x','y','z','un','m'])
not_word = set(['的','是','有','啊','呢','么','好'])
keyword_split = dict()
word_count = defaultdict(int)
for keyword in keyword_list:
word_set = set()
for word,flag in jp.cut(keyword):
if flag in not_flag:
continue
if word in not_word:
continue
if word == 'pdf' or word == 'PDF':
continue
word_count[word] += 1
word_set.add(word)
keyword_split[keyword] = word_set
id_keyword_list = defaultdict(list)
id_count = defaultdict(int)
for keyword,word_set in keyword_split.items():
word_sort = dict()
for word in word_set:
word_sort[word] = word_count[word]
word_sort = sorted(word_sort.items(),key=lambda x:x[1],reverse=True)
word_id = ','.join([word for word,count in word_sort[0:3]])
id_keyword_list[word_id] += [keyword]
id_count[word_id] += 1
result = []
id_count = sorted(id_count.items(),key=lambda x:x[1],reverse=True)
for word_id,count in id_count:
if count < 3:
continue
for keyword in id_keyword_list[word_id]:
result.append('%s\t%s' % (keyword,word_id))
result.append('')
with open('result.txt','wb') as file:
file.write('\n'.join(result).encode('utf-8'))
二、實(shí)現(xiàn)的效果
keyword.txt如下圖:
有50萬(wàn)的關(guān)于pdf的關(guān)鍵詞數(shù)據(jù)

最后的輸出result.txt 就是將里面的含有關(guān)鍵詞相同的句子統(tǒng)一輸出出來(lái):

這里會(huì)將一個(gè)句子的3個(gè)關(guān)鍵詞輸出出來(lái) 關(guān)鍵詞是根據(jù)詞頻排序的。
最后將所有關(guān)鍵詞一樣的句子組合在一起,就可以知道這些句子表達(dá)的意思大致一致
三、代碼解讀
keyword_list 是從keyword.txt讀取到的所有的句子
not_flag 是要排除的標(biāo)記,不統(tǒng)計(jì)這些標(biāo)記
not_word 是要排除的單詞,不統(tǒng)計(jì)這些單詞
keyword_split 是句子對(duì)應(yīng)到他的所有單詞的字典,key是句子,value是他的所有單詞的集合

word_count 是所有的拆分后的單詞的次數(shù)的字典,key是單詞,value是單詞出現(xiàn)的次數(shù)

id_keyword_list 是一個(gè)字典,它的key是一個(gè)字符串 value是列表


id_count 是一個(gè)字典,它的key是一個(gè)字符串,value是int


最后對(duì)id_count處理 將結(jié)果輸出出來(lái)
id_count = sorted(id_count.items(), key=lambda x: x[1], reverse=True)
for word_id, count in id_count:
if count < 3:
continue
for keyword in id_keyword_list[word_id]:
result.append('%s\t%s' % (keyword, word_id))
result.append('')
到此這篇關(guān)于Python基于詞頻排序?qū)崿F(xiàn)快速挖掘關(guān)鍵詞的文章就介紹到這了,更多相關(guān)Python挖掘關(guān)鍵詞內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch深度神經(jīng)網(wǎng)絡(luò)入門(mén)準(zhǔn)備自己的圖片數(shù)據(jù)
這篇文章主要為大家介紹了pytorch深度神經(jīng)網(wǎng)絡(luò)入門(mén)準(zhǔn)備自己的圖片數(shù)據(jù)示例過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Python實(shí)現(xiàn)二叉排序樹(shù)與平衡二叉樹(shù)的示例代碼
樹(shù)表查詢即借助具有特殊性質(zhì)的樹(shù)數(shù)據(jù)結(jié)構(gòu)進(jìn)行關(guān)鍵字查找,本文所涉及到的特殊結(jié)構(gòu)性質(zhì)的樹(shù)包括:二叉排序樹(shù)、平衡二叉樹(shù)。文中詳細(xì)介紹了二者的實(shí)現(xiàn)代碼,需要的可以參考一下2022-04-04
Python使用openpyxl讀寫(xiě)excel文件的方法
本篇文章主要介紹了Python使用openpyxl讀寫(xiě)excel文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
python+requests接口壓力測(cè)試500次,查看響應(yīng)時(shí)間的實(shí)例
這篇文章主要介紹了python+requests接口壓力測(cè)試500次,查看響應(yīng)時(shí)間的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Phantomjs抓取渲染JS后的網(wǎng)頁(yè)(Python代碼)
phantomjs:我的理解就是它是一個(gè)無(wú)顯示的瀏覽器,也就是說(shuō)除了不能顯示頁(yè)面內(nèi)容以外,瀏覽器能干的活兒它基本上都能干。下面我們就來(lái)利用他做點(diǎn)有趣的事情2016-05-05

