forked from explosion/spaCy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter subtoken matches in merge_subtokens() (explosion#4539)
The `Matcher` in `merge_subtokens()` returns all possible subsequences of `subtok`, so for sequences of two or more subtoks it's necessary to filter the matches so that the retokenizer is only merging the longest matches with no overlapping spans.
- Loading branch information
1 parent
d5509e0
commit f2bfaa1
Showing
2 changed files
with
26 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# coding: utf-8 | ||
from __future__ import unicode_literals | ||
|
||
import pytest | ||
from spacy.pipeline.functions import merge_subtokens | ||
from ..util import get_doc | ||
|
||
|
||
@pytest.fixture | ||
def doc(en_tokenizer): | ||
# fmt: off | ||
text = "This is a sentence. This is another sentence. And a third." | ||
heads = [1, 0, 1, -2, -3, 1, 0, 1, -2, -3, 1, 1, 1, 0] | ||
deps = ["nsubj", "ROOT", "subtok", "attr", "punct", "nsubj", "ROOT", | ||
"subtok", "attr", "punct", "subtok", "subtok", "subtok", "ROOT"] | ||
# fmt: on | ||
tokens = en_tokenizer(text) | ||
return get_doc(tokens.vocab, words=[t.text for t in tokens], heads=heads, deps=deps) | ||
|
||
|
||
def test_merge_subtokens(doc): | ||
doc = merge_subtokens(doc) | ||
# get_doc() doesn't set spaces, so the result is "And a third ." | ||
assert [t.text for t in doc] == ["This", "is", "a sentence", ".", "This", "is", "another sentence", ".", "And a third ."] |