NLP(十四) 情感分析
Posted peng8098
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NLP(十四) 情感分析相关的知识,希望对你有一定的参考价值。
- 情感在自然语言中的表达方式
例句 | 解释 |
---|---|
I am very happy | 开心的情感 |
She is so :( | 表达悲伤的图标 |
import nltk
import nltk.sentiment.sentiment_analyzer
def wordBasedSentiment():
positive_words = ['love','hope','joy']
text = 'Rainfall this year brings lot of hope and joy to Farmers.'.split()
analysis = nltk.sentiment.util.extract_unigram_feats(text,positive_words)
# 查看text中是否含positive_words中的单词
print('-- single word sentiment --')
print(analysis)
def multiWordBasedSentiment():
word_sets = [('heavy','rains'),('flood','bengaluru')]
text = 'heavy rains cause flash flooding in bengaluru'.split()
analysis = nltk.sentiment.util.extract_bigram_feats(text,word_sets)
# 查看text中是否含word_sets中的单词集
print('-- multi word sentiment --')
print(analysis)
def markNegativity():
text = 'Rainfall last year did not bring joy to Farmers'.split()
negation = nltk.sentiment.util.mark_negation(text) # 负向性单词分析
print('-- negativity --')
print(negation)
if __name__ == "__main__":
wordBasedSentiment()
multiWordBasedSentiment()
markNegativity()
输出:
-- single word sentiment --
'contains(love)': False, 'contains(hope)': True, 'contains(joy)': True
-- multi word sentiment --
'contains(heavy - rains)': True, 'contains(flood - bengaluru)': False
-- negativity --
['Rainfall', 'last', 'year', 'did', 'not', 'bring_NEG', 'joy_NEG', 'to_NEG', 'Farmers_NEG']
- 高阶情感分析
import nltk
import nltk.sentiment.util
import nltk.sentiment.sentiment_analyzer
from nltk.sentiment.vader import SentimentIntensityAnalyzer
def mySentimentAnalyzer():
def score_feedback(text): # 输入句子 输出得分 1正 0中性 -1负
# 优先级 -1 => +1 => 0
positive_words = ['love','genuine','liked']
# 先用函数标记,如果词标记了_NEG,得分为-1
if '_NEG' in ' '.join(nltk.sentiment.util.mark_negation(text.split())):
score = -1
else:
analysis = nltk.sentiment.util.extract_unigram_feats(text.split(),positive_words)
if True in analysis.values(): # 如果存在文本存在正向词
score = 1
else:
score = 0
return score
feedback = """I love the items in this shop, very genuine and quality is well maintained.
I have visited this shop and had samosa, my friends liked it very much.
ok average food in this shop.
Fridays are very busy in this shop, do not place orders during this day."""
print('-- custom scorer --')
for text in feedback.split('\n'): # 分句遍历
print('score = for >> '.format(score_feedback(text),text))
def advancedSentimentAnalyzer():
sentences = [
':)',
':(',
'she is so :(',
'I love the way cricket is played by the champions',
'She neither likes coffee or tea',
]
senti = SentimentIntensityAnalyzer()
print('-- built-in intensity analyser --')
for sentence in sentences:
print('[]'.format(sentence),end=' --> ') # 打印每个句子
kvp = senti.polarity_scores(sentence) # 处理句子,得到各种得分得字典
for k in kvp: # 遍历字典
print(' = , '.format(k,kvp[k]),end='')
print()
if __name__ == "__main__":
mySentimentAnalyzer()
advancedSentimentAnalyzer()
输出:
-- custom scorer --
score = 1 for >> I love the items in this shop, very genuine and quality is well maintained.
score = 1 for >> I have visited this shop and had samosa, my friends liked it very much.
score = 0 for >> ok average food in this shop.
score = -1 for >> Fridays are very busy in this shop, do not place orders during this day.
-- built-in intensity analyser --
[:)] --> neg = 0.0, neu = 0.0, pos = 1.0, compound = 0.4588,
[:(] --> neg = 1.0, neu = 0.0, pos = 0.0, compound = -0.4404,
[she is so :(] --> neg = 0.555, neu = 0.445, pos = 0.0, compound = -0.5777,
[I love the way cricket is played by the champions] --> neg = 0.0, neu = 0.375, pos = 0.625, compound = 0.875,
[She neither likes coffee or tea] --> neg = 0.318, neu = 0.682, pos = 0.0, compound = -0.3252,
以上是关于NLP(十四) 情感分析的主要内容,如果未能解决你的问题,请参考以下文章