doc_or_span -> obj

This commit is contained in:
Ines Montani 2020-05-20 14:56:52 +02:00
parent 40e65d6f63
commit 78bb9ff5e0

View File

@ -213,28 +213,28 @@ cdef class Matcher:
else: else:
yield doc yield doc
def __call__(self, object doc_or_span): def __call__(self, object obj):
"""Find all token sequences matching the supplied pattern. """Find all token sequences matching the supplied pattern.
doc_or_span (Doc or Span): The document to match over. obj (Doc / Span): The document to match over.
RETURNS (list): A list of `(key, start, end)` tuples, RETURNS (list): A list of `(key, start, end)` tuples,
describing the matches. A match tuple describes a span describing the matches. A match tuple describes a span
`doc[start:end]`. The `label_id` and `key` are both integers. `doc[start:end]`. The `label_id` and `key` are both integers.
""" """
if isinstance(doc_or_span, Doc): if isinstance(obj, Doc):
doc = doc_or_span doc = obj
length = len(doc) length = len(doc)
elif isinstance(doc_or_span, Span): elif isinstance(obj, Span):
doc = doc_or_span.doc doc = obj.doc
length = doc_or_span.end - doc_or_span.start length = obj.end - obj.start
else: else:
raise ValueError(Errors.E195.format(good="Doc or Span", got=type(doc_or_span).__name__)) raise ValueError(Errors.E195.format(good="Doc or Span", got=type(obj).__name__))
if len(set([LEMMA, POS, TAG]) & self._seen_attrs) > 0 \ if len(set([LEMMA, POS, TAG]) & self._seen_attrs) > 0 \
and not doc.is_tagged: and not doc.is_tagged:
raise ValueError(Errors.E155.format()) raise ValueError(Errors.E155.format())
if DEP in self._seen_attrs and not doc.is_parsed: if DEP in self._seen_attrs and not doc.is_parsed:
raise ValueError(Errors.E156.format()) raise ValueError(Errors.E156.format())
matches = find_matches(&self.patterns[0], self.patterns.size(), doc_or_span, length, matches = find_matches(&self.patterns[0], self.patterns.size(), obj, length,
extensions=self._extensions, predicates=self._extra_predicates) extensions=self._extensions, predicates=self._extra_predicates)
for i, (key, start, end) in enumerate(matches): for i, (key, start, end) in enumerate(matches):
on_match = self._callbacks.get(key, None) on_match = self._callbacks.get(key, None)
@ -257,7 +257,7 @@ def unpickle_matcher(vocab, patterns, callbacks):
return matcher return matcher
cdef find_matches(TokenPatternC** patterns, int n, object doc_or_span, int length, extensions=None, predicates=tuple()): cdef find_matches(TokenPatternC** patterns, int n, object obj, int length, extensions=None, predicates=tuple()):
"""Find matches in a doc, with a compiled array of patterns. Matches are """Find matches in a doc, with a compiled array of patterns. Matches are
returned as a list of (id, start, end) tuples. returned as a list of (id, start, end) tuples.
@ -286,7 +286,7 @@ cdef find_matches(TokenPatternC** patterns, int n, object doc_or_span, int lengt
else: else:
nr_extra_attr = 0 nr_extra_attr = 0
extra_attr_values = <attr_t*>mem.alloc(length, sizeof(attr_t)) extra_attr_values = <attr_t*>mem.alloc(length, sizeof(attr_t))
for i, token in enumerate(doc_or_span): for i, token in enumerate(obj):
for name, index in extensions.items(): for name, index in extensions.items():
value = token._.get(name) value = token._.get(name)
if isinstance(value, basestring): if isinstance(value, basestring):
@ -298,7 +298,7 @@ cdef find_matches(TokenPatternC** patterns, int n, object doc_or_span, int lengt
for j in range(n): for j in range(n):
states.push_back(PatternStateC(patterns[j], i, 0)) states.push_back(PatternStateC(patterns[j], i, 0))
transition_states(states, matches, predicate_cache, transition_states(states, matches, predicate_cache,
doc_or_span[i], extra_attr_values, predicates) obj[i], extra_attr_values, predicates)
extra_attr_values += nr_extra_attr extra_attr_values += nr_extra_attr
predicate_cache += len(predicates) predicate_cache += len(predicates)
# Handle matches that end in 0-width patterns # Handle matches that end in 0-width patterns