iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 5
1
自我挑戰組

自然語言技術與AI/ML初探系列 第 5

Sentiment analysis

參考這篇文章”手把手教你如何用 Python 做情感分析”進行練習
https://www.itread01.com/articles/1498721884.html

安裝以下套件
pip install snownlp
pip install -U textblob
python -m textblob.download_corpora

安裝完後,首先

應用TextBlob分析英文句子
text = "I am happy today. I feel sad today."
匯入TextBlob
from textblob import TextBlob
blob = TextBlob(text)
用sententces方法將text斷句:blob.sentences
印出第一句的情緒分數 會輸出polarity(情感極性)以及subjectivity(主觀性)
blob.sentences[0].sentiment
輸出:Sentiment(polarity=0.8, subjectivity=1.0)

https://ithelp.ithome.com.tw/upload/images/20181020/2011253842f0FFV88p.png

接下來,練習中文文本情感分析
使用SnowNLP: http://t.cn/8kf1c3p

宣告一段中文文本
text = u"我今天很快樂。我今天很憤怒。"
其中u代表文本的編碼是Unicode

匯入SnowNLP
from snownlp import SnowNLP
s=SnowNLP(text)
用sententces方法將text斷句:
for sentence in s.sentences:
print(sentence)
印出斷完的句子:
我今天很快樂
我今天很憤怒

用以下command印出第1句的情緒分數
s1 = SnowNLP(s.sentences[0])
s1.sentiments
印出:0.9268071116367116 (越接近1表示越正面)
第2句的情緒分數
s1 = SnowNLP(s.sentences[1])
s1.sentiments
印出:0.1702660762575916 (表達正面情感的機率0.1,表示偏向負面)

https://ithelp.ithome.com.tw/upload/images/20181020/20112538wr477ASOKj.png


上一篇
CNN(下)
下一篇
Pandas
系列文
自然語言技術與AI/ML初探30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

我要留言

立即登入留言