mirror of
https://github.com/explosion/spaCy.git
synced 2025-07-10 08:12:24 +03:00
Modernise token API tests and don't depend on loading models
This commit is contained in:
parent
c682b8ca90
commit
eb23424ab0
|
@ -1,138 +1,157 @@
|
||||||
|
# coding: utf-8
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from spacy.en import English
|
|
||||||
from spacy.attrs import IS_ALPHA, IS_ASCII, IS_DIGIT, IS_LOWER, IS_PUNCT
|
from ...attrs import IS_ALPHA, IS_DIGIT, IS_LOWER, IS_PUNCT, IS_TITLE, IS_STOP
|
||||||
from spacy.attrs import IS_SPACE, IS_TITLE, IS_UPPER, LIKE_URL, LIKE_NUM
|
from ..util import get_doc
|
||||||
from spacy.attrs import IS_STOP
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import numpy
|
import numpy
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.models
|
def test_token_api_strings(en_tokenizer):
|
||||||
def test_strings(EN):
|
text = "Give it back! He pleaded."
|
||||||
tokens = EN(u'Give it back! He pleaded.')
|
tags = ['VERB', 'PRON', 'PART', 'PUNCT', 'PRON', 'VERB', 'PUNCT']
|
||||||
token = tokens[0]
|
heads = [0, -1, -2, -3, 1, 0, -1]
|
||||||
assert token.orth_ == 'Give'
|
deps = ['ROOT', 'dobj', 'prt', 'punct', 'nsubj', 'ROOT', 'punct']
|
||||||
assert token.text == 'Give'
|
|
||||||
assert token.text_with_ws == 'Give '
|
tokens = en_tokenizer(text)
|
||||||
assert token.lower_ == 'give'
|
doc = get_doc(tokens.vocab, [t.text for t in tokens], tags, heads, deps)
|
||||||
assert token.shape_ == 'Xxxx'
|
assert doc[0].orth_ == 'Give'
|
||||||
assert token.prefix_ == 'G'
|
assert doc[0].text == 'Give'
|
||||||
assert token.suffix_ == 'ive'
|
assert doc[0].text_with_ws == 'Give '
|
||||||
assert token.lemma_ == 'give'
|
assert doc[0].lower_ == 'give'
|
||||||
assert token.pos_ == 'VERB'
|
assert doc[0].shape_ == 'Xxxx'
|
||||||
assert token.tag_ == 'VB'
|
assert doc[0].prefix_ == 'G'
|
||||||
assert token.dep_ == 'ROOT'
|
assert doc[0].suffix_ == 'ive'
|
||||||
|
assert doc[0].pos_ == 'VERB'
|
||||||
|
assert doc[0].dep_ == 'ROOT'
|
||||||
|
|
||||||
|
|
||||||
def test_flags(EN):
|
def test_token_api_flags(en_tokenizer):
|
||||||
tokens = EN(u'Give it back! He pleaded.')
|
text = "Give it back! He pleaded."
|
||||||
token = tokens[0]
|
tokens = en_tokenizer(text)
|
||||||
|
assert tokens[0].check_flag(IS_ALPHA)
|
||||||
assert token.check_flag(IS_ALPHA)
|
assert not tokens[0].check_flag(IS_DIGIT)
|
||||||
assert not token.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)
|
||||||
# TODO: Test more of these, esp. if a bug is found
|
# TODO: Test more of these, esp. if a bug is found
|
||||||
|
|
||||||
|
|
||||||
def test_single_token_string(EN):
|
@pytest.mark.parametrize('text', ["Give it back! He pleaded."])
|
||||||
tokens = EN(u'foobar')
|
def test_token_api_prob_inherited_from_vocab(en_tokenizer, text):
|
||||||
assert tokens[0].text == 'foobar'
|
word = text.split()[0]
|
||||||
|
en_tokenizer.vocab[word].prob = -1
|
||||||
|
tokens = en_tokenizer(text)
|
||||||
|
assert tokens[0].prob != 0
|
||||||
|
|
||||||
|
|
||||||
def test_str_builtin(EN):
|
@pytest.mark.parametrize('text', ["one two"])
|
||||||
tokens = EN('one two')
|
def test_token_api_str_builtin(en_tokenizer, text):
|
||||||
assert str(tokens[0]) == u'one'
|
tokens = en_tokenizer(text)
|
||||||
assert str(tokens[1]) == u'two'
|
assert str(tokens[0]) == text.split(' ')[0]
|
||||||
|
assert str(tokens[1]) == text.split(' ')[1]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.models
|
def test_token_api_is_properties(en_vocab):
|
||||||
def test_is_properties(EN):
|
text = ["Hi", ",", "my", "email", "is", "test@me.com"]
|
||||||
Hi, comma, my, email, is_, addr = EN(u'Hi, my email is test@me.com')
|
doc = get_doc(en_vocab, text)
|
||||||
assert Hi.is_title
|
assert doc[0].is_title
|
||||||
assert Hi.is_alpha
|
assert doc[0].is_alpha
|
||||||
assert not Hi.is_digit
|
assert not doc[0].is_digit
|
||||||
assert comma.is_punct
|
assert doc[1].is_punct
|
||||||
assert email.is_ascii
|
assert doc[3].is_ascii
|
||||||
assert not email.like_url
|
assert not doc[3].like_url
|
||||||
assert is_.is_lower
|
assert doc[4].is_lower
|
||||||
assert addr.like_email
|
assert doc[5].like_email
|
||||||
assert addr.is_oov
|
|
||||||
assert not Hi.is_oov
|
|
||||||
|
|
||||||
@pytest.mark.models
|
|
||||||
def test_vectors(EN):
|
@pytest.mark.parametrize('text,vectors', [
|
||||||
apples, oranges, oov = EN(u'apples oranges ldskbjlsdkbflzdfbl')
|
("apples oranges ldskbjls", ["apples -1 -1 -1", "oranges -1 -1 0"])
|
||||||
assert apples.has_vector
|
])
|
||||||
assert oranges.has_vector
|
def test_token_api_vectors(en_tokenizer, text_file, text, vectors):
|
||||||
assert not oov.has_vector
|
text_file.write('\n'.join(vectors))
|
||||||
assert apples.similarity(oranges) > apples.similarity(oov)
|
text_file.seek(0)
|
||||||
assert apples.similarity(oranges) == oranges.similarity(apples)
|
vector_length = en_tokenizer.vocab.load_vectors(text_file)
|
||||||
assert sum(apples.vector) != sum(oranges.vector)
|
assert vector_length == 3
|
||||||
|
|
||||||
|
tokens = en_tokenizer(text)
|
||||||
|
assert tokens[0].has_vector
|
||||||
|
assert tokens[1].has_vector
|
||||||
|
assert not tokens[2].has_vector
|
||||||
|
assert tokens[0].similarity(tokens[1]) > tokens[0].similarity(tokens[2])
|
||||||
|
assert tokens[0].similarity(tokens[1]) == tokens[1].similarity(tokens[0])
|
||||||
|
assert sum(tokens[0].vector) != sum(tokens[1].vector)
|
||||||
assert numpy.isclose(
|
assert numpy.isclose(
|
||||||
apples.vector_norm,
|
tokens[0].vector_norm,
|
||||||
numpy.sqrt(numpy.dot(apples.vector, apples.vector)))
|
numpy.sqrt(numpy.dot(tokens[0].vector, tokens[0].vector)))
|
||||||
|
|
||||||
@pytest.mark.models
|
|
||||||
def test_ancestors(EN):
|
def test_token_api_ancestors(en_tokenizer):
|
||||||
# the structure of this sentence depends on the English annotation scheme
|
# the structure of this sentence depends on the English annotation scheme
|
||||||
tokens = EN(u'Yesterday I saw a dog that barked loudly.')
|
text = "Yesterday I saw a dog that barked loudly."
|
||||||
ancestors = [ t.orth_ for t in tokens[6].ancestors ]
|
heads = [2, 1, 0, 1, -2, 1, -2, -1, -6]
|
||||||
assert ancestors == ['dog','saw']
|
tokens = en_tokenizer(text)
|
||||||
ancestors = [ t.orth_ for t in tokens[1].ancestors ]
|
doc = get_doc(tokens.vocab, [t.text for t in tokens], heads=heads)
|
||||||
assert ancestors == ['saw']
|
assert [t.text for t in doc[6].ancestors] == ["dog", "saw"]
|
||||||
ancestors = [ t.orth_ for t in tokens[2].ancestors ]
|
assert [t.text for t in doc[1].ancestors] == ["saw"]
|
||||||
assert ancestors == []
|
assert [t.text for t in doc[2].ancestors] == []
|
||||||
|
|
||||||
assert tokens[2].is_ancestor_of(tokens[7])
|
assert doc[2].is_ancestor_of(doc[7])
|
||||||
assert not tokens[6].is_ancestor_of(tokens[2])
|
assert not doc[6].is_ancestor_of(doc[2])
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.models
|
def test_token_api_head_setter(en_tokenizer):
|
||||||
def test_head_setter(EN):
|
|
||||||
# the structure of this sentence depends on the English annotation scheme
|
# the structure of this sentence depends on the English annotation scheme
|
||||||
yesterday, i, saw, a, dog, that, barked, loudly, dot = EN(u'Yesterday I saw a dog that barked loudly.')
|
text = "Yesterday I saw a dog that barked loudly."
|
||||||
assert barked.n_lefts == 1
|
heads = [2, 1, 0, 1, -2, 1, -2, -1, -6]
|
||||||
assert barked.n_rights == 1
|
tokens = en_tokenizer(text)
|
||||||
assert barked.left_edge == that
|
doc = get_doc(tokens.vocab, [t.text for t in tokens], heads=heads)
|
||||||
assert barked.right_edge == loudly
|
|
||||||
|
|
||||||
assert dog.n_lefts == 1
|
assert doc[6].n_lefts == 1
|
||||||
assert dog.n_rights == 1
|
assert doc[6].n_rights == 1
|
||||||
assert dog.left_edge == a
|
assert doc[6].left_edge.i == 5
|
||||||
assert dog.right_edge == loudly
|
assert doc[6].right_edge.i == 7
|
||||||
|
|
||||||
assert a.n_lefts == 0
|
|
||||||
assert a.n_rights == 0
|
|
||||||
assert a.left_edge == a
|
|
||||||
assert a.right_edge == a
|
|
||||||
|
|
||||||
assert saw.left_edge == yesterday
|
assert doc[4].n_lefts == 1
|
||||||
assert saw.right_edge == dot
|
assert doc[4].n_rights == 1
|
||||||
|
assert doc[4].left_edge.i == 3
|
||||||
|
assert doc[4].right_edge.i == 7
|
||||||
|
|
||||||
barked.head = a
|
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 barked.n_lefts == 1
|
assert doc[2].left_edge.i == 0
|
||||||
assert barked.n_rights == 1
|
assert doc[2].right_edge.i == 8
|
||||||
assert barked.left_edge == that
|
|
||||||
assert barked.right_edge == loudly
|
|
||||||
|
|
||||||
assert a.n_lefts == 0
|
doc[6].head = doc[3]
|
||||||
assert a.n_rights == 1
|
|
||||||
assert a.left_edge == a
|
|
||||||
assert a.right_edge == loudly
|
|
||||||
|
|
||||||
assert dog.n_lefts == 1
|
assert doc[6].n_lefts == 1
|
||||||
assert dog.n_rights == 0
|
assert doc[6].n_rights == 1
|
||||||
assert dog.left_edge == a
|
assert doc[6].left_edge.i == 5
|
||||||
assert dog.right_edge == loudly
|
assert doc[6].right_edge.i == 7
|
||||||
|
|
||||||
assert saw.left_edge == yesterday
|
assert doc[3].n_lefts == 0
|
||||||
assert saw.right_edge == dot
|
assert doc[3].n_rights == 1
|
||||||
|
assert doc[3].left_edge.i == 3
|
||||||
|
assert doc[3].right_edge.i == 7
|
||||||
|
|
||||||
yesterday.head = that
|
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 that.left_edge == yesterday
|
assert doc[2].left_edge.i == 0
|
||||||
assert barked.left_edge == yesterday
|
assert doc[2].right_edge.i == 8
|
||||||
assert a.left_edge == yesterday
|
|
||||||
assert dog.left_edge == yesterday
|
doc[0].head = doc[5]
|
||||||
assert saw.left_edge == yesterday
|
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user