<jupyter_start><jupyter_text>Custom Rule Based Entity Recognition     The entity ruler is an exciting new component that lets you add named entities based on pattern dictionaries and  make it easy to combine rule based and statistical named entity recognition for even more powerful models.  Entity Patterns     Entity patterns are patterns with two keys:         'label', specifying the label to assign to the entity if the pattern is matched and         'pattern' the match pattern.     The entity ruler accepts two types of patterns:    >  Phase Pattern : {'label' : 'ORG', 'pattern' : 'APPLE'}    >  Token Pattern : {'label' : 'GPE', 'pattern' : [{'LOWER' : 'san'}, {'LOWER':'franscisco'}]}  Using the Entity Ruler         The entity ruler is a pipeline component that is typically added via nlp.add_pipe. When the nlp object is called on a text, it will find matches in the doc and add them as entities to doc.ents using the specified pattern label as entity label.<jupyter_code>import spacy
from spacy.pipeline import EntityRuler
nlp = spacy.load('en_core_web_sm')
ruler = EntityRuler(nlp)
mypattern = [{'label' : 'ORG', 'pattern' : 'AESM'}, 
             {'label' : 'GPE', 'pattern' : [{'LOWER' : 'san'}, {'LOWER':'franscisco'}]}
            ]
ruler.add_patterns(mypattern)
nlp.add_pipe(ruler)
doc = nlp("AESM  is opening its first big office in San Franscisco")
doc

myents = [  (token.text, token.label_) for token in doc.ents ]
myents<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09769335142469471, 'fraction_numerical': 0.0, 'mean_word_length': 4.051369863013699, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '558390', 'license': 'MIT', 'n_tokens_mistral': 437, 'n_tokens_neox': 424, 'n_words': 181, 'path': 'Spacy/16_Spacy_Custom_Entity_Recognition.ipynb', 'repo_name': 'sudevansujit/7_NLP', 'size': '1474'}