中文情感分析python
① 用python對中文做情感分析,有沒有好的介面推薦
import jieba
import numpy as np
# 打開詞典文件,返回列表
def open_dict(Dict='hahah',path = r'/Users/zhangzhenghai/Downloads/Textming/'):
path = path + '%s.txt' %Dict
dictionary = open(path, 'r', encoding='utf-8')
dict = []
for word in dictionary:
word = word.strip('\n')
dict.append(word)
return dict
def judgeodd(num):
if num % 2 == 0:
return 'even'
else:
return 'odd'
deny_word = open_dict(Dict='否定詞')
posdict = open_dict(Dict='positive')
negdict = open_dict(Dict = 'negative')
degree_word = open_dict(Dict = '程度級別詞語',path=r'/Users/zhangzhenghai/Downloads/Textming/')
mostdict = degree_word[degree_word.index('extreme')+1: degree_word.index('very')] #權重4,即在情感前乘以3
verydict = degree_word[degree_word.index('very')+1: degree_word.index('more')] #權重3
moredict = degree_word[degree_word.index('more')+1: degree_word.index('ish')]#權重2
ishdict = degree_word[degree_word.index('ish')+1: degree_word.index('last')]#權重0.5
def sentiment_score_list(dataset):
seg_sentence = dataset.split('。')
count1 = []
count2 = []
for sen in seg_sentence: # 循環遍歷每一個評論
segtmp = jieba.lcut(sen, cut_all=False) # 把句子進行分詞,以列表的形式返回
i = 0 #記錄掃描到的詞的位置
a = 0 #記錄情感詞的位置
poscount = 0 # 積極詞的第一次分值
poscount2 = 0 # 積極反轉後的分值
poscount3 = 0 # 積極詞的最後分值(包括嘆號的分值)
negcount = 0
negcount2 = 0
negcount3 = 0
for word in segtmp:
if word in posdict: # 判斷詞語是否是情感詞
poscount +=1
c = 0
for w in segtmp[a:i]: # 掃描情感詞前的程度詞
if w in mostdict:
poscount *= 4.0
elif w in verydict:
poscount *= 3.0
elif w in moredict:
poscount *= 2.0
elif w in ishdict:
poscount *= 0.5
elif w in deny_word: c+= 1
if judgeodd(c) == 'odd': # 掃描情感詞前的否定詞數
poscount *= -1.0
poscount2 += poscount
poscount = 0
poscount3 = poscount + poscount2 + poscount3
poscount2 = 0
else:
poscount3 = poscount + poscount2 + poscount3
poscount = 0
a = i+1
elif word in negdict: # 消極情感的分析,與上面一致
negcount += 1
d = 0
for w in segtmp[a:i]:
if w in mostdict:
negcount *= 4.0
elif w in verydict:
negcount *= 3.0
elif w in moredict:
negcount *= 2.0
elif w in ishdict:
negcount *= 0.5
elif w in degree_word:
d += 1
if judgeodd(d) == 'odd':
negcount *= -1.0
negcount2 += negcount
negcount = 0
negcount3 = negcount + negcount2 + negcount3
negcount2 = 0
else:
negcount3 = negcount + negcount2 + negcount3
negcount = 0
a = i + 1
elif word == '!' or word == '!': # 判斷句子是否有感嘆號
for w2 in segtmp[::-1]: # 掃描感嘆號前的情感詞,發現後權值+2,然後退出循環
if w2 in posdict or negdict:
poscount3 += 2
negcount3 += 2
break
i += 1
# 以下是防止出現負數的情況
pos_count = 0
neg_count = 0
if poscount3 <0 and negcount3 > 0:
neg_count += negcount3 - poscount3
pos_count = 0
elif negcount3 <0 and poscount3 > 0:
pos_count = poscount3 - negcount3
neg_count = 0
elif poscount3 <0 and negcount3 < 0:
neg_count = -pos_count
pos_count = -neg_count
else:
pos_count = poscount3
neg_count = negcount3
count1.append([pos_count,neg_count])
count2.append(count1)
count1=[]
return count2
def sentiment_score(senti_score_list):
score = []
for review in senti_score_list:
score_array = np.array(review)
Pos = np.sum(score_array[:,0])
Neg = np.sum(score_array[:,1])
AvgPos = np.mean(score_array[:,0])
AvgPos = float('%.lf' % AvgPos)
AvgNeg = np.mean(score_array[:, 1])
AvgNeg = float('%.1f' % AvgNeg)
StdPos = np.std(score_array[:, 0])
StdPos = float('%.1f' % StdPos)
StdNeg = np.std(score_array[:, 1])
StdNeg = float('%.1f' % StdNeg)
score.append([Pos,Neg,AvgPos,AvgNeg,StdPos,StdNeg])
return score
data = '用了幾天又來評價的,手機一點也不卡,玩榮耀的什麼的不是問題,充電快,電池夠大,玩游戲可以玩幾個小時,待機應該可以兩三天吧,很贊'
data2 = '不知道怎麼講,真心不怎麼喜歡,通話時聲音小,新手機來電話竟然卡住了接不了,原本打算退,剛剛手機摔了,又退不了,感覺不會再愛,像素不知道是我不懂還是怎麼滴 感覺還沒z11mini好,哎要我怎麼評價 要我如何喜歡努比亞 太失望了'
print(sentiment_score(sentiment_score_list(data)))
print(sentiment_score(sentiment_score_list(data2)))
② 如何用Python和機器學習訓練中文文本情感分類模
Python在科學計算領域,有兩個重要的擴展模塊:Numpy和Scipy。其中Numpy是一個用python實現的科學計算包。包括: 一個強大的N維數組對象Array; 比較成熟的(廣播)函數庫; 用於整合C/C++和Fortran代碼的工具包; 實用的線性代數
③ 怎樣用python處理文本情感分析
Python 有良好的程序包可以進行情感分類,那就是Python 自然語言處理包,Natural Language Toolkit ,簡稱NLTK 。NLTK 當然不只是處理情感分析,NLTK 有著整套自然語言處理的工具,從分詞到實體識別,從情感分類到句法分析,完整而豐富,功能強大。
④ python情感分析怎麼擴展情緒類型
安裝snownlp。
WIN鍵+R輸入jupyternotebook。
我們使用的是SnowNLP,SnowNLP是一個用Python寫的可以方便的處理中文文本內容類庫,是受到了TextBlob的啟發而寫的。
⑤ 如何使用python做中文情感分析
pip install snownlppip install -U
textblobpython -m textblob.download_corpora
⑥ 用python做自然語言處理,中文情感分析用貝葉斯分類器不行嗎
中文已經分好詞了,用貝葉斯分類器不行嗎?classifier = NaiveBayesClassifier.train(traintext) 只有五十條訓練集而且每個都不長啊。怎麼會有這么多特徵呢
這句話應該不是說你feature太多了,而是說for循環中,使用了兩個變數去unpack featuresets太多了。
所以應該是你的數據結構有問題,featuresets可能不是適合兩個變數來解包的數據結構,或者中文編碼有問題。
⑦ 如何用Python做情感分析
可以使用snownlp包,也可以用nltk 和 scikit-learn 結合,或者自己寫演算法實現。
簡單話就是情感詞典的匹配,想提高效果的需要考慮特徵之間的搭配,語法順序等,可以查詢搜索相關的入門例子和演算法詳細了解。
⑧ 用python找文獻,並從文本中分析情緒,做一個數據分析
到你的系統「終端」(macOS, Linux)或者「命令提示符」(Windows)下,進入我們的工作目錄demo,執行以下命令。
pip install snownlppip install -U textblobpython -m textblob.download_corpora
好了,至此你的情感分析運行環境已經配置完畢。
在終端或者命令提示符下鍵入:
jupyter notebook
你會看到目錄里之前的那些文件,忽略他們就好。
⑨ github中的python程序編寫的基於中文文本的情感分析怎麼運行
麻煩你吧問題描述清楚,如果你想問github上的某個項目怎麼使用,麻煩吧項目名發出來,你這樣問題很難解決
⑩ 給了一堆數據 用python做文本情感分析 但是課題要求是事先將無意義的評論去處 這要怎麼做
既然你已經學到了數據分析,那麼基本的語法應該大都知道了吧。
這無非就是篩選數據的問題,先搞清楚什麼是「無意義的評論」,它滿足什麼條件,再遍歷評論,如果滿足這個「無意義」的條件,那麼就刪除掉就是了。