【问题标题】:Why is not TextBlob using / detecting the negation?为什么 TextBlob 不使用/检测否定?
【发布时间】:2016-10-04 16:18:09
【问题描述】:

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

【问题讨论】:

    标签: python sentiment-analysis textblob


    【解决方案1】:

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

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

    我们可以看到:

    pattern.en 模块捆绑了一个形容词词典(例如,好的、坏的、令人惊奇的、恼人的……),这些词经常出现在产品评论中,并带有情绪分数的注释极性(正面 ↔ 负面)和主观性(客观 ↔ 主观)。

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

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

    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 只是试图根据语法分析的直接结果(这里是形容词的极性)给出结果。这是问题的根源。

    不会尝试检查一般句子是否否定(使用“not”词)。它尝试检查形容词是否被否定(因为它只适用于形容词,不适用于一般结构)。在这里,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 句子匹配。

    希望能帮到你

    【讨论】:

    • 浏览 TextBlob 的文档以了解默认情绪分类器的工作原理(PatternAnalyzer 是默认分类器)我认为您错了。 PatternAnalyzer 确实使用语法分析。如果您想了解更多信息,请查看here。很快,它使用了一个带注释的词的词典,然后在词典中的分数和句子的属性之间执行各种映射。它也在积极寻找否定,你可以看到here
    • 所以也许你的回答真正描述了 TextBlob 中的其他分类器是如何工作的,比如 NaiveBayerClassifier,但我还没有完成这些部分的实现。我怀疑尽管总是涉及语法分析。通过特征提取或其他类型的启发式方法。
    • 好吧,我的错。因此,对于模式分析器,答案将基于该文档:clips.ua.ac.be/pages/pattern-en#parser(模式分析器文档)。 > Sentiment() 函数返回给定句子的(极性,主观性)元组,基于它包含的形容词所以,因为 not 不是形容词,它不会影响极性。您可以检查:“播放器”。它具有零极性。但是,如果你做“最佳玩家”,它就会有一个一元极性。 “最差玩家”会给 -1 我会将其添加到我的帖子中。
    • 这可能更接近正确答案,但我再次不确定它是否完全正确,或者更好的是完全准确。看,例如,如果您接受句子“我不是最好的”的情绪,则否定被成功应用,而 athe 根据 textblob pos tagger 在句子中扮演完全相同的角色。
    • 我认为这是因为它把“不是最好的”这个组作为一个否定形容词。对我来说,它分为“(不是最好的)球员”和“不是(最好的球员)”。这就是为什么第一种情况是负面的,而第二种情况是正面的。但是,这只是我对问题的解释。当然,这非常棘手,因为它是一种错误,而不是功能。我想我已经找到了为什么这句话是一个极端案例。为了全面解释,我们需要考虑 textblob 分析器的确切实现:github.com/sloria/TextBlob/tree/dev/textblob
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    相关资源
    最近更新 更多