我正在使用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
我们可以看到:
下面是一个显示算法行为的示例。极性直接取决于所使用的形容词。在
^{1}$专业软件通常使用基于神经网络和分类器与词汇分析相结合的复杂工具。但对我来说,TextBlob只是试图根据语法分析的直接结果(这里是形容词的极性)给出一个结果。这是问题的根源。在
它不试图检查一般句子是否是否定的(用“not”这个词)。它试图检查形容词是否被否定(因为它只对形容词起作用,而不适用于一般结构)。在这里,best用作名词,而不是否定形容词。所以,极性是正的。在
^{pr2}$只需记住单词的顺序来否定形容词,而不是整个句子。在
这里,形容词是否定的。所以,极性是负的。 这是我对“奇怪行为”的解释。在
真正的实现在文件中定义: https://github.com/sloria/TextBlob/blob/dev/textblob/_text.py
交错部分由以下公式给出:
如果我们输入“not a good”或“not the good”,它将匹配其他部分,因为它不是一个形容词。在
“不好”部分将匹配
elif n and len(w.strip("'")) > 1:
,因此它将反转极性。not the good
将不匹配任何模式,因此,极性将与“最佳”相同。在整个代码是一系列精细的调整,语法说明(例如添加!增加极性,加一个笑脸表示讽刺……)。这就是为什么某些特定的模式会产生奇怪的结果。要处理每个特定的情况,您必须检查您的句子是否与代码中该部分中的任何if语句匹配。在
我希望我能帮忙
相关问题 更多 >
编程相关推荐