很少記錄自己的學(xué)習(xí)歷程,無奈太健忘你辣,而且剛?cè)胧諴ython,還是寫下來供以后參考和思考尘执。
本篇主要利用python jieba分詞和wordcloud進(jìn)行詞的可視化舍哄,其中去了停用詞,單獨(dú)計(jì)算了詞頻誊锭。也可以利用結(jié)巴自帶的關(guān)鍵詞提取方法表悬。
附網(wǎng)址jieba:https://github.com/fxsjy/jieba
wordcloud:https://github.com/amueller/word_cloud
#導(dǎo)入要用的包
import pandas as pd
import numpy as np
import jieba
import jieba.analyse
import wordcloud.WordCloud
import os
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
%matplotlib inline
#我將要處理的文件放在了D盤,文件類似dataframe結(jié)構(gòu)
os.chdir('D:')
comtent = pd.read_csv('dataframe.csv',dtype = 'object')
#將每列的keyword合并成一個字符串以便于處理
action = ''
for kw in content['keyword']:
? ? ? action += kw.strip() + ' '
stopwords = open('stopword.txt').read().strip().splitlines()
seg = jieba.cut(action)
seg = ' '.join(seg).split() ? ? ?######分詞后的詞都是Unicode格式
words = ''
for word in seg:
? ? word = word.encode('utf-8') ? ? ?#####因?yàn)橥S迷~是utf-8編碼的丧靡,所以將其也編碼為utf-8
? ? if word not in stopwords:
? ? ? ? words += word.strip() + ' '
words =words.decode('utf-8')
此處可以直接用wordcloud直接畫圖啦蟆沫,用generate()函數(shù)
#計(jì)算詞頻
words = ?words.split()
word_freq = {}
for word in words:
? ? if word in word_freq:
? ? ? ? word_freq[word] += 1
? ? else:
? ? ? ? word_freq[word] = 1
#按詞頻排序,將dict類型轉(zhuǎn)換成list類型
sort_word = []
for word,freq in word_freq.items():
? ? sort_word.append((word,freq))
sorted_word = sorted(sort_word,key = lambda x:x[1]温治,reverse = True)
##查看前100個高頻詞
for word in sorted_word[:100]:
? ? print word[0],word[1]
#發(fā)現(xiàn)一個字的挺多饭庞,所以選擇長度大于2的詞
lengther = []
for word in sorted_word:
? ? if len(word[0]) > 1:
? ? ? ? lengther.append(word)
#畫圖啦
wordcloud1 = WordCloud(font_path = '..matplotlib\\mpl-data\\fonts\\ttf\\msyh.ttf',background_color = 'white',max_words = 200,stopwords = ? ? ? ? ? ? ? stopwords).generate_from_frequencies(dict(lengther))
plt.imshow(wordcloud1)
plt.axis('off')
plt.show()
結(jié)巴自動關(guān)鍵詞提取(tf-idf罐盔,textrank)
tf-idf = jieba.analyse.extract_tags(action,topK = 200,withWeight = True)
textrank = jieba.analyse.textrank(action,topK = 200,withWeight = True)
畫圖部分省略但绕,和上面的一樣。。捏顺。
注:本文為原創(chuàng)六孵,轉(zhuǎn)載請注明出處。