【问题标题】:How is the Vader 'compound' polarity score calculated in Python NLTK?如何在 Python NLTK 中计算 Vader 'compound' 极性分数?
【发布时间】:2016-10-30 04:15:52
【问题描述】:

我正在使用 Vader SentimentAnalyzer 来获取极性分数。我之前使用了正/负/中性的概率分数,但我刚刚意识到“复合”分数,范围从 -1(最负)到 1(最正)将提供一个单一的极性度量。我想知道“复合”分数是如何计算的。是从 [pos, neu, neg] 向量计算的吗?

【问题讨论】:

标签: python nlp nltk sentiment-analysis vader


【解决方案1】:

VADER 算法将情感分数输出到 4 类情感https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L441

  • neg:否定
  • neu:中立
  • pos:肯定的
  • compound:复合(即总分)

让我们看一下代码,compound 的第一个实例位于 https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L421,它在此处进行计算:

compound = normalize(sum_s)

normalize() 函数在https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L107 中定义为:

def normalize(score, alpha=15):
    """
    Normalize the score to be between -1 and 1 using an alpha that
    approximates the max expected value
    """
    norm_score = score/math.sqrt((score*score) + alpha)
    return norm_score

所以有一个超参数alpha

对于sum_s,它是传递给score_valence()函数https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L413的情绪参数的总和

如果我们追溯这个sentiment 参数,我们会看到它是在https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L217 调用polarity_scores() 函数时计算的:

def polarity_scores(self, text):
    """
    Return a float for sentiment strength based on the input text.
    Positive values are positive valence, negative value are negative
    valence.
    """
    sentitext = SentiText(text)
    #text, words_and_emoticons, is_cap_diff = self.preprocess(text)

    sentiments = []
    words_and_emoticons = sentitext.words_and_emoticons
    for item in words_and_emoticons:
        valence = 0
        i = words_and_emoticons.index(item)
        if (i < len(words_and_emoticons) - 1 and item.lower() == "kind" and \
            words_and_emoticons[i+1].lower() == "of") or \
            item.lower() in BOOSTER_DICT:
            sentiments.append(valence)
            continue

        sentiments = self.sentiment_valence(valence, sentitext, item, i, sentiments)

    sentiments = self._but_check(words_and_emoticons, sentiments)

查看polarity_scores 函数,它所做的是遍历整个 SentiText 词典并检查基于规则的 sentiment_valence() 函数以将价分数分配给情绪 https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L243,请参阅第 2.1.1 节http://comp.social.gatech.edu/papers/icwsm14.vader.hutto.pdf

所以回到复合分数,我们看到:

  • compound 分数是sum_s 的归一化分数,并且
  • sum_s 是根据一些启发式算法和情感词典(又名情感强度)计算的效价和
  • 归一化分数只是sum_s 除以其平方加上一个增加归一化函数分母的 alpha 参数。

是根据 [pos, neu, neg] 向量计算的吗?

不是真的=)

如果我们看一下score_valence 函数https://github.com/nltk/nltk/blob/develop/nltk/sentiment/vader.py#L411,我们会看到复合分数是使用sum_s 计算的,然后是使用计算单个位置的_sift_sentiment_scores() 计算 pos、neg 和 neu 分数, neg 和 neu 使用来自sentiment_valence() 的原始分数而不是总和。


如果我们看一下这个alpha mathemagic,归一化的输出似乎相当不稳定(如果不受约束),取决于alpha 的值:

alpha=0:

alpha=15:

alpha=50000:

alpha=0.001:

当它是负面的时候会变得很时髦:

alpha=-10:

alpha=-1,000,000:

alpha=-1,000,000,000:

【讨论】:

  • 很好的解释,好像你在图表和方程式中缺少 sqrt 部分
  • 嗨,Alvas,我可以请你看看我在这里发布的问题:stackoverflow.com/questions/51707282/…
  • 我认为图中的函数缺少平方根。函数是score/math.sqrt(score*score+alpha),但您正在绘制score/(score*score+alpha)。否则,很好的分析!
【解决方案2】:

github repo 的“关于评分”部分有说明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多