为什么 TextBlob 不使用/检测否定?

Why is not TextBlob using / detecting the negation?

我正在使用 TextBlob 执行情绪分析任务。我注意到 TextBlob 在某些情况下能够检测到否定,而在其他情况下则不能。

这里有两个简单的例子

>>> from textblob.sentiments import PatternAnalyzer

>>> sentiment_analyzer = PatternAnalyzer()
# example 1
>>> sentiment_analyzer.analyze('This is good')
Sentiment(polarity=0.7, subjectivity=0.6000000000000001)

>>> sentiment_analyzer.analyze('This is not good')
Sentiment(polarity=-0.35, subjectivity=0.6000000000000001)

# example 2
>>> sentiment_analyzer.analyze('I am the best')
Sentiment(polarity=1.0, subjectivity=0.3)

>>> sentiment_analyzer.analyze('I am not the best')  
Sentiment(polarity=1.0, subjectivity=0.3)

正如您在第二个示例中看到的,当使用形容词 best 时,极性没有改变。我怀疑这与形容词 best 是一个非常强大的指标这一事实有关,但似乎不正确,因为否定应该颠倒极性(在我的理解中)。

谁能解释一下这是怎么回事? textblob 到底是在使用某种否定机制,还是只是 not 这个词在句子中添加了负面情绪?在任何一种情况下,为什么第二个例子在两种情况下都具有完全相同的情绪?关于如何克服这些障碍,有什么建议吗?

(编辑:我的旧答案更多是关于一般分类器而不是关于 PatternAnalyzer)

TextBlob 在您的代码中使用“PatternAnalyzer”。该文档中简要描述了它的行为:http://www.clips.ua.ac.be/pages/pattern-en#parser

我们可以看到:

The pattern.en module bundles a lexicon of adjectives (e.g., good, bad, amazing, irritating, ...) that occur frequently in product reviews, annotated with scores for sentiment polarity (positive ↔ negative) and subjectivity (objective ↔ subjective).

The sentiment() function returns a (polarity, subjectivity)-tuple for the given sentence, based on the adjectives it contains,

这是一个展示算法行为的例子。极性直接取决于所使用的形容词。

sentiment_analyzer.analyze('player')
Sentiment(polarity=0.0, subjectivity=0.0)

sentiment_analyzer.analyze('bad player')
Sentiment(polarity=-0.6999998, subjectivity=0.66666)

sentiment_analyzer.analyze('worst player')
Sentiment(polarity=-1.0, subjectivity=1.0)

sentiment_analyzer.analyze('best player')
Sentiment(polarity=1.0, subjectivity=0.3)

专业软件通常使用基于神经网络和分类器并结合词法分析的复杂工具。但对我来说,TextBlob 只是试图根据 语法分析的直接结果 (这里是形容词的极性)给出结果。这是问题的根源。

不会尝试检查一般句子是否否定(使用“不”字)。它会尝试检查 形容词是否被否定 (因为它只适用于形容词,不适用于一般结构)。 best在这里用作名词,而不是否定形容词。所以,极性是正的。

sentiment_analyzer.analyze('not the best')
Sentiment(polarity=1.0, subjectivity=0.3)

只需更换单词的顺序即可对形容词而不是整个句子进行否定。

sentiment_analyzer.analyze('the not best')
Sentiment(polarity=-0.5, subjectivity=0.3)

这里,形容词被否定。所以,极性是负的。 这是我对“奇怪行为”的解释。


真正的实现在文件中定义: https://github.com/sloria/TextBlob/blob/dev/textblob/_text.py

中间部分由:

if w in self and pos in self[w]:
    p, s, i = self[w][pos]
    # Known word not preceded by a modifier ("good").
    if m is None:
        a.append(dict(w=[w], p=p, s=s, i=i, n=1, x=self.labeler.get(w)))
    # Known word preceded by a modifier ("really good").
    
    ...
    

else:
    # Unknown word may be a negation ("not good").
    if negation and w in self.negations:
        n = w
    # Unknown word. Retain negation across small words ("not a good").
    elif n and len(w.strip("'")) > 1:
        n = None
    # Unknown word may be a negation preceded by a modifier ("really not good").
    if n is not None and m is not None and (pos in self.modifiers or self.modifier(m[0])):
        a[-1]["w"].append(n)
        a[-1]["n"] = -1
        n = None
    # Unknown word. Retain modifier across small words ("really is a good").
    elif m and len(w) > 2:
        m = None
    # Exclamation marks boost previous word.
    if w == "!" and len(a) > 0:
    
    ...

如果我们输入“not a good”或“not the good”,它将匹配 else 部分,因为它不是单个形容词。

“不好”的部分将与 elif n and len(w.strip("'")) > 1: 匹配,因此它会反转极性。 not the good 不会匹配任何模式,因此,极性将与“最佳”相同。

整个代码是一连串的微调,语法指示(例如添加!增加极性,添加笑脸表示讽刺,...)。这就是为什么某些特定模式会产生奇怪结果的原因。要处理每个特定情况,您必须检查您的句子是否与该部分代码中的任何 if 句子匹配。

希望能帮到你