2017-01-12 18:50:22 +03:00
|
|
|
import pytest
|
2018-07-25 00:38:44 +03:00
|
|
|
import numpy
|
|
|
|
from spacy.tokens import Doc
|
|
|
|
|
|
|
|
from ..util import get_cosine, add_vecs_to_vocab
|
2016-10-23 15:33:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2017-01-12 18:50:22 +03:00
|
|
|
def vectors():
|
2017-01-13 16:29:54 +03:00
|
|
|
return [("apple", [1, 2, 3]), ("orange", [-1, -2, -3])]
|
2017-01-12 18:50:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def vocab(en_vocab, vectors):
|
2017-08-19 21:34:58 +03:00
|
|
|
add_vecs_to_vocab(en_vocab, vectors)
|
|
|
|
return en_vocab
|
2017-01-12 18:50:22 +03:00
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
|
2017-01-12 18:50:22 +03:00
|
|
|
def test_vectors_similarity_LL(vocab, vectors):
|
2017-01-13 16:29:54 +03:00
|
|
|
[(word1, vec1), (word2, vec2)] = vectors
|
2017-01-12 18:50:22 +03:00
|
|
|
lex1 = vocab[word1]
|
|
|
|
lex2 = vocab[word2]
|
|
|
|
assert lex1.has_vector
|
|
|
|
assert lex2.has_vector
|
|
|
|
assert lex1.vector_norm != 0
|
|
|
|
assert lex2.vector_norm != 0
|
|
|
|
assert lex1.vector[0] != lex2.vector[0] and lex1.vector[1] != lex2.vector[1]
|
|
|
|
assert numpy.isclose(lex1.similarity(lex2), get_cosine(vec1, vec2))
|
|
|
|
assert numpy.isclose(lex2.similarity(lex2), lex1.similarity(lex1))
|
|
|
|
|
|
|
|
|
|
|
|
def test_vectors_similarity_TT(vocab, vectors):
|
2017-01-13 16:29:54 +03:00
|
|
|
[(word1, vec1), (word2, vec2)] = vectors
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(vocab, words=[word1, word2])
|
2017-01-12 18:50:22 +03:00
|
|
|
assert doc[0].has_vector
|
|
|
|
assert doc[1].has_vector
|
|
|
|
assert doc[0].vector_norm != 0
|
|
|
|
assert doc[1].vector_norm != 0
|
|
|
|
assert doc[0].vector[0] != doc[1].vector[0] and doc[0].vector[1] != doc[1].vector[1]
|
|
|
|
assert numpy.isclose(doc[0].similarity(doc[1]), get_cosine(vec1, vec2))
|
|
|
|
assert numpy.isclose(doc[1].similarity(doc[0]), doc[0].similarity(doc[1]))
|
|
|
|
|
|
|
|
|
|
|
|
def test_vectors_similarity_TD(vocab, vectors):
|
2017-01-13 16:29:54 +03:00
|
|
|
[(word1, vec1), (word2, vec2)] = vectors
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(vocab, words=[word1, word2])
|
2019-02-10 16:02:19 +03:00
|
|
|
with pytest.warns(UserWarning):
|
2018-05-21 02:22:38 +03:00
|
|
|
assert doc.similarity(doc[0]) == doc[0].similarity(doc)
|
2017-01-12 18:50:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_vectors_similarity_DS(vocab, vectors):
|
2017-01-13 16:29:54 +03:00
|
|
|
[(word1, vec1), (word2, vec2)] = vectors
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(vocab, words=[word1, word2])
|
2017-01-12 18:50:22 +03:00
|
|
|
assert doc.similarity(doc[:2]) == doc[:2].similarity(doc)
|
|
|
|
|
|
|
|
|
|
|
|
def test_vectors_similarity_TS(vocab, vectors):
|
2017-01-13 16:29:54 +03:00
|
|
|
[(word1, vec1), (word2, vec2)] = vectors
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(vocab, words=[word1, word2])
|
2019-02-10 16:02:19 +03:00
|
|
|
with pytest.warns(UserWarning):
|
2018-05-21 02:22:38 +03:00
|
|
|
assert doc[:2].similarity(doc[0]) == doc[0].similarity(doc[:2])
|