mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-26 01:46:28 +03:00
698b8b495f
* Clean up old Matcher call style related stuff In v2 Matcher.add was called with (key, on_match, *patterns). In v3 this was changed to (key, patterns, *, on_match=None), but there were various points where the old call syntax was documented or handled specially. This removes all those. The Matcher itself didn't need any code changes, as it just gives a generic type error. However the PhraseMatcher required some changes because it would automatically "fix" the old call style. Surprisingly, the tokenizer was still using the old call style in one place. After these changes tests failed in two places: 1. one test for the "new" call style, including the "old" call style. I removed this test. 2. deserializing the PhraseMatcher fails because the input docs are a set. I am not sure why 2 is happening - I guess it's a quirk of the serialization format? - so for now I just convert the set to a list when deserializing. The check that the input Docs are a List in the PhraseMatcher is a new check, but makes it parallel with the other Matchers, which seemed like the right thing to do. * Add notes related to input docs / deserialization type * Remove Typing import * Remove old note about call style change * Apply suggestions from code review Co-authored-by: Adriane Boyd <adrianeboyd@gmail.com> * Use separate method for setting internal doc representations In addition to the title change, this changes the internal dict to be a defaultdict, instead of a dict with frequent use of setdefault. * Add _add_from_arrays for unpickling * Cleanup around adding from arrays This moves adding to internal structures into the private batch method, and removes the single-add method. This has one behavioral change for `add`, in that if something is wrong with the list of input Docs (such as one of the items not being a Doc), valid items before the invalid one will not be added. Also the callback will not be updated if anything is invalid. This change should not be significant. This also adds a test to check failure when given a non-Doc. * Update spacy/matcher/phrasematcher.pyx Co-authored-by: Adriane Boyd <adrianeboyd@gmail.com> Co-authored-by: Adriane Boyd <adrianeboyd@gmail.com>
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
from typing import List, Tuple, Union, Optional, Callable, Any, Dict, overload
|
|
from ..compat import Literal
|
|
from .matcher import Matcher
|
|
from ..vocab import Vocab
|
|
from ..tokens import Doc, Span
|
|
|
|
class PhraseMatcher:
|
|
def __init__(
|
|
self, vocab: Vocab, attr: Optional[Union[int, str]], validate: bool = ...
|
|
) -> None: ...
|
|
def __reduce__(self) -> Any: ...
|
|
def __len__(self) -> int: ...
|
|
def __contains__(self, key: str) -> bool: ...
|
|
def add(
|
|
self,
|
|
key: str,
|
|
docs: List[Doc],
|
|
*,
|
|
on_match: Optional[
|
|
Callable[[Matcher, Doc, int, List[Tuple[Any, ...]]], Any]
|
|
] = ...,
|
|
) -> None: ...
|
|
def _add_from_arrays(
|
|
self,
|
|
key: str,
|
|
specs: List[List[int]],
|
|
*,
|
|
on_match: Optional[
|
|
Callable[[Matcher, Doc, int, List[Tuple[Any, ...]]], Any]
|
|
] = ...,
|
|
) -> None: ...
|
|
def remove(self, key: str) -> None: ...
|
|
@overload
|
|
def __call__(
|
|
self,
|
|
doclike: Union[Doc, Span],
|
|
*,
|
|
as_spans: Literal[False] = ...,
|
|
) -> List[Tuple[int, int, int]]: ...
|
|
@overload
|
|
def __call__(
|
|
self,
|
|
doclike: Union[Doc, Span],
|
|
*,
|
|
as_spans: Literal[True],
|
|
) -> List[Span]: ...
|