大家好,我是皮皮。
一、前言前幾天在Python最強(qiáng)王者交流群【W(wǎng)endy Zheng】問(wèn)了一個(gè)英文文本中統(tǒng)計(jì)關(guān)鍵詞得問(wèn)題,這里拿出來(lái)給大家分享下。
二、實(shí)現(xiàn)過(guò)程針對(duì)這個(gè)問(wèn)題,感謝給出一個(gè)思路方法,也許有幫助,首先我們需要將Excel中得文本進(jìn)行導(dǎo)入到一個(gè)文感謝件中去,代碼如下:
# coding: utf-8import pandas as pddf = pd.read_excel('./文本.xlsx')# print(df.head())# df['可以關(guān)鍵詞']for text in df['工作要求']: # print(text) if text is not None: with open('工作要求.txt', mode='a', encoding='utf-8') as file: file.write(str(text))print('寫入完成')
接下來(lái)就可以針對(duì)這個(gè)文感謝件進(jìn)行相關(guān)得詞頻統(tǒng)計(jì)了,如果你有自己自定義得關(guān)鍵詞,也可以就著關(guān)鍵詞去統(tǒng)計(jì),沒(méi)有得話,就自己在關(guān)鍵詞范圍內(nèi),任意取多少個(gè)關(guān)鍵詞都可以,相關(guān)得代碼如下所示:
from collections import Counterimport pandas as pddf = pd.read_excel('./文本.xlsx')# print(df.head())words = []with open('工作要求.txt', 'r', encoding='utf-8') as f: line = f.readlines() for word in line[0].split(' '): words.append(word)print(len(words))counter = Counter(words)# print(counter)# df['可以關(guān)鍵詞']for text in df['可以關(guān)鍵詞']: for k, v in counter.items(): if k == text: print(k, v)
這個(gè)代碼對(duì)于英文文本還是適用得,不過(guò)有個(gè)小問(wèn)題,如下。
最后這里也給出中文分詞得代碼和可視化代碼,兩者結(jié)合在一起得,感興趣得小伙伴們可以試試看。
from collections import Counter # 統(tǒng)計(jì)詞頻from pyecharts.charts import Barfrom pyecharts import options as optsfrom snownlp import SnowNLPimport jieba # 分詞with open('text_分詞后_outputs.txt', 'r',encoding='utf-8') as f: read = f.read()with open('stop_word.txt', 'r', encoding='utf-8') as f: stop_word = f.read()word = jieba.cut(read)words = []for i in list(word): if i not in stop_word: words.append(i)columns = []data = []for k, v in dict(Counter(words).most_common(10)).items(): columns.append(k) data.append(v)bar = ( Bar() .add_xaxis(columns) .add_yaxis("詞頻", data) .set_global_opts(title_opts=opts.TitleOpts(title="詞頻top10")) )bar.render("詞頻.html")
三、總結(jié)
大家好,我是皮皮。這篇文章主要盤點(diǎn)了一個(gè)英文文本中統(tǒng)計(jì)關(guān)鍵詞方法處理得問(wèn)題,文中針對(duì)該問(wèn)題,給出了具體得解析和代碼實(shí)現(xiàn),幫助粉絲順利解決了問(wèn)題。
最后感謝粉絲【W(wǎng)endy Zheng】提問(wèn),感謝【Python進(jìn)階者】給出得思路和代碼解析,感謝【Python狗】等人參與學(xué)習(xí)交流。