Match sequences of tokens, based on pattern rules.


Create the rule-based Matcher.

vocabVocab The vocabulary object, which must be shared with the documents the matcher will operate on.
patternsdictPatterns to add to the matcher, keyed by ID.
returnsMatcherThe newly constructed object.


Find all token sequences matching the supplied patterns on the Doc.

docDocThe document to match over.
returnslist A list of (match_id, start, end) tuples, describing the matches. A match tuple describes a span doc[start:end]. The match_id is the ID of the added match pattern.


Match a stream of documents, yielding them in turn.

docsiterableA stream of documents.
batch_sizeintThe number of documents to accumulate into a working set.
n_threadsint The number of threads with which to work on the buffer in parallel, if the Matcher implementation supports multi-threading.
yieldsDocDocuments, in order.


Get the number of rules added to the matcher. Note that this only returns the number of rules (identical with the number of IDs), not the number of individual patterns.

returnsintThe number of rules.


Check whether the matcher contains rules for a match ID.

keyunicodeThe match ID.
returnsintWhether the matcher contains rules for this match ID.


Add a rule to the matcher, consisting of an ID key, one or more patterns, and a callback function to act on the matches. The callback function will receive the arguments matcher, doc, i and matches. If a pattern already exists for the given ID, the patterns will be extended. An on_match callback will be overwritten.

match_idunicodeAn ID for the thing you're matching.
on_matchcallable or None Callback function to act on matches. Takes the arguments matcher, doc, i and matches.
*patternslist Match pattern. A pattern consists of a list of dicts, where each dict describes a token.


Remove a rule from the matcher. A KeyError is raised if the match ID does not exist.

keyunicodeThe ID of the match rule.


Retrieve the pattern stored for a key. Returns the rule as an (on_match, patterns) tuple containing the callback and available patterns.

keyunicodeThe ID of the match rule.
returnstupleThe rule, as an (on_match, patterns) tuple.