Creating Entity Rules with spaCy#s EntityRuler

  • Share this:

Code introduction


This function uses spaCy's EntityRuler to create a simple entity rule for a given text. It first loads the English model, then creates an EntityRuler instance, and adds a pattern to identify personal names in the text. Finally, it saves the rules to disk.


Technology Stack : spaCy

Code Type : Function

Code Difficulty : Intermediate


                
                    
import spacy
from spacy.pipeline import EntityRuler
import random

def create_entity_rules(text):
    """
    This function creates a simple entity ruler for a given text using spaCy.
    """
    nlp = spacy.load('en_core_web_sm')
    ruler = EntityRuler(nlp)
    pattern = [{"label": "PERSON", "pattern": text}]
    ruler.add_pattern(pattern)
    ruler.to_disk('entity_ruler_model')
    return ruler                
              
Tags: