Pipeline

EntityRuler

classv2.1

The EntityRuler lets you add spans to the Doc.ents using token-based rules or exact phrase matches. It can be combined with the statistical EntityRecognizer to boost accuracy, or used on its own to implement a purely rule-based entity recognition system. After initialization, the component is typically added to the processing pipeline using nlp.add_pipe.

EntityRuler.__init__ method

Initialize the entity ruler. If patterns are supplied here, they need to be a list of dictionaries with a "label" and "pattern" key. A pattern can either be a token pattern (list) or a phrase pattern (string). For example: {'label': 'ORG', 'pattern': 'Apple'}.

NameTypeDescription
nlpLanguageThe shared nlp object to pass the vocab to the matchers and process phrase patterns.
patternsiterableOptional patterns to load in.
overwrite_entsboolIf existing entities are present, e.g. entities added by the model, overwrite them by matches if necessary. Defaults to False.
**cfg-Other config parameters. If pipeline component is loaded as part of a model pipeline, this will include all keyword arguments passed to spacy.load.

EntityRuler._len__ method

The number of all patterns added to the entity ruler.

NameTypeDescription

EntityRuler.__contains__ method

Whether a label is present in the patterns.

NameTypeDescription
labelunicodeThe label to check.

EntityRuler.__call__ method

Find matches in the Doc and add them to the doc.ents. Typically, this happens automatically after the component has been added to the pipeline using nlp.add_pipe. If the entity ruler was initialized with overwrite_ents=True, existing entities will be replaced if they overlap with the matches.

NameTypeDescription
docDocThe Doc object to process, e.g. the Doc in the pipeline.

EntityRuler.add_patterns method

Add patterns to the entity ruler. A pattern can either be a token pattern (list of dicts) or a phrase pattern (string). For more details, see the usage guide on rule-based matching.

NameTypeDescription
patternslistThe patterns to add.

EntityRuler.to_disk method

Save the entity ruler patterns to a directory. The patterns will be saved as newline-delimited JSON (JSONL).

NameTypeDescription
pathunicode / PathA path to a file, which will be created if it doesn’t exist. Paths may be either strings or Path-like objects.

EntityRuler.from_disk method

Load the entity ruler from a file. Expects a file containing newline-delimited JSON (JSONL) with one entry per line.

NameTypeDescription
pathunicode / PathA path to a JSONL file. Paths may be either strings or Path-like objects.

EntityRuler.to_bytes method

Serialize the entity ruler patterns to a bytestring.

NameTypeDescription

EntityRuler.from_bytes method

Load the pipe from a bytestring. Modifies the object in place and returns it.

NameTypeDescription
patterns_bytesbytesThe bytestring to load.

EntityRuler.labels property

All labels present in the match patterns.

NameTypeDescription

EntityRuler.patterns property

Get all patterns that were added to the entity ruler.

NameTypeDescription

Attributes

NameTypeDescription
matcherMatcherThe underlying matcher used to process token patterns.
phrase_matcherPhraseMatcherThe underlying phrase matcher, used to process phrase patterns.
token_patternsdictThe token patterns present in the entity ruler, keyed by label.
phrase_patternsdictThe phrase patterns present in the entity ruler, keyed by label.