2019-11-23 16:32:15 +03:00
|
|
|
from spacy.errors import AlignmentError
|
2020-01-28 13:36:29 +03:00
|
|
|
from spacy.gold import biluo_tags_from_offsets, offsets_from_biluo_tags
|
|
|
|
from spacy.gold import spans_from_biluo_tags, GoldParse, iob_to_biluo, align
|
|
|
|
from spacy.gold import GoldCorpus, docs_to_json, Example, DocAnnotation
|
2019-09-15 23:31:31 +03:00
|
|
|
from spacy.lang.en import English
|
2019-11-23 16:32:15 +03:00
|
|
|
from spacy.syntax.nonproj import is_nonproj_tree
|
2018-07-25 00:38:44 +03:00
|
|
|
from spacy.tokens import Doc
|
2019-11-11 19:35:27 +03:00
|
|
|
from spacy.util import compounding, minibatch
|
2019-09-15 23:31:31 +03:00
|
|
|
from .util import make_tempdir
|
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
|
|
|
|
2019-12-22 03:53:56 +03:00
|
|
|
|
2019-11-23 16:32:15 +03:00
|
|
|
@pytest.fixture
|
|
|
|
def doc():
|
|
|
|
text = "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-01-28 13:36:29 +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]
|
2019-12-22 03:53:56 +03:00
|
|
|
deps = [
|
|
|
|
"poss",
|
|
|
|
"case",
|
|
|
|
"nsubj",
|
|
|
|
"ROOT",
|
|
|
|
"prep",
|
|
|
|
"compound",
|
|
|
|
"pobj",
|
|
|
|
"prep",
|
|
|
|
"pobj",
|
|
|
|
"punct",
|
|
|
|
]
|
|
|
|
lemmas = [
|
|
|
|
"Sarah",
|
|
|
|
"'s",
|
|
|
|
"sister",
|
|
|
|
"fly",
|
|
|
|
"to",
|
|
|
|
"Silicon",
|
|
|
|
"Valley",
|
|
|
|
"via",
|
|
|
|
"London",
|
|
|
|
".",
|
|
|
|
]
|
2019-11-23 16:32:15 +03:00
|
|
|
biluo_tags = ["U-PERSON", "O", "O", "O", "O", "B-LOC", "L-LOC", "O", "U-GPE", "O"]
|
|
|
|
cats = {"TRAVEL": 1.0, "BAKING": 0.0}
|
|
|
|
nlp = English()
|
|
|
|
doc = nlp(text)
|
|
|
|
for i in range(len(tags)):
|
|
|
|
doc[i].tag_ = tags[i]
|
2020-01-28 13:36:29 +03:00
|
|
|
doc[i].pos_ = pos[i]
|
|
|
|
doc[i].morph_ = morphs[i]
|
|
|
|
doc[i].lemma_ = lemmas[i]
|
2019-11-23 16:32:15 +03:00
|
|
|
doc[i].dep_ = deps[i]
|
|
|
|
doc[i].head = doc[heads[i]]
|
|
|
|
doc.ents = spans_from_biluo_tags(doc, biluo_tags)
|
|
|
|
doc.cats = cats
|
|
|
|
doc.is_tagged = True
|
|
|
|
doc.is_parsed = True
|
|
|
|
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"],
|
|
|
|
"tags": ["INTJ", "ADV", "PRON", "PRON", "AUX", "ADV", "PRON"],
|
|
|
|
"sent_starts": [1, 0, 0, 1, 0, 0, 0, 0],
|
2019-12-22 03:53:56 +03:00
|
|
|
}
|
2019-11-25 18:03:28 +03:00
|
|
|
|
|
|
|
|
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")]
|
2016-10-15 22:50:43 +03:00
|
|
|
tags = biluo_tags_from_offsets(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")]
|
2016-10-15 22:50:43 +03:00
|
|
|
tags = biluo_tags_from_offsets(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")]
|
2016-10-15 22:50:43 +03:00
|
|
|
tags = biluo_tags_from_offsets(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):
|
2019-08-18 16:09:16 +03:00
|
|
|
biluo_tags_from_offsets(doc, entities)
|
|
|
|
|
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")]
|
2016-10-15 22:50:43 +03:00
|
|
|
tags = biluo_tags_from_offsets(doc, entities)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert tags == ["O", "O", "O", "-", "-", "-"]
|
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)
|
|
|
|
biluo_tags_converted = biluo_tags_from_offsets(doc, offsets)
|
|
|
|
assert biluo_tags_converted == biluo_tags
|
|
|
|
offsets_converted = offsets_from_biluo_tags(doc, biluo_tags)
|
|
|
|
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"]
|
|
|
|
spans = spans_from_biluo_tags(doc, biluo_tags)
|
|
|
|
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
|
|
|
|
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"]
|
2019-02-27 16:24:55 +03:00
|
|
|
gold = GoldParse(doc, entities=biluo_tags) # noqa: F841
|
2019-09-15 23:31:31 +03:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2019-11-23 16:32:15 +03:00
|
|
|
def test_roundtrip_docs_to_json(doc):
|
2019-09-15 23:31:31 +03:00
|
|
|
nlp = English()
|
2019-11-23 16:32:15 +03:00
|
|
|
text = doc.text
|
|
|
|
tags = [t.tag_ for t in doc]
|
2020-01-28 13:36:29 +03:00
|
|
|
pos = [t.pos_ for t in doc]
|
|
|
|
morphs = [t.morph_ for t in doc]
|
|
|
|
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]
|
2019-12-22 03:53:56 +03:00
|
|
|
biluo_tags = iob_to_biluo(
|
|
|
|
[t.ent_iob_ + "-" + t.ent_type_ if t.ent_type_ else "O" for t in doc]
|
|
|
|
)
|
2019-11-23 16:32:15 +03:00
|
|
|
cats = doc.cats
|
2019-09-15 23:31:31 +03:00
|
|
|
|
2019-10-23 17:01:44 +03:00
|
|
|
# roundtrip to JSON
|
2019-09-15 23:31:31 +03:00
|
|
|
with make_tempdir() as tmpdir:
|
|
|
|
json_file = tmpdir / "roundtrip.json"
|
|
|
|
srsly.write_json(json_file, [docs_to_json(doc)])
|
2019-11-11 19:35:27 +03:00
|
|
|
goldcorpus = GoldCorpus(train=str(json_file), dev=str(json_file))
|
2019-09-15 23:31:31 +03:00
|
|
|
|
2019-11-23 16:32:15 +03:00
|
|
|
reloaded_example = next(goldcorpus.dev_dataset(nlp))
|
2019-11-11 19:35:27 +03:00
|
|
|
goldparse = reloaded_example.gold
|
2019-09-15 23:31:31 +03:00
|
|
|
|
|
|
|
assert len(doc) == goldcorpus.count_train()
|
2019-11-11 19:35:27 +03:00
|
|
|
assert text == reloaded_example.text
|
2019-10-23 17:01:44 +03:00
|
|
|
assert tags == goldparse.tags
|
2020-01-28 13:36:29 +03:00
|
|
|
assert pos == goldparse.pos
|
|
|
|
assert morphs == goldparse.morphs
|
|
|
|
assert lemmas == goldparse.lemmas
|
2019-10-23 17:01:44 +03:00
|
|
|
assert deps == goldparse.labels
|
|
|
|
assert heads == goldparse.heads
|
|
|
|
assert biluo_tags == goldparse.ner
|
|
|
|
assert "TRAVEL" in goldparse.cats
|
|
|
|
assert "BAKING" in goldparse.cats
|
|
|
|
assert cats["TRAVEL"] == goldparse.cats["TRAVEL"]
|
|
|
|
assert cats["BAKING"] == goldparse.cats["BAKING"]
|
|
|
|
|
|
|
|
# roundtrip to JSONL train dicts
|
|
|
|
with make_tempdir() as tmpdir:
|
|
|
|
jsonl_file = tmpdir / "roundtrip.jsonl"
|
|
|
|
srsly.write_jsonl(jsonl_file, [docs_to_json(doc)])
|
|
|
|
goldcorpus = GoldCorpus(str(jsonl_file), str(jsonl_file))
|
|
|
|
|
2019-11-23 16:32:15 +03:00
|
|
|
reloaded_example = next(goldcorpus.dev_dataset(nlp))
|
2019-11-11 19:35:27 +03:00
|
|
|
goldparse = reloaded_example.gold
|
2019-10-23 17:01:44 +03:00
|
|
|
|
|
|
|
assert len(doc) == goldcorpus.count_train()
|
2019-11-11 19:35:27 +03:00
|
|
|
assert text == reloaded_example.text
|
2019-10-23 17:01:44 +03:00
|
|
|
assert tags == goldparse.tags
|
2020-01-28 13:36:29 +03:00
|
|
|
assert pos == goldparse.pos
|
|
|
|
assert morphs == goldparse.morphs
|
|
|
|
assert lemmas == goldparse.lemmas
|
2019-10-23 17:01:44 +03:00
|
|
|
assert deps == goldparse.labels
|
|
|
|
assert heads == goldparse.heads
|
|
|
|
assert biluo_tags == goldparse.ner
|
|
|
|
assert "TRAVEL" in goldparse.cats
|
|
|
|
assert "BAKING" in goldparse.cats
|
|
|
|
assert cats["TRAVEL"] == goldparse.cats["TRAVEL"]
|
|
|
|
assert cats["BAKING"] == goldparse.cats["BAKING"]
|
|
|
|
|
|
|
|
# roundtrip to JSONL tuples
|
|
|
|
with make_tempdir() as tmpdir:
|
|
|
|
jsonl_file = tmpdir / "roundtrip.jsonl"
|
|
|
|
# write to JSONL train dicts
|
|
|
|
srsly.write_jsonl(jsonl_file, [docs_to_json(doc)])
|
|
|
|
goldcorpus = GoldCorpus(str(jsonl_file), str(jsonl_file))
|
|
|
|
# load and rewrite as JSONL tuples
|
2019-11-11 19:35:27 +03:00
|
|
|
srsly.write_jsonl(jsonl_file, goldcorpus.train_examples)
|
2019-10-23 17:01:44 +03:00
|
|
|
goldcorpus = GoldCorpus(str(jsonl_file), str(jsonl_file))
|
|
|
|
|
2019-11-23 16:32:15 +03:00
|
|
|
reloaded_example = next(goldcorpus.dev_dataset(nlp))
|
2019-11-11 19:35:27 +03:00
|
|
|
goldparse = reloaded_example.gold
|
2019-10-23 17:01:44 +03:00
|
|
|
|
|
|
|
assert len(doc) == goldcorpus.count_train()
|
2019-11-11 19:35:27 +03:00
|
|
|
assert text == reloaded_example.text
|
2019-10-23 17:01:44 +03:00
|
|
|
assert tags == goldparse.tags
|
|
|
|
assert deps == goldparse.labels
|
|
|
|
assert heads == goldparse.heads
|
2019-11-28 16:53:44 +03:00
|
|
|
assert lemmas == goldparse.lemmas
|
2019-10-23 17:01:44 +03:00
|
|
|
assert biluo_tags == goldparse.ner
|
2019-09-15 23:31:31 +03:00
|
|
|
assert "TRAVEL" in goldparse.cats
|
|
|
|
assert "BAKING" in goldparse.cats
|
|
|
|
assert cats["TRAVEL"] == goldparse.cats["TRAVEL"]
|
|
|
|
assert cats["BAKING"] == goldparse.cats["BAKING"]
|
2019-10-27 15:38:04 +03:00
|
|
|
|
|
|
|
|
2019-11-23 16:32:15 +03:00
|
|
|
def test_projective_train_vs_nonprojective_dev(doc):
|
|
|
|
nlp = English()
|
|
|
|
deps = [t.dep_ for t in doc]
|
|
|
|
heads = [t.head.i for t in doc]
|
|
|
|
|
|
|
|
with make_tempdir() as tmpdir:
|
|
|
|
jsonl_file = tmpdir / "test.jsonl"
|
|
|
|
# write to JSONL train dicts
|
|
|
|
srsly.write_jsonl(jsonl_file, [docs_to_json(doc)])
|
|
|
|
goldcorpus = GoldCorpus(str(jsonl_file), str(jsonl_file))
|
|
|
|
|
|
|
|
train_reloaded_example = next(goldcorpus.train_dataset(nlp))
|
|
|
|
train_goldparse = train_reloaded_example.gold
|
|
|
|
|
|
|
|
dev_reloaded_example = next(goldcorpus.dev_dataset(nlp))
|
|
|
|
dev_goldparse = dev_reloaded_example.gold
|
|
|
|
|
|
|
|
assert is_nonproj_tree([t.head.i for t in doc]) is True
|
|
|
|
assert is_nonproj_tree(train_goldparse.heads) is False
|
|
|
|
assert heads[:-1] == train_goldparse.heads[:-1]
|
|
|
|
assert heads[-1] != train_goldparse.heads[-1]
|
|
|
|
assert deps[:-1] == train_goldparse.labels[:-1]
|
|
|
|
assert deps[-1] != train_goldparse.labels[-1]
|
|
|
|
|
|
|
|
assert heads == dev_goldparse.heads
|
|
|
|
assert deps == dev_goldparse.labels
|
|
|
|
|
|
|
|
|
|
|
|
def test_ignore_misaligned(doc):
|
|
|
|
nlp = English()
|
|
|
|
text = doc.text
|
|
|
|
with make_tempdir() as tmpdir:
|
|
|
|
jsonl_file = tmpdir / "test.jsonl"
|
|
|
|
data = [docs_to_json(doc)]
|
|
|
|
data[0]["paragraphs"][0]["raw"] = text.replace("Sarah", "Jane")
|
|
|
|
# write to JSONL train dicts
|
|
|
|
srsly.write_jsonl(jsonl_file, data)
|
|
|
|
goldcorpus = GoldCorpus(str(jsonl_file), str(jsonl_file))
|
|
|
|
|
|
|
|
with pytest.raises(AlignmentError):
|
|
|
|
train_reloaded_example = next(goldcorpus.train_dataset(nlp))
|
|
|
|
|
|
|
|
with make_tempdir() as tmpdir:
|
|
|
|
jsonl_file = tmpdir / "test.jsonl"
|
|
|
|
data = [docs_to_json(doc)]
|
|
|
|
data[0]["paragraphs"][0]["raw"] = text.replace("Sarah", "Jane")
|
|
|
|
# write to JSONL train dicts
|
|
|
|
srsly.write_jsonl(jsonl_file, data)
|
|
|
|
goldcorpus = GoldCorpus(str(jsonl_file), str(jsonl_file))
|
|
|
|
|
|
|
|
# doesn't raise an AlignmentError, but there is nothing to iterate over
|
|
|
|
# because the only example can't be aligned
|
2019-12-22 03:53:56 +03:00
|
|
|
train_reloaded_example = list(goldcorpus.train_dataset(nlp, ignore_misaligned=True))
|
2019-11-23 16:32:15 +03:00
|
|
|
assert len(train_reloaded_example) == 0
|
|
|
|
|
2019-11-25 18:03:28 +03:00
|
|
|
|
|
|
|
def test_make_orth_variants(doc):
|
|
|
|
nlp = English()
|
|
|
|
with make_tempdir() as tmpdir:
|
|
|
|
jsonl_file = tmpdir / "test.jsonl"
|
|
|
|
# write to JSONL train dicts
|
|
|
|
srsly.write_jsonl(jsonl_file, [docs_to_json(doc)])
|
|
|
|
goldcorpus = GoldCorpus(str(jsonl_file), str(jsonl_file))
|
|
|
|
|
|
|
|
# due to randomness, test only that this runs with no errors for now
|
2019-12-22 03:53:56 +03:00
|
|
|
train_reloaded_example = next(goldcorpus.train_dataset(nlp, orth_variant_level=0.2))
|
|
|
|
train_goldparse = train_reloaded_example.gold # noqa: F841
|
2019-11-23 16:32:15 +03:00
|
|
|
|
|
|
|
|
2019-10-27 15:38:04 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"tokens_a,tokens_b,expected",
|
|
|
|
[
|
|
|
|
(["a", "b", "c"], ["ab", "c"], (3, [-1, -1, 1], [-1, 2], {0: 0, 1: 0}, {})),
|
|
|
|
(
|
2019-11-26 01:13:26 +03:00
|
|
|
["a", "b", '"', "c"],
|
2019-10-27 15:38:04 +03:00
|
|
|
['ab"', "c"],
|
|
|
|
(4, [-1, -1, -1, 1], [-1, 3], {0: 0, 1: 0, 2: 0}, {}),
|
|
|
|
),
|
|
|
|
(["a", "bc"], ["ab", "c"], (4, [-1, -1], [-1, -1], {0: 0}, {1: 1})),
|
|
|
|
(
|
|
|
|
["ab", "c", "d"],
|
|
|
|
["a", "b", "cd"],
|
|
|
|
(6, [-1, -1, -1], [-1, -1, -1], {1: 2, 2: 2}, {0: 0, 1: 0}),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
["a", "b", "cd"],
|
|
|
|
["a", "b", "c", "d"],
|
|
|
|
(3, [0, 1, -1], [0, 1, -1, -1], {}, {2: 2, 3: 2}),
|
|
|
|
),
|
2019-10-28 17:44:28 +03:00
|
|
|
([" ", "a"], ["a"], (1, [-1, 0], [1], {}, {})),
|
2019-10-27 15:38:04 +03:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_align(tokens_a, tokens_b, expected):
|
|
|
|
cost, a2b, b2a, a2b_multi, b2a_multi = align(tokens_a, tokens_b)
|
|
|
|
assert (cost, list(a2b), list(b2a), a2b_multi, b2a_multi) == expected
|
|
|
|
# check symmetry
|
|
|
|
cost, a2b, b2a, a2b_multi, b2a_multi = align(tokens_b, tokens_a)
|
|
|
|
assert (cost, list(b2a), list(a2b), b2a_multi, a2b_multi) == expected
|
2019-10-28 17:44:28 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_goldparse_startswith_space(en_tokenizer):
|
|
|
|
text = " a"
|
|
|
|
doc = en_tokenizer(text)
|
|
|
|
g = GoldParse(doc, words=["a"], entities=["U-DATE"], deps=["ROOT"], heads=[0])
|
|
|
|
assert g.words == [" ", "a"]
|
|
|
|
assert g.ner == [None, "U-DATE"]
|
|
|
|
assert g.labels == [None, "ROOT"]
|
2019-11-11 19:35:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_gold_constructor():
|
|
|
|
"""Test that the GoldParse constructor works fine"""
|
|
|
|
nlp = English()
|
|
|
|
doc = nlp("This is a sentence")
|
|
|
|
gold = GoldParse(doc, cats={"cat1": 1.0, "cat2": 0.0})
|
|
|
|
|
|
|
|
assert gold.cats["cat1"]
|
|
|
|
assert not gold.cats["cat2"]
|
|
|
|
assert gold.words == ["This", "is", "a", "sentence"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_gold_orig_annot():
|
|
|
|
nlp = English()
|
|
|
|
doc = nlp("This is a sentence")
|
|
|
|
gold = GoldParse(doc, cats={"cat1": 1.0, "cat2": 0.0})
|
|
|
|
|
|
|
|
assert gold.orig.words == ["This", "is", "a", "sentence"]
|
|
|
|
assert gold.cats["cat1"]
|
|
|
|
|
|
|
|
doc_annotation = DocAnnotation(cats={"cat1": 0.0, "cat2": 1.0})
|
|
|
|
gold2 = GoldParse.from_annotation(doc, doc_annotation, gold.orig)
|
|
|
|
assert gold2.orig.words == ["This", "is", "a", "sentence"]
|
|
|
|
assert not gold2.cats["cat1"]
|
|
|
|
|
|
|
|
|
|
|
|
def test_tuple_format_implicit():
|
|
|
|
"""Test tuple format with implicit GoldParse creation"""
|
|
|
|
|
|
|
|
train_data = [
|
|
|
|
("Uber blew through $1 million a week", {"entities": [(0, 4, "ORG")]}),
|
|
|
|
(
|
|
|
|
"Spotify steps up Asia expansion",
|
|
|
|
{"entities": [(0, 8, "ORG"), (17, 21, "LOC")]},
|
|
|
|
),
|
|
|
|
("Google rebrands its business apps", {"entities": [(0, 6, "ORG")]}),
|
|
|
|
]
|
|
|
|
|
|
|
|
_train(train_data)
|
|
|
|
|
|
|
|
|
|
|
|
def test_tuple_format_implicit_invalid():
|
|
|
|
"""Test that an error is thrown for an implicit invalid GoldParse field"""
|
|
|
|
|
|
|
|
train_data = [
|
|
|
|
("Uber blew through $1 million a week", {"frumble": [(0, 4, "ORG")]}),
|
|
|
|
(
|
|
|
|
"Spotify steps up Asia expansion",
|
|
|
|
{"entities": [(0, 8, "ORG"), (17, 21, "LOC")]},
|
|
|
|
),
|
|
|
|
("Google rebrands its business apps", {"entities": [(0, 6, "ORG")]}),
|
|
|
|
]
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
_train(train_data)
|
|
|
|
|
|
|
|
|
|
|
|
def _train(train_data):
|
|
|
|
nlp = English()
|
|
|
|
ner = nlp.create_pipe("ner")
|
|
|
|
ner.add_label("ORG")
|
|
|
|
ner.add_label("LOC")
|
|
|
|
nlp.add_pipe(ner)
|
|
|
|
|
|
|
|
optimizer = nlp.begin_training()
|
|
|
|
for i in range(5):
|
|
|
|
losses = {}
|
|
|
|
batches = minibatch(train_data, size=compounding(4.0, 32.0, 1.001))
|
|
|
|
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()
|
|
|
|
example = Example()
|
2019-11-25 18:03:28 +03:00
|
|
|
example.set_token_annotation(**merged_dict)
|
2019-11-11 19:35:27 +03:00
|
|
|
assert len(example.get_gold_parses(merge=False, vocab=nlp.vocab)) == 2
|
2019-11-25 18:03:28 +03:00
|
|
|
assert len(example.get_gold_parses(merge=True, vocab=nlp.vocab)) == 1
|
2019-11-11 19:35:27 +03:00
|
|
|
|
2019-11-25 18:03:28 +03:00
|
|
|
split_examples = example.split_sents()
|
|
|
|
assert len(split_examples) == 2
|
2019-11-11 19:35:27 +03:00
|
|
|
|
2019-11-25 18:03:28 +03:00
|
|
|
token_annotation_1 = split_examples[0].token_annotation
|
2019-11-11 19:35:27 +03:00
|
|
|
assert token_annotation_1.ids == [1, 2, 3]
|
|
|
|
assert token_annotation_1.words == ["Hi", "there", "everyone"]
|
|
|
|
assert token_annotation_1.tags == ["INTJ", "ADV", "PRON"]
|
2019-11-25 18:03:28 +03:00
|
|
|
assert token_annotation_1.sent_starts == [1, 0, 0]
|
2019-11-11 19:35:27 +03:00
|
|
|
|
2019-11-25 18:03:28 +03:00
|
|
|
token_annotation_2 = split_examples[1].token_annotation
|
|
|
|
assert token_annotation_2.ids == [4, 5, 6, 7]
|
|
|
|
assert token_annotation_2.words == ["It", "is", "just", "me"]
|
|
|
|
assert token_annotation_2.tags == ["PRON", "AUX", "ADV", "PRON"]
|
|
|
|
assert token_annotation_2.sent_starts == [1, 0, 0, 0]
|
2019-11-11 19:35:27 +03:00
|
|
|
|
|
|
|
|
2019-11-25 18:03:28 +03:00
|
|
|
def test_tuples_to_example(merged_dict):
|
2019-11-11 19:35:27 +03:00
|
|
|
ex = Example()
|
2019-11-25 18:03:28 +03:00
|
|
|
ex.set_token_annotation(**merged_dict)
|
|
|
|
cats = {"TRAVEL": 1.0, "BAKING": 0.0}
|
|
|
|
ex.set_doc_annotation(cats=cats)
|
2019-11-11 19:35:27 +03:00
|
|
|
ex_dict = ex.to_dict()
|
|
|
|
|
2019-11-25 18:03:28 +03:00
|
|
|
assert ex_dict["token_annotation"]["ids"] == merged_dict["ids"]
|
|
|
|
assert ex_dict["token_annotation"]["words"] == merged_dict["words"]
|
|
|
|
assert ex_dict["token_annotation"]["tags"] == merged_dict["tags"]
|
|
|
|
assert ex_dict["token_annotation"]["sent_starts"] == merged_dict["sent_starts"]
|
|
|
|
assert ex_dict["doc_annotation"]["cats"] == cats
|
2020-03-29 14:57:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_empty_example_goldparse():
|
|
|
|
nlp = English()
|
|
|
|
doc = nlp("")
|
|
|
|
example = Example(doc=doc)
|
|
|
|
assert len(example.get_gold_parses()) == 1
|