2020-07-09 20:43:39 +03:00
|
|
|
import numpy
|
2020-09-22 12:50:19 +03:00
|
|
|
from spacy.training import offsets_to_biluo_tags, biluo_tags_to_offsets, Alignment
|
|
|
|
from spacy.training import biluo_tags_to_spans, iob_to_biluo
|
2020-10-04 18:46:29 +03:00
|
|
|
from spacy.training import Corpus, docs_to_json, Example
|
2020-11-03 18:24:38 +03:00
|
|
|
from spacy.training.align import get_alignments
|
2020-09-22 12:50:19 +03:00
|
|
|
from spacy.training.converters import json_to_docs
|
2019-09-15 23:31:31 +03:00
|
|
|
from spacy.lang.en import English
|
2020-06-26 20:34:12 +03:00
|
|
|
from spacy.tokens import Doc, DocBin
|
2020-07-06 14:06:25 +03:00
|
|
|
from spacy.util import get_words_and_spaces, minibatch
|
|
|
|
from thinc.api import compounding
|
2019-08-15 19:13:32 +03:00
|
|
|
import pytest
|
2019-09-15 23:31:31 +03:00
|
|
|
import srsly
|
2016-10-15 22:50:43 +03:00
|
|
|
|
2020-09-21 21:43:54 +03:00
|
|
|
from ..util import make_tempdir
|
2020-05-21 19:39:06 +03:00
|
|
|
|
2019-12-22 03:53:56 +03:00
|
|
|
|
2019-11-23 16:32:15 +03:00
|
|
|
@pytest.fixture
|
2020-10-04 18:46:29 +03:00
|
|
|
def doc():
|
2020-09-21 21:43:54 +03:00
|
|
|
nlp = English() # make sure we get a new vocab every time
|
2020-07-22 14:42:59 +03:00
|
|
|
# fmt: off
|
2020-09-21 21:43:54 +03:00
|
|
|
words = ["Sarah", "'s", "sister", "flew", "to", "Silicon", "Valley", "via", "London", "."]
|
2019-12-22 03:53:56 +03:00
|
|
|
tags = ["NNP", "POS", "NN", "VBD", "IN", "NNP", "NNP", "IN", "NNP", "."]
|
2020-07-22 14:42:59 +03:00
|
|
|
pos = ["PROPN", "PART", "NOUN", "VERB", "ADP", "PROPN", "PROPN", "ADP", "PROPN", "PUNCT"]
|
|
|
|
morphs = ["NounType=prop|Number=sing", "Poss=yes", "Number=sing", "Tense=past|VerbForm=fin",
|
|
|
|
"", "NounType=prop|Number=sing", "NounType=prop|Number=sing", "",
|
|
|
|
"NounType=prop|Number=sing", "PunctType=peri"]
|
2019-11-23 16:32:15 +03:00
|
|
|
# head of '.' is intentionally nonprojective for testing
|
|
|
|
heads = [2, 0, 3, 3, 3, 6, 4, 3, 7, 5]
|
2020-07-22 14:42:59 +03:00
|
|
|
deps = ["poss", "case", "nsubj", "ROOT", "prep", "compound", "pobj", "prep", "pobj", "punct"]
|
|
|
|
lemmas = ["Sarah", "'s", "sister", "fly", "to", "Silicon", "Valley", "via", "London", "."]
|
2020-10-01 17:22:18 +03:00
|
|
|
ents = ["O"] * len(words)
|
|
|
|
ents[0] = "B-PERSON"
|
|
|
|
ents[1] = "I-PERSON"
|
|
|
|
ents[5] = "B-LOC"
|
|
|
|
ents[6] = "I-LOC"
|
|
|
|
ents[8] = "B-GPE"
|
2019-11-23 16:32:15 +03:00
|
|
|
cats = {"TRAVEL": 1.0, "BAKING": 0.0}
|
2020-07-22 14:42:59 +03:00
|
|
|
# fmt: on
|
2020-09-21 21:43:54 +03:00
|
|
|
doc = Doc(
|
2020-09-21 11:59:07 +03:00
|
|
|
nlp.vocab,
|
|
|
|
words=words,
|
|
|
|
tags=tags,
|
|
|
|
pos=pos,
|
|
|
|
morphs=morphs,
|
|
|
|
heads=heads,
|
|
|
|
deps=deps,
|
|
|
|
lemmas=lemmas,
|
|
|
|
ents=ents,
|
|
|
|
)
|
2019-11-23 16:32:15 +03:00
|
|
|
doc.cats = cats
|
|
|
|
return doc
|
|
|
|
|
2019-08-18 16:09:16 +03:00
|
|
|
|
2019-11-25 18:03:28 +03:00
|
|
|
@pytest.fixture()
|
|
|
|
def merged_dict():
|
|
|
|
return {
|
|
|
|
"ids": [1, 2, 3, 4, 5, 6, 7],
|
|
|
|
"words": ["Hi", "there", "everyone", "It", "is", "just", "me"],
|
2020-06-26 20:34:12 +03:00
|
|
|
"spaces": [True, True, True, True, True, True, False],
|
2019-11-25 18:03:28 +03:00
|
|
|
"tags": ["INTJ", "ADV", "PRON", "PRON", "AUX", "ADV", "PRON"],
|
2020-06-26 20:34:12 +03:00
|
|
|
"sent_starts": [1, 0, 0, 1, 0, 0, 0],
|
2019-12-22 03:53:56 +03:00
|
|
|
}
|
2019-11-25 18:03:28 +03:00
|
|
|
|
2019-08-18 16:09:16 +03:00
|
|
|
|
2020-06-26 20:34:12 +03:00
|
|
|
@pytest.fixture
|
|
|
|
def vocab():
|
|
|
|
nlp = English()
|
|
|
|
return nlp.vocab
|
|
|
|
|
|
|
|
|
2017-01-13 01:39:18 +03:00
|
|
|
def test_gold_biluo_U(en_vocab):
|
2018-11-27 03:09:36 +03:00
|
|
|
words = ["I", "flew", "to", "London", "."]
|
|
|
|
spaces = [True, True, True, False, True]
|
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
|
|
|
entities = [(len("I flew to "), len("I flew to London"), "LOC")]
|
2020-09-22 12:50:19 +03:00
|
|
|
tags = offsets_to_biluo_tags(doc, entities)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert tags == ["O", "O", "O", "U-LOC", "O"]
|
2016-10-15 22:50:43 +03:00
|
|
|
|
|
|
|
|
2017-01-13 01:39:18 +03:00
|
|
|
def test_gold_biluo_BL(en_vocab):
|
2018-11-27 03:09:36 +03:00
|
|
|
words = ["I", "flew", "to", "San", "Francisco", "."]
|
|
|
|
spaces = [True, True, True, True, False, True]
|
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
|
|
|
entities = [(len("I flew to "), len("I flew to San Francisco"), "LOC")]
|
2020-09-22 12:50:19 +03:00
|
|
|
tags = offsets_to_biluo_tags(doc, entities)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert tags == ["O", "O", "O", "B-LOC", "L-LOC", "O"]
|
2016-10-15 22:50:43 +03:00
|
|
|
|
|
|
|
|
2017-01-13 01:39:18 +03:00
|
|
|
def test_gold_biluo_BIL(en_vocab):
|
2018-11-27 03:09:36 +03:00
|
|
|
words = ["I", "flew", "to", "San", "Francisco", "Valley", "."]
|
|
|
|
spaces = [True, True, True, True, True, False, True]
|
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
|
|
|
entities = [(len("I flew to "), len("I flew to San Francisco Valley"), "LOC")]
|
2020-09-22 12:50:19 +03:00
|
|
|
tags = offsets_to_biluo_tags(doc, entities)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert tags == ["O", "O", "O", "B-LOC", "I-LOC", "L-LOC", "O"]
|
2016-10-15 22:50:43 +03:00
|
|
|
|
2019-08-18 16:09:16 +03:00
|
|
|
|
2019-08-15 19:13:32 +03:00
|
|
|
def test_gold_biluo_overlap(en_vocab):
|
|
|
|
words = ["I", "flew", "to", "San", "Francisco", "Valley", "."]
|
|
|
|
spaces = [True, True, True, True, True, False, True]
|
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
2019-08-18 16:09:16 +03:00
|
|
|
entities = [
|
|
|
|
(len("I flew to "), len("I flew to San Francisco Valley"), "LOC"),
|
|
|
|
(len("I flew to "), len("I flew to San Francisco"), "LOC"),
|
|
|
|
]
|
2019-08-15 19:13:32 +03:00
|
|
|
with pytest.raises(ValueError):
|
2020-09-22 12:50:19 +03:00
|
|
|
offsets_to_biluo_tags(doc, entities)
|
2019-08-18 16:09:16 +03:00
|
|
|
|
2016-10-15 22:50:43 +03:00
|
|
|
|
2017-01-13 01:39:18 +03:00
|
|
|
def test_gold_biluo_misalign(en_vocab):
|
2018-11-27 03:09:36 +03:00
|
|
|
words = ["I", "flew", "to", "San", "Francisco", "Valley."]
|
|
|
|
spaces = [True, True, True, True, True, False]
|
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
|
|
|
entities = [(len("I flew to "), len("I flew to San Francisco Valley"), "LOC")]
|
2020-05-19 17:01:18 +03:00
|
|
|
with pytest.warns(UserWarning):
|
2020-09-22 12:50:19 +03:00
|
|
|
tags = offsets_to_biluo_tags(doc, entities)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert tags == ["O", "O", "O", "-", "-", "-"]
|
2017-11-26 18:38:01 +03:00
|
|
|
|
|
|
|
|
2020-07-09 20:43:39 +03:00
|
|
|
def test_example_constructor(en_vocab):
|
|
|
|
words = ["I", "like", "stuff"]
|
|
|
|
tags = ["NOUN", "VERB", "NOUN"]
|
|
|
|
tag_ids = [en_vocab.strings.add(tag) for tag in tags]
|
|
|
|
predicted = Doc(en_vocab, words=words)
|
|
|
|
reference = Doc(en_vocab, words=words)
|
|
|
|
reference = reference.from_array("TAG", numpy.array(tag_ids, dtype="uint64"))
|
|
|
|
example = Example(predicted, reference)
|
|
|
|
tags = example.get_aligned("TAG", as_string=True)
|
|
|
|
assert tags == ["NOUN", "VERB", "NOUN"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_example_from_dict_tags(en_vocab):
|
|
|
|
words = ["I", "like", "stuff"]
|
|
|
|
tags = ["NOUN", "VERB", "NOUN"]
|
|
|
|
predicted = Doc(en_vocab, words=words)
|
|
|
|
example = Example.from_dict(predicted, {"TAGS": tags})
|
|
|
|
tags = example.get_aligned("TAG", as_string=True)
|
|
|
|
assert tags == ["NOUN", "VERB", "NOUN"]
|
|
|
|
|
|
|
|
|
2020-06-26 20:34:12 +03:00
|
|
|
def test_example_from_dict_no_ner(en_vocab):
|
|
|
|
words = ["a", "b", "c", "d"]
|
|
|
|
spaces = [True, True, False, True]
|
|
|
|
predicted = Doc(en_vocab, words=words, spaces=spaces)
|
|
|
|
example = Example.from_dict(predicted, {"words": words})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
|
|
|
assert ner_tags == [None, None, None, None]
|
|
|
|
|
2020-07-04 17:25:34 +03:00
|
|
|
|
2020-06-26 20:34:12 +03:00
|
|
|
def test_example_from_dict_some_ner(en_vocab):
|
|
|
|
words = ["a", "b", "c", "d"]
|
|
|
|
spaces = [True, True, False, True]
|
|
|
|
predicted = Doc(en_vocab, words=words, spaces=spaces)
|
|
|
|
example = Example.from_dict(
|
2020-07-04 17:25:34 +03:00
|
|
|
predicted, {"words": words, "entities": ["U-LOC", None, None, None]}
|
2020-06-26 20:34:12 +03:00
|
|
|
)
|
|
|
|
ner_tags = example.get_aligned_ner()
|
|
|
|
assert ner_tags == ["U-LOC", None, None, None]
|
|
|
|
|
|
|
|
|
2020-08-14 16:00:52 +03:00
|
|
|
@pytest.mark.filterwarnings("ignore::UserWarning")
|
2020-09-22 12:50:19 +03:00
|
|
|
def test_json_to_docs_no_ner(en_vocab):
|
2020-07-04 17:25:34 +03:00
|
|
|
data = [
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"paragraphs": [
|
|
|
|
{
|
|
|
|
"sentences": [
|
|
|
|
{
|
|
|
|
"tokens": [
|
|
|
|
{"dep": "nn", "head": 1, "tag": "NNP", "orth": "Ms."},
|
|
|
|
{
|
|
|
|
"dep": "nsubj",
|
|
|
|
"head": 1,
|
|
|
|
"tag": "NNP",
|
|
|
|
"orth": "Haag",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"dep": "ROOT",
|
|
|
|
"head": 0,
|
|
|
|
"tag": "VBZ",
|
|
|
|
"orth": "plays",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"dep": "dobj",
|
|
|
|
"head": -1,
|
|
|
|
"tag": "NNP",
|
|
|
|
"orth": "Elianti",
|
|
|
|
},
|
|
|
|
{"dep": "punct", "head": -2, "tag": ".", "orth": "."},
|
|
|
|
]
|
|
|
|
}
|
2020-06-26 20:34:12 +03:00
|
|
|
]
|
2020-07-04 17:25:34 +03:00
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
]
|
2020-12-15 11:47:16 +03:00
|
|
|
docs = list(json_to_docs(data))
|
2020-06-26 20:34:12 +03:00
|
|
|
assert len(docs) == 1
|
|
|
|
for doc in docs:
|
2020-09-17 01:14:01 +03:00
|
|
|
assert not doc.has_annotation("ENT_IOB")
|
2020-06-26 20:34:12 +03:00
|
|
|
for token in doc:
|
|
|
|
assert token.ent_iob == 0
|
|
|
|
eg = Example(
|
|
|
|
Doc(
|
|
|
|
doc.vocab,
|
|
|
|
words=[w.text for w in doc],
|
2020-06-29 14:59:17 +03:00
|
|
|
spaces=[bool(w.whitespace_) for w in doc],
|
2020-06-26 20:34:12 +03:00
|
|
|
),
|
2020-06-29 14:59:17 +03:00
|
|
|
doc,
|
2020-06-26 20:34:12 +03:00
|
|
|
)
|
|
|
|
ner_tags = eg.get_aligned_ner()
|
|
|
|
assert ner_tags == [None, None, None, None, None]
|
|
|
|
|
|
|
|
|
|
|
|
def test_split_sentences(en_vocab):
|
2020-09-21 21:43:54 +03:00
|
|
|
# fmt: off
|
2020-06-26 20:34:12 +03:00
|
|
|
words = ["I", "flew", "to", "San Francisco Valley", "had", "loads of fun"]
|
2020-09-21 21:43:54 +03:00
|
|
|
gold_words = ["I", "flew", "to", "San", "Francisco", "Valley", "had", "loads", "of", "fun"]
|
2020-06-26 20:34:12 +03:00
|
|
|
sent_starts = [True, False, False, False, False, False, True, False, False, False]
|
2020-09-21 21:43:54 +03:00
|
|
|
# fmt: on
|
|
|
|
doc = Doc(en_vocab, words=words)
|
2020-06-26 20:34:12 +03:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "sent_starts": sent_starts})
|
|
|
|
assert example.text == "I flew to San Francisco Valley had loads of fun "
|
|
|
|
split_examples = example.split_sents()
|
|
|
|
assert len(split_examples) == 2
|
|
|
|
assert split_examples[0].text == "I flew to San Francisco Valley "
|
|
|
|
assert split_examples[1].text == "had loads of fun "
|
2020-09-21 21:43:54 +03:00
|
|
|
# fmt: off
|
2020-06-26 20:34:12 +03:00
|
|
|
words = ["I", "flew", "to", "San", "Francisco", "Valley", "had", "loads", "of fun"]
|
2020-09-21 21:43:54 +03:00
|
|
|
gold_words = ["I", "flew", "to", "San Francisco", "Valley", "had", "loads of", "fun"]
|
2020-06-26 20:34:12 +03:00
|
|
|
sent_starts = [True, False, False, False, False, True, False, False]
|
2020-09-21 21:43:54 +03:00
|
|
|
# fmt: on
|
|
|
|
doc = Doc(en_vocab, words=words)
|
2020-06-26 20:34:12 +03:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "sent_starts": sent_starts})
|
|
|
|
assert example.text == "I flew to San Francisco Valley had loads of fun "
|
|
|
|
split_examples = example.split_sents()
|
|
|
|
assert len(split_examples) == 2
|
|
|
|
assert split_examples[0].text == "I flew to San Francisco Valley "
|
|
|
|
assert split_examples[1].text == "had loads of fun "
|
|
|
|
|
|
|
|
|
2020-06-29 14:59:17 +03:00
|
|
|
def test_gold_biluo_one_to_many(en_vocab, en_tokenizer):
|
2020-07-07 19:46:00 +03:00
|
|
|
words = ["Mr and ", "Mrs Smith", "flew to", "San Francisco Valley", "."]
|
2020-07-06 18:39:31 +03:00
|
|
|
spaces = [True, True, True, False, False]
|
2020-04-23 17:58:23 +03:00
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
2020-07-07 19:46:00 +03:00
|
|
|
prefix = "Mr and Mrs Smith flew to "
|
2020-07-06 18:39:31 +03:00
|
|
|
entities = [(len(prefix), len(prefix + "San Francisco Valley"), "LOC")]
|
2020-07-07 19:46:00 +03:00
|
|
|
gold_words = ["Mr and Mrs Smith", "flew", "to", "San", "Francisco", "Valley", "."]
|
2020-06-26 20:34:12 +03:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 18:39:31 +03:00
|
|
|
assert ner_tags == ["O", "O", "O", "U-LOC", "O"]
|
2020-06-29 14:59:17 +03:00
|
|
|
|
|
|
|
entities = [
|
2020-07-07 19:46:00 +03:00
|
|
|
(len("Mr and "), len("Mr and Mrs Smith"), "PERSON"), # "Mrs Smith" is a PERSON
|
2020-07-06 18:39:31 +03:00
|
|
|
(len(prefix), len(prefix + "San Francisco Valley"), "LOC"),
|
2020-06-29 14:59:17 +03:00
|
|
|
]
|
2020-07-22 14:42:59 +03:00
|
|
|
# fmt: off
|
2020-07-07 19:46:00 +03:00
|
|
|
gold_words = ["Mr and", "Mrs", "Smith", "flew", "to", "San", "Francisco", "Valley", "."]
|
2020-07-22 14:42:59 +03:00
|
|
|
# fmt: on
|
2020-06-29 14:59:17 +03:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 18:39:31 +03:00
|
|
|
assert ner_tags == ["O", "U-PERSON", "O", "U-LOC", "O"]
|
2020-06-29 14:59:17 +03:00
|
|
|
|
|
|
|
entities = [
|
2020-07-07 19:46:00 +03:00
|
|
|
(len("Mr and "), len("Mr and Mrs"), "PERSON"), # "Mrs" is a Person
|
2020-07-06 18:39:31 +03:00
|
|
|
(len(prefix), len(prefix + "San Francisco Valley"), "LOC"),
|
2020-06-29 14:59:17 +03:00
|
|
|
]
|
2020-07-22 14:42:59 +03:00
|
|
|
# fmt: off
|
2020-07-07 19:46:00 +03:00
|
|
|
gold_words = ["Mr and", "Mrs", "Smith", "flew", "to", "San", "Francisco", "Valley", "."]
|
2020-07-22 14:42:59 +03:00
|
|
|
# fmt: on
|
2020-06-29 14:59:17 +03:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 18:39:31 +03:00
|
|
|
assert ner_tags == ["O", None, "O", "U-LOC", "O"]
|
2020-06-29 14:59:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_gold_biluo_many_to_one(en_vocab, en_tokenizer):
|
2020-07-07 19:46:00 +03:00
|
|
|
words = ["Mr and", "Mrs", "Smith", "flew", "to", "San", "Francisco", "Valley", "."]
|
2020-07-06 18:39:31 +03:00
|
|
|
spaces = [True, True, True, True, True, True, True, False, False]
|
2020-04-23 17:58:23 +03:00
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
2020-07-07 19:46:00 +03:00
|
|
|
prefix = "Mr and Mrs Smith flew to "
|
2020-07-06 18:39:31 +03:00
|
|
|
entities = [(len(prefix), len(prefix + "San Francisco Valley"), "LOC")]
|
2020-07-07 19:46:00 +03:00
|
|
|
gold_words = ["Mr and Mrs Smith", "flew to", "San Francisco Valley", "."]
|
2020-06-26 20:34:12 +03:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 18:39:31 +03:00
|
|
|
assert ner_tags == ["O", "O", "O", "O", "O", "B-LOC", "I-LOC", "L-LOC", "O"]
|
2020-04-23 17:58:23 +03:00
|
|
|
|
2020-06-29 14:59:17 +03:00
|
|
|
entities = [
|
2020-07-07 19:46:00 +03:00
|
|
|
(len("Mr and "), len("Mr and Mrs Smith"), "PERSON"), # "Mrs Smith" is a PERSON
|
2020-07-06 18:39:31 +03:00
|
|
|
(len(prefix), len(prefix + "San Francisco Valley"), "LOC"),
|
2020-06-29 14:59:17 +03:00
|
|
|
]
|
2020-07-07 19:46:00 +03:00
|
|
|
gold_words = ["Mr and", "Mrs Smith", "flew to", "San Francisco Valley", "."]
|
2020-06-29 14:59:17 +03:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-22 14:42:59 +03:00
|
|
|
expected = ["O", "B-PERSON", "L-PERSON", "O", "O", "B-LOC", "I-LOC", "L-LOC", "O"]
|
|
|
|
assert ner_tags == expected
|
2020-06-29 14:59:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_gold_biluo_misaligned(en_vocab, en_tokenizer):
|
2020-07-07 19:46:00 +03:00
|
|
|
words = ["Mr and Mrs", "Smith", "flew", "to", "San Francisco", "Valley", "."]
|
2020-07-06 18:39:31 +03:00
|
|
|
spaces = [True, True, True, True, True, False, False]
|
2020-04-23 17:58:23 +03:00
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
2020-07-07 19:46:00 +03:00
|
|
|
prefix = "Mr and Mrs Smith flew to "
|
2020-07-06 18:39:31 +03:00
|
|
|
entities = [(len(prefix), len(prefix + "San Francisco Valley"), "LOC")]
|
2020-07-07 19:46:00 +03:00
|
|
|
gold_words = ["Mr", "and Mrs Smith", "flew to", "San", "Francisco Valley", "."]
|
2020-06-29 14:59:17 +03:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
2020-06-26 20:34:12 +03:00
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 18:39:31 +03:00
|
|
|
assert ner_tags == ["O", "O", "O", "O", "B-LOC", "L-LOC", "O"]
|
2020-04-23 17:58:23 +03:00
|
|
|
|
2020-06-29 14:59:17 +03:00
|
|
|
entities = [
|
2020-07-07 19:46:00 +03:00
|
|
|
(len("Mr and "), len("Mr and Mrs Smith"), "PERSON"), # "Mrs Smith" is a PERSON
|
2020-07-06 18:39:31 +03:00
|
|
|
(len(prefix), len(prefix + "San Francisco Valley"), "LOC"),
|
2020-06-29 14:59:17 +03:00
|
|
|
]
|
2020-07-07 19:46:00 +03:00
|
|
|
gold_words = ["Mr and", "Mrs Smith", "flew to", "San", "Francisco Valley", "."]
|
2020-06-29 14:59:17 +03:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 18:39:31 +03:00
|
|
|
assert ner_tags == [None, None, "O", "O", "B-LOC", "L-LOC", "O"]
|
2020-06-29 14:59:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_gold_biluo_additional_whitespace(en_vocab, en_tokenizer):
|
2020-04-23 17:58:23 +03:00
|
|
|
# additional whitespace tokens in GoldParse words
|
|
|
|
words, spaces = get_words_and_spaces(
|
|
|
|
["I", "flew", "to", "San Francisco", "Valley", "."],
|
|
|
|
"I flew to San Francisco Valley.",
|
|
|
|
)
|
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
2020-07-06 18:39:31 +03:00
|
|
|
prefix = "I flew to "
|
|
|
|
entities = [(len(prefix), len(prefix + "San Francisco Valley"), "LOC")]
|
2020-06-26 20:34:12 +03:00
|
|
|
gold_words = ["I", "flew", " ", "to", "San Francisco Valley", "."]
|
|
|
|
gold_spaces = [True, True, False, True, False, False]
|
|
|
|
example = Example.from_dict(
|
|
|
|
doc, {"words": gold_words, "spaces": gold_spaces, "entities": entities}
|
2020-04-23 17:58:23 +03:00
|
|
|
)
|
2020-06-26 20:34:12 +03:00
|
|
|
ner_tags = example.get_aligned_ner()
|
|
|
|
assert ner_tags == ["O", "O", "O", "O", "B-LOC", "L-LOC", "O"]
|
2020-04-23 17:58:23 +03:00
|
|
|
|
2020-06-29 14:59:17 +03:00
|
|
|
|
|
|
|
def test_gold_biluo_4791(en_vocab, en_tokenizer):
|
2021-05-31 11:03:40 +03:00
|
|
|
doc = en_tokenizer("I'll return the A54 amount")
|
|
|
|
gold_words = ["I", "'ll", "return", "the", "A", "54", "amount"]
|
2020-06-26 20:34:12 +03:00
|
|
|
gold_spaces = [False, True, True, True, False, True, False]
|
|
|
|
entities = [(16, 19, "MONEY")]
|
|
|
|
example = Example.from_dict(
|
|
|
|
doc, {"words": gold_words, "spaces": gold_spaces, "entities": entities}
|
2020-04-23 17:58:23 +03:00
|
|
|
)
|
2020-06-26 20:34:12 +03:00
|
|
|
ner_tags = example.get_aligned_ner()
|
|
|
|
assert ner_tags == ["O", "O", "O", "O", "U-MONEY", "O"]
|
|
|
|
|
|
|
|
doc = en_tokenizer("I'll return the $54 amount")
|
|
|
|
gold_words = ["I", "'ll", "return", "the", "$", "54", "amount"]
|
|
|
|
gold_spaces = [False, True, True, True, False, True, False]
|
|
|
|
entities = [(16, 19, "MONEY")]
|
|
|
|
example = Example.from_dict(
|
|
|
|
doc, {"words": gold_words, "spaces": gold_spaces, "entities": entities}
|
2020-04-23 17:58:23 +03:00
|
|
|
)
|
2020-06-26 20:34:12 +03:00
|
|
|
ner_tags = example.get_aligned_ner()
|
|
|
|
assert ner_tags == ["O", "O", "O", "O", "B-MONEY", "L-MONEY", "O"]
|
2020-04-23 17:58:23 +03:00
|
|
|
|
|
|
|
|
2017-11-26 18:38:01 +03:00
|
|
|
def test_roundtrip_offsets_biluo_conversion(en_tokenizer):
|
|
|
|
text = "I flew to Silicon Valley via London."
|
2018-11-27 03:09:36 +03:00
|
|
|
biluo_tags = ["O", "O", "O", "B-LOC", "L-LOC", "O", "U-GPE", "O"]
|
|
|
|
offsets = [(10, 24, "LOC"), (29, 35, "GPE")]
|
2017-11-26 18:38:01 +03:00
|
|
|
doc = en_tokenizer(text)
|
2020-09-22 12:50:19 +03:00
|
|
|
biluo_tags_converted = offsets_to_biluo_tags(doc, offsets)
|
2017-11-26 18:38:01 +03:00
|
|
|
assert biluo_tags_converted == biluo_tags
|
2020-09-22 12:50:19 +03:00
|
|
|
offsets_converted = biluo_tags_to_offsets(doc, biluo_tags)
|
2020-06-26 20:34:12 +03:00
|
|
|
offsets_converted = [ent for ent in offsets if ent[2]]
|
2017-11-26 18:38:01 +03:00
|
|
|
assert offsets_converted == offsets
|
2019-02-06 13:50:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_biluo_spans(en_tokenizer):
|
|
|
|
doc = en_tokenizer("I flew to Silicon Valley via London.")
|
|
|
|
biluo_tags = ["O", "O", "O", "B-LOC", "L-LOC", "O", "U-GPE", "O"]
|
2020-09-22 12:50:19 +03:00
|
|
|
spans = biluo_tags_to_spans(doc, biluo_tags)
|
2020-06-26 20:34:12 +03:00
|
|
|
spans = [span for span in spans if span.label_]
|
2019-02-06 13:50:26 +03:00
|
|
|
assert len(spans) == 2
|
|
|
|
assert spans[0].text == "Silicon Valley"
|
|
|
|
assert spans[0].label_ == "LOC"
|
|
|
|
assert spans[1].text == "London"
|
|
|
|
assert spans[1].label_ == "GPE"
|
2019-02-27 14:06:32 +03:00
|
|
|
|
2019-02-27 16:24:55 +03:00
|
|
|
|
2020-07-07 19:46:00 +03:00
|
|
|
def test_aligned_spans_y2x(en_vocab, en_tokenizer):
|
|
|
|
words = ["Mr and Mrs Smith", "flew", "to", "San Francisco Valley", "."]
|
|
|
|
spaces = [True, True, True, False, False]
|
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
|
|
|
prefix = "Mr and Mrs Smith flew to "
|
|
|
|
entities = [
|
|
|
|
(0, len("Mr and Mrs Smith"), "PERSON"),
|
|
|
|
(len(prefix), len(prefix + "San Francisco Valley"), "LOC"),
|
|
|
|
]
|
2020-07-22 14:42:59 +03:00
|
|
|
# fmt: off
|
2020-07-07 19:46:00 +03:00
|
|
|
tokens_ref = ["Mr", "and", "Mrs", "Smith", "flew", "to", "San", "Francisco", "Valley", "."]
|
2020-07-22 14:42:59 +03:00
|
|
|
# fmt: on
|
2020-07-07 19:46:00 +03:00
|
|
|
example = Example.from_dict(doc, {"words": tokens_ref, "entities": entities})
|
|
|
|
ents_ref = example.reference.ents
|
|
|
|
assert [(ent.start, ent.end) for ent in ents_ref] == [(0, 4), (6, 9)]
|
|
|
|
ents_y2x = example.get_aligned_spans_y2x(ents_ref)
|
|
|
|
assert [(ent.start, ent.end) for ent in ents_y2x] == [(0, 1), (3, 4)]
|
|
|
|
|
|
|
|
|
|
|
|
def test_aligned_spans_x2y(en_vocab, en_tokenizer):
|
|
|
|
text = "Mr and Mrs Smith flew to San Francisco Valley"
|
|
|
|
nlp = English()
|
2020-07-22 14:42:59 +03:00
|
|
|
patterns = [
|
|
|
|
{"label": "PERSON", "pattern": "Mr and Mrs Smith"},
|
|
|
|
{"label": "LOC", "pattern": "San Francisco Valley"},
|
|
|
|
]
|
|
|
|
ruler = nlp.add_pipe("entity_ruler")
|
2020-07-07 19:46:00 +03:00
|
|
|
ruler.add_patterns(patterns)
|
|
|
|
doc = nlp(text)
|
|
|
|
assert [(ent.start, ent.end) for ent in doc.ents] == [(0, 4), (6, 9)]
|
|
|
|
prefix = "Mr and Mrs Smith flew to "
|
|
|
|
entities = [
|
|
|
|
(0, len("Mr and Mrs Smith"), "PERSON"),
|
|
|
|
(len(prefix), len(prefix + "San Francisco Valley"), "LOC"),
|
|
|
|
]
|
|
|
|
tokens_ref = ["Mr and Mrs", "Smith", "flew", "to", "San Francisco", "Valley"]
|
|
|
|
example = Example.from_dict(doc, {"words": tokens_ref, "entities": entities})
|
|
|
|
assert [(ent.start, ent.end) for ent in example.reference.ents] == [(0, 2), (4, 6)]
|
|
|
|
# Ensure that 'get_aligned_spans_x2y' has the aligned entities correct
|
|
|
|
ents_pred = example.predicted.ents
|
|
|
|
assert [(ent.start, ent.end) for ent in ents_pred] == [(0, 4), (6, 9)]
|
|
|
|
ents_x2y = example.get_aligned_spans_x2y(ents_pred)
|
|
|
|
assert [(ent.start, ent.end) for ent in ents_x2y] == [(0, 2), (4, 6)]
|
|
|
|
|
|
|
|
|
2021-04-08 13:19:17 +03:00
|
|
|
def test_aligned_spans_y2x_overlap(en_vocab, en_tokenizer):
|
|
|
|
text = "I flew to San Francisco Valley"
|
|
|
|
nlp = English()
|
|
|
|
doc = nlp(text)
|
|
|
|
# the reference doc has overlapping spans
|
|
|
|
gold_doc = nlp.make_doc(text)
|
|
|
|
spans = []
|
|
|
|
prefix = "I flew to "
|
2021-06-28 12:48:00 +03:00
|
|
|
spans.append(
|
|
|
|
gold_doc.char_span(len(prefix), len(prefix + "San Francisco"), label="CITY")
|
|
|
|
)
|
|
|
|
spans.append(
|
|
|
|
gold_doc.char_span(
|
|
|
|
len(prefix), len(prefix + "San Francisco Valley"), label="VALLEY"
|
|
|
|
)
|
|
|
|
)
|
2021-04-08 13:19:17 +03:00
|
|
|
spans_key = "overlap_ents"
|
|
|
|
gold_doc.spans[spans_key] = spans
|
|
|
|
example = Example(doc, gold_doc)
|
|
|
|
spans_gold = example.reference.spans[spans_key]
|
|
|
|
assert [(ent.start, ent.end) for ent in spans_gold] == [(3, 5), (3, 6)]
|
|
|
|
|
|
|
|
# Ensure that 'get_aligned_spans_y2x' has the aligned entities correct
|
2021-06-28 12:48:00 +03:00
|
|
|
spans_y2x_no_overlap = example.get_aligned_spans_y2x(
|
|
|
|
spans_gold, allow_overlap=False
|
|
|
|
)
|
2021-04-08 13:19:17 +03:00
|
|
|
assert [(ent.start, ent.end) for ent in spans_y2x_no_overlap] == [(3, 5)]
|
|
|
|
spans_y2x_overlap = example.get_aligned_spans_y2x(spans_gold, allow_overlap=True)
|
|
|
|
assert [(ent.start, ent.end) for ent in spans_y2x_overlap] == [(3, 5), (3, 6)]
|
|
|
|
|
|
|
|
|
2019-02-27 14:06:32 +03:00
|
|
|
def test_gold_ner_missing_tags(en_tokenizer):
|
|
|
|
doc = en_tokenizer("I flew to Silicon Valley via London.")
|
|
|
|
biluo_tags = [None, "O", "O", "B-LOC", "L-LOC", "O", "U-GPE", "O"]
|
2020-06-26 20:34:12 +03:00
|
|
|
example = Example.from_dict(doc, {"entities": biluo_tags})
|
|
|
|
assert example.get_aligned("ENT_IOB") == [0, 2, 2, 3, 1, 2, 3, 2]
|
2019-09-15 23:31:31 +03:00
|
|
|
|
|
|
|
|
2020-07-07 19:46:00 +03:00
|
|
|
def test_projectivize(en_tokenizer):
|
|
|
|
doc = en_tokenizer("He pretty quickly walks away")
|
|
|
|
heads = [3, 2, 3, 0, 2]
|
2021-01-12 19:17:06 +03:00
|
|
|
deps = ["dep"] * len(heads)
|
|
|
|
example = Example.from_dict(doc, {"heads": heads, "deps": deps})
|
2020-07-07 19:46:00 +03:00
|
|
|
proj_heads, proj_labels = example.get_aligned_parse(projectivize=True)
|
|
|
|
nonproj_heads, nonproj_labels = example.get_aligned_parse(projectivize=False)
|
|
|
|
assert proj_heads == [3, 2, 3, 0, 3]
|
|
|
|
assert nonproj_heads == [3, 2, 3, 0, 2]
|
|
|
|
|
|
|
|
|
2019-10-21 13:20:28 +03:00
|
|
|
def test_iob_to_biluo():
|
|
|
|
good_iob = ["O", "O", "B-LOC", "I-LOC", "O", "B-PERSON"]
|
|
|
|
good_biluo = ["O", "O", "B-LOC", "L-LOC", "O", "U-PERSON"]
|
2019-10-24 17:21:08 +03:00
|
|
|
bad_iob = ["O", "O", '"', "B-LOC", "I-LOC"]
|
2019-10-21 13:20:28 +03:00
|
|
|
converted_biluo = iob_to_biluo(good_iob)
|
|
|
|
assert good_biluo == converted_biluo
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
iob_to_biluo(bad_iob)
|
|
|
|
|
|
|
|
|
2020-06-26 20:34:12 +03:00
|
|
|
def test_roundtrip_docs_to_docbin(doc):
|
2019-11-23 16:32:15 +03:00
|
|
|
text = doc.text
|
2020-06-26 20:34:12 +03:00
|
|
|
idx = [t.idx for t in doc]
|
2019-11-23 16:32:15 +03:00
|
|
|
tags = [t.tag_ for t in doc]
|
2020-01-28 13:36:29 +03:00
|
|
|
pos = [t.pos_ for t in doc]
|
2020-10-01 23:21:46 +03:00
|
|
|
morphs = [str(t.morph) for t in doc]
|
2020-01-28 13:36:29 +03:00
|
|
|
lemmas = [t.lemma_ for t in doc]
|
2019-11-23 16:32:15 +03:00
|
|
|
deps = [t.dep_ for t in doc]
|
|
|
|
heads = [t.head.i for t in doc]
|
|
|
|
cats = doc.cats
|
2020-06-26 20:34:12 +03:00
|
|
|
ents = [(e.start_char, e.end_char, e.label_) for e in doc.ents]
|
|
|
|
# roundtrip to DocBin
|
2019-09-15 23:31:31 +03:00
|
|
|
with make_tempdir() as tmpdir:
|
2020-07-14 15:07:35 +03:00
|
|
|
# use a separate vocab to test that all labels are added
|
|
|
|
reloaded_nlp = English()
|
2019-09-15 23:31:31 +03:00
|
|
|
json_file = tmpdir / "roundtrip.json"
|
|
|
|
srsly.write_json(json_file, [docs_to_json(doc)])
|
2020-06-26 20:34:12 +03:00
|
|
|
output_file = tmpdir / "roundtrip.spacy"
|
2020-08-07 15:30:59 +03:00
|
|
|
DocBin(docs=[doc]).to_disk(output_file)
|
2020-08-04 16:09:37 +03:00
|
|
|
reader = Corpus(output_file)
|
|
|
|
reloaded_examples = list(reader(reloaded_nlp))
|
2020-08-07 15:30:59 +03:00
|
|
|
assert len(doc) == sum(len(eg) for eg in reloaded_examples)
|
2020-08-04 16:09:37 +03:00
|
|
|
reloaded_example = reloaded_examples[0]
|
2020-06-26 20:34:12 +03:00
|
|
|
assert text == reloaded_example.reference.text
|
|
|
|
assert idx == [t.idx for t in reloaded_example.reference]
|
|
|
|
assert tags == [t.tag_ for t in reloaded_example.reference]
|
|
|
|
assert pos == [t.pos_ for t in reloaded_example.reference]
|
2020-10-01 23:21:46 +03:00
|
|
|
assert morphs == [str(t.morph) for t in reloaded_example.reference]
|
2020-06-26 20:34:12 +03:00
|
|
|
assert lemmas == [t.lemma_ for t in reloaded_example.reference]
|
|
|
|
assert deps == [t.dep_ for t in reloaded_example.reference]
|
|
|
|
assert heads == [t.head.i for t in reloaded_example.reference]
|
|
|
|
assert ents == [
|
|
|
|
(e.start_char, e.end_char, e.label_) for e in reloaded_example.reference.ents
|
|
|
|
]
|
|
|
|
assert "TRAVEL" in reloaded_example.reference.cats
|
|
|
|
assert "BAKING" in reloaded_example.reference.cats
|
|
|
|
assert cats["TRAVEL"] == reloaded_example.reference.cats["TRAVEL"]
|
|
|
|
assert cats["BAKING"] == reloaded_example.reference.cats["BAKING"]
|
2019-11-23 16:32:15 +03:00
|
|
|
|
2021-10-01 13:37:39 +03:00
|
|
|
def test_docbin_user_data_serialized(doc):
|
|
|
|
doc.user_data["check"] = True
|
|
|
|
nlp = English()
|
|
|
|
|
|
|
|
with make_tempdir() as tmpdir:
|
|
|
|
output_file = tmpdir / "userdata.spacy"
|
|
|
|
DocBin(docs=[doc], store_user_data=True).to_disk(output_file)
|
|
|
|
reloaded_docs = DocBin().from_disk(output_file).get_docs(nlp.vocab)
|
|
|
|
reloaded_doc = list(reloaded_docs)[0]
|
|
|
|
|
|
|
|
assert reloaded_doc.user_data["check"] == True
|
|
|
|
|
|
|
|
def test_docbin_user_data_not_serialized(doc):
|
|
|
|
# this isn't serializable, but that shouldn't cause an error
|
|
|
|
doc.user_data["check"] = set()
|
|
|
|
nlp = English()
|
|
|
|
|
|
|
|
with make_tempdir() as tmpdir:
|
|
|
|
output_file = tmpdir / "userdata.spacy"
|
|
|
|
DocBin(docs=[doc], store_user_data=False).to_disk(output_file)
|
|
|
|
reloaded_docs = DocBin().from_disk(output_file).get_docs(nlp.vocab)
|
|
|
|
reloaded_doc = list(reloaded_docs)[0]
|
|
|
|
|
|
|
|
assert "check" not in reloaded_doc.user_data
|
2019-11-23 16:32:15 +03:00
|
|
|
|
2019-10-27 15:38:04 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"tokens_a,tokens_b,expected",
|
|
|
|
[
|
2020-11-03 18:24:38 +03:00
|
|
|
(["a", "b", "c"], ["ab", "c"], ([[0], [0], [1]], [[0, 1], [2]])),
|
2019-10-27 15:38:04 +03:00
|
|
|
(
|
2020-04-21 20:31:03 +03:00
|
|
|
["a", "b", '"', "c"],
|
2019-10-27 15:38:04 +03:00
|
|
|
['ab"', "c"],
|
2020-11-03 18:24:38 +03:00
|
|
|
([[0], [0], [0], [1]], [[0, 1, 2], [3]]),
|
2019-10-27 15:38:04 +03:00
|
|
|
),
|
2020-11-03 18:24:38 +03:00
|
|
|
(["a", "bc"], ["ab", "c"], ([[0], [0, 1]], [[0, 1], [1]])),
|
2019-10-27 15:38:04 +03:00
|
|
|
(
|
|
|
|
["ab", "c", "d"],
|
|
|
|
["a", "b", "cd"],
|
2020-11-03 18:24:38 +03:00
|
|
|
([[0, 1], [2], [2]], [[0], [0], [1, 2]]),
|
2019-10-27 15:38:04 +03:00
|
|
|
),
|
|
|
|
(
|
|
|
|
["a", "b", "cd"],
|
|
|
|
["a", "b", "c", "d"],
|
2020-11-03 18:24:38 +03:00
|
|
|
([[0], [1], [2, 3]], [[0], [1], [2], [2]]),
|
2019-10-27 15:38:04 +03:00
|
|
|
),
|
2020-11-03 18:24:38 +03:00
|
|
|
([" ", "a"], ["a"], ([[], [0]], [[1]])),
|
2020-12-08 09:25:16 +03:00
|
|
|
(
|
|
|
|
["a", "''", "'", ","],
|
|
|
|
["a'", "''", ","],
|
|
|
|
([[0], [0, 1], [1], [2]], [[0, 1], [1, 2], [3]]),
|
|
|
|
),
|
2019-10-27 15:38:04 +03:00
|
|
|
],
|
|
|
|
)
|
2020-07-06 18:39:31 +03:00
|
|
|
def test_align(tokens_a, tokens_b, expected): # noqa
|
2020-11-03 18:24:38 +03:00
|
|
|
a2b, b2a = get_alignments(tokens_a, tokens_b)
|
|
|
|
assert (a2b, b2a) == expected # noqa
|
2019-10-27 15:38:04 +03:00
|
|
|
# check symmetry
|
2020-11-03 18:24:38 +03:00
|
|
|
a2b, b2a = get_alignments(tokens_b, tokens_a) # noqa
|
|
|
|
assert (b2a, a2b) == expected # noqa
|
2019-10-28 17:44:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_goldparse_startswith_space(en_tokenizer):
|
|
|
|
text = " a"
|
|
|
|
doc = en_tokenizer(text)
|
2020-06-26 20:34:12 +03:00
|
|
|
gold_words = ["a"]
|
|
|
|
entities = ["U-DATE"]
|
|
|
|
deps = ["ROOT"]
|
|
|
|
heads = [0]
|
|
|
|
example = Example.from_dict(
|
|
|
|
doc, {"words": gold_words, "entities": entities, "deps": deps, "heads": heads}
|
|
|
|
)
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 18:39:31 +03:00
|
|
|
assert ner_tags == ["O", "U-DATE"]
|
2020-06-26 20:34:12 +03:00
|
|
|
assert example.get_aligned("DEP", as_string=True) == [None, "ROOT"]
|
2019-11-11 19:35:27 +03:00
|
|
|
|
|
|
|
|
2020-11-03 18:24:38 +03:00
|
|
|
def test_goldparse_endswith_space(en_tokenizer):
|
|
|
|
text = "a\n"
|
|
|
|
doc = en_tokenizer(text)
|
|
|
|
gold_words = ["a"]
|
|
|
|
entities = ["U-DATE"]
|
|
|
|
deps = ["ROOT"]
|
|
|
|
heads = [0]
|
|
|
|
example = Example.from_dict(
|
|
|
|
doc, {"words": gold_words, "entities": entities, "deps": deps, "heads": heads}
|
|
|
|
)
|
|
|
|
ner_tags = example.get_aligned_ner()
|
|
|
|
assert ner_tags == ["U-DATE", "O"]
|
|
|
|
assert example.get_aligned("DEP", as_string=True) == ["ROOT", None]
|
|
|
|
|
|
|
|
|
2019-11-11 19:35:27 +03:00
|
|
|
def test_gold_constructor():
|
2020-06-26 20:34:12 +03:00
|
|
|
"""Test that the Example constructor works fine"""
|
2019-11-11 19:35:27 +03:00
|
|
|
nlp = English()
|
|
|
|
doc = nlp("This is a sentence")
|
2020-06-26 20:34:12 +03:00
|
|
|
example = Example.from_dict(doc, {"cats": {"cat1": 1.0, "cat2": 0.0}})
|
|
|
|
assert example.get_aligned("ORTH", as_string=True) == [
|
|
|
|
"This",
|
|
|
|
"is",
|
|
|
|
"a",
|
|
|
|
"sentence",
|
|
|
|
]
|
|
|
|
assert example.reference.cats["cat1"]
|
|
|
|
assert not example.reference.cats["cat2"]
|
2019-11-11 19:35:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_tuple_format_implicit():
|
2020-06-26 20:34:12 +03:00
|
|
|
"""Test tuple format"""
|
2019-11-11 19:35:27 +03:00
|
|
|
|
|
|
|
train_data = [
|
|
|
|
("Uber blew through $1 million a week", {"entities": [(0, 4, "ORG")]}),
|
|
|
|
(
|
|
|
|
"Spotify steps up Asia expansion",
|
2020-08-14 16:00:52 +03:00
|
|
|
{"entities": [(0, 7, "ORG"), (17, 21, "LOC")]},
|
2019-11-11 19:35:27 +03:00
|
|
|
),
|
|
|
|
("Google rebrands its business apps", {"entities": [(0, 6, "ORG")]}),
|
|
|
|
]
|
|
|
|
|
2020-07-06 14:02:36 +03:00
|
|
|
_train_tuples(train_data)
|
2019-11-11 19:35:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_tuple_format_implicit_invalid():
|
2020-06-26 20:34:12 +03:00
|
|
|
"""Test that an error is thrown for an implicit invalid field"""
|
2019-11-11 19:35:27 +03:00
|
|
|
train_data = [
|
|
|
|
("Uber blew through $1 million a week", {"frumble": [(0, 4, "ORG")]}),
|
|
|
|
(
|
|
|
|
"Spotify steps up Asia expansion",
|
2020-08-14 16:00:52 +03:00
|
|
|
{"entities": [(0, 7, "ORG"), (17, 21, "LOC")]},
|
2019-11-11 19:35:27 +03:00
|
|
|
),
|
|
|
|
("Google rebrands its business apps", {"entities": [(0, 6, "ORG")]}),
|
|
|
|
]
|
2020-06-26 20:34:12 +03:00
|
|
|
with pytest.raises(KeyError):
|
2020-07-06 14:02:36 +03:00
|
|
|
_train_tuples(train_data)
|
2019-11-11 19:35:27 +03:00
|
|
|
|
|
|
|
|
2020-07-06 14:02:36 +03:00
|
|
|
def _train_tuples(train_data):
|
2019-11-11 19:35:27 +03:00
|
|
|
nlp = English()
|
2020-07-22 14:42:59 +03:00
|
|
|
ner = nlp.add_pipe("ner")
|
2019-11-11 19:35:27 +03:00
|
|
|
ner.add_label("ORG")
|
|
|
|
ner.add_label("LOC")
|
2020-07-06 14:02:36 +03:00
|
|
|
train_examples = []
|
|
|
|
for t in train_data:
|
|
|
|
train_examples.append(Example.from_dict(nlp.make_doc(t[0]), t[1]))
|
2020-09-28 22:35:09 +03:00
|
|
|
optimizer = nlp.initialize()
|
2019-11-11 19:35:27 +03:00
|
|
|
for i in range(5):
|
|
|
|
losses = {}
|
2020-07-06 14:02:36 +03:00
|
|
|
batches = minibatch(train_examples, size=compounding(4.0, 32.0, 1.001))
|
2019-11-11 19:35:27 +03:00
|
|
|
for batch in batches:
|
|
|
|
nlp.update(batch, sgd=optimizer, losses=losses)
|
|
|
|
|
|
|
|
|
2019-11-25 18:03:28 +03:00
|
|
|
def test_split_sents(merged_dict):
|
2019-11-11 19:35:27 +03:00
|
|
|
nlp = English()
|
2020-06-26 20:34:12 +03:00
|
|
|
example = Example.from_dict(
|
|
|
|
Doc(nlp.vocab, words=merged_dict["words"], spaces=merged_dict["spaces"]),
|
|
|
|
merged_dict,
|
|
|
|
)
|
|
|
|
assert example.text == "Hi there everyone It is just me"
|
2019-11-25 18:03:28 +03:00
|
|
|
split_examples = example.split_sents()
|
|
|
|
assert len(split_examples) == 2
|
2020-06-26 20:34:12 +03:00
|
|
|
assert split_examples[0].text == "Hi there everyone "
|
|
|
|
assert split_examples[1].text == "It is just me"
|
|
|
|
token_annotation_1 = split_examples[0].to_dict()["token_annotation"]
|
2020-08-04 23:22:26 +03:00
|
|
|
assert token_annotation_1["ORTH"] == ["Hi", "there", "everyone"]
|
|
|
|
assert token_annotation_1["TAG"] == ["INTJ", "ADV", "PRON"]
|
|
|
|
assert token_annotation_1["SENT_START"] == [1, 0, 0]
|
2020-06-26 20:34:12 +03:00
|
|
|
token_annotation_2 = split_examples[1].to_dict()["token_annotation"]
|
2020-08-04 23:22:26 +03:00
|
|
|
assert token_annotation_2["ORTH"] == ["It", "is", "just", "me"]
|
|
|
|
assert token_annotation_2["TAG"] == ["PRON", "AUX", "ADV", "PRON"]
|
|
|
|
assert token_annotation_2["SENT_START"] == [1, 0, 0, 0]
|
2020-08-04 15:31:32 +03:00
|
|
|
|
|
|
|
|
2020-08-04 17:29:18 +03:00
|
|
|
def test_alignment():
|
|
|
|
other_tokens = ["i", "listened", "to", "obama", "'", "s", "podcasts", "."]
|
|
|
|
spacy_tokens = ["i", "listened", "to", "obama", "'s", "podcasts", "."]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
assert list(align.x2y.lengths) == [1, 1, 1, 1, 1, 1, 1, 1]
|
|
|
|
assert list(align.x2y.dataXd) == [0, 1, 2, 3, 4, 4, 5, 6]
|
|
|
|
assert list(align.y2x.lengths) == [1, 1, 1, 1, 2, 1, 1]
|
|
|
|
assert list(align.y2x.dataXd) == [0, 1, 2, 3, 4, 5, 6, 7]
|
|
|
|
|
|
|
|
|
|
|
|
def test_alignment_case_insensitive():
|
|
|
|
other_tokens = ["I", "listened", "to", "obama", "'", "s", "podcasts", "."]
|
|
|
|
spacy_tokens = ["i", "listened", "to", "Obama", "'s", "PODCASTS", "."]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
assert list(align.x2y.lengths) == [1, 1, 1, 1, 1, 1, 1, 1]
|
|
|
|
assert list(align.x2y.dataXd) == [0, 1, 2, 3, 4, 4, 5, 6]
|
|
|
|
assert list(align.y2x.lengths) == [1, 1, 1, 1, 2, 1, 1]
|
|
|
|
assert list(align.y2x.dataXd) == [0, 1, 2, 3, 4, 5, 6, 7]
|
|
|
|
|
|
|
|
|
|
|
|
def test_alignment_complex():
|
|
|
|
other_tokens = ["i listened to", "obama", "'", "s", "podcasts", "."]
|
|
|
|
spacy_tokens = ["i", "listened", "to", "obama", "'s", "podcasts."]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
assert list(align.x2y.lengths) == [3, 1, 1, 1, 1, 1]
|
|
|
|
assert list(align.x2y.dataXd) == [0, 1, 2, 3, 4, 4, 5, 5]
|
|
|
|
assert list(align.y2x.lengths) == [1, 1, 1, 1, 2, 2]
|
|
|
|
assert list(align.y2x.dataXd) == [0, 0, 0, 1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
|
|
|
|
def test_alignment_complex_example(en_vocab):
|
|
|
|
other_tokens = ["i listened to", "obama", "'", "s", "podcasts", "."]
|
|
|
|
spacy_tokens = ["i", "listened", "to", "obama", "'s", "podcasts."]
|
|
|
|
predicted = Doc(
|
|
|
|
en_vocab, words=other_tokens, spaces=[True, False, False, True, False, False]
|
|
|
|
)
|
|
|
|
reference = Doc(
|
|
|
|
en_vocab, words=spacy_tokens, spaces=[True, True, True, False, True, False]
|
|
|
|
)
|
|
|
|
assert predicted.text == "i listened to obama's podcasts."
|
|
|
|
assert reference.text == "i listened to obama's podcasts."
|
|
|
|
example = Example(predicted, reference)
|
|
|
|
align = example.alignment
|
|
|
|
assert list(align.x2y.lengths) == [3, 1, 1, 1, 1, 1]
|
|
|
|
assert list(align.x2y.dataXd) == [0, 1, 2, 3, 4, 4, 5, 5]
|
|
|
|
assert list(align.y2x.lengths) == [1, 1, 1, 1, 2, 2]
|
|
|
|
assert list(align.y2x.dataXd) == [0, 0, 0, 1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
|
|
|
|
def test_alignment_different_texts():
|
|
|
|
other_tokens = ["she", "listened", "to", "obama", "'s", "podcasts", "."]
|
|
|
|
spacy_tokens = ["i", "listened", "to", "obama", "'s", "podcasts", "."]
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
|
2020-08-05 17:00:59 +03:00
|
|
|
|
2020-11-03 18:24:38 +03:00
|
|
|
def test_alignment_spaces(en_vocab):
|
|
|
|
# single leading whitespace
|
|
|
|
other_tokens = [" ", "i listened to", "obama", "'", "s", "podcasts", "."]
|
|
|
|
spacy_tokens = ["i", "listened", "to", "obama", "'s", "podcasts."]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
assert list(align.x2y.lengths) == [0, 3, 1, 1, 1, 1, 1]
|
|
|
|
assert list(align.x2y.dataXd) == [0, 1, 2, 3, 4, 4, 5, 5]
|
2020-12-08 09:25:16 +03:00
|
|
|
assert list(align.y2x.lengths) == [1, 1, 1, 1, 2, 2]
|
2020-11-03 18:24:38 +03:00
|
|
|
assert list(align.y2x.dataXd) == [1, 1, 1, 2, 3, 4, 5, 6]
|
|
|
|
|
|
|
|
# multiple leading whitespace tokens
|
|
|
|
other_tokens = [" ", " ", "i listened to", "obama", "'", "s", "podcasts", "."]
|
|
|
|
spacy_tokens = ["i", "listened", "to", "obama", "'s", "podcasts."]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
assert list(align.x2y.lengths) == [0, 0, 3, 1, 1, 1, 1, 1]
|
|
|
|
assert list(align.x2y.dataXd) == [0, 1, 2, 3, 4, 4, 5, 5]
|
2020-12-08 09:25:16 +03:00
|
|
|
assert list(align.y2x.lengths) == [1, 1, 1, 1, 2, 2]
|
2020-11-03 18:24:38 +03:00
|
|
|
assert list(align.y2x.dataXd) == [2, 2, 2, 3, 4, 5, 6, 7]
|
|
|
|
|
|
|
|
# both with leading whitespace, not identical
|
|
|
|
other_tokens = [" ", " ", "i listened to", "obama", "'", "s", "podcasts", "."]
|
|
|
|
spacy_tokens = [" ", "i", "listened", "to", "obama", "'s", "podcasts."]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
assert list(align.x2y.lengths) == [1, 0, 3, 1, 1, 1, 1, 1]
|
|
|
|
assert list(align.x2y.dataXd) == [0, 1, 2, 3, 4, 5, 5, 6, 6]
|
|
|
|
assert list(align.y2x.lengths) == [1, 1, 1, 1, 1, 2, 2]
|
|
|
|
assert list(align.y2x.dataXd) == [0, 2, 2, 2, 3, 4, 5, 6, 7]
|
|
|
|
|
|
|
|
# same leading whitespace, different tokenization
|
|
|
|
other_tokens = [" ", " ", "i listened to", "obama", "'", "s", "podcasts", "."]
|
|
|
|
spacy_tokens = [" ", "i", "listened", "to", "obama", "'s", "podcasts."]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
assert list(align.x2y.lengths) == [1, 1, 3, 1, 1, 1, 1, 1]
|
|
|
|
assert list(align.x2y.dataXd) == [0, 0, 1, 2, 3, 4, 5, 5, 6, 6]
|
|
|
|
assert list(align.y2x.lengths) == [2, 1, 1, 1, 1, 2, 2]
|
|
|
|
assert list(align.y2x.dataXd) == [0, 1, 2, 2, 2, 3, 4, 5, 6, 7]
|
|
|
|
|
|
|
|
# only one with trailing whitespace
|
|
|
|
other_tokens = ["i listened to", "obama", "'", "s", "podcasts", ".", " "]
|
|
|
|
spacy_tokens = ["i", "listened", "to", "obama", "'s", "podcasts."]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
assert list(align.x2y.lengths) == [3, 1, 1, 1, 1, 1, 0]
|
|
|
|
assert list(align.x2y.dataXd) == [0, 1, 2, 3, 4, 4, 5, 5]
|
|
|
|
assert list(align.y2x.lengths) == [1, 1, 1, 1, 2, 2]
|
|
|
|
assert list(align.y2x.dataXd) == [0, 0, 0, 1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
# different trailing whitespace
|
|
|
|
other_tokens = ["i listened to", "obama", "'", "s", "podcasts", ".", " ", " "]
|
|
|
|
spacy_tokens = ["i", "listened", "to", "obama", "'s", "podcasts.", " "]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
assert list(align.x2y.lengths) == [3, 1, 1, 1, 1, 1, 1, 0]
|
|
|
|
assert list(align.x2y.dataXd) == [0, 1, 2, 3, 4, 4, 5, 5, 6]
|
|
|
|
assert list(align.y2x.lengths) == [1, 1, 1, 1, 2, 2, 1]
|
|
|
|
assert list(align.y2x.dataXd) == [0, 0, 0, 1, 2, 3, 4, 5, 6]
|
|
|
|
|
|
|
|
# same trailing whitespace, different tokenization
|
|
|
|
other_tokens = ["i listened to", "obama", "'", "s", "podcasts", ".", " ", " "]
|
|
|
|
spacy_tokens = ["i", "listened", "to", "obama", "'s", "podcasts.", " "]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
assert list(align.x2y.lengths) == [3, 1, 1, 1, 1, 1, 1, 1]
|
|
|
|
assert list(align.x2y.dataXd) == [0, 1, 2, 3, 4, 4, 5, 5, 6, 6]
|
|
|
|
assert list(align.y2x.lengths) == [1, 1, 1, 1, 2, 2, 2]
|
|
|
|
assert list(align.y2x.dataXd) == [0, 0, 0, 1, 2, 3, 4, 5, 6, 7]
|
|
|
|
|
|
|
|
# differing whitespace is allowed
|
|
|
|
other_tokens = ["a", " \n ", "b", "c"]
|
|
|
|
spacy_tokens = ["a", "b", " ", "c"]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
assert list(align.x2y.dataXd) == [0, 1, 3]
|
|
|
|
assert list(align.y2x.dataXd) == [0, 2, 3]
|
|
|
|
|
|
|
|
# other differences in whitespace are allowed
|
|
|
|
other_tokens = [" ", "a"]
|
|
|
|
spacy_tokens = [" ", "a", " "]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
|
|
|
|
other_tokens = ["a", " "]
|
|
|
|
spacy_tokens = ["a", " "]
|
|
|
|
align = Alignment.from_strings(other_tokens, spacy_tokens)
|
|
|
|
|
|
|
|
|
2020-08-04 15:31:32 +03:00
|
|
|
def test_retokenized_docs(doc):
|
|
|
|
a = doc.to_array(["TAG"])
|
|
|
|
doc1 = Doc(doc.vocab, words=[t.text for t in doc]).from_array(["TAG"], a)
|
|
|
|
doc2 = Doc(doc.vocab, words=[t.text for t in doc]).from_array(["TAG"], a)
|
|
|
|
example = Example(doc1, doc2)
|
2020-08-05 17:00:59 +03:00
|
|
|
# fmt: off
|
|
|
|
expected1 = ["Sarah", "'s", "sister", "flew", "to", "Silicon", "Valley", "via", "London", "."]
|
|
|
|
expected2 = [None, "sister", "flew", "to", None, "via", "London", "."]
|
|
|
|
# fmt: on
|
|
|
|
assert example.get_aligned("ORTH", as_string=True) == expected1
|
2020-08-04 15:31:32 +03:00
|
|
|
with doc1.retokenize() as retokenizer:
|
|
|
|
retokenizer.merge(doc1[0:2])
|
|
|
|
retokenizer.merge(doc1[5:7])
|
2020-08-05 17:00:59 +03:00
|
|
|
assert example.get_aligned("ORTH", as_string=True) == expected2
|