找到一个词的反义词
Finding the Antonym of a Word
我正在使用 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","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')]
此任务似乎最好用 WordNet to provide you the antonym. You can then potentially use either WordNet or some spellchecking library to list synonyms and find antonyms for these (they are likely to be not exact antonyms then). Good python libraries for this are: pyenchant or hunspell 解决。
WordNet(使用 NLTK 提供的 API - spaCy 的 'older sister' NLP 库):参见 this answer or another one。
我正在使用 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","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')]
此任务似乎最好用 WordNet to provide you the antonym. You can then potentially use either WordNet or some spellchecking library to list synonyms and find antonyms for these (they are likely to be not exact antonyms then). Good python libraries for this are: pyenchant or hunspell 解决。
WordNet(使用 NLTK 提供的 API - spaCy 的 'older sister' NLP 库):参见 this answer or another one。