use*_*960 6 python nltk sentiment-analysis
我需要一点点帮助,我需要识别"不好","不坏"等否定词,然后确定情绪的极性(消极或积极).除了处理否定之外,我做了一切.我只是想知道如何将否定纳入其中.我该怎么办呢?
否定处理是一个相当广泛的领域,具有许多不同的潜在实现.在这里,我可以提供示例代码来否定一系列文本并在not_表单中存储否定的uni/bi/trigrams .请注意,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
Run Code Online (Sandbox Code Playgroud)
如果我们在示例输入上运行此程序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']
Run Code Online (Sandbox Code Playgroud)
我们随后可以将这些三元组存储在一个阵列中,以便将来进行后退和分析.将这些not_词处理为你为对方定义的[情绪,极性]的负面词.
自从我从事情感分析工作以来已经有一段时间了,所以不确定这个领域现在的状况,无论如何我从来没有使用过nltk。所以我无法向您指出那里的任何内容。但总的来说,我认为可以肯定地说,这是一个活跃的研究领域,也是 NLP 的重要组成部分。这肯定不是一个已经“解决”的问题。这是 NLP 中更精细、更有趣的领域之一,涉及反讽、讽刺、(否定)范围。通常,提出正确的分析意味着解释大量上下文/领域/话语信息。这根本就不简单。您可能想看看这个主题:算法能否检测讽刺。一些谷歌搜索可能会给你更多的信息。
简而言之; 你的问题太宽泛,无法给出具体答案。
另外,我想知道你所说的“除了处理否定之外我做了所有事情”是什么意思。你的意思是你识别出了“负面”词语?您是否考虑过,除了“不”、“不”等词语之外,还可以通过更多方式传达此信息?例如,考虑“您的解决方案不好”与“您的解决方案次优”。您到底在寻找什么,以及什么足以满足您的情况,显然取决于应用程序的上下文和领域。这可能不是您所希望的答案,但我建议您多做一些研究(因为这个领域的聪明人已经做了很多聪明的事情)。