Skip to content

Instantly share code, notes, and snippets.

@ahmedbesbes
Last active August 21, 2022 09:14
Show Gist options
  • Save ahmedbesbes/8f69146a4ad450290c1cb555c7fe48e0 to your computer and use it in GitHub Desktop.
Save ahmedbesbes/8f69146a4ad450290c1cb555c7fe48e0 to your computer and use it in GitHub Desktop.
import spacy
nlp = spacy.blanc("en")
doc_before = nlp("John lives in Atlanta")
# No entities are detected
print(doc_before.ents)
# ()
# Create an entity ruler and add it some patterns
entity_ruler = nlp.add_pipe("entity_ruler")
patterns = [
{
"label": "PERSON",
"pattern": "John",
"id": "john",
},
{
"label": "GPE",
"pattern": [{"LOWER": "atlanta"}],
"id": "atlanta",
},
]
entity_ruler.add_patterns(patterns)
doc_after = nlp("Jonh lives in Atlanta.")
for ent in doc.ents:
print(ent.text, ":", ent.label_)
# John : PERSON
# atlanta : GPE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment