【发布时间】:2019-07-17 00:58:36
【问题描述】:
我在这里需要一点帮助,我需要识别“不好”、“不错”等负面词,然后识别情绪的极性(消极或积极)。除了处理否定之外,我什么都做了。我只想知道如何在其中包含否定。我该怎么办?
【问题讨论】:
标签: python nltk sentiment-analysis
我在这里需要一点帮助,我需要识别“不好”、“不错”等负面词,然后识别情绪的极性(消极或积极)。除了处理否定之外,我什么都做了。我只想知道如何在其中包含否定。我该怎么办?
【问题讨论】:
标签: python nltk sentiment-analysis
否定处理是一个相当广泛的领域,有许多不同的潜在实现。在这里,我可以提供对文本序列求反并以not_ 形式存储求反的 uni/bi/trigram 的示例代码。请注意,此处未使用 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" 上运行此程序,我们会获得以下一元、二元和三元序列:
[ '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_ 词处理为您为其对应对象定义的[情绪,极性] 的否定词。
【讨论】:
这似乎在python中作为一个穷人的单词否定工作得很好。它绝对不是完美的,但在某些情况下可能有用。它需要一个空的句子对象。
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 指出的那样,根据您的用例,您可能还希望在以下行中包含名词、形容词、副词:if word.pos_ in {'VERB'}:。
【讨论】:
python -m spacy download en_core_web_lg(实际下载lib以供使用)。
自从我从事情绪分析以来已经有一段时间了,所以不确定这个领域现在的状态如何,无论如何我从来没有使用过nltk。所以我无法向你指出那里的任何东西。但总的来说,我认为可以肯定地说这是一个活跃的研究领域,也是 NLP 的重要组成部分。这肯定不是一个已经“解决”的问题。它是 NLP 中更精细、更有趣的领域之一,涉及反讽、讽刺、范围(否定)。通常,提出正确的分析意味着解释大量的上下文/领域/话语信息。这一点都不简单。 你可能想看看这个主题:Can an algorithm detect sarcasm。一些谷歌搜索可能会给你更多的信息。
简而言之;你的问题太宽泛了,无法给出具体的答案。
另外,我想知道您所说的“除了处理否定之外,我什么都做了”是什么意思。您的意思是您确定了“负面”词?您是否考虑过这些信息可以用更多的词来传达,而不是“不”、“不”等词?例如,考虑“您的解决方案不好”与“您的解决方案次优”。 您到底在寻找什么,以及在您的情况下什么就足够了,显然取决于应用程序的上下文和领域。 这可能不是您希望的答案,但我建议您做更多的研究(因为该领域的聪明人已经做了很多聪明的事情)。
【讨论】: