Created
March 16, 2021 14:52
-
-
Save kayuzee/926660cbd99314d8c4bb178de892ca7f to your computer and use it in GitHub Desktop.
Clearing the Spacy EntityRuler in a loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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