Search code examples
python-3.xspacy-3

How to remove/add entities in a custom entity ruler in spaCy 3.x


I have a custom entity ruler added to the spacy "en_core_web_sm" model. I want to add or remove entities in it when needed. This question has already been answered here, however I believe that is not correct as the person is talking about the ner component not the entity ruler.
Short version of that answer is that Spacy tends to forget previous patterns when you add new ones.
However that only happens when you are training the model's ner compenent with examples. Entity ruler is not trained on examples, it is simply given the patterns and labels to match and it has worked for me perfectly (I added it after the parser component).
If I'm wrong please correct me and if I'm right then how do I add/delete entities in the entity ruler (patterns and labels both or separately, whatever is possible).

def custom_ruler(file_path):
    ruler = nlp.add_pipe('entity_ruler', after='parser')
    ruler.from_disk(file_path)

This function is given a jsonl file that contains the entities.


Solution

  • You can add items to the entity ruler as usual.

    ruler = nlp.get_pipe("entity_ruler")
    patterns = ... whatever your patterns are ...
    ruler.add_patterns(patterns)
    

    See the Entity Ruler docs. See the API docs for examples of removal.