2017-01-12 19:51:46 +03:00
|
|
|
import pytest
|
2019-10-08 13:07:02 +03:00
|
|
|
from mock import Mock
|
2020-08-31 21:04:26 +03:00
|
|
|
from spacy.matcher import Matcher
|
2020-08-31 15:53:22 +03:00
|
|
|
from spacy.tokens import Doc, Token, Span
|
|
|
|
|
2020-03-25 14:28:12 +03:00
|
|
|
from ..doc.test_underscore import clean_underscore # noqa: F401
|
2017-01-12 19:51:46 +03:00
|
|
|
|
|
|
|
|
2018-07-06 13:17:50 +03:00
|
|
|
@pytest.fixture
|
2017-01-13 00:23:11 +03:00
|
|
|
def matcher(en_vocab):
|
2018-11-27 03:09:36 +03:00
|
|
|
rules = {
|
|
|
|
"JS": [[{"ORTH": "JavaScript"}]],
|
|
|
|
"GoogleNow": [[{"ORTH": "Google"}, {"ORTH": "Now"}]],
|
|
|
|
"Java": [[{"LOWER": "java"}]],
|
|
|
|
}
|
2017-05-22 13:59:50 +03:00
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
for key, patterns in rules.items():
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add(key, patterns)
|
2017-05-22 13:59:50 +03:00
|
|
|
return matcher
|
2017-01-13 00:23:11 +03:00
|
|
|
|
|
|
|
|
2018-07-06 13:17:50 +03:00
|
|
|
def test_matcher_from_api_docs(en_vocab):
|
|
|
|
matcher = Matcher(en_vocab)
|
2018-11-27 03:09:36 +03:00
|
|
|
pattern = [{"ORTH": "test"}]
|
2018-07-06 13:17:50 +03:00
|
|
|
assert len(matcher) == 0
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("Rule", [pattern])
|
2018-07-06 13:17:50 +03:00
|
|
|
assert len(matcher) == 1
|
2018-11-27 03:09:36 +03:00
|
|
|
matcher.remove("Rule")
|
|
|
|
assert "Rule" not in matcher
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("Rule", [pattern])
|
2018-11-27 03:09:36 +03:00
|
|
|
assert "Rule" in matcher
|
|
|
|
on_match, patterns = matcher.get("Rule")
|
2018-07-06 13:17:50 +03:00
|
|
|
assert len(patterns[0])
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_from_usage_docs(en_vocab):
|
|
|
|
text = "Wow 😀 This is really cool! 😂 😂"
|
2018-11-27 03:09:36 +03:00
|
|
|
doc = Doc(en_vocab, words=text.split(" "))
|
|
|
|
pos_emoji = ["😀", "😃", "😂", "🤣", "😊", "😍"]
|
|
|
|
pos_patterns = [[{"ORTH": emoji}] for emoji in pos_emoji]
|
2018-07-06 13:17:50 +03:00
|
|
|
|
|
|
|
def label_sentiment(matcher, doc, i, matches):
|
|
|
|
match_id, start, end = matches[i]
|
2018-11-27 03:09:36 +03:00
|
|
|
if doc.vocab.strings[match_id] == "HAPPY":
|
2018-07-06 13:17:50 +03:00
|
|
|
doc.sentiment += 0.1
|
2018-11-27 03:09:36 +03:00
|
|
|
span = doc[start:end]
|
2019-02-15 12:29:44 +03:00
|
|
|
with doc.retokenize() as retokenizer:
|
|
|
|
retokenizer.merge(span)
|
|
|
|
token = doc[start]
|
2018-11-27 03:09:36 +03:00
|
|
|
token.vocab[token.text].norm_ = "happy emoji"
|
2018-07-06 13:17:50 +03:00
|
|
|
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("HAPPY", pos_patterns, on_match=label_sentiment)
|
2018-11-30 19:43:08 +03:00
|
|
|
matcher(doc)
|
2018-07-06 13:17:50 +03:00
|
|
|
assert doc.sentiment != 0
|
2018-11-27 03:09:36 +03:00
|
|
|
assert doc[1].norm_ == "happy emoji"
|
2018-07-06 13:17:50 +03:00
|
|
|
|
|
|
|
|
2018-07-25 00:38:44 +03:00
|
|
|
def test_matcher_len_contains(matcher):
|
|
|
|
assert len(matcher) == 3
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("TEST", [[{"ORTH": "test"}]])
|
2018-11-27 03:09:36 +03:00
|
|
|
assert "TEST" in matcher
|
|
|
|
assert "TEST2" not in matcher
|
2018-07-06 13:17:50 +03:00
|
|
|
|
|
|
|
|
2020-07-29 12:04:43 +03:00
|
|
|
def test_matcher_add_new_api(en_vocab):
|
2019-10-25 23:21:08 +03:00
|
|
|
doc = Doc(en_vocab, words=["a", "b"])
|
|
|
|
patterns = [[{"TEXT": "a"}], [{"TEXT": "a"}, {"TEXT": "b"}]]
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
on_match = Mock()
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
matcher.add("NEW_API", patterns)
|
|
|
|
assert len(matcher(doc)) == 2
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
on_match = Mock()
|
|
|
|
matcher.add("NEW_API_CALLBACK", patterns, on_match=on_match)
|
|
|
|
assert len(matcher(doc)) == 2
|
|
|
|
assert on_match.call_count == 2
|
|
|
|
|
|
|
|
|
2018-07-06 13:17:50 +03:00
|
|
|
def test_matcher_no_match(matcher):
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(matcher.vocab, words=["I", "like", "cheese", "."])
|
2018-07-06 13:17:50 +03:00
|
|
|
assert matcher(doc) == []
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_start(matcher):
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(matcher.vocab, words=["JavaScript", "is", "good"])
|
2018-11-27 03:09:36 +03:00
|
|
|
assert matcher(doc) == [(matcher.vocab.strings["JS"], 0, 1)]
|
2018-07-06 13:17:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_end(matcher):
|
|
|
|
words = ["I", "like", "java"]
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(matcher.vocab, words=words)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert matcher(doc) == [(doc.vocab.strings["Java"], 2, 3)]
|
2018-07-06 13:17:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_middle(matcher):
|
|
|
|
words = ["I", "like", "Google", "Now", "best"]
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(matcher.vocab, words=words)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert matcher(doc) == [(doc.vocab.strings["GoogleNow"], 2, 4)]
|
2018-07-06 13:17:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_multi(matcher):
|
|
|
|
words = ["I", "like", "Google", "Now", "and", "java", "best"]
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(matcher.vocab, words=words)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert matcher(doc) == [
|
|
|
|
(doc.vocab.strings["GoogleNow"], 2, 4),
|
|
|
|
(doc.vocab.strings["Java"], 5, 6),
|
|
|
|
]
|
2018-07-06 13:17:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_empty_dict(en_vocab):
|
2018-07-25 00:38:44 +03:00
|
|
|
"""Test matcher allows empty token specs, meaning match on any token."""
|
2018-07-06 13:17:50 +03:00
|
|
|
matcher = Matcher(en_vocab)
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(matcher.vocab, words=["a", "b", "c"])
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("A.C", [[{"ORTH": "a"}, {}, {"ORTH": "c"}]])
|
2018-07-06 13:17:50 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 1
|
|
|
|
assert matches[0][1:] == (0, 3)
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("A.", [[{"ORTH": "a"}, {}]])
|
2018-07-06 13:17:50 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert matches[0][1:] == (0, 2)
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_operator_shadow(en_vocab):
|
|
|
|
matcher = Matcher(en_vocab)
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(matcher.vocab, words=["a", "b", "c"])
|
2018-11-27 03:09:36 +03:00
|
|
|
pattern = [{"ORTH": "a"}, {"IS_ALPHA": True, "OP": "+"}, {"ORTH": "c"}]
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("A.C", [pattern])
|
2018-07-06 13:17:50 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 1
|
|
|
|
assert matches[0][1:] == (0, 3)
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_zero(matcher):
|
|
|
|
words1 = 'He said , " some words " ...'.split()
|
|
|
|
words2 = 'He said , " some three words " ...'.split()
|
2018-11-27 03:09:36 +03:00
|
|
|
pattern1 = [
|
|
|
|
{"ORTH": '"'},
|
|
|
|
{"OP": "!", "IS_PUNCT": True},
|
|
|
|
{"OP": "!", "IS_PUNCT": True},
|
|
|
|
{"ORTH": '"'},
|
|
|
|
]
|
|
|
|
pattern2 = [
|
|
|
|
{"ORTH": '"'},
|
|
|
|
{"IS_PUNCT": True},
|
|
|
|
{"IS_PUNCT": True},
|
|
|
|
{"IS_PUNCT": True},
|
|
|
|
{"ORTH": '"'},
|
|
|
|
]
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("Quote", [pattern1])
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(matcher.vocab, words=words1)
|
2018-07-06 13:17:50 +03:00
|
|
|
assert len(matcher(doc)) == 1
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(matcher.vocab, words=words2)
|
2018-07-06 13:17:50 +03:00
|
|
|
assert len(matcher(doc)) == 0
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("Quote", [pattern2])
|
2018-07-06 13:17:50 +03:00
|
|
|
assert len(matcher(doc)) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_zero_plus(matcher):
|
|
|
|
words = 'He said , " some words " ...'.split()
|
2018-11-27 03:09:36 +03:00
|
|
|
pattern = [{"ORTH": '"'}, {"OP": "*", "IS_PUNCT": False}, {"ORTH": '"'}]
|
2018-07-06 13:17:50 +03:00
|
|
|
matcher = Matcher(matcher.vocab)
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("Quote", [pattern])
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(matcher.vocab, words=words)
|
2018-07-06 13:17:50 +03:00
|
|
|
assert len(matcher(doc)) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_one_plus(matcher):
|
|
|
|
control = Matcher(matcher.vocab)
|
2020-07-29 12:04:43 +03:00
|
|
|
control.add("BasicPhilippe", [[{"ORTH": "Philippe"}]])
|
2018-11-27 03:09:36 +03:00
|
|
|
doc = Doc(control.vocab, words=["Philippe", "Philippe"])
|
2018-07-06 13:17:50 +03:00
|
|
|
m = control(doc)
|
|
|
|
assert len(m) == 2
|
2019-12-25 14:39:49 +03:00
|
|
|
pattern = [{"ORTH": "Philippe"}, {"ORTH": "Philippe", "OP": "+"}]
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("KleenePhilippe", [pattern])
|
2018-07-06 13:17:50 +03:00
|
|
|
m = matcher(doc)
|
|
|
|
assert len(m) == 1
|
|
|
|
|
2017-10-16 14:38:01 +03:00
|
|
|
|
2018-07-06 13:17:50 +03:00
|
|
|
def test_matcher_any_token_operator(en_vocab):
|
|
|
|
"""Test that patterns with "any token" {} work with operators."""
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("TEST", [[{"ORTH": "test"}, {"OP": "*"}]])
|
2018-11-27 03:09:36 +03:00
|
|
|
doc = Doc(en_vocab, words=["test", "hello", "world"])
|
2018-07-06 13:17:50 +03:00
|
|
|
matches = [doc[start:end].text for _, start, end in matcher(doc)]
|
|
|
|
assert len(matches) == 3
|
2018-11-27 03:09:36 +03:00
|
|
|
assert matches[0] == "test"
|
|
|
|
assert matches[1] == "test hello"
|
|
|
|
assert matches[2] == "test hello world"
|
2018-09-05 06:53:21 +03:00
|
|
|
|
|
|
|
|
2020-02-23 17:49:20 +03:00
|
|
|
@pytest.mark.usefixtures("clean_underscore")
|
2019-01-21 15:23:15 +03:00
|
|
|
def test_matcher_extension_attribute(en_vocab):
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-02-06 15:32:18 +03:00
|
|
|
get_is_fruit = lambda token: token.text in ("apple", "banana")
|
|
|
|
Token.set_extension("is_fruit", getter=get_is_fruit, force=True)
|
|
|
|
pattern = [{"ORTH": "an"}, {"_": {"is_fruit": True}}]
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("HAVING_FRUIT", [pattern])
|
2019-02-06 15:32:18 +03:00
|
|
|
doc = Doc(en_vocab, words=["an", "apple"])
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 1
|
2019-02-06 15:32:18 +03:00
|
|
|
doc = Doc(en_vocab, words=["an", "aardvark"])
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_set_value(en_vocab):
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-02-06 15:32:18 +03:00
|
|
|
pattern = [{"ORTH": {"IN": ["an", "a"]}}]
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("A_OR_AN", [pattern])
|
2019-02-06 15:32:18 +03:00
|
|
|
doc = Doc(en_vocab, words=["an", "a", "apple"])
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 2
|
2019-02-06 15:32:18 +03:00
|
|
|
doc = Doc(en_vocab, words=["aardvark"])
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 0
|
|
|
|
|
|
|
|
|
2019-02-06 15:40:11 +03:00
|
|
|
def test_matcher_set_value_operator(en_vocab):
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
pattern = [{"ORTH": {"IN": ["a", "the"]}, "OP": "?"}, {"ORTH": "house"}]
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("DET_HOUSE", [pattern])
|
2019-02-06 15:40:11 +03:00
|
|
|
doc = Doc(en_vocab, words=["In", "a", "house"])
|
|
|
|
matches = matcher(doc)
|
2019-02-20 23:30:39 +03:00
|
|
|
assert len(matches) == 2
|
2019-02-06 15:40:11 +03:00
|
|
|
doc = Doc(en_vocab, words=["my", "house"])
|
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 1
|
|
|
|
|
|
|
|
|
2020-09-24 17:55:09 +03:00
|
|
|
def test_matcher_subset_value_operator(en_vocab):
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
pattern = [{"MORPH": {"IS_SUBSET": ["Feat=Val", "Feat2=Val2"]}}]
|
|
|
|
matcher.add("M", [pattern])
|
|
|
|
doc = Doc(en_vocab, words=["a", "b", "c"])
|
|
|
|
assert len(matcher(doc)) == 3
|
2020-10-01 23:21:46 +03:00
|
|
|
doc[0].set_morph("Feat=Val")
|
2020-09-24 17:55:09 +03:00
|
|
|
assert len(matcher(doc)) == 3
|
2020-10-01 23:21:46 +03:00
|
|
|
doc[0].set_morph("Feat=Val|Feat2=Val2")
|
2020-09-24 17:55:09 +03:00
|
|
|
assert len(matcher(doc)) == 3
|
2020-10-01 23:21:46 +03:00
|
|
|
doc[0].set_morph("Feat=Val|Feat2=Val2|Feat3=Val3")
|
2020-09-24 17:55:09 +03:00
|
|
|
assert len(matcher(doc)) == 2
|
2020-10-01 23:21:46 +03:00
|
|
|
doc[0].set_morph("Feat=Val|Feat2=Val2|Feat3=Val3|Feat4=Val4")
|
2020-09-24 17:55:09 +03:00
|
|
|
assert len(matcher(doc)) == 2
|
|
|
|
|
|
|
|
# IS_SUBSET acts like "IN" for attrs other than MORPH
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
pattern = [{"TAG": {"IS_SUBSET": ["A", "B"]}}]
|
|
|
|
matcher.add("M", [pattern])
|
|
|
|
doc = Doc(en_vocab, words=["a", "b", "c"])
|
|
|
|
doc[0].tag_ = "A"
|
|
|
|
assert len(matcher(doc)) == 1
|
|
|
|
|
|
|
|
# IS_SUBSET with an empty list matches nothing
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
pattern = [{"TAG": {"IS_SUBSET": []}}]
|
|
|
|
matcher.add("M", [pattern])
|
|
|
|
doc = Doc(en_vocab, words=["a", "b", "c"])
|
|
|
|
doc[0].tag_ = "A"
|
|
|
|
assert len(matcher(doc)) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_superset_value_operator(en_vocab):
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
pattern = [{"MORPH": {"IS_SUPERSET": ["Feat=Val", "Feat2=Val2", "Feat3=Val3"]}}]
|
|
|
|
matcher.add("M", [pattern])
|
|
|
|
doc = Doc(en_vocab, words=["a", "b", "c"])
|
|
|
|
assert len(matcher(doc)) == 0
|
2020-10-01 23:21:46 +03:00
|
|
|
doc[0].set_morph("Feat=Val|Feat2=Val2")
|
2020-09-24 17:55:09 +03:00
|
|
|
assert len(matcher(doc)) == 0
|
2020-10-01 23:21:46 +03:00
|
|
|
doc[0].set_morph("Feat=Val|Feat2=Val2|Feat3=Val3")
|
2020-09-24 17:55:09 +03:00
|
|
|
assert len(matcher(doc)) == 1
|
2020-10-01 23:21:46 +03:00
|
|
|
doc[0].set_morph("Feat=Val|Feat2=Val2|Feat3=Val3|Feat4=Val4")
|
2020-09-24 17:55:09 +03:00
|
|
|
assert len(matcher(doc)) == 1
|
|
|
|
|
|
|
|
# IS_SUPERSET with more than one value only matches for MORPH
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
pattern = [{"TAG": {"IS_SUPERSET": ["A", "B"]}}]
|
|
|
|
matcher.add("M", [pattern])
|
|
|
|
doc = Doc(en_vocab, words=["a", "b", "c"])
|
|
|
|
doc[0].tag_ = "A"
|
|
|
|
assert len(matcher(doc)) == 0
|
|
|
|
|
|
|
|
# IS_SUPERSET with one value is the same as ==
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
pattern = [{"TAG": {"IS_SUPERSET": ["A"]}}]
|
|
|
|
matcher.add("M", [pattern])
|
|
|
|
doc = Doc(en_vocab, words=["a", "b", "c"])
|
|
|
|
doc[0].tag_ = "A"
|
|
|
|
assert len(matcher(doc)) == 1
|
|
|
|
|
|
|
|
# IS_SUPERSET with an empty value matches everything
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
pattern = [{"TAG": {"IS_SUPERSET": []}}]
|
|
|
|
matcher.add("M", [pattern])
|
|
|
|
doc = Doc(en_vocab, words=["a", "b", "c"])
|
|
|
|
doc[0].tag_ = "A"
|
|
|
|
assert len(matcher(doc)) == 3
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_morph_handling(en_vocab):
|
|
|
|
# order of features in pattern doesn't matter
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
pattern1 = [{"MORPH": {"IN": ["Feat1=Val1|Feat2=Val2"]}}]
|
|
|
|
pattern2 = [{"MORPH": {"IN": ["Feat2=Val2|Feat1=Val1"]}}]
|
|
|
|
matcher.add("M", [pattern1])
|
|
|
|
matcher.add("N", [pattern2])
|
|
|
|
doc = Doc(en_vocab, words=["a", "b", "c"])
|
|
|
|
assert len(matcher(doc)) == 0
|
|
|
|
|
2020-10-01 23:21:46 +03:00
|
|
|
doc[0].set_morph("Feat2=Val2|Feat1=Val1")
|
2020-09-24 17:55:09 +03:00
|
|
|
assert len(matcher(doc)) == 2
|
2020-10-01 23:21:46 +03:00
|
|
|
doc[0].set_morph("Feat1=Val1|Feat2=Val2")
|
2020-09-24 17:55:09 +03:00
|
|
|
assert len(matcher(doc)) == 2
|
|
|
|
|
|
|
|
# multiple values are split
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
pattern1 = [{"MORPH": {"IS_SUPERSET": ["Feat1=Val1", "Feat2=Val2"]}}]
|
|
|
|
pattern2 = [{"MORPH": {"IS_SUPERSET": ["Feat1=Val1", "Feat1=Val3", "Feat2=Val2"]}}]
|
|
|
|
matcher.add("M", [pattern1])
|
|
|
|
matcher.add("N", [pattern2])
|
|
|
|
doc = Doc(en_vocab, words=["a", "b", "c"])
|
|
|
|
assert len(matcher(doc)) == 0
|
|
|
|
|
2020-10-01 23:21:46 +03:00
|
|
|
doc[0].set_morph("Feat2=Val2,Val3|Feat1=Val1")
|
2020-09-24 17:55:09 +03:00
|
|
|
assert len(matcher(doc)) == 1
|
2020-10-01 23:21:46 +03:00
|
|
|
doc[0].set_morph("Feat1=Val1,Val3|Feat2=Val2")
|
2020-09-24 17:55:09 +03:00
|
|
|
assert len(matcher(doc)) == 2
|
|
|
|
|
|
|
|
|
2019-01-21 15:23:15 +03:00
|
|
|
def test_matcher_regex(en_vocab):
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-02-06 15:32:18 +03:00
|
|
|
pattern = [{"ORTH": {"REGEX": r"(?:a|an)"}}]
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("A_OR_AN", [pattern])
|
2019-02-06 15:32:18 +03:00
|
|
|
doc = Doc(en_vocab, words=["an", "a", "hi"])
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 2
|
2019-02-06 15:32:18 +03:00
|
|
|
doc = Doc(en_vocab, words=["bye"])
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 0
|
|
|
|
|
2019-02-06 15:32:18 +03:00
|
|
|
|
2019-01-21 15:23:15 +03:00
|
|
|
def test_matcher_regex_shape(en_vocab):
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-02-06 15:32:18 +03:00
|
|
|
pattern = [{"SHAPE": {"REGEX": r"^[^x]+$"}}]
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("NON_ALPHA", [pattern])
|
2019-02-06 15:32:18 +03:00
|
|
|
doc = Doc(en_vocab, words=["99", "problems", "!"])
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 2
|
2019-02-06 15:32:18 +03:00
|
|
|
doc = Doc(en_vocab, words=["bye"])
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 0
|
|
|
|
|
2019-02-06 15:32:18 +03:00
|
|
|
|
2020-04-14 20:14:15 +03:00
|
|
|
@pytest.mark.parametrize(
|
2020-05-21 15:14:01 +03:00
|
|
|
"cmp, bad",
|
2020-04-14 20:14:15 +03:00
|
|
|
[
|
|
|
|
("==", ["a", "aaa"]),
|
|
|
|
("!=", ["aa"]),
|
|
|
|
(">=", ["a"]),
|
|
|
|
("<=", ["aaa"]),
|
|
|
|
(">", ["a", "aa"]),
|
2020-05-21 15:14:01 +03:00
|
|
|
("<", ["aa", "aaa"]),
|
|
|
|
],
|
2020-04-14 20:14:15 +03:00
|
|
|
)
|
|
|
|
def test_matcher_compare_length(en_vocab, cmp, bad):
|
2019-01-21 15:23:15 +03:00
|
|
|
matcher = Matcher(en_vocab)
|
2020-04-14 20:14:15 +03:00
|
|
|
pattern = [{"LENGTH": {cmp: 2}}]
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("LENGTH_COMPARE", [pattern])
|
2019-02-06 15:32:18 +03:00
|
|
|
doc = Doc(en_vocab, words=["a", "aa", "aaa"])
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
2020-04-14 20:14:15 +03:00
|
|
|
assert len(matches) == len(doc) - len(bad)
|
|
|
|
doc = Doc(en_vocab, words=bad)
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_extension_set_membership(en_vocab):
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-02-06 15:32:18 +03:00
|
|
|
get_reversed = lambda token: "".join(reversed(token.text))
|
|
|
|
Token.set_extension("reversed", getter=get_reversed, force=True)
|
|
|
|
pattern = [{"_": {"reversed": {"IN": ["eyb", "ih"]}}}]
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("REVERSED", [pattern])
|
2019-02-06 15:32:18 +03:00
|
|
|
doc = Doc(en_vocab, words=["hi", "bye", "hello"])
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 2
|
2019-02-06 15:32:18 +03:00
|
|
|
doc = Doc(en_vocab, words=["aardvark"])
|
2019-01-21 15:23:15 +03:00
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 0
|
|
|
|
|
|
|
|
|
2019-10-25 23:21:08 +03:00
|
|
|
def test_matcher_basic_check(en_vocab):
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
# Potential mistake: pass in pattern instead of list of patterns
|
|
|
|
pattern = [{"TEXT": "hello"}, {"TEXT": "world"}]
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
matcher.add("TEST", pattern)
|
|
|
|
|
|
|
|
|
2019-08-21 21:52:36 +03:00
|
|
|
def test_attr_pipeline_checks(en_vocab):
|
|
|
|
doc1 = Doc(en_vocab, words=["Test"])
|
2020-09-17 01:14:01 +03:00
|
|
|
doc1[0].dep_ = "ROOT"
|
2019-08-21 21:52:36 +03:00
|
|
|
doc2 = Doc(en_vocab, words=["Test"])
|
2020-09-17 01:14:01 +03:00
|
|
|
doc2[0].tag_ = "TAG"
|
|
|
|
doc2[0].pos_ = "X"
|
2020-10-01 23:21:46 +03:00
|
|
|
doc2[0].set_morph("Feat=Val")
|
2020-09-17 01:14:01 +03:00
|
|
|
doc2[0].lemma_ = "LEMMA"
|
2019-08-21 21:52:36 +03:00
|
|
|
doc3 = Doc(en_vocab, words=["Test"])
|
2020-09-17 01:14:01 +03:00
|
|
|
# DEP requires DEP
|
2019-08-21 21:52:36 +03:00
|
|
|
matcher = Matcher(en_vocab)
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("TEST", [[{"DEP": "a"}]])
|
2019-08-21 21:52:36 +03:00
|
|
|
matcher(doc1)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
matcher(doc2)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
matcher(doc3)
|
2020-09-24 17:54:39 +03:00
|
|
|
# errors can be suppressed if desired
|
|
|
|
matcher(doc2, allow_missing=True)
|
|
|
|
matcher(doc3, allow_missing=True)
|
2020-09-17 01:14:01 +03:00
|
|
|
# TAG, POS, LEMMA require those values
|
2019-08-21 21:52:36 +03:00
|
|
|
for attr in ("TAG", "POS", "LEMMA"):
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("TEST", [[{attr: "a"}]])
|
2019-08-21 21:52:36 +03:00
|
|
|
matcher(doc2)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
matcher(doc1)
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
matcher(doc3)
|
|
|
|
# TEXT/ORTH only require tokens
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("TEST", [[{"ORTH": "a"}]])
|
2019-08-21 21:52:36 +03:00
|
|
|
matcher(doc1)
|
|
|
|
matcher(doc2)
|
|
|
|
matcher(doc3)
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("TEST", [[{"TEXT": "a"}]])
|
2019-08-21 21:52:36 +03:00
|
|
|
matcher(doc1)
|
|
|
|
matcher(doc2)
|
|
|
|
matcher(doc3)
|
2019-08-29 13:02:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"pattern,text",
|
|
|
|
[
|
|
|
|
([{"IS_ALPHA": True}], "a"),
|
|
|
|
([{"IS_ASCII": True}], "a"),
|
|
|
|
([{"IS_DIGIT": True}], "1"),
|
|
|
|
([{"IS_LOWER": True}], "a"),
|
|
|
|
([{"IS_UPPER": True}], "A"),
|
|
|
|
([{"IS_TITLE": True}], "Aaaa"),
|
|
|
|
([{"IS_PUNCT": True}], "."),
|
|
|
|
([{"IS_SPACE": True}], "\n"),
|
|
|
|
([{"IS_BRACKET": True}], "["),
|
2019-08-31 14:39:06 +03:00
|
|
|
([{"IS_QUOTE": True}], '"'),
|
2019-08-29 13:02:26 +03:00
|
|
|
([{"IS_LEFT_PUNCT": True}], "``"),
|
|
|
|
([{"IS_RIGHT_PUNCT": True}], "''"),
|
|
|
|
([{"IS_STOP": True}], "the"),
|
2020-11-30 04:34:50 +03:00
|
|
|
([{"SPACY": True}], "the"),
|
2019-08-29 13:02:26 +03:00
|
|
|
([{"LIKE_NUM": True}], "1"),
|
|
|
|
([{"LIKE_URL": True}], "http://example.com"),
|
|
|
|
([{"LIKE_EMAIL": True}], "mail@example.com"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_matcher_schema_token_attributes(en_vocab, pattern, text):
|
|
|
|
matcher = Matcher(en_vocab)
|
2019-08-31 14:39:06 +03:00
|
|
|
doc = Doc(en_vocab, words=text.split(" "))
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("Rule", [pattern])
|
2019-08-29 13:02:26 +03:00
|
|
|
assert len(matcher) == 1
|
|
|
|
matches = matcher(doc)
|
|
|
|
assert len(matches) == 1
|
2019-09-25 00:06:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_valid_callback(en_vocab):
|
|
|
|
"""Test that on_match can only be None or callable."""
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
with pytest.raises(ValueError):
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("TEST", [[{"TEXT": "test"}]], on_match=[])
|
2019-09-25 00:06:24 +03:00
|
|
|
matcher(Doc(en_vocab, words=["test"]))
|
2019-10-08 13:07:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_callback(en_vocab):
|
|
|
|
mock = Mock()
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
pattern = [{"ORTH": "test"}]
|
2019-10-25 23:21:08 +03:00
|
|
|
matcher.add("Rule", [pattern], on_match=mock)
|
2019-10-08 13:07:02 +03:00
|
|
|
doc = Doc(en_vocab, words=["This", "is", "a", "test", "."])
|
|
|
|
matches = matcher(doc)
|
|
|
|
mock.assert_called_once_with(matcher, doc, 0, matches)
|
2020-04-15 14:51:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_span(matcher):
|
|
|
|
text = "JavaScript is good but Java is better"
|
|
|
|
doc = Doc(matcher.vocab, words=text.split())
|
|
|
|
span_js = doc[:3]
|
|
|
|
span_java = doc[4:]
|
|
|
|
assert len(matcher(doc)) == 2
|
|
|
|
assert len(matcher(span_js)) == 1
|
|
|
|
assert len(matcher(span_java)) == 1
|
2020-08-31 15:53:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_as_spans(matcher):
|
|
|
|
"""Test the new as_spans=True API."""
|
|
|
|
text = "JavaScript is good but Java is better"
|
|
|
|
doc = Doc(matcher.vocab, words=text.split())
|
|
|
|
matches = matcher(doc, as_spans=True)
|
|
|
|
assert len(matches) == 2
|
|
|
|
assert isinstance(matches[0], Span)
|
|
|
|
assert matches[0].text == "JavaScript"
|
|
|
|
assert matches[0].label_ == "JS"
|
|
|
|
assert isinstance(matches[1], Span)
|
|
|
|
assert matches[1].text == "Java"
|
|
|
|
assert matches[1].label_ == "Java"
|
2020-08-31 18:01:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_deprecated(matcher):
|
|
|
|
doc = Doc(matcher.vocab, words=["hello", "world"])
|
|
|
|
with pytest.warns(DeprecationWarning) as record:
|
|
|
|
for _ in matcher.pipe([doc]):
|
|
|
|
pass
|
|
|
|
assert record.list
|
|
|
|
assert "spaCy v3.0" in str(record.list[0].message)
|