这一基本功能是在进行情绪分析时检测否定的。所以not good这类词可以被认为是否定的。
这段代码运行良好。有人能帮助改进它或发现是否有任何错误存在吗?
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发布于 2019-05-27 15:02:50
下面是一些关于如何改进代码的建议:
这个功能的目的是什么?输入是什么,输出是什么?用人类的话来说,算法是做什么的?回答这些基本问题的docstring将是有帮助的。一系列简洁的unit tests就太好了。
我运行这个函数来查看输出:
>>> 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并进一步改进功能了。
发布于 2019-05-27 13:28:45
我可以提个建议。
您能用真正的autonyms替换它吗?这比在不预先解析的情况下使其为负值要有用得多。
https://codereview.stackexchange.com/questions/144766
复制相似问题