Skip to content

Instantly share code, notes, and snippets.

@koaning
Created July 14, 2022 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koaning/ae2fc2eee2fe5e45acd6eda84aaead36 to your computer and use it in GitHub Desktop.
Save koaning/ae2fc2eee2fe5e45acd6eda84aaead36 to your computer and use it in GitHub Desktop.
Demonstration of Operators and Quantifiers from spaCy.
import spacy
from spacy import displacy
def show_results(text, patterns):
nlp = spacy.blank("en")
ruler = nlp.add_pipe("entity_ruler")
ruler.add_patterns(patterns)
doc = nlp(text)
displacy.render(doc, style="ent")
patterns = [
{
"label":"MUSEUM",
"pattern":[
{"IS_UPPER": True, "OP": "*"},
{"IS_TITLE": True, "OP": "*"},
{"LOWER": {"IN": ["gallery", "museum"]}},
{"LOWER": "of", "OP": "?"},
{"IS_TITLE": True, "OP": "*"},
]
}
]
text = "On my trip to Amsterdam I visited the National Museum of Arts, the Museum of Natural Sciences, the NEMO Science Museum and the Royal Gallery."
show_results(text, patterns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment