2019-02-07 08:42:25 +00:00
|
|
|
# cython: infer_types=True
|
|
|
|
# cython: profile=True
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2019-09-24 12:39:50 +00:00
|
|
|
from libc.stdint cimport uintptr_t
|
|
|
|
|
2019-09-25 09:03:58 +00:00
|
|
|
from preshed.maps cimport map_init, map_set, map_get, map_clear, map_iter
|
2019-09-24 12:39:50 +00:00
|
|
|
|
2019-09-25 09:03:58 +00:00
|
|
|
from ..attrs cimport ORTH, POS, TAG, DEP, LEMMA
|
2019-09-25 07:41:27 +00:00
|
|
|
from ..structs cimport TokenC
|
|
|
|
from ..tokens.token cimport Token
|
2019-02-07 08:42:25 +00:00
|
|
|
|
2019-08-21 12:00:37 +00:00
|
|
|
from ._schemas import TOKEN_PATTERN_SCHEMA
|
2019-03-08 10:42:26 +00:00
|
|
|
from ..errors import Errors, Warnings, deprecation_warning, user_warning
|
2019-02-07 08:42:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
cdef class PhraseMatcher:
|
2019-03-08 10:42:26 +00:00
|
|
|
"""Efficiently match large terminology lists. While the `Matcher` matches
|
|
|
|
sequences based on lists of token descriptions, the `PhraseMatcher` accepts
|
|
|
|
match patterns in the form of `Doc` objects.
|
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/phrasematcher
|
|
|
|
USAGE: https://spacy.io/usage/rule-based-matching#phrasematcher
|
2019-09-19 14:36:12 +00:00
|
|
|
|
|
|
|
Adapted from FlashText: https://github.com/vi3k6i5/flashtext
|
|
|
|
MIT License (see `LICENSE`)
|
|
|
|
Copyright (c) 2017 Vikash Singh (vikash.duliajan@gmail.com)
|
2019-03-08 10:42:26 +00:00
|
|
|
"""
|
2019-09-24 12:39:50 +00:00
|
|
|
|
2019-03-08 10:42:26 +00:00
|
|
|
def __init__(self, Vocab vocab, max_length=0, attr="ORTH", validate=False):
|
|
|
|
"""Initialize the PhraseMatcher.
|
|
|
|
|
|
|
|
vocab (Vocab): The shared vocabulary.
|
|
|
|
attr (int / unicode): Token attribute to match on.
|
|
|
|
validate (bool): Perform additional validation when patterns are added.
|
|
|
|
RETURNS (PhraseMatcher): The newly constructed object.
|
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/phrasematcher#init
|
|
|
|
"""
|
2019-02-07 08:42:25 +00:00
|
|
|
if max_length != 0:
|
|
|
|
deprecation_warning(Warnings.W010)
|
|
|
|
self.vocab = vocab
|
2019-09-19 14:36:12 +00:00
|
|
|
self._callbacks = {}
|
|
|
|
self._keywords = {}
|
2019-09-19 18:20:53 +00:00
|
|
|
self._docs = {}
|
2019-09-19 14:36:12 +00:00
|
|
|
self._validate = validate
|
|
|
|
|
2019-09-24 12:39:50 +00:00
|
|
|
self.mem = Pool()
|
|
|
|
self.c_map = <MapStruct*>self.mem.alloc(1, sizeof(MapStruct))
|
2019-09-25 07:41:27 +00:00
|
|
|
self._terminal_hash = 826361138722620965
|
2019-09-24 12:39:50 +00:00
|
|
|
map_init(self.mem, self.c_map, 8)
|
|
|
|
|
2019-02-07 08:42:25 +00:00
|
|
|
if isinstance(attr, long):
|
|
|
|
self.attr = attr
|
|
|
|
else:
|
2019-08-21 12:00:37 +00:00
|
|
|
attr = attr.upper()
|
|
|
|
if attr == "TEXT":
|
|
|
|
attr = "ORTH"
|
|
|
|
if attr not in TOKEN_PATTERN_SCHEMA["items"]["properties"]:
|
|
|
|
raise ValueError(Errors.E152.format(attr=attr))
|
2019-02-07 08:42:25 +00:00
|
|
|
self.attr = self.vocab.strings[attr]
|
|
|
|
|
|
|
|
def __len__(self):
|
2019-09-19 14:36:12 +00:00
|
|
|
"""Get the number of match IDs added to the matcher.
|
2019-02-07 08:42:25 +00:00
|
|
|
|
|
|
|
RETURNS (int): The number of rules.
|
2019-03-08 10:42:26 +00:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/phrasematcher#len
|
2019-02-07 08:42:25 +00:00
|
|
|
"""
|
2019-09-19 14:36:12 +00:00
|
|
|
return len(self._callbacks)
|
2019-02-07 08:42:25 +00:00
|
|
|
|
|
|
|
def __contains__(self, key):
|
|
|
|
"""Check whether the matcher contains rules for a match ID.
|
|
|
|
|
|
|
|
key (unicode): The match ID.
|
|
|
|
RETURNS (bool): Whether the matcher contains rules for this match ID.
|
2019-03-08 10:42:26 +00:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/phrasematcher#contains
|
2019-02-07 08:42:25 +00:00
|
|
|
"""
|
2019-09-19 14:36:12 +00:00
|
|
|
return key in self._callbacks
|
|
|
|
|
2019-09-19 18:20:53 +00:00
|
|
|
def __reduce__(self):
|
|
|
|
data = (self.vocab, self._docs, self._callbacks)
|
|
|
|
return (unpickle_matcher, data, None, None)
|
|
|
|
|
2019-09-19 14:36:12 +00:00
|
|
|
def remove(self, key):
|
2019-09-26 09:31:03 +00:00
|
|
|
"""Remove a rule from the matcher by match ID. A KeyError is raised if
|
|
|
|
the key does not exist.
|
2019-09-19 14:36:12 +00:00
|
|
|
|
|
|
|
key (unicode): The match ID.
|
|
|
|
"""
|
|
|
|
if key not in self._keywords:
|
2019-09-26 09:31:03 +00:00
|
|
|
raise KeyError(key)
|
2019-09-24 12:39:50 +00:00
|
|
|
cdef MapStruct* current_node
|
|
|
|
cdef MapStruct* terminal_map
|
|
|
|
cdef MapStruct* node_pointer
|
2019-09-24 14:20:24 +00:00
|
|
|
cdef void* result
|
2019-09-24 12:39:50 +00:00
|
|
|
cdef key_t terminal_key
|
|
|
|
cdef void* value
|
|
|
|
cdef int c_i = 0
|
2019-09-26 09:31:03 +00:00
|
|
|
cdef vector[MapStruct*] path_nodes
|
|
|
|
cdef vector[key_t] path_keys
|
|
|
|
cdef key_t key_to_remove
|
2019-09-19 14:36:12 +00:00
|
|
|
for keyword in self._keywords[key]:
|
2019-09-24 12:39:50 +00:00
|
|
|
current_node = self.c_map
|
|
|
|
for token in keyword:
|
2019-09-24 14:20:24 +00:00
|
|
|
result = map_get(current_node, token)
|
|
|
|
if result:
|
2019-09-26 09:31:03 +00:00
|
|
|
path_nodes.push_back(current_node)
|
|
|
|
path_keys.push_back(token)
|
2019-09-24 14:20:24 +00:00
|
|
|
current_node = <MapStruct*>result
|
2019-09-19 14:36:12 +00:00
|
|
|
else:
|
|
|
|
# if token is not found, break out of the loop
|
2019-09-24 12:39:50 +00:00
|
|
|
current_node = NULL
|
2019-09-19 14:36:12 +00:00
|
|
|
break
|
2019-09-24 12:39:50 +00:00
|
|
|
# remove the tokens from trie node if there are no other
|
2019-09-19 14:36:12 +00:00
|
|
|
# keywords with them
|
2019-09-25 07:41:27 +00:00
|
|
|
result = map_get(current_node, self._terminal_hash)
|
2019-09-24 14:20:24 +00:00
|
|
|
if current_node != NULL and result:
|
|
|
|
terminal_map = <MapStruct*>result
|
2019-09-24 12:39:50 +00:00
|
|
|
terminal_keys = []
|
|
|
|
c_i = 0
|
|
|
|
while map_iter(terminal_map, &c_i, &terminal_key, &value):
|
|
|
|
terminal_keys.append(self.vocab.strings[terminal_key])
|
2019-09-26 09:31:03 +00:00
|
|
|
# if this is the only remaining key, remove unnecessary paths
|
|
|
|
if terminal_keys == [key]:
|
|
|
|
while not path_nodes.empty():
|
|
|
|
node_pointer = path_nodes.back()
|
|
|
|
path_nodes.pop_back()
|
|
|
|
key_to_remove = path_keys.back()
|
|
|
|
path_keys.pop_back()
|
2019-09-24 14:20:24 +00:00
|
|
|
result = map_get(node_pointer, key_to_remove)
|
2019-09-24 12:39:50 +00:00
|
|
|
if node_pointer.filled == 1:
|
|
|
|
map_clear(node_pointer, key_to_remove)
|
2019-09-24 14:20:24 +00:00
|
|
|
self.mem.free(result)
|
2019-09-19 14:36:12 +00:00
|
|
|
else:
|
|
|
|
# more than one key means more than 1 path,
|
2019-09-26 09:31:03 +00:00
|
|
|
# delete not required path and keep the others
|
2019-09-24 12:39:50 +00:00
|
|
|
map_clear(node_pointer, key_to_remove)
|
2019-09-24 14:20:24 +00:00
|
|
|
self.mem.free(result)
|
2019-09-19 14:36:12 +00:00
|
|
|
break
|
|
|
|
# otherwise simply remove the key
|
|
|
|
else:
|
2019-09-25 07:41:27 +00:00
|
|
|
result = map_get(current_node, self._terminal_hash)
|
2019-09-24 14:20:24 +00:00
|
|
|
if result:
|
|
|
|
map_clear(<MapStruct*>result, self.vocab.strings[key])
|
2019-02-07 08:42:25 +00:00
|
|
|
|
2019-09-19 14:36:12 +00:00
|
|
|
del self._keywords[key]
|
|
|
|
del self._callbacks[key]
|
2019-09-19 18:20:53 +00:00
|
|
|
del self._docs[key]
|
2019-02-07 08:42:25 +00:00
|
|
|
|
|
|
|
def add(self, key, on_match, *docs):
|
|
|
|
"""Add a match-rule to the phrase-matcher. A match-rule consists of: an ID
|
|
|
|
key, an on_match callback, and one or more patterns.
|
|
|
|
|
|
|
|
key (unicode): The match ID.
|
|
|
|
on_match (callable): Callback executed on match.
|
|
|
|
*docs (Doc): `Doc` objects representing match patterns.
|
2019-03-08 10:42:26 +00:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/phrasematcher#add
|
2019-02-07 08:42:25 +00:00
|
|
|
"""
|
2019-09-19 14:36:12 +00:00
|
|
|
|
2019-09-24 14:07:38 +00:00
|
|
|
_ = self.vocab[key]
|
2019-09-19 14:36:12 +00:00
|
|
|
self._callbacks[key] = on_match
|
|
|
|
self._keywords.setdefault(key, [])
|
2019-09-19 18:20:53 +00:00
|
|
|
self._docs.setdefault(key, set())
|
|
|
|
self._docs[key].update(docs)
|
2019-09-19 14:36:12 +00:00
|
|
|
|
2019-09-24 12:39:50 +00:00
|
|
|
cdef MapStruct* current_node
|
|
|
|
cdef MapStruct* internal_node
|
2019-09-24 14:20:24 +00:00
|
|
|
cdef void* result
|
2019-09-24 12:39:50 +00:00
|
|
|
|
2019-02-07 08:42:25 +00:00
|
|
|
for doc in docs:
|
2019-09-19 14:36:12 +00:00
|
|
|
if len(doc) == 0:
|
2019-02-07 08:42:25 +00:00
|
|
|
continue
|
2019-08-21 12:00:37 +00:00
|
|
|
if self.attr in (POS, TAG, LEMMA) and not doc.is_tagged:
|
|
|
|
raise ValueError(Errors.E155.format())
|
|
|
|
if self.attr == DEP and not doc.is_parsed:
|
|
|
|
raise ValueError(Errors.E156.format())
|
2019-02-12 14:45:31 +00:00
|
|
|
if self._validate and (doc.is_tagged or doc.is_parsed) \
|
|
|
|
and self.attr not in (DEP, POS, TAG, LEMMA):
|
|
|
|
string_attr = self.vocab.strings[self.attr]
|
|
|
|
user_warning(Warnings.W012.format(key=key, attr=string_attr))
|
2019-09-19 14:36:12 +00:00
|
|
|
keyword = self._convert_to_array(doc)
|
|
|
|
# keep track of keywords per key to make remove easier
|
|
|
|
# (would use a set, but can't hash numpy arrays)
|
2019-09-20 07:18:38 +00:00
|
|
|
self._keywords[key].append(keyword)
|
2019-09-24 12:39:50 +00:00
|
|
|
|
|
|
|
current_node = self.c_map
|
2019-09-19 14:36:12 +00:00
|
|
|
for token in keyword:
|
2019-09-25 07:41:27 +00:00
|
|
|
if token == self._terminal_hash:
|
|
|
|
user_warning(Warnings.W021)
|
|
|
|
break
|
2019-09-24 14:20:24 +00:00
|
|
|
result = <MapStruct*>map_get(current_node, token)
|
|
|
|
if not result:
|
2019-09-24 12:39:50 +00:00
|
|
|
internal_node = <MapStruct*>self.mem.alloc(1, sizeof(MapStruct))
|
|
|
|
map_init(self.mem, internal_node, 8)
|
|
|
|
map_set(self.mem, current_node, token, internal_node)
|
2019-09-24 14:20:24 +00:00
|
|
|
result = internal_node
|
|
|
|
current_node = <MapStruct*>result
|
2019-09-25 07:41:27 +00:00
|
|
|
result = <MapStruct*>map_get(current_node, self._terminal_hash)
|
2019-09-24 14:20:24 +00:00
|
|
|
if not result:
|
2019-09-24 12:39:50 +00:00
|
|
|
internal_node = <MapStruct*>self.mem.alloc(1, sizeof(MapStruct))
|
|
|
|
map_init(self.mem, internal_node, 8)
|
2019-09-25 07:41:27 +00:00
|
|
|
map_set(self.mem, current_node, self._terminal_hash, internal_node)
|
2019-09-24 14:20:24 +00:00
|
|
|
result = internal_node
|
|
|
|
map_set(self.mem, <MapStruct*>result, self.vocab.strings[key], NULL)
|
2019-09-19 14:36:12 +00:00
|
|
|
|
|
|
|
def __call__(self, doc):
|
2019-02-07 08:42:25 +00:00
|
|
|
"""Find all sequences matching the supplied patterns on the `Doc`.
|
|
|
|
|
|
|
|
doc (Doc): The document to match over.
|
|
|
|
RETURNS (list): A list of `(key, start, end)` tuples,
|
|
|
|
describing the matches. A match tuple describes a span
|
|
|
|
`doc[start:end]`. The `label_id` and `key` are both integers.
|
2019-03-08 10:42:26 +00:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/phrasematcher#call
|
2019-02-07 08:42:25 +00:00
|
|
|
"""
|
|
|
|
matches = []
|
2019-09-25 07:41:27 +00:00
|
|
|
if doc is None or len(doc) == 0:
|
|
|
|
# if doc is empty or None just return empty list
|
2019-09-19 14:36:12 +00:00
|
|
|
return matches
|
2019-09-24 12:39:50 +00:00
|
|
|
|
|
|
|
cdef vector[MatchStruct] c_matches
|
2019-09-25 07:41:27 +00:00
|
|
|
self.find_matches(doc, &c_matches)
|
2019-09-24 12:39:50 +00:00
|
|
|
for i in range(c_matches.size()):
|
|
|
|
matches.append((c_matches[i].match_id, c_matches[i].start, c_matches[i].end))
|
|
|
|
for i, (ent_id, start, end) in enumerate(matches):
|
|
|
|
on_match = self._callbacks.get(ent_id)
|
|
|
|
if on_match is not None:
|
|
|
|
on_match(self, doc, i, matches)
|
|
|
|
return matches
|
|
|
|
|
2019-09-25 07:41:27 +00:00
|
|
|
cdef void find_matches(self, Doc doc, vector[MatchStruct] *matches) nogil:
|
2019-09-24 12:39:50 +00:00
|
|
|
cdef MapStruct* current_node = self.c_map
|
|
|
|
cdef int start = 0
|
|
|
|
cdef int idx = 0
|
|
|
|
cdef int idy = 0
|
|
|
|
cdef key_t key
|
|
|
|
cdef void* value
|
|
|
|
cdef int i = 0
|
|
|
|
cdef MatchStruct ms
|
2019-09-24 14:20:24 +00:00
|
|
|
cdef void* result
|
2019-09-25 07:41:27 +00:00
|
|
|
while idx < doc.length:
|
2019-09-23 07:11:13 +00:00
|
|
|
start = idx
|
2019-09-25 07:41:27 +00:00
|
|
|
token = Token.get_struct_attr(&doc.c[idx], self.attr)
|
2019-09-23 07:11:13 +00:00
|
|
|
# look for sequences from this position
|
2019-09-24 14:20:24 +00:00
|
|
|
result = map_get(current_node, token)
|
|
|
|
if result:
|
|
|
|
current_node = <MapStruct*>result
|
2019-09-23 07:11:13 +00:00
|
|
|
idy = idx + 1
|
2019-09-25 07:41:27 +00:00
|
|
|
while idy < doc.length:
|
|
|
|
result = map_get(current_node, self._terminal_hash)
|
2019-09-24 14:20:24 +00:00
|
|
|
if result:
|
2019-09-24 12:39:50 +00:00
|
|
|
i = 0
|
2019-09-24 14:20:24 +00:00
|
|
|
while map_iter(<MapStruct*>result, &i, &key, &value):
|
2019-09-24 12:39:50 +00:00
|
|
|
ms = make_matchstruct(key, start, idy)
|
|
|
|
matches.push_back(ms)
|
2019-09-25 07:41:27 +00:00
|
|
|
inner_token = Token.get_struct_attr(&doc.c[idy], self.attr)
|
2019-09-24 14:20:24 +00:00
|
|
|
result = map_get(current_node, inner_token)
|
|
|
|
if result:
|
|
|
|
current_node = <MapStruct*>result
|
2019-09-19 14:36:12 +00:00
|
|
|
idy += 1
|
|
|
|
else:
|
2019-09-23 07:11:13 +00:00
|
|
|
break
|
|
|
|
else:
|
2019-09-25 07:41:27 +00:00
|
|
|
# end of doc reached
|
|
|
|
result = map_get(current_node, self._terminal_hash)
|
2019-09-24 14:20:24 +00:00
|
|
|
if result:
|
2019-09-24 12:39:50 +00:00
|
|
|
i = 0
|
2019-09-24 14:20:24 +00:00
|
|
|
while map_iter(<MapStruct*>result, &i, &key, &value):
|
2019-09-24 12:39:50 +00:00
|
|
|
ms = make_matchstruct(key, start, idy)
|
|
|
|
matches.push_back(ms)
|
|
|
|
current_node = self.c_map
|
2019-09-19 14:36:12 +00:00
|
|
|
idx += 1
|
2019-02-07 08:42:25 +00:00
|
|
|
|
2019-03-15 15:24:26 +00:00
|
|
|
def pipe(self, stream, batch_size=1000, n_threads=-1, return_matches=False,
|
2019-02-07 08:42:25 +00:00
|
|
|
as_tuples=False):
|
|
|
|
"""Match a stream of documents, yielding them in turn.
|
|
|
|
|
|
|
|
docs (iterable): A stream of documents.
|
|
|
|
batch_size (int): Number of documents to accumulate into a working set.
|
|
|
|
return_matches (bool): Yield the match lists along with the docs, making
|
|
|
|
results (doc, matches) tuples.
|
|
|
|
as_tuples (bool): Interpret the input stream as (doc, context) tuples,
|
|
|
|
and yield (result, context) tuples out.
|
|
|
|
If both return_matches and as_tuples are True, the output will
|
|
|
|
be a sequence of ((doc, matches), context) tuples.
|
|
|
|
YIELDS (Doc): Documents, in order.
|
2019-03-08 10:42:26 +00:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/phrasematcher#pipe
|
2019-02-07 08:42:25 +00:00
|
|
|
"""
|
2019-03-15 15:38:44 +00:00
|
|
|
if n_threads != -1:
|
|
|
|
deprecation_warning(Warnings.W016)
|
2019-02-07 08:42:25 +00:00
|
|
|
if as_tuples:
|
|
|
|
for doc, context in stream:
|
|
|
|
matches = self(doc)
|
|
|
|
if return_matches:
|
|
|
|
yield ((doc, matches), context)
|
|
|
|
else:
|
|
|
|
yield (doc, context)
|
|
|
|
else:
|
|
|
|
for doc in stream:
|
|
|
|
matches = self(doc)
|
|
|
|
if return_matches:
|
|
|
|
yield (doc, matches)
|
|
|
|
else:
|
|
|
|
yield doc
|
|
|
|
|
2019-09-19 14:36:12 +00:00
|
|
|
def _convert_to_array(self, Doc doc):
|
2019-09-25 07:41:27 +00:00
|
|
|
return [Token.get_struct_attr(&doc.c[i], self.attr) for i in range(len(doc))]
|
2019-09-19 18:20:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def unpickle_matcher(vocab, docs, callbacks):
|
|
|
|
matcher = PhraseMatcher(vocab)
|
|
|
|
for key, specs in docs.items():
|
|
|
|
callback = callbacks.get(key, None)
|
|
|
|
matcher.add(key, callback, *specs)
|
|
|
|
return matcher
|
2019-09-24 12:39:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
cdef MatchStruct make_matchstruct(key_t match_id, int start, int end) nogil:
|
|
|
|
cdef MatchStruct ms
|
|
|
|
ms.match_id = match_id
|
|
|
|
ms.start = start
|
|
|
|
ms.end = end
|
|
|
|
return ms
|