基于python實現(xiàn)分析識別文章/內(nèi)容中的高頻詞和關(guān)鍵詞
nltk 和 collections 庫
首先,需要安裝 nltk 庫和 collections 庫??梢允褂靡韵旅顏戆惭b:
pip install nltk pip install collections
接下來,需要下載 nltk 庫中的 stopwords 和 punkt 數(shù)據(jù)??梢允褂靡韵麓a來下載:
import nltk nltk.download('stopwords') nltk.download('punkt')
下載完成后,可以使用以下代碼來讀取文章并進行分析:
import collections import nltk from nltk.corpus import stopwords from nltk.tokenize import word_tokenize # 讀取文章 with open('article.txt', 'r',encoding='utf-8') as f: article = f.read() # 分詞 tokens = word_tokenize(article) # 去除停用詞 stop_words = set(stopwords.words('english')) filtered_tokens = [token for token in tokens if token.lower() not in stop_words] # 統(tǒng)計詞頻 word_freq = collections.Counter(filtered_tokens) # 輸出高頻詞 print('Top 10 frequent words:') for word, freq in word_freq.most_common(10): print(f'{word}: {freq}') # 提取關(guān)鍵詞 keywords = nltk.FreqDist(filtered_tokens).keys() # 輸出關(guān)鍵詞 print('Keywords:') for keyword in keywords: print(keyword)
上述代碼中,首先使用 open() 函數(shù)讀取文章,然后使用 word_tokenize() 函數(shù)將文章分詞。接著,使用 stopwords 數(shù)據(jù)集去除停用詞,使用 collections.Counter() 函數(shù)統(tǒng)計詞頻,并輸出高頻詞。最后,使用 nltk.FreqDist() 函數(shù)提取關(guān)鍵詞,并輸出關(guān)鍵詞。
需要注意的是,上述代碼中的 article.txt 文件需要替換為實際的文章文件路徑。
結(jié)巴(jieba)庫實現(xiàn)
# 導(dǎo)入必要的庫 import jieba import jieba.analyse from collections import Counter from wordcloud import WordCloud import matplotlib.pyplot as plt # 讀取文章 with open('./data/2.txt', 'r', encoding='utf-8') as f: article = f.read() # 分詞 words = jieba.cut(article) # 統(tǒng)計詞頻 word_counts = Counter(words) # 輸出高頻詞 print('高頻詞:') for word, count in word_counts.most_common(10): print(word, count) # 輸出關(guān)鍵詞 print('關(guān)鍵詞:') keywords = jieba.analyse.extract_tags(article, topK=10, withWeight=True, allowPOS=('n', 'nr', 'ns')) for keyword, weight in keywords: print(keyword, weight) # 生成詞云 wordcloud = WordCloud(font_path='msyh.ttc', background_color='white', width=800, height=600).generate(article) plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.show()
導(dǎo)入jieba庫:首先需要導(dǎo)入jieba庫,才能使用其中的分詞功能。
讀取文章:需要讀取要分析的文章,可以使用Python內(nèi)置的open函數(shù)打開文件,然后使用read方法讀取文件內(nèi)容。
分詞:使用jieba庫的cut方法對文章進行分詞,得到一個生成器對象,可以使用for循環(huán)遍歷生成器對象,得到每個詞。
統(tǒng)計詞頻:使用Python內(nèi)置的collections庫中的Counter類,對分詞后的詞進行統(tǒng)計,得到每個詞出現(xiàn)的次數(shù)。
輸出高頻詞:根據(jù)詞頻統(tǒng)計結(jié)果,輸出出現(xiàn)頻率最高的詞,即為高頻詞。
輸出關(guān)鍵詞:使用jieba庫的analyse模塊中的extract_tags方法,根據(jù)TF-IDF算法計算每個詞的權(quán)重,輸出權(quán)重最高的詞,即為關(guān)鍵詞。
生成詞云:使用wordcloud庫生成詞云,將文章中的詞按照詞頻生成詞云,詞頻越高的詞在詞云中出現(xiàn)的越大。
到此這篇關(guān)于基于python實現(xiàn)分析識別文章/內(nèi)容中的高頻詞和關(guān)鍵詞的文章就介紹到這了,更多相關(guān)python分析識別高頻詞和關(guān)鍵詞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python:Scrapy框架中Item Pipeline組件使用詳解
這篇文章主要介紹了Python:Scrapy框架中Item Pipeline組件使用詳解,具有一定借鑒價值,需要的朋友可以參考下2017-12-12使用PyCharm和venv進行Python項目環(huán)境配置避坑指南
在進行 Python 項目開發(fā)時,一個干凈,隔離且配置正確的開發(fā)環(huán)境至關(guān)重要,本文結(jié)合之前安裝 Vanna 庫時遇到的問題,總結(jié)了使用 PyCharm 和 venv 進行 Python 項目環(huán)境設(shè)置的最佳實踐和常見坑的解決方法,有需要的小伙伴可以參考下2025-04-04Google開源的Python格式化工具YAPF的安裝和使用教程
Google的開發(fā)者文檔中有一套Python的代碼書寫規(guī)范,而在GitHub上同樣開源了一款名為YAPF的命令行程序用作Python的格式化,下面我們就來看下這款Google開源的Python格式化工具YAPF的安裝和使用教程2016-05-05