2018-07-25 00:38:44 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import gc
|
|
|
|
import numpy
|
|
|
|
import copy
|
|
|
|
from spacy.lang.en import English
|
|
|
|
from spacy.lang.en.stop_words import STOP_WORDS
|
|
|
|
from spacy.lang.lex_attrs import is_stop
|
|
|
|
from spacy.vectors import Vectors
|
|
|
|
from spacy.vocab import Vocab
|
|
|
|
from spacy.language import Language
|
|
|
|
from spacy.tokens import Doc, Span
|
|
|
|
from spacy.pipeline import Tagger, EntityRecognizer
|
|
|
|
from spacy.attrs import HEAD, DEP
|
|
|
|
from spacy.matcher import Matcher
|
|
|
|
|
|
|
|
from ..util import make_tempdir
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1506():
|
|
|
|
def string_generator():
|
|
|
|
for _ in range(10001):
|
|
|
|
yield "It's sentence produced by that bug."
|
|
|
|
for _ in range(10001):
|
|
|
|
yield "I erase some hbdsaj lemmas."
|
|
|
|
for _ in range(10001):
|
|
|
|
yield "I erase lemmas."
|
|
|
|
for _ in range(10001):
|
|
|
|
yield "It's sentence produced by that bug."
|
|
|
|
for _ in range(10001):
|
|
|
|
yield "It's sentence produced by that bug."
|
|
|
|
|
|
|
|
nlp = English()
|
|
|
|
for i, d in enumerate(nlp.pipe(string_generator())):
|
|
|
|
# We should run cleanup more than one time to actually cleanup data.
|
|
|
|
# In first run — clean up only mark strings as «not hitted».
|
|
|
|
if i == 10000 or i == 20000 or i == 30000:
|
|
|
|
gc.collect()
|
|
|
|
for t in d:
|
|
|
|
str(t.lemma_)
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1518():
|
|
|
|
"""Test vectors.resize() works."""
|
|
|
|
vectors = Vectors(shape=(10, 10))
|
2018-11-27 03:09:36 +03:00
|
|
|
vectors.add("hello", row=2)
|
2018-07-25 00:38:44 +03:00
|
|
|
vectors.resize((5, 9))
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1537():
|
|
|
|
"""Test that Span.as_doc() doesn't segfault."""
|
2018-11-27 03:09:36 +03:00
|
|
|
string = "The sky is blue . The man is pink . The dog is purple ."
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(Vocab(), words=string.split())
|
|
|
|
doc[0].sent_start = True
|
|
|
|
for word in doc[1:]:
|
2018-11-27 03:09:36 +03:00
|
|
|
if word.nbor(-1).text == ".":
|
2018-07-25 00:38:44 +03:00
|
|
|
word.sent_start = True
|
|
|
|
else:
|
|
|
|
word.sent_start = False
|
|
|
|
sents = list(doc.sents)
|
|
|
|
sent0 = sents[0].as_doc()
|
|
|
|
sent1 = sents[1].as_doc()
|
|
|
|
assert isinstance(sent0, Doc)
|
|
|
|
assert isinstance(sent1, Doc)
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: Currently segfaulting, due to l_edge and r_edge misalignment
|
2018-11-27 03:09:36 +03:00
|
|
|
# def test_issue1537_model():
|
2018-07-25 00:38:44 +03:00
|
|
|
# nlp = load_spacy('en')
|
|
|
|
# doc = nlp('The sky is blue. The man is pink. The dog is purple.')
|
|
|
|
# sents = [s.as_doc() for s in doc.sents]
|
|
|
|
# print(list(sents[0].noun_chunks))
|
|
|
|
# print(list(sents[1].noun_chunks))
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1539():
|
|
|
|
"""Ensure vectors.resize() doesn't try to modify dictionary during iteration."""
|
2018-11-27 03:09:36 +03:00
|
|
|
v = Vectors(shape=(10, 10), keys=[5, 3, 98, 100])
|
|
|
|
v.resize((100, 100))
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_issue1547():
|
|
|
|
"""Test that entity labels still match after merging tokens."""
|
2018-11-27 03:09:36 +03:00
|
|
|
words = ["\n", "worda", ".", "\n", "wordb", "-", "Biosphere", "2", "-", " \n"]
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = Doc(Vocab(), words=words)
|
2018-11-27 03:09:36 +03:00
|
|
|
doc.ents = [Span(doc, 6, 8, label=doc.vocab.strings["PRODUCT"])]
|
2018-07-25 00:38:44 +03:00
|
|
|
doc[5:7].merge()
|
|
|
|
assert [ent.text for ent in doc.ents]
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1612(en_tokenizer):
|
2018-11-27 03:09:36 +03:00
|
|
|
doc = en_tokenizer("The black cat purrs.")
|
|
|
|
span = doc[1:3]
|
2018-07-25 00:38:44 +03:00
|
|
|
assert span.orth_ == span.text
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1654():
|
|
|
|
nlp = Language(Vocab())
|
|
|
|
assert not nlp.pipeline
|
2018-11-27 03:09:36 +03:00
|
|
|
nlp.add_pipe(lambda doc: doc, name="1")
|
|
|
|
nlp.add_pipe(lambda doc: doc, name="2", after="1")
|
|
|
|
nlp.add_pipe(lambda doc: doc, name="3", after="2")
|
|
|
|
assert nlp.pipe_names == ["1", "2", "3"]
|
2018-07-25 00:38:44 +03:00
|
|
|
nlp2 = Language(Vocab())
|
|
|
|
assert not nlp2.pipeline
|
2018-11-27 03:09:36 +03:00
|
|
|
nlp2.add_pipe(lambda doc: doc, name="3")
|
|
|
|
nlp2.add_pipe(lambda doc: doc, name="2", before="3")
|
|
|
|
nlp2.add_pipe(lambda doc: doc, name="1", before="2")
|
|
|
|
assert nlp2.pipe_names == ["1", "2", "3"]
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.parametrize("text", ["test@example.com", "john.doe@example.co.uk"])
|
2018-07-25 00:38:44 +03:00
|
|
|
def test_issue1698(en_tokenizer, text):
|
|
|
|
doc = en_tokenizer(text)
|
|
|
|
assert len(doc) == 1
|
|
|
|
assert not doc[0].like_url
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1727():
|
|
|
|
"""Test that models with no pretrained vectors can be deserialized
|
|
|
|
correctly after vectors are added."""
|
2018-11-27 03:09:36 +03:00
|
|
|
data = numpy.ones((3, 300), dtype="f")
|
|
|
|
vectors = Vectors(data=data, keys=["I", "am", "Matt"])
|
2018-07-25 00:38:44 +03:00
|
|
|
tagger = Tagger(Vocab())
|
2018-11-27 03:09:36 +03:00
|
|
|
tagger.add_label("PRP")
|
2018-07-25 00:38:44 +03:00
|
|
|
tagger.begin_training()
|
2018-11-27 03:09:36 +03:00
|
|
|
assert tagger.cfg.get("pretrained_dims", 0) == 0
|
2018-07-25 00:38:44 +03:00
|
|
|
tagger.vocab.vectors = vectors
|
|
|
|
with make_tempdir() as path:
|
|
|
|
tagger.to_disk(path)
|
|
|
|
tagger = Tagger(Vocab()).from_disk(path)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert tagger.cfg.get("pretrained_dims", 0) == 0
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_issue1757():
|
|
|
|
"""Test comparison against None doesn't cause segfault."""
|
2018-11-27 03:09:36 +03:00
|
|
|
doc = Doc(Vocab(), words=["a", "b", "c"])
|
2018-07-25 00:38:44 +03:00
|
|
|
assert not doc[0] < None
|
2018-11-30 19:43:08 +03:00
|
|
|
assert not doc[0] is None
|
2018-07-25 00:38:44 +03:00
|
|
|
assert doc[0] >= None
|
|
|
|
assert not doc[:2] < None
|
2018-11-30 19:43:08 +03:00
|
|
|
assert not doc[:2] is None
|
2018-07-25 00:38:44 +03:00
|
|
|
assert doc[:2] >= None
|
2018-11-30 19:43:08 +03:00
|
|
|
assert not doc.vocab["a"] is None
|
2018-11-27 03:09:36 +03:00
|
|
|
assert not doc.vocab["a"] < None
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_issue1758(en_tokenizer):
|
|
|
|
"""Test that "would've" is handled by the English tokenizer exceptions."""
|
|
|
|
tokens = en_tokenizer("would've")
|
|
|
|
assert len(tokens) == 2
|
|
|
|
assert tokens[0].tag_ == "MD"
|
|
|
|
assert tokens[1].lemma_ == "have"
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1799():
|
|
|
|
"""Test sentence boundaries are deserialized correctly, even for
|
|
|
|
non-projective sentences."""
|
2018-11-27 03:09:36 +03:00
|
|
|
heads_deps = numpy.asarray(
|
|
|
|
[
|
|
|
|
[1, 397],
|
|
|
|
[4, 436],
|
|
|
|
[2, 426],
|
|
|
|
[1, 402],
|
|
|
|
[0, 8206900633647566924],
|
|
|
|
[18446744073709551615, 440],
|
|
|
|
[18446744073709551614, 442],
|
|
|
|
],
|
|
|
|
dtype="uint64",
|
|
|
|
)
|
|
|
|
doc = Doc(Vocab(), words="Just what I was looking for .".split())
|
|
|
|
doc.vocab.strings.add("ROOT")
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = doc.from_array([HEAD, DEP], heads_deps)
|
|
|
|
assert len(list(doc.sents)) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1807():
|
|
|
|
"""Test vocab.set_vector also adds the word to the vocab."""
|
|
|
|
vocab = Vocab()
|
2018-11-27 03:09:36 +03:00
|
|
|
assert "hello" not in vocab
|
|
|
|
vocab.set_vector("hello", numpy.ones((50,), dtype="f"))
|
|
|
|
assert "hello" in vocab
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_issue1834():
|
|
|
|
"""Test that sentence boundaries & parse/tag flags are not lost
|
|
|
|
during serialization."""
|
|
|
|
string = "This is a first sentence . And another one"
|
|
|
|
doc = Doc(Vocab(), words=string.split())
|
|
|
|
doc[6].sent_start = True
|
|
|
|
new_doc = Doc(doc.vocab).from_bytes(doc.to_bytes())
|
|
|
|
assert new_doc[6].sent_start
|
|
|
|
assert not new_doc.is_parsed
|
|
|
|
assert not new_doc.is_tagged
|
|
|
|
doc.is_parsed = True
|
|
|
|
doc.is_tagged = True
|
|
|
|
new_doc = Doc(doc.vocab).from_bytes(doc.to_bytes())
|
|
|
|
assert new_doc.is_parsed
|
|
|
|
assert new_doc.is_tagged
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1868():
|
|
|
|
"""Test Vocab.__contains__ works with int keys."""
|
|
|
|
vocab = Vocab()
|
2018-11-27 03:09:36 +03:00
|
|
|
lex = vocab["hello"]
|
2018-07-25 00:38:44 +03:00
|
|
|
assert lex.orth in vocab
|
|
|
|
assert lex.orth_ in vocab
|
2018-11-27 03:09:36 +03:00
|
|
|
assert "some string" not in vocab
|
|
|
|
int_id = vocab.strings.add("some string")
|
2018-07-25 00:38:44 +03:00
|
|
|
assert int_id not in vocab
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1883():
|
|
|
|
matcher = Matcher(Vocab())
|
2018-11-27 03:09:36 +03:00
|
|
|
matcher.add("pat1", None, [{"orth": "hello"}])
|
|
|
|
doc = Doc(matcher.vocab, words=["hello"])
|
2018-07-25 00:38:44 +03:00
|
|
|
assert len(matcher(doc)) == 1
|
|
|
|
new_matcher = copy.deepcopy(matcher)
|
2018-11-27 03:09:36 +03:00
|
|
|
new_doc = Doc(new_matcher.vocab, words=["hello"])
|
2018-07-25 00:38:44 +03:00
|
|
|
assert len(new_matcher(new_doc)) == 1
|
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.parametrize("word", ["the"])
|
2018-07-25 00:38:44 +03:00
|
|
|
def test_issue1889(word):
|
|
|
|
assert is_stop(word, STOP_WORDS) == is_stop(word.upper(), STOP_WORDS)
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1915():
|
2018-11-27 03:09:36 +03:00
|
|
|
cfg = {"hidden_depth": 2} # should error out
|
2018-07-25 00:38:44 +03:00
|
|
|
nlp = Language()
|
2018-11-27 03:09:36 +03:00
|
|
|
nlp.add_pipe(nlp.create_pipe("ner"))
|
|
|
|
nlp.get_pipe("ner").add_label("answer")
|
2018-07-25 00:38:44 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
nlp.begin_training(**cfg)
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue1945():
|
|
|
|
"""Test regression in Matcher introduced in v2.0.6."""
|
|
|
|
matcher = Matcher(Vocab())
|
2018-11-27 03:09:36 +03:00
|
|
|
matcher.add("MWE", None, [{"orth": "a"}, {"orth": "a"}])
|
|
|
|
doc = Doc(matcher.vocab, words=["a", "a", "a"])
|
2018-07-25 00:38:44 +03:00
|
|
|
matches = matcher(doc) # we should see two overlapping matches here
|
|
|
|
assert len(matches) == 2
|
|
|
|
assert matches[0][1:] == (0, 2)
|
|
|
|
assert matches[1][1:] == (1, 3)
|
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.parametrize("label", ["U-JOB-NAME"])
|
2018-07-25 00:38:44 +03:00
|
|
|
def test_issue1967(label):
|
|
|
|
ner = EntityRecognizer(Vocab())
|
2018-11-27 03:09:36 +03:00
|
|
|
entry = ([0], ["word"], ["tag"], [0], ["dep"], [label])
|
2018-07-25 00:38:44 +03:00
|
|
|
gold_parses = [(None, [(entry, None)])]
|
|
|
|
ner.moves.get_actions(gold_parses=gold_parses)
|