Skip to content

Instantly share code, notes, and snippets.

@kayuzee
Created March 16, 2021 14:52
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 kayuzee/926660cbd99314d8c4bb178de892ca7f to your computer and use it in GitHub Desktop.
Save kayuzee/926660cbd99314d8c4bb178de892ca7f to your computer and use it in GitHub Desktop.
Clearing the Spacy EntityRuler in a loop
# Use case: You have a large list of terms, but you only need to match one for each loop
# e.g I have artciles for multiple topics, but I only want to match one topic at a time
# Instead of enabling or disabling a pipe component, or keeping a large dictionary with all your patters
# you can dynamically add your patters, and then clear the entity ruler
#add a ruler to spacy
ruler = nlp.add_pipe("entity_ruler", before='ner')
def get_patterns():
... #your dynamic function to get patterns here
for i in y:
ruler.clear() #clears the patterns on each call
patterns = get_patterns()
ruler.add_patterns(patterns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment