Skip to content

Solve nested entities problems by using SpanCategorizer #88

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions quickumls/spacy_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __call__(self, doc):
matches = self.quickumls._match(doc, best_match=self.best_match, ignore_syntax=self.ignore_syntax)

# Convert QuickUMLS match objects into Spans
doc.spans['sc'] = []
for match in matches:
# each match may match multiple ngrams
for ngram_match_dict in match:
Expand All @@ -62,6 +63,16 @@ def __call__(self, doc):
# add some custom metadata to the spans
span._.similarity = ngram_match_dict['similarity']
span._.semtypes = ngram_match_dict['semtypes']
doc.ents = list(doc.ents) + [span]

return doc
# OLD: doc.ents = list(doc.ents) + [span]
# Using doc.spans["sc"] (SpanCategorizer) to solve the problem of overlapped tokens in nested NER for spacy.
# With doc.spans["sc"], all possible entities are stored without throwing errors.
doc.spans["sc"] = list(doc.spans["sc"]) + [span]

# After storing all possible spans, we filter out overlapping spans before adding them to doc.ents.
# Here we remove overlapping spans using spacy.util.filter_spans
# When spans overlap, the rule is to prefer the first longest span over shorter ones.
for span in spacy.util.filter_spans(doc.spans["sc"]):
doc.ents = list(doc.ents) + [span]

return doc