Python基于jieba分詞實現(xiàn)snownlp情感分析
情感分析(sentiment analysis)是2018年公布的計算機科學技術名詞。
它可以根據(jù)文本內(nèi)容判斷出所代表的含義是積極的還是負面的,也可以用來分析文本中的意思是褒義還是貶義。
一般應用場景就是能用來做電商的大量評論數(shù)據(jù)的分析,比如好評率或者差評率的統(tǒng)計等等。
我們這里使用到的情感分析的模塊是snownlp,為了提高情感分析的準確度選擇加入了jieba模塊的分詞處理。
由于以上的兩個python模塊都是非標準庫,因此我們可以使用pip的方式進行安裝。
pip?install?jieba pip?install?snownlp
jieba是一個強大的中文分詞處理庫,能夠滿足大多數(shù)的中文分詞處理,協(xié)助snownlp的情感分析。
#?Importing?the?jieba?module?and?renaming?it?to?ja. import?jieba?as?ja from?snownlp?import?SnowNLP #?Importing?the?snownlp?module?and?renaming?it?to?nlp.
為了避免大家使用過程中出現(xiàn)的版本沖突問題,這里將python的內(nèi)核版本展示出來。
python解釋器版本:3.6.8
接下來首先創(chuàng)建一組需要進行情感分的數(shù)據(jù)源,最后直接分析出該文本代表的是一個積極情緒還是消極情緒。
#?Creating?a?variable?called?analysis_text?and?assigning?it?the?value?of?a?string. analysis_text?=?'這個實在是太好用了,我非常的喜歡,下次一定還會購買的!'
定義好了需要分析的數(shù)據(jù)來源語句,然后就是分詞處理了。這里說明一下為什么需要分詞處理,是因為snownlp這個情感分析模塊它的中文分詞結果不太標準。
比如說,'不好看',這個詞如果使用snownlp來直接分詞的話大概率的就會分為'不'和'好看'這兩個詞。
這樣的明明是一個帶有負面情緒的中文詞匯可能就直接被定義為正面情緒了,這也就是為什么這里需要先使用jieba進行分詞處理了。
#?Using?the?jieba?module?to?cut?the?analysis_text?into?a?list?of?words. analysis_list?=?list(ja.cut(analysis_text)) #?Printing?the?list?of?words?that?were?cut?from?the?analysis_text. print(analysis_list) #?['這個', '實在', '是', '太', '好', '用', '了', ',', '我', '非常', '的', '喜歡', ',', '下次', '一定', '還會', '購買', '的', '!']
根據(jù)上面分詞以后的結果來看,分詞的粒度還是比較細致的,每個詞都是最多兩個字符串的長度。
使用jieba提供的cut()函數(shù),關鍵詞已經(jīng)分割完成了,接著就是提取主要的關鍵字。
一般情況下我們做情感分析都會提取形容詞類型的關鍵字,因為形容詞能夠代表該文本所表現(xiàn)出來的情緒。
#?Importing?the?`posseg`?module?from?the?`jieba`?module?and?renaming?it?to?`seg`. import?jieba.posseg?as?seg #?This?is?a?list?comprehension?that?is?creating?a?list?of?tuples.?Each?tuple?contains?the?word?and?the?flag. analysis_words?=?[(word.word,?word.flag)?for?word?in?seg.cut(analysis_text)] #?Printing?the?list?of?tuples?that?were?created?in?the?list?comprehension. print(analysis_words) #?[('這個', 'r'), ('實在', 'v'), ('是', 'v'), ('太', 'd'), ('好用', 'v'), ('了', 'ul'), (',', 'x'), ('我', 'r'), ('非常', 'd'), ('的', 'uj'), ('喜歡', 'v'), (',', 'x'), ('下次', 't'), ('一定', 'd'), ('還', 'd'), ('會', 'v'), ('購買', 'v'), ('的', 'uj'), ('!', 'x')]
根據(jù)上面的python推導式,將分詞以后的關鍵字和該關鍵自對應的詞性提取出來。
下面是一份jieba模塊使用過程中對應的詞性表,比如詞性標記a代表的就是形容詞。
#?This?is?a?list?comprehension?that?is?creating?a?list?of?tuples.?Each?tuple?contains?the?word?and?the?flag. keywords?=?[x?for?x?in?analysis_words?if?x[1]?in?['a',?'d',?'v']] #?Printing?the?list?of?tuples?that?were?created?in?the?list?comprehension. print(keywords) #?[('實在',?'v'),?('是',?'v'),?('太',?'d'),?('好用',?'v'),?('非常',?'d'),?('喜歡',?'v'),?('一定',?'d'),?('還',?'d'),?('會',?'v'),?('購買',?'v')]
根據(jù)關鍵詞的標簽提取出關鍵字以后,這個時候可以將情感標記去除只保留關鍵字就可以了。
#?This?is?a?list?comprehension?that?is?creating?a?list?of?words. keywords?=?[x[0]?for?x?in?keywords] #?Printing?the?list?of?keywords?that?were?created?in?the?list?comprehension. print(keywords) #?['實在',?'是',?'太',?'好用',?'非常',?'喜歡',?'一定',?'還',?'會',?'購買']
到現(xiàn)在為至,分詞的工作已經(jīng)處理完了,接下來就是情感分析直接使用snownlp分析出結果。
#?Creating?a?variable?called?`pos_num`?and?assigning?it?the?value?of?0. pos_num?=?0 #?Creating?a?variable?called?`neg_num`?and?assigning?it?the?value?of?0. neg_num?=?0 #?This?is?a?for?loop?that?is?looping?through?each?word?in?the?list?of?keywords. for?word?in?keywords: ????#?Creating?a?variable?called?`sl`?and?assigning?it?the?value?of?the?`SnowNLP`?function. ????sl?=?SnowNLP(word) ????#?This?is?an?if?statement?that?is?checking?to?see?if?the?sentiment?of?the?word?is?greater?than?0.5. ????if?sl.sentiments?>?0.5: ????????#?Adding?1?to?the?value?of?`pos_num`. ????????pos_num?=?pos_num?+?1 ????else: ????????#?Adding?1?to?the?value?of?`neg_num`. ????????neg_num?=?neg_num?+?1 ????#?This?is?printing?the?word?and?the?sentiment?of?the?word. ????print(word,?str(sl.sentiments))
下面就是對原始文本提取關鍵詞以后的每個詞的情感分析結果,0-1之間代表情緒越接近于1代表情緒表現(xiàn)的越是積極向上。
#?實在?0.3047790802524796 #?是?0.5262327818078083 #?太?0.34387502381406 #?好用?0.6558628208940429 #?非常?0.5262327818078083 #?喜歡?0.6994590939824207 #?一定?0.5262327818078083 #?還?0.5746682977321914 #?會?0.5539033457249072 #?購買?0.6502590673575129
為了使得關鍵詞的分析結果更加的符合我們的想法也可以對負面和正面的關鍵詞進行統(tǒng)計得到一個結果。
#?This?is?a?string?that?is?using?the?`format`?method?to?insert?the?value?of?`pos_num`?into?the?string. print('正面情緒關鍵詞數(shù)量:{}'.format(pos_num)) #?This?is?a?string?that?is?using?the?`format`?method?to?insert?the?value?of?`neg_num`?into?the?string. print('負面情緒關鍵詞數(shù)量:{}'.format(neg_num)) #?This?is?a?string?that?is?using?the?`format`?method?to?insert?the?value?of?`pos_num`?divided?by?the?value?of?`pos_num` #?plus?the?value?of?`neg_num`?into?the?string. print('正面情緒所占比例:{}'.format(pos_num/(pos_num?+?neg_num))) #?正面情緒關鍵詞數(shù)量:8 #?負面情緒關鍵詞數(shù)量:2 #?正面情緒所占比例:0.8
以上就是Python基于jieba分詞實現(xiàn)snownlp情感分析的詳細內(nèi)容,更多關于Python snownlp情感分析的資料請關注腳本之家其它相關文章!
相關文章
python里讀寫excel等數(shù)據(jù)文件的6種常用方式(小結)
這篇文章主要介紹了python里讀寫excel等數(shù)據(jù)文件的6種常用方式(小結),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04Python的Flask框架中實現(xiàn)簡單的登錄功能的教程
這篇文章主要介紹了Python的Flask框架中實現(xiàn)簡單的登錄功能的教程,登錄是各個web框架中的基礎功能,需要的朋友可以參考下2015-04-04Python報錯no?module?named?torch的幾種原因及解決方案
這篇文章主要給大家介紹了關于Python報錯no?module?named?torch的幾種原因及解決方案,這是小白時常犯的錯,這個報錯一般說明在你電腦當前環(huán)境下沒有安裝torch這個模塊,但也有其他情況,需要的朋友可以參考下2023-10-10