scikit

PhraseMatcher
class
v2.0 This feature is new and was introduced in spaCy v2.0
Match sequences of tokens, based on documents.

The PhraseMatcher lets you efficiently match large terminology lists. While the Matcher lets you match squences based on lists of token descriptions, the PhraseMatcher accepts match patterns in the form of Doc objects.

PhraseMatcher.__init__
method

Create the rule-based PhraseMatcher.

NameTypeDescription
vocabVocab The vocabulary object, which must be shared with the documents the matcher will operate on.
max_lengthintMaximum length of a phrase pattern to add.
returnsPhraseMatcherThe newly constructed object.

PhraseMatcher.__call__
method

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

NameTypeDescription
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.

PhraseMatcher.pipe
method

Match a stream of documents, yielding them in turn.

NameTypeDescription
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 PhraseMatcher implementation supports multi-threading.
yieldsDocDocuments, in order.

PhraseMatcher.__len__
method

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.

NameTypeDescription
returnsintThe number of rules.

PhraseMatcher.__contains__
method

Check whether the matcher contains rules for a match ID.

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

PhraseMatcher.add
method

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.

NameTypeDescription
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.
*docslist Doc objects of the phrases to match.