如何构建函数来检测情绪分析中的否定

how to build function to detect negation in sentiment analysis

我想为马来语文本建立否定检测,它是为了解决像 'not beautiful' 被检测为肯定词这样的问题。所以这是我修改的一些代码,但结果不是我想要的。

结果是

text= "is not good, danish died,"
se=negate(self=None,text=text)
print(se)
['is', 'not', 'not_good', 'not_danish', 'not_died']

I wanted it to be
['is', 'not', 'not_good', 'danish', 'died']

只有"not"后的词被改为"not_"形式。 这是我使用的功能,有什么建议可以更改和添加以获得我想要的结果吗?

def negate(self,text):

    negation = False
    result = []
    words = text.split()

    for word in words:
        # stripped = word.strip(delchars)
        stripped = word.strip(delims).lower()
        negated = "not_" + stripped if negation else stripped
        result.append(negated)

        if any(neg in word for neg in ["not", "n't", "no"]):
            negation = not negation

    return result

如果我理解你的问题是正确的,你只想否定紧跟在 "not" 或 "n't" 或 "no" 之后的词。

因此,在将否定词添加到结果后,将否定设置为假,在 "not" 之后,将否定设置为真