2016-10-23 20:56:16 +03:00
|
|
|
import numpy
|
2023-06-14 18:48:41 +03:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from spacy.attrs import IS_ALPHA, IS_DIGIT, IS_LOWER, IS_PUNCT, IS_STOP, IS_TITLE
|
2018-07-25 00:38:44 +03:00
|
|
|
from spacy.symbols import VERB
|
|
|
|
from spacy.tokens import Doc
|
2021-01-12 19:17:06 +03:00
|
|
|
from spacy.training import Example
|
2023-06-14 18:48:41 +03:00
|
|
|
from spacy.vocab import Vocab
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-09-21 21:43:54 +03:00
|
|
|
def doc(en_vocab):
|
2018-11-27 03:09:36 +03:00
|
|
|
# fmt: off
|
2020-09-21 21:43:54 +03:00
|
|
|
words = ["This", "is", "a", "sentence", ".", "This", "is", "another", "sentence", ".", "And", "a", "third", "."]
|
|
|
|
heads = [1, 1, 3, 1, 1, 6, 6, 8, 6, 6, 10, 12, 10, 12]
|
2018-11-27 03:09:36 +03:00
|
|
|
deps = ["nsubj", "ROOT", "det", "attr", "punct", "nsubj", "ROOT", "det",
|
|
|
|
"attr", "punct", "ROOT", "det", "npadvmod", "punct"]
|
|
|
|
# fmt: on
|
2020-09-21 21:43:54 +03:00
|
|
|
return Doc(en_vocab, words=words, heads=heads, deps=deps)
|
2015-03-26 17:06:26 +03:00
|
|
|
|
2015-04-19 22:39:18 +03:00
|
|
|
|
2020-09-21 21:43:54 +03:00
|
|
|
def test_doc_token_api_strings(en_vocab):
|
|
|
|
words = ["Give", "it", "back", "!", "He", "pleaded", "."]
|
2018-11-27 03:09:36 +03:00
|
|
|
pos = ["VERB", "PRON", "PART", "PUNCT", "PRON", "VERB", "PUNCT"]
|
2020-09-21 21:43:54 +03:00
|
|
|
heads = [0, 0, 0, 0, 5, 5, 5]
|
2018-11-27 03:09:36 +03:00
|
|
|
deps = ["ROOT", "dobj", "prt", "punct", "nsubj", "ROOT", "punct"]
|
2020-09-21 21:43:54 +03:00
|
|
|
doc = Doc(en_vocab, words=words, pos=pos, heads=heads, deps=deps)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert doc[0].orth_ == "Give"
|
|
|
|
assert doc[0].text == "Give"
|
|
|
|
assert doc[0].text_with_ws == "Give "
|
|
|
|
assert doc[0].lower_ == "give"
|
|
|
|
assert doc[0].shape_ == "Xxxx"
|
|
|
|
assert doc[0].prefix_ == "G"
|
|
|
|
assert doc[0].suffix_ == "ive"
|
|
|
|
assert doc[0].pos_ == "VERB"
|
|
|
|
assert doc[0].dep_ == "ROOT"
|
2017-01-11 15:56:54 +03:00
|
|
|
|
|
|
|
|
2017-01-11 21:00:52 +03:00
|
|
|
def test_doc_token_api_flags(en_tokenizer):
|
2017-01-11 15:56:54 +03:00
|
|
|
text = "Give it back! He pleaded."
|
|
|
|
tokens = en_tokenizer(text)
|
|
|
|
assert tokens[0].check_flag(IS_ALPHA)
|
|
|
|
assert not tokens[0].check_flag(IS_DIGIT)
|
|
|
|
assert tokens[0].check_flag(IS_TITLE)
|
|
|
|
assert tokens[1].check_flag(IS_LOWER)
|
|
|
|
assert tokens[3].check_flag(IS_PUNCT)
|
|
|
|
assert tokens[2].check_flag(IS_STOP)
|
|
|
|
assert not tokens[5].check_flag(IS_STOP)
|
2015-03-26 17:06:26 +03:00
|
|
|
# TODO: Test more of these, esp. if a bug is found
|
2015-04-07 07:05:18 +03:00
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.parametrize("text", ["Give it back! He pleaded."])
|
2017-01-11 21:00:52 +03:00
|
|
|
def test_doc_token_api_prob_inherited_from_vocab(en_tokenizer, text):
|
2017-01-11 15:56:54 +03:00
|
|
|
word = text.split()[0]
|
|
|
|
en_tokenizer.vocab[word].prob = -1
|
|
|
|
tokens = en_tokenizer(text)
|
|
|
|
assert tokens[0].prob != 0
|
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.parametrize("text", ["one two"])
|
2017-01-11 21:00:52 +03:00
|
|
|
def test_doc_token_api_str_builtin(en_tokenizer, text):
|
2017-01-11 15:56:54 +03:00
|
|
|
tokens = en_tokenizer(text)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert str(tokens[0]) == text.split(" ")[0]
|
|
|
|
assert str(tokens[1]) == text.split(" ")[1]
|
2017-01-11 15:56:54 +03:00
|
|
|
|
|
|
|
|
2017-01-11 21:00:52 +03:00
|
|
|
def test_doc_token_api_is_properties(en_vocab):
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(en_vocab, words=["Hi", ",", "my", "email", "is", "test@me.com"])
|
2017-01-11 15:56:54 +03:00
|
|
|
assert doc[0].is_title
|
|
|
|
assert doc[0].is_alpha
|
|
|
|
assert not doc[0].is_digit
|
|
|
|
assert doc[1].is_punct
|
|
|
|
assert doc[3].is_ascii
|
|
|
|
assert not doc[3].like_url
|
|
|
|
assert doc[4].is_lower
|
|
|
|
assert doc[5].like_email
|
|
|
|
|
|
|
|
|
2017-10-24 18:05:15 +03:00
|
|
|
def test_doc_token_api_vectors():
|
|
|
|
vocab = Vocab()
|
2017-10-31 20:25:08 +03:00
|
|
|
vocab.reset_vectors(width=2)
|
2018-11-27 03:09:36 +03:00
|
|
|
vocab.set_vector("apples", vector=numpy.asarray([0.0, 2.0], dtype="f"))
|
|
|
|
vocab.set_vector("oranges", vector=numpy.asarray([0.0, 1.0], dtype="f"))
|
|
|
|
doc = Doc(vocab, words=["apples", "oranges", "oov"])
|
2017-10-24 18:05:15 +03:00
|
|
|
assert doc.has_vector
|
|
|
|
assert doc[0].has_vector
|
|
|
|
assert doc[1].has_vector
|
|
|
|
assert not doc[2].has_vector
|
2018-11-27 03:09:36 +03:00
|
|
|
apples_norm = (0 * 0 + 2 * 2) ** 0.5
|
|
|
|
oranges_norm = (0 * 0 + 1 * 1) ** 0.5
|
|
|
|
cosine = ((0 * 0) + (2 * 1)) / (apples_norm * oranges_norm)
|
2017-10-24 18:05:15 +03:00
|
|
|
assert doc[0].similarity(doc[1]) == cosine
|
2017-01-11 15:56:54 +03:00
|
|
|
|
|
|
|
|
2020-09-21 21:43:54 +03:00
|
|
|
def test_doc_token_api_ancestors(en_vocab):
|
2016-03-11 19:31:06 +03:00
|
|
|
# the structure of this sentence depends on the English annotation scheme
|
2020-09-21 21:43:54 +03:00
|
|
|
words = ["Yesterday", "I", "saw", "a", "dog", "that", "barked", "loudly", "."]
|
|
|
|
heads = [2, 2, 2, 4, 2, 6, 4, 6, 2]
|
2021-06-15 14:23:32 +03:00
|
|
|
deps = ["dep"] * len(heads)
|
|
|
|
doc = Doc(en_vocab, words=words, heads=heads, deps=deps)
|
2017-01-11 15:56:54 +03:00
|
|
|
assert [t.text for t in doc[6].ancestors] == ["dog", "saw"]
|
|
|
|
assert [t.text for t in doc[1].ancestors] == ["saw"]
|
|
|
|
assert [t.text for t in doc[2].ancestors] == []
|
2017-05-19 21:23:40 +03:00
|
|
|
assert doc[2].is_ancestor(doc[7])
|
|
|
|
assert not doc[6].is_ancestor(doc[2])
|
2016-03-11 19:31:06 +03:00
|
|
|
|
|
|
|
|
2020-09-21 21:43:54 +03:00
|
|
|
def test_doc_token_api_head_setter(en_vocab):
|
|
|
|
words = ["Yesterday", "I", "saw", "a", "dog", "that", "barked", "loudly", "."]
|
|
|
|
heads = [2, 2, 2, 4, 2, 6, 4, 6, 2]
|
2020-09-17 01:14:01 +03:00
|
|
|
deps = ["dep"] * len(heads)
|
2020-09-21 21:43:54 +03:00
|
|
|
doc = Doc(en_vocab, words=words, heads=heads, deps=deps)
|
2017-01-11 15:56:54 +03:00
|
|
|
assert doc[6].n_lefts == 1
|
|
|
|
assert doc[6].n_rights == 1
|
|
|
|
assert doc[6].left_edge.i == 5
|
|
|
|
assert doc[6].right_edge.i == 7
|
|
|
|
assert doc[4].n_lefts == 1
|
|
|
|
assert doc[4].n_rights == 1
|
|
|
|
assert doc[4].left_edge.i == 3
|
|
|
|
assert doc[4].right_edge.i == 7
|
|
|
|
assert doc[3].n_lefts == 0
|
|
|
|
assert doc[3].n_rights == 0
|
|
|
|
assert doc[3].left_edge.i == 3
|
|
|
|
assert doc[3].right_edge.i == 3
|
|
|
|
assert doc[2].left_edge.i == 0
|
|
|
|
assert doc[2].right_edge.i == 8
|
|
|
|
|
|
|
|
doc[6].head = doc[3]
|
|
|
|
assert doc[6].n_lefts == 1
|
|
|
|
assert doc[6].n_rights == 1
|
|
|
|
assert doc[6].left_edge.i == 5
|
|
|
|
assert doc[6].right_edge.i == 7
|
|
|
|
assert doc[3].n_lefts == 0
|
|
|
|
assert doc[3].n_rights == 1
|
|
|
|
assert doc[3].left_edge.i == 3
|
|
|
|
assert doc[3].right_edge.i == 7
|
|
|
|
assert doc[4].n_lefts == 1
|
|
|
|
assert doc[4].n_rights == 0
|
|
|
|
assert doc[4].left_edge.i == 3
|
|
|
|
assert doc[4].right_edge.i == 7
|
|
|
|
assert doc[2].left_edge.i == 0
|
|
|
|
assert doc[2].right_edge.i == 8
|
|
|
|
|
|
|
|
doc[0].head = doc[5]
|
|
|
|
assert doc[5].left_edge.i == 0
|
|
|
|
assert doc[6].left_edge.i == 0
|
|
|
|
assert doc[3].left_edge.i == 0
|
|
|
|
assert doc[4].left_edge.i == 0
|
|
|
|
assert doc[2].left_edge.i == 0
|
2020-03-03 23:44:51 +03:00
|
|
|
# head token must be from the same document
|
2021-06-15 14:23:32 +03:00
|
|
|
doc2 = Doc(en_vocab, words=words, heads=heads, deps=["dep"] * len(heads))
|
2020-03-03 23:44:51 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
doc[0].head = doc2[0]
|
2020-09-16 21:32:38 +03:00
|
|
|
# test sentence starts when two sentences are joined
|
2020-09-21 21:43:54 +03:00
|
|
|
# fmt: off
|
|
|
|
words = ["This", "is", "one", "sentence", ".", "This", "is", "another", "sentence", "."]
|
|
|
|
heads = [0, 0, 0, 0, 0, 5, 5, 5, 5, 5]
|
|
|
|
# fmt: on
|
|
|
|
doc = Doc(en_vocab, words=words, heads=heads, deps=["dep"] * len(heads))
|
2020-09-16 21:32:38 +03:00
|
|
|
# initially two sentences
|
|
|
|
assert doc[0].is_sent_start
|
|
|
|
assert doc[5].is_sent_start
|
|
|
|
assert doc[0].left_edge == doc[0]
|
|
|
|
assert doc[0].right_edge == doc[4]
|
|
|
|
assert doc[5].left_edge == doc[5]
|
|
|
|
assert doc[5].right_edge == doc[9]
|
|
|
|
# modifying with a sentence doesn't change sent starts
|
|
|
|
doc[2].head = doc[3]
|
|
|
|
assert doc[0].is_sent_start
|
|
|
|
assert doc[5].is_sent_start
|
|
|
|
assert doc[0].left_edge == doc[0]
|
|
|
|
assert doc[0].right_edge == doc[4]
|
|
|
|
assert doc[5].left_edge == doc[5]
|
|
|
|
assert doc[5].right_edge == doc[9]
|
|
|
|
# attach the second sentence to the first, resulting in one sentence
|
|
|
|
doc[5].head = doc[0]
|
|
|
|
assert doc[0].is_sent_start
|
|
|
|
assert not doc[5].is_sent_start
|
|
|
|
assert doc[0].left_edge == doc[0]
|
|
|
|
assert doc[0].right_edge == doc[9]
|
|
|
|
|
2016-05-05 13:10:07 +03:00
|
|
|
|
2017-11-01 15:27:14 +03:00
|
|
|
def test_is_sent_start(en_tokenizer):
|
2018-11-27 03:09:36 +03:00
|
|
|
doc = en_tokenizer("This is a sentence. This is another.")
|
2017-11-01 15:27:14 +03:00
|
|
|
assert doc[5].is_sent_start is None
|
|
|
|
doc[5].is_sent_start = True
|
|
|
|
assert doc[5].is_sent_start is True
|
2016-05-05 13:10:07 +03:00
|
|
|
assert len(list(doc.sents)) == 2
|
2018-03-27 22:21:11 +03:00
|
|
|
|
2020-05-21 15:14:01 +03:00
|
|
|
|
2020-04-29 13:53:16 +03:00
|
|
|
def test_is_sent_end(en_tokenizer):
|
|
|
|
doc = en_tokenizer("This is a sentence. This is another.")
|
|
|
|
assert doc[4].is_sent_end is None
|
|
|
|
doc[5].is_sent_start = True
|
|
|
|
assert doc[4].is_sent_end is True
|
|
|
|
assert len(list(doc.sents)) == 2
|
|
|
|
|
2018-07-09 19:05:10 +03:00
|
|
|
|
2018-03-27 22:21:11 +03:00
|
|
|
def test_set_pos():
|
2018-11-27 03:09:36 +03:00
|
|
|
doc = Doc(Vocab(), words=["hello", "world"])
|
|
|
|
doc[0].pos_ = "NOUN"
|
|
|
|
assert doc[0].pos_ == "NOUN"
|
2018-03-27 22:21:11 +03:00
|
|
|
doc[1].pos = VERB
|
2018-11-27 03:09:36 +03:00
|
|
|
assert doc[1].pos_ == "VERB"
|
2018-07-09 19:05:10 +03:00
|
|
|
|
2021-09-20 09:49:19 +03:00
|
|
|
|
2021-09-16 14:28:05 +03:00
|
|
|
def test_set_invalid_pos():
|
|
|
|
doc = Doc(Vocab(), words=["hello", "world"])
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
doc[0].pos_ = "blah"
|
2018-07-09 19:05:10 +03:00
|
|
|
|
2021-09-20 09:49:19 +03:00
|
|
|
|
2018-07-06 16:54:15 +03:00
|
|
|
def test_tokens_sent(doc):
|
|
|
|
"""Test token.sent property"""
|
|
|
|
assert len(list(doc.sents)) == 3
|
2018-11-27 03:09:36 +03:00
|
|
|
assert doc[1].sent.text == "This is a sentence ."
|
|
|
|
assert doc[7].sent.text == "This is another sentence ."
|
|
|
|
assert doc[1].sent.root.left_edge.text == "This"
|
|
|
|
assert doc[7].sent.root.left_edge.text == "This"
|
2019-02-27 13:17:17 +03:00
|
|
|
|
2019-02-27 13:56:45 +03:00
|
|
|
|
2019-02-27 13:17:17 +03:00
|
|
|
def test_token0_has_sent_start_true():
|
|
|
|
doc = Doc(Vocab(), words=["hello", "world"])
|
|
|
|
assert doc[0].is_sent_start is True
|
|
|
|
assert doc[1].is_sent_start is None
|
2020-09-17 01:14:01 +03:00
|
|
|
assert not doc.has_annotation("SENT_START")
|
2019-03-11 17:46:52 +03:00
|
|
|
|
2020-05-21 15:14:01 +03:00
|
|
|
|
2020-04-29 13:53:16 +03:00
|
|
|
def test_tokenlast_has_sent_end_true():
|
|
|
|
doc = Doc(Vocab(), words=["hello", "world"])
|
|
|
|
assert doc[0].is_sent_end is None
|
|
|
|
assert doc[1].is_sent_end is True
|
2020-09-17 01:14:01 +03:00
|
|
|
assert not doc.has_annotation("SENT_START")
|
2020-04-29 13:53:16 +03:00
|
|
|
|
2019-03-11 17:46:52 +03:00
|
|
|
|
|
|
|
def test_token_api_conjuncts_chain(en_vocab):
|
2020-09-21 21:43:54 +03:00
|
|
|
words = ["The", "boy", "and", "the", "girl", "and", "the", "man", "went", "."]
|
|
|
|
heads = [1, 8, 1, 4, 1, 4, 7, 4, 8, 8]
|
2019-03-11 17:46:52 +03:00
|
|
|
deps = ["det", "nsubj", "cc", "det", "conj", "cc", "det", "conj", "ROOT", "punct"]
|
2020-09-21 21:43:54 +03:00
|
|
|
doc = Doc(en_vocab, words=words, heads=heads, deps=deps)
|
2019-03-11 17:46:52 +03:00
|
|
|
assert [w.text for w in doc[1].conjuncts] == ["girl", "man"]
|
|
|
|
assert [w.text for w in doc[4].conjuncts] == ["boy", "man"]
|
|
|
|
assert [w.text for w in doc[7].conjuncts] == ["boy", "girl"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_token_api_conjuncts_simple(en_vocab):
|
2020-09-21 21:43:54 +03:00
|
|
|
words = ["They", "came", "and", "went", "."]
|
|
|
|
heads = [1, 1, 1, 1, 3]
|
2020-03-02 13:49:28 +03:00
|
|
|
deps = ["nsubj", "ROOT", "cc", "conj", "dep"]
|
2020-09-21 21:43:54 +03:00
|
|
|
doc = Doc(en_vocab, words=words, heads=heads, deps=deps)
|
2019-03-11 17:46:52 +03:00
|
|
|
assert [w.text for w in doc[1].conjuncts] == ["went"]
|
|
|
|
assert [w.text for w in doc[3].conjuncts] == ["came"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_token_api_non_conjuncts(en_vocab):
|
2020-09-21 21:43:54 +03:00
|
|
|
words = ["They", "came", "."]
|
|
|
|
heads = [1, 1, 1]
|
2019-03-11 17:46:52 +03:00
|
|
|
deps = ["nsubj", "ROOT", "punct"]
|
2020-09-21 21:43:54 +03:00
|
|
|
doc = Doc(en_vocab, words=words, heads=heads, deps=deps)
|
2019-03-11 17:46:52 +03:00
|
|
|
assert [w.text for w in doc[0].conjuncts] == []
|
|
|
|
assert [w.text for w in doc[1].conjuncts] == []
|
2021-01-12 19:17:06 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_missing_head_dep(en_vocab):
|
2021-07-02 10:48:26 +03:00
|
|
|
"""Check that the Doc constructor and Example.from_dict parse missing information the same"""
|
2021-01-15 03:57:36 +03:00
|
|
|
heads = [1, 1, 1, 1, 2, None] # element 5 is missing
|
|
|
|
deps = ["", "ROOT", "dobj", "cc", "conj", None] # element 0 and 5 are missing
|
2021-01-12 19:17:06 +03:00
|
|
|
words = ["I", "like", "London", "and", "Berlin", "."]
|
|
|
|
doc = Doc(en_vocab, words=words, heads=heads, deps=deps)
|
2021-01-12 19:28:41 +03:00
|
|
|
pred_has_heads = [t.has_head() for t in doc]
|
2021-01-13 16:20:05 +03:00
|
|
|
pred_has_deps = [t.has_dep() for t in doc]
|
|
|
|
pred_heads = [t.head.i for t in doc]
|
2021-01-12 19:28:41 +03:00
|
|
|
pred_deps = [t.dep_ for t in doc]
|
2021-01-13 15:47:25 +03:00
|
|
|
pred_sent_starts = [t.is_sent_start for t in doc]
|
2021-01-13 16:20:05 +03:00
|
|
|
assert pred_has_heads == [False, True, True, True, True, False]
|
|
|
|
assert pred_has_deps == [False, True, True, True, True, False]
|
|
|
|
assert pred_heads[1:5] == [1, 1, 1, 2]
|
|
|
|
assert pred_deps[1:5] == ["ROOT", "dobj", "cc", "conj"]
|
2021-01-13 15:47:25 +03:00
|
|
|
assert pred_sent_starts == [True, False, False, False, False, False]
|
2021-01-12 19:17:06 +03:00
|
|
|
example = Example.from_dict(doc, {"heads": heads, "deps": deps})
|
2021-01-13 16:20:05 +03:00
|
|
|
ref_has_heads = [t.has_head() for t in example.reference]
|
|
|
|
ref_has_deps = [t.has_dep() for t in example.reference]
|
2021-01-12 19:17:06 +03:00
|
|
|
ref_heads = [t.head.i for t in example.reference]
|
|
|
|
ref_deps = [t.dep_ for t in example.reference]
|
2021-01-13 15:47:25 +03:00
|
|
|
ref_sent_starts = [t.is_sent_start for t in example.reference]
|
2021-01-13 16:20:05 +03:00
|
|
|
assert ref_has_heads == pred_has_heads
|
|
|
|
assert ref_has_deps == pred_has_heads
|
|
|
|
assert ref_heads == pred_heads
|
|
|
|
assert ref_deps == pred_deps
|
|
|
|
assert ref_sent_starts == pred_sent_starts
|
|
|
|
# check that the aligned parse preserves the missing information
|
2021-01-12 19:17:06 +03:00
|
|
|
aligned_heads, aligned_deps = example.get_aligned_parse(projectivize=True)
|
2021-01-13 16:20:05 +03:00
|
|
|
assert aligned_deps[0] == ref_deps[0]
|
|
|
|
assert aligned_heads[0] == ref_heads[0]
|
|
|
|
assert aligned_deps[5] == ref_deps[5]
|
2021-01-12 19:17:06 +03:00
|
|
|
assert aligned_heads[5] == ref_heads[5]
|