【发布时间】:2020-11-01 22:08:36
【问题描述】:
我正在使用 spaCy 开发基于方面的情感分析模型。我设法将方面和形容词成对提取到一个列表中。我还在任何形容词前加上“不”来处理任何否定。如果形容词前面有“不”,我想用它的反义词交换形容词。我知道 spaCy 有一些相似性检测工具,但我找不到任何关于反义词的信息。可以用 spaCy 做到这一点吗?如果不是,我该怎么做或者有更好的方法来处理否定?
import spacy
from spacy.matcher import Matcher
nlp = spacy.load('en_core_web_sm')
txt = "The performance of the product is not great but The price is fair."
txt = txt.lower()
output = []
doc = nlp(txt)
matcher = Matcher(nlp.vocab, validate=True)
matcher.add("mood",None,[{"LOWER":{"IN":["is","are"]}},{"LOWER":{"IN":["no","not"]},"OP":"?"},{"DEP":"advmod","OP":"?"},{"DEP":"acomp"}])
for nc in doc.noun_chunks:
d = doc[nc.root.right_edge.i+1:nc.root.right_edge.i+1+3]
matches = matcher(d)
if matches:
_, start, end = matches[0]
output.append((nc.text, d[start+1:end].text))
print(output)
预期输出:
[('the performance', 'not great'), ('the product', 'not great'), ('the price', 'fair')]
【问题讨论】:
-
也许许多词库网站中的一些晚上有一个您可以调用的 API?有趣的问题,因为“伟大”的常见反义词是“小”。在上下文中并不真正适用(例如“性能很小”)?
标签: python nlp nltk stanford-nlp spacy