4

我正在使用 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在句子中添加了负面情绪?无论哪种情况,为什么第二个示例在两种情况下都具有完全相同的情绪?关于如何克服这些障碍有什么建议吗?

4

1 回答 1

4

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

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

我们可以看到:

pattern.en 模块捆绑了产品评论中经常出现的形容词词典(例如,好、坏、惊人、恼人……),并用情感极性(正面 ↔ 负面)和主观性(客观 ↔ 主观)的分数进行注释.

情感()函数返回给定句子的(极性,主观性)元组,基于它包含的形容词

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

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 句子匹配。

我希望我会有所帮助

于 2016-06-04T19:46:40.130 回答