2018-07-25 00:38:44 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from spacy.tokens import Doc
|
|
|
|
from spacy.displacy import render
|
|
|
|
from spacy.gold import iob_to_biluo
|
2018-09-27 17:41:57 +03:00
|
|
|
from spacy.lang.it import Italian
|
2019-02-08 16:14:49 +03:00
|
|
|
import numpy
|
2019-02-21 00:10:13 +03:00
|
|
|
from spacy.lang.en import English
|
2018-07-25 00:38:44 +03:00
|
|
|
|
2019-02-08 16:14:49 +03:00
|
|
|
from ..util import add_vecs_to_vocab, get_doc
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
2019-02-21 00:10:13 +03:00
|
|
|
@pytest.mark.xfail(
|
|
|
|
reason="The dot is now properly split off, but the prefix/suffix rules are not applied again afterwards."
|
|
|
|
"This means that the quote will still be attached to the remaining token."
|
|
|
|
)
|
|
|
|
def test_issue2070():
|
|
|
|
"""Test that checks that a dot followed by a quote is handled appropriately."""
|
|
|
|
nlp = English()
|
|
|
|
doc = nlp('First sentence."A quoted sentence" he said ...')
|
|
|
|
assert len(doc) == 11
|
|
|
|
|
|
|
|
|
2018-09-27 17:41:57 +03:00
|
|
|
def test_issue2179():
|
|
|
|
"""Test that spurious 'extra_labels' aren't created when initializing NER."""
|
|
|
|
nlp = Italian()
|
2018-11-27 03:09:36 +03:00
|
|
|
ner = nlp.create_pipe("ner")
|
|
|
|
ner.add_label("CITIZENSHIP")
|
2018-09-27 17:41:57 +03:00
|
|
|
nlp.add_pipe(ner)
|
|
|
|
nlp.begin_training()
|
|
|
|
nlp2 = Italian()
|
2018-11-27 03:09:36 +03:00
|
|
|
nlp2.add_pipe(nlp2.create_pipe("ner"))
|
2018-09-27 17:41:57 +03:00
|
|
|
nlp2.from_bytes(nlp.to_bytes())
|
2018-11-27 03:09:36 +03:00
|
|
|
assert "extra_labels" not in nlp2.get_pipe("ner").cfg
|
2019-02-14 22:03:19 +03:00
|
|
|
assert nlp2.get_pipe("ner").labels == ("CITIZENSHIP",)
|
2018-09-27 17:41:57 +03:00
|
|
|
|
|
|
|
|
2018-07-25 00:38:44 +03:00
|
|
|
def test_issue2219(en_vocab):
|
|
|
|
vectors = [("a", [1, 2, 3]), ("letter", [4, 5, 6])]
|
|
|
|
add_vecs_to_vocab(en_vocab, vectors)
|
|
|
|
[(word1, vec1), (word2, vec2)] = vectors
|
|
|
|
doc = Doc(en_vocab, words=[word1, word2])
|
|
|
|
assert doc[0].similarity(doc[1]) == doc[1].similarity(doc[0])
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue2361(de_tokenizer):
|
2018-11-27 03:09:36 +03:00
|
|
|
chars = ("<", ">", "&", """)
|
2018-07-25 00:38:44 +03:00
|
|
|
doc = de_tokenizer('< > & " ')
|
|
|
|
doc.is_parsed = True
|
|
|
|
doc.is_tagged = True
|
|
|
|
html = render(doc)
|
|
|
|
for char in chars:
|
|
|
|
assert char in html
|
|
|
|
|
|
|
|
|
|
|
|
def test_issue2385():
|
|
|
|
"""Test that IOB tags are correctly converted to BILUO tags."""
|
|
|
|
# fix bug in labels with a 'b' character
|
2018-11-27 03:09:36 +03:00
|
|
|
tags1 = ("B-BRAWLER", "I-BRAWLER", "I-BRAWLER")
|
|
|
|
assert iob_to_biluo(tags1) == ["B-BRAWLER", "I-BRAWLER", "L-BRAWLER"]
|
2018-07-25 00:38:44 +03:00
|
|
|
# maintain support for iob1 format
|
2018-11-27 03:09:36 +03:00
|
|
|
tags2 = ("I-ORG", "I-ORG", "B-ORG")
|
|
|
|
assert iob_to_biluo(tags2) == ["B-ORG", "L-ORG", "U-ORG"]
|
2018-07-25 00:38:44 +03:00
|
|
|
# maintain support for iob2 format
|
2018-11-27 03:09:36 +03:00
|
|
|
tags3 = ("B-PERSON", "I-PERSON", "B-PERSON")
|
|
|
|
assert iob_to_biluo(tags3) == ["B-PERSON", "L-PERSON", "U-PERSON"]
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"tags",
|
|
|
|
[
|
|
|
|
("B-ORG", "L-ORG"),
|
|
|
|
("B-PERSON", "I-PERSON", "L-PERSON"),
|
|
|
|
("U-BRAWLER", "U-BRAWLER"),
|
|
|
|
],
|
|
|
|
)
|
2018-07-25 00:38:44 +03:00
|
|
|
def test_issue2385_biluo(tags):
|
|
|
|
"""Test that BILUO-compatible tags aren't modified."""
|
|
|
|
assert iob_to_biluo(tags) == list(tags)
|
2018-09-28 16:18:30 +03:00
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
|
2019-02-08 16:14:49 +03:00
|
|
|
def test_issue2396(en_vocab):
|
|
|
|
words = ["She", "created", "a", "test", "for", "spacy"]
|
|
|
|
heads = [1, 0, 1, -2, -1, -1]
|
|
|
|
matrix = numpy.array(
|
|
|
|
[
|
|
|
|
[0, 1, 1, 1, 1, 1],
|
|
|
|
[1, 1, 1, 1, 1, 1],
|
|
|
|
[1, 1, 2, 3, 3, 3],
|
|
|
|
[1, 1, 3, 3, 3, 3],
|
|
|
|
[1, 1, 3, 3, 4, 4],
|
|
|
|
[1, 1, 3, 3, 4, 5],
|
|
|
|
],
|
|
|
|
dtype=numpy.int32,
|
|
|
|
)
|
|
|
|
doc = get_doc(en_vocab, words=words, heads=heads)
|
|
|
|
span = doc[:]
|
|
|
|
assert (doc.get_lca_matrix() == matrix).all()
|
|
|
|
assert (span.get_lca_matrix() == matrix).all()
|
|
|
|
|
|
|
|
|
2018-09-28 16:18:30 +03:00
|
|
|
def test_issue2482():
|
2018-11-27 03:09:36 +03:00
|
|
|
"""Test we can serialize and deserialize a blank NER or parser model."""
|
2018-09-28 16:18:30 +03:00
|
|
|
nlp = Italian()
|
2018-11-27 03:09:36 +03:00
|
|
|
nlp.add_pipe(nlp.create_pipe("ner"))
|
2018-09-28 16:18:30 +03:00
|
|
|
b = nlp.to_bytes()
|
2018-11-30 19:43:08 +03:00
|
|
|
Italian().from_bytes(b)
|