繁体   English   中英

情绪分析中的否定处理

[英]Negation handling in sentiment analysis

我需要一点点帮助,我需要识别“不好”,“不坏”等否定词,然后确定情绪的极性(消极或积极)。 除了处理否定之外,我做了一切。 我只想知道如何将否定纳入其中。 我该怎么办呢?

否定处理是一个相当广泛的领域,具有许多不同的潜在实现。 在这里,我可以提供示例代码来否定一系列文本,并以not_形式存储否定的uni / bi / not_ 请注意,这里不使用nltk来支持简单的文本处理。

# negate_sequence(text)
#   text: sentence to process (creation of uni/bi/trigrams
#    is handled here)
#
# Detects negations and transforms negated words into 'not_' form
#
def negate_sequence(text):
    negation = False
    delims = "?.,!:;"
    result = []
    words = text.split()
    prev = None
    pprev = None
    for word in words:
        stripped = word.strip(delims).lower()
        negated = "not_" + stripped if negation else stripped
        result.append(negated)
        if prev:
            bigram = prev + " " + negated
            result.append(bigram)
            if pprev:
                trigram = pprev + " " + bigram
                result.append(trigram)
            pprev = prev
        prev = negated

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

        if any(c in word for c in delims):
            negation = False

    return result

如果我们在一个示例输入text = "I am not happy today, and I am not feeling well"上运行这个程序text = "I am not happy today, and I am not feeling well" ,我们获得了以下unigrams,bigrams和trigrams序列:

[   'i',
    'am',
    'i am',
    'not',
    'am not',
    'i am not',
    'not_happy',
    'not not_happy',
    'am not not_happy',
    'not_today',
    'not_happy not_today',
    'not not_happy not_today',
    'and',
    'not_today and',
    'not_happy not_today and',
    'i',
    'and i',
    'not_today and i',
    'am',
    'i am',
    'and i am',
    'not',
    'am not',
    'i am not',
    'not_feeling',
    'not not_feeling',
    'am not not_feeling',
    'not_well',
    'not_feeling not_well',
    'not not_feeling not_well']

我们随后可以将这些三元组存储在一个阵列中,以便将来进行后退和分析。 not_ words处理为您为其对应not_的[情绪,极性]的否定。

我已经有一段时间从事情绪分析了,所以不确定这个区域的现状是什么,无论如何我从未使用过nltk。 所以我不能指出你那里的任何东西。 但总的来说,我认为可以说这是一个活跃的研究领域,也是NLP的重要组成部分。 而且这肯定不是一个已经“解决”的问题。 它是NLP更精细,更有趣的领域之一,涉及讽刺,sarcams,范围(否定)。 通常,提出正确的分析意味着解释许多背景/领域/话语信息。 这根本不是直截了当的。 您可能需要查看此主题: 算法可以检测讽刺 一些谷歌搜索可能会给你更多的信息。

简而言之; 你的问题太广泛了,无法提出具体的答案。

另外,我想知道你的意思是“除了处理否定之外,我做了一切”。 你的意思是你发现了'消极'字样? 您是否认为这些信息可以传达的内容远远超过“不”,“不”等字样? 例如,考虑“你的解决方案不好”与“你的解决方案不是最理想的”。 你究竟在寻找什么,以及在你的情况下什么就足够了,显然取决于应用的背景和领域。 这可能不是你所希望的答案,但我建议你做一些更多的研究(因为在这个领域聪明人已经完成了很多聪明的事情)。

这看起来像是一个穷人在python中的否定词。 它绝对不是完美的,但对某些情况可能有用。 它需要一个spacy句子对象。

def word_is_negated(word):
    """ """

    for child in word.children:
        if child.dep_ == 'neg':
            return True

    if word.pos_ in {'VERB'}:
        for ancestor in word.ancestors:
            if ancestor.pos_ in {'VERB'}:
                for child2 in ancestor.children:
                    if child2.dep_ == 'neg':
                        return True

    return False

def find_negated_wordSentIdxs_in_sent(sent, idxs_of_interest=None):
    """ """

    negated_word_idxs = set()
    for word_sent_idx, word in enumerate(sent):
        if idxs_of_interest:
            if word_sent_idx not in idxs_of_interest:
                continue
        if word_is_negated(word):
            negated_word_idxs.add(word_sent_idx)

    return negated_word_idxs

这样叫:

import spacy
nlp = spacy.load('en_core_web_lg')
find_negated_wordSentIdxs_in_sent(nlp("I have hope, but I do not like summer"))

编辑:正如@Amandeep所指出的,根据你的使用案例,你可能还想在行中包含NOUNS,ADJECTIVES,ADVERBS: if word.pos_ in {'VERB'}: .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM