一起做個文字雲吧!

Python 情感分析

Violeta Misheva

Data Scientist

文字雲範例

黑色背景的文字雲範例

Python 情感分析

文字雲怎麼運作?

 

字詞越常見,在文字雲中就會顯得越大、越粗體

Python 情感分析
  • 由一則最長的評論產生的文字雲

從一則關於鐵達尼號的評論產生的圓形文字雲

Python 情感分析

為什麼用文字雲?

優點
  • 能呈現關鍵
  • 提供整體文本的感覺
  • 容易理解且有吸引力
缺點

  • 有時易混淆且資訊量低
  • 文本越大,調整工作越多
Python 情感分析

用 Python 來做文字雲!

from wordcloud import WordCloud
import matplotlib.pyplot as plt
two_cities = "It was the best of times, it was the worst of times, 
        it was the age of wisdom, it was the age of foolishness, 
        it was the epoch of belief, it was the epoch of incredulity, 
        it was the season of Light, it was the season of Darkness, 
        it was the spring of hope, it was the winter of despair, 
        we had everything before us, we had nothing before us, 
        we were all going direct to Heaven, we were all going
        direct the other way – in short, the period was so far
        like the present period, that some of its noisiest
        authorities insisted on its being received, for good
        or for evil, in the superlative degree of comparison only."
Python 情感分析

定義 WordCloud 物件

cloud_two_cities = WordCloud().generate(two_cities)
# To see all arguments of the function
?WordCloud
  • 背景色
  • 字體大小與縮放
  • 停用詞(stopwords)
# How does cloud_two_cities look like?
cloud_two_cities
<wordcloud.wordcloud.WordCloud at 0x2585f286d68>
Python 情感分析

顯示文字雲!

plt.imshow(cloud_two_cities, interpolation='bilinear')
plt.axis('off')
plt.show()

以黑色背景顯示、取材自《雙城記》開頭句的文字雲

Python 情感分析

一起來練習吧!

Python 情感分析

Preparing Video For Download...