查找单词的反义词

问题描述

我正在使用spaCy建立基于方面的情感分析模型。我设法将方面和形容词成对地提取到列表中。在处理任何否定的形容词之前,我还添加了“ not”。如果形容词前没有“ not”,我想用其反义词交换形容词。我知道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",{"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',('the price','fair')]

解决方法

似乎最好用WordNet解决此任务,以便为您提供反义词。然后,您可以潜在地使用WordNet或某些拼写检查库来列出同义词并找到这些词的反义词(那时它们可能不是 exact 的反义词)。好的python库是:pyenchanthunspell

WordNet(使用NLTK提供的API-spaCy的“姐姐” NLP库):请参见this answeranother one