首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >情感分析中的否定检测

情感分析中的否定检测
EN

Code Review用户
提问于 2016-10-20 12:55:50
回答 2查看 2.9K关注 0票数 2

这一基本功能是在进行情绪分析时检测否定的。所以not good这类词可以被认为是否定的。

这段代码运行良好。有人能帮助改进它或发现是否有任何错误存在吗?

代码语言:javascript
复制
def negate_sequence(self,text):
    """
    Detects negations and transforms negated words into "not_" form.
    """
    negation = False
    delims = "?.,!:;"
    result = []
#Here rather then applying split, we can directly feed our extracted symptoms list
    words = text.split()
    prev = None
    pprev = None
    for word in words:
        # stripped = word.strip(delchars)
        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
EN

回答 2

Code Review用户

发布于 2019-05-27 15:02:50

下面是一些关于如何改进代码的建议:

述明意图

这个功能的目的是什么?输入是什么,输出是什么?用人类的话来说,算法是做什么的?回答这些基本问题的docstring将是有帮助的。一系列简洁的unit tests就太好了。

我运行这个函数来查看输出:

代码语言:javascript
复制
>>> text = "The weather is not good."
>>> result = negate_sequence(self=None, text=text)
>>> print(result)

['the', 'weather', 'the weather', 'is', 'weather is', 'the weather is', 'not', 'is not',
'weather is not', 'not_good', 'not not_good', 'is not not_good']

这对我来说没有什么意义,所以我不再试图去理解它的目的了。

避免有状态循环

迭代i通过negation变量耦合到迭代i-1,使得逻辑难以理解,容易出错。如果您处理bigram/trigram,我会创建一个bigram/trigram的列表,并对元组进行迭代。这将使迭代解耦。

分解长函数

这几乎是无穷无尽的好处,作为一个起点参见这篇文章。一些可能性:

  • extract_words(text)将文本分解成所有小写,不加标点符号
  • 有由make_trigrams(words)创建的trigram列表
  • process(trigrams)检查三角图
  • 如果需要的话,可以使用某种aggregate(results)

一旦完成了这个任务,我想我们就可以更好地识别bug并进一步改进功能了。

票数 3
EN

Code Review用户

发布于 2019-05-27 13:28:45

我可以提个建议。

您能用真正的autonyms替换它吗?这比在不预先解析的情况下使其为负值要有用得多。

票数 0
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/144766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档