为什么不检测blob/text?

2024-10-05 19:28:01 发布

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

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


Tags: examplenot情况analyzeranalyze例子best情绪
1条回答
网友
1楼 · 发布于 2024-10-05 19:28:01

(编辑:我以前的回答更多的是关于一般分类器,而不是关于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,

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

^{1}$

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

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

^{pr2}$

只需记住单词的顺序来否定形容词,而不是整个句子。在

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”,它将匹配其他部分,因为它不是一个形容词。在

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

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

我希望我能帮忙

相关问题 更多 >