1.實(shí)驗(yàn)內(nèi)容:
- 根據(jù)qq音樂獲取的信息,對某一首歌曲的評論內(nèi)容進(jìn)行處理。
- 分析評論中的詞性分布。
- 統(tǒng)計高頻詞,畫出詞云。
2.實(shí)驗(yàn)步驟:
? ? 1. 文本信息初處理:
根據(jù)實(shí)驗(yàn)二QQ音樂抓取周杰倫的前五首歌曲評論等信息篩查晴天這首歌的相關(guān)信息存為“晴天-周杰倫”的文本文件方便后續(xù)數(shù)據(jù)讀取。
文本信息節(jié)選展示:
? ? 2. 編寫詞云繪制,詞頻統(tǒng)計以及詞性分析代碼:
首先打開文件進(jìn)行讀取信息,去掉長度為一的詞存下來并統(tǒng)計頻率最高的三十個詞打印輸出詞和詞出現(xiàn)的次數(shù)。獲取信息進(jìn)行文本切割以后進(jìn)行詞云的相關(guān)繪制。進(jìn)行詞性分析,統(tǒng)計評論中詞的詞性并打印輸出各類詞性的數(shù)量。
詞頻統(tǒng)計節(jié)選展示:
詞性分析節(jié)選展示:
?詞云如圖:
詞云背景圖:
? ? 3. 編寫詞性分析數(shù)據(jù)可視化代碼:
利用2步驟中的切割好的信息繪制條形圖,設(shè)置縱軸橫軸名稱和數(shù)據(jù)獲取繪制以及大小等。
運(yùn)行展示:
條狀圖如下:
3.實(shí)驗(yàn)總結(jié):
? 本次實(shí)驗(yàn)進(jìn)行實(shí)驗(yàn)二信息的數(shù)據(jù)分析和可視化操作,在實(shí)操中學(xué)會了很多,特別是在詞云繪制和詞性條狀圖繪制中學(xué)會了很多繪制技巧的基本內(nèi)容,實(shí)驗(yàn)難點(diǎn)是在做文本信息切割存儲的時候需要考慮詞的長度以及各類非漢字字符的去除等需要特別注意??傊敬螌?shí)驗(yàn)學(xué)習(xí)和實(shí)踐了評論的數(shù)據(jù)分析詞性分析等可視化的繪制詞云繪制詞性條狀圖的內(nèi)容,加深了對數(shù)據(jù)分析可視化的理解,難點(diǎn)痛點(diǎn)還需要多加理解和實(shí)踐。?
4.實(shí)驗(yàn)源碼:
源碼一:(進(jìn)行詞頻統(tǒng)計和詞性分析以及詞云的繪制)文章來源:http://www.zghlxwxcb.cn/news/detail-433477.html
#詞云繪制及詞性、詞頻統(tǒng)計分析
import jieba,wordcloud,re
import jieba.posseg as pseg
import imageio
#統(tǒng)計出現(xiàn)頻率最高的前三十個詞并輸出
txt = open("晴天-周杰倫","r",encoding="utf-8").read()
words = jieba.lcut(txt)
counts = {}
#去掉長度為1的詞
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key = lambda x:x[1],reverse=True)
print("統(tǒng)計高頻詞匯如下:")
print("詞語 出現(xiàn)次數(shù)\n")
for i in range(30):
word,count = items[i]
print("{0:<10}{1:>5}".format(word,count))
#'''除去文本中的非漢字字符
# for ch in r'●━━━━━━───────???????「」~??\
# ??1234567890”`~!@#$%^&*()-_=+[]{}\\|;\
# :\'\",./<>?·~!@#¥%……&*()——+-=【】{}、|;:\‘\“,?!丁??qwertyuiop\
# asdfghjklzxcvbnmQAZXSWEDCVFRTGBNHYUJMKIOLP年月日':'''
#定義去除掉文本中的非漢字字符的函數(shù)
def find_chinese(file):
pattern = re.compile(r'[^\u4e00-\u9fa5]')
chinese = re.sub(pattern,'', file)
return chinese
#獲取文件文本
def getText():
txt = open("晴天-周杰倫","r",encoding="utf-8").read()
txt = find_chinese(txt)
return txt
#獲取文本信息并進(jìn)行切割
txt = getText()
txt = jieba.lcut(txt)
#創(chuàng)建列表存入長度大于1的詞語
words = []
for word in txt:
if len(word) == 1:
continue
else:
words.append(word)
txt = words
color_mask =imageio.v2.imread("b.jpg")#詞云背景圖
txt = " ".join(txt)
w = wordcloud.WordCloud(width = 390, height = 390,\
background_color = "black",\
font_path = "msyh.ttc",\
contour_color= "green",\
contour_width= 2,\
mask=color_mask,\
mode="RGB")
w.generate(txt)
w.to_file("詞云.png")
txt = open("晴天-周杰倫","r",encoding="utf-8").read()
words = pseg.lcut(txt)
counts = {}
for word in words:
counts[word.flag] = counts.get(word.flag,0) + 1
items = list(counts.items())
items.sort(key = lambda x:x[1],reverse=True)
print("經(jīng)過統(tǒng)計和分析文件,\n其中的評論含有的詞種類以及數(shù)量如下所示:")
print("詞性 數(shù)量\n")
for i in range(len(counts)):
flag,count = items[i]
print("{0:<10}{1:>5}".format(flag,count))
源碼2:詞性分析可視化?文章來源地址http://www.zghlxwxcb.cn/news/detail-433477.html
import jieba.posseg as pseg
import matplotlib.pyplot as plt
from collections import Counter
txt = open("晴天-周杰倫","r",encoding="utf-8").read()
words = pseg.lcut(txt)
word_list=[]
flag_list=[]
for word,flag in words:
if(word!=' ')and(flag!='x'):
word_list.append(word)
flag_list.append(flag)
print(word_list)
print(flag_list)
word_dict = Counter(flag_list)
print(word_dict)
# 畫條狀圖
key_list = list(word_dict.keys())
value_list = list(word_dict.values())
plt.figure(figsize=(25, 30), dpi=100) # 設(shè)置大小
plt.barh(key_list, value_list) # 畫圖
plt.rcParams["font.sans-serif"] = ["SimHei"] # 設(shè)置中文字體
plt.xlabel("數(shù)量")
plt.ylabel("詞性英文簡稱")
plt.title("評論詞頻統(tǒng)計")
plt.xticks([i for i in range(0, 301, 25)])
plt.savefig("詞性分析.png") # 保存文件
plt.show() # 畫圖'''
到了這里,關(guān)于Python文本數(shù)據(jù)及其可視化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!