0

I'm working on an aspect-based sentiment analysis model using spaCy. I managed to extract aspects and adjectives as pairs in a list. I also included "not" before any adjective to handle any negations. I want to swap the adjective with its antonym if there is "not" before the adjective. I know spaCy has some similarity detection tools but I couldn't find anything about antonyms. Is it possible to do this with spaCy? If not how can I do it or is there a better way to handle the negations?

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)

Expected output:

[('the performance', 'not great'), ('the product', 'not great'), ('the price', 'fair')]
sophros
  • 14,672
  • 11
  • 46
  • 75
dorukr0t
  • 55
  • 1
  • 8
  • 1
    Perhaps some of the many thesaurus sites night have an API you could call? Interesting problem though, because a common antonym for "great" would be "small." Not really applicable in context (e.g. "the performance was small")? – TomServo Nov 01 '20 at 22:12

1 Answers1

1

This task seems to best addressed with 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 (using API provided by NLTK - an 'older sister' NLP library to spaCy): see this answer or another one.

sophros
  • 14,672
  • 11
  • 46
  • 75