Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying Negex to Adjectives #50

Open
ZeroCool2u opened this issue Aug 26, 2022 · 4 comments
Open

Applying Negex to Adjectives #50

ZeroCool2u opened this issue Aug 26, 2022 · 4 comments

Comments

@ZeroCool2u
Copy link

Is there a straight forward way to apply Negex to adjectives? I already incorporated Negex into my pipeline with my own custom component, but I didn't realize until after the fact that it seems to only be searching for negations in relation to Named Entities. For example, I was hoping to apply it, so I'd get positive matches on something like:

doc = nlp("Eve is not nice. Eve is friendly. Eve is not chill.")
for s in doc.sents:
    for t in s.tokens:
      print(t._.negex, t.text)

True nice
False friendly
True chill

It seems like this is not supported at the moment, but if anyone has any advice on how to customize Negex to achieve this it would be much appreciated. Also, if there's a good reason to not bother trying to do this at all, would love to understand that too.

Thanks!

@jenojp
Copy link
Owner

jenojp commented Aug 30, 2022

I suppose you could check every single token (rather than entities) in negspacy/negation.py starting at line 296.

This library's goal was to implement the Negex algorithm in spaCy which looks at named entities so it's probably not an additional feature I'd fold into the package.

@ZeroCool2u
Copy link
Author

Got it. I'll look into trying it on my own fork. If I get it working maybe I can post the changes here for others to find if they're interested.

@casparhsws
Copy link

Hi @ZeroCool2u , this merged PR allows you to apply negex to span groups.
You could write a function to add adjectives to a span group and then apply negex to that group. Something like... (not tested)

import spacy
from spacy.language import Language

@Language.component("adjectives_to_spans")
def adjectives_to_spans(doc):
    adjective_spans = [ent for ent in doc.ents if ent.label_ == "ADJ"]
    doc.spans["adjectives"] = adjective_spans
    return doc

nlp = spacy.load("en_core_web_sm")
nlp.add_pipe("sentencizer", last=True)
nlp.add_pipe("adjectives_to_spans", last=True)
nlp.add_pipe("negex", last=True, config={"span_keys": ["adjectives"]})
nlp(doc)

@ZeroCool2u
Copy link
Author

Thanks for following up here!

I ended up implementing it myself, but it was not super simple and didn't think it was worth posting. Your example here looks shockingly similar to some of the most relevant code I ended up writing though and I imagine it largely accomplishes what I was going for.

Anyone that finds this thread in the future should definitely try this suggested approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants