文字情感分析
- 1、參考圖檔
- 2、簡單介紹
-
- 2.1、積極詞彙
- 2.2、消極詞彙
- 3、程式舉例
-
- 3.1、參考文本
- 3.2、源代碼
- 3.3、導出圖檔
- *4、補充
-
- 4.1、原生詞雲圖(一行二個)
- 4.2、源代碼
1、參考圖檔

2、簡單介紹
2.1、積極詞彙
import snownlp
word = snownlp.SnowNLP("生活的理想,就是為了理想的生活")
print(word.sentiments)
# output:0.9698538001511074
2.2、消極詞彙
import snownlp
word = snownlp.SnowNLP("外賣慢死了,快餓死了")
print(word.sentiments)
# output:0.2805277439985002
3、程式舉例
3.1、參考文本
《巴黎聖母院》
3.2、源代碼
import jieba
import wordcloud
# 導入詞雲制作第三方庫 wordcloud 和中文分詞第三方庫 jieba
import imageio
# 導入處理圖檔輸入輸出第三方 imageio 庫
photo = imageio.imread("map.png")
# 利用 imageio 庫中的 imread 函數讀取本地圖檔并将此圖檔對象傳遞給參數 photo
w1 = wordcloud.WordCloud(width=1000,
height=700,
background_color='white',
font_path='msyh.ttc',
mask=photo,
scale=15)
w2 = wordcloud.WordCloud(width=1000,
height=700,
background_color='white',
font_path='msyh.ttc',
mask=photo,
scale=15)
# 建構并配置兩個詞雲對象 w1 和 w2 、分别存放積極詞和消極詞
f = open('巴黎聖母院.txt', encoding='utf-8')
txt = f.read()
txtlist = jieba.lcut(txt)
positivelist = []
negativelist = []
# 對來自外部檔案的文本進行中文分詞、得到積極詞彙和消極詞彙的兩個清單
import snownlp
# 導入自然語言處理第三方庫 snownlp
for each in txtlist:
each_word = snownlp.SnowNLP(each)
feeling = each_word.sentiments
if feeling > 0.96:
positivelist.append(each)
elif feeling < 0.06:
negativelist.append(each)
else:
pass
# 對文本中的每個詞進行情感分析、情感 > 0.96判為積極詞、情感 < 0.06判為消極詞
positive_string = " ".join(positivelist)
negative_string = " ".join(negativelist)
# 将積極和消極的兩個清單各自合并成積極字元串和消極字元串,字元串中的詞用空格分隔
w1.generate(positive_string)
w2.generate(negative_string)
# 将 string 變量傳入 w 的generate()方法、給詞雲輸入文字
w1.to_file('wordcloud11.png')
w2.to_file('wordcloud12.png')
# 将積極、消極的兩個詞雲圖檔導出到目前檔案夾
3.3、導出圖檔
- 積極詞彙
Python wordcloud 庫 自學81、參考圖檔2、簡單介紹3、程式舉例*4、補充 - 消極詞彙
*4、補充
4.1、原生詞雲圖(一行二個)
4.2、源代碼
import jieba
import wordcloud
# 導入詞雲制作第三方庫 wordcloud 和中文分詞第三方庫 jieba
import imageio
# 導入處理圖檔輸入輸出第三方 imageio 庫
photo = imageio.imread("chinamap.png")
# 利用 imageio 庫中的 imread 函數讀取本地圖檔并将此圖檔對象傳遞給參數 photo
w1 = wordcloud.WordCloud(width=1000,
height=700,
background_color='white',
font_path='msyh.ttc',
mask=photo,
scale=15)
w2 = wordcloud.WordCloud(width=1000,
height=700,
background_color='white',
font_path='msyh.ttc',
mask=photo,
scale=15)
# 建構并配置兩個詞雲對象 w1 和 w2 、分别存放積極詞和消極詞
f = open('巴黎聖母院.txt', encoding='utf-8')
txt = f.read()
txtlist = jieba.lcut(txt)
positivelist = []
negativelist = []
# 對來自外部檔案的文本進行中文分詞、得到積極詞彙和消極詞彙的兩個清單
import snownlp
# 導入自然語言處理第三方庫 snownlp
for each in txtlist:
each_word = snownlp.SnowNLP(each)
feeling = each_word.sentiments
if feeling > 0.96:
positivelist.append(each)
elif feeling < 0.06:
negativelist.append(each)
else:
pass
# 對文本中的每個詞進行情感分析、情感 > 0.96判為積極詞、情感 < 0.06判為消極詞
positive_string = " ".join(positivelist)
negative_string = " ".join(negativelist)
# 将積極和消極的兩個清單各自合并成積極字元串和消極字元串,字元串中的詞用空格分隔
w1.generate(positive_string)
w2.generate(negative_string)
# 将 string 變量傳入 w 的generate()方法、給詞雲輸入文字
import matplotlib.pyplot as plt
# 導入繪圖第三方庫 matplotlib
fig, axes = plt.subplots(1, 2)
# 顯示積極、消極詞雲圖 (一行二個)
axes[0].imshow(w1)
axes[1].imshow(w2)
for ax in axes:
ax.set_axis_off()
plt.show()
w1.to_file('wordcloud11.png')
w2.to_file('wordcloud12.png')
# 将積極、消極的兩個詞雲圖檔導出到目前檔案夾