NLTK维德情绪强度分析仪Bigram

2024-05-29 11:00:20 发布

您现在位置:Python中文网/ 问答频道 /正文

对于Python中的维德情感强度分析器,有没有办法添加二元规则?我试着用两个单词的输入来更新词汇,但这并没有改变极性分数。提前谢谢

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

analyser = SentimentIntensityAnalyzer()

#returns a compound score of -0.296
print(analyser.polarity_scores('no issues'))

analyser.lexicon['no issues'] = 0.0
#still returns a compound score of -0.296
print(analyser.polarity_scores('no issues'))

Tags: ofno分析器returns情感scoreissuesprint
1条回答
网友
1楼 · 发布于 2024-05-29 11:00:20

在维德词典中没有直接的方法来添加bigram。这是因为维德考虑个人代币进行情绪分析。但是,可以使用以下步骤来完成此操作:

  1. 创建bigram作为标记。例如,您可以将bigram(“无问题”)转换为令牌(“噪音”)
  2. 维护一本词典,了解新事物的极性 创建令牌。{“噪音”:2}
  3. 然后执行附加的文本处理 通过文本进行情绪分数计算

以下代码完成了上述操作:

allowed_bigrams = {'noissues' : 2} #add more as per your requirement
    
def process_text(text):
    tokens = text.lower().split() # list of tokens
    bigrams = list(nltk.bigrams(tokens)) # create bigrams as tuples of tokens
    bigrams = list(map(''.join, bigrams)) # join each word without space to create new bigram
    bigrams.append('...') # make length of tokens and bigrams list equal
     
    #begin recreating the text
    final = ''
    for i, token in enumerate(tokens):
        b = bigrams[i]
        
        if b in allowed_bigrams:
          join_word = b # replace the word in text by bigram
          tokens[i+1] = '' #skip the next word
        else:
            join_word = token
        final += join_word + ' '
    return final
text  = 'Hello, I have no issues with you'
print (text)
print (analyser.polarity_scores(text))
final = process_text(text)
print (final)
print(analyser.polarity_scores(final))

输出:

Hello, I have no issues with you
{'neg': 0.268, 'neu': 0.732, 'pos': 0.0, 'compound': -0.296}
hello, i have noissues  with you 
{'neg': 0.0, 'neu': 0.625, 'pos': 0.375, 'compound': 0.4588}

请注意,在输出中,两个词“no”和“issues”是如何加在一起形成二元结构“noissues”的

相关问题 更多 >

    热门问题