2017-01-13 01:39:18 +03:00
|
|
|
# coding: utf-8
|
2016-10-15 22:50:43 +03:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2018-07-25 00:38:44 +03:00
|
|
|
from spacy.gold import biluo_tags_from_offsets, offsets_from_biluo_tags
|
2019-10-21 13:20:28 +03:00
|
|
|
from spacy.gold import spans_from_biluo_tags, GoldParse, iob_to_biluo
|
2019-10-27 15:38:04 +03:00
|
|
|
from spacy.gold import GoldCorpus, docs_to_json, align
|
2019-09-15 23:31:31 +03:00
|
|
|
from spacy.lang.en import English
|
2018-07-25 00:38:44 +03:00
|
|
|
from spacy.tokens import Doc
|
2020-04-23 17:58:23 +03:00
|
|
|
from spacy.util import get_words_and_spaces
|
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-08-18 16:09:16 +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")]
|
2020-05-19 17:01:18 +03:00
|
|
|
with pytest.warns(UserWarning):
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-04-23 17:58:23 +03:00
|
|
|
def test_gold_biluo_different_tokenization(en_vocab, en_tokenizer):
|
|
|
|
# one-to-many
|
|
|
|
words = ["I", "flew to", "San Francisco Valley", "."]
|
|
|
|
spaces = [True, True, False, False]
|
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
|
|
|
entities = [(len("I flew to "), len("I flew to San Francisco Valley"), "LOC")]
|
|
|
|
gp = GoldParse(
|
|
|
|
doc,
|
|
|
|
words=["I", "flew", "to", "San", "Francisco", "Valley", "."],
|
|
|
|
entities=entities,
|
|
|
|
)
|
|
|
|
assert gp.ner == ["O", "O", "U-LOC", "O"]
|
|
|
|
|
|
|
|
# many-to-one
|
|
|
|
words = ["I", "flew", "to", "San", "Francisco", "Valley", "."]
|
|
|
|
spaces = [True, True, True, True, True, False, False]
|
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
|
|
|
entities = [(len("I flew to "), len("I flew to San Francisco Valley"), "LOC")]
|
|
|
|
gp = GoldParse(
|
|
|
|
doc, words=["I", "flew to", "San Francisco Valley", "."], entities=entities
|
|
|
|
)
|
|
|
|
assert gp.ner == ["O", "O", "O", "B-LOC", "I-LOC", "L-LOC", "O"]
|
|
|
|
|
|
|
|
# misaligned
|
|
|
|
words = ["I flew", "to", "San Francisco", "Valley", "."]
|
|
|
|
spaces = [True, True, True, False, False]
|
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
|
|
|
entities = [(len("I flew to "), len("I flew to San Francisco Valley"), "LOC")]
|
|
|
|
gp = GoldParse(
|
|
|
|
doc, words=["I", "flew to", "San", "Francisco Valley", "."], entities=entities,
|
|
|
|
)
|
|
|
|
assert gp.ner == ["O", "O", "B-LOC", "L-LOC", "O"]
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
entities = [(len("I flew to "), len("I flew to San Francisco Valley"), "LOC")]
|
|
|
|
gp = GoldParse(
|
|
|
|
doc,
|
|
|
|
words=["I", "flew", " ", "to", "San Francisco Valley", "."],
|
|
|
|
entities=entities,
|
|
|
|
)
|
|
|
|
assert gp.ner == ["O", "O", "O", "O", "B-LOC", "L-LOC", "O"]
|
|
|
|
|
|
|
|
# from issue #4791
|
|
|
|
data = (
|
|
|
|
"I'll return the ₹54 amount",
|
|
|
|
{
|
2020-05-21 15:14:01 +03:00
|
|
|
"words": ["I", "'ll", "return", "the", "₹", "54", "amount"],
|
2020-04-23 17:58:23 +03:00
|
|
|
"entities": [(16, 19, "MONEY")],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
gp = GoldParse(en_tokenizer(data[0]), **data[1])
|
|
|
|
assert gp.ner == ["O", "O", "O", "O", "U-MONEY", "O"]
|
|
|
|
|
|
|
|
data = (
|
|
|
|
"I'll return the $54 amount",
|
|
|
|
{
|
2020-05-21 15:14:01 +03:00
|
|
|
"words": ["I", "'ll", "return", "the", "$", "54", "amount"],
|
2020-04-23 17:58:23 +03:00
|
|
|
"entities": [(16, 19, "MONEY")],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
gp = GoldParse(en_tokenizer(data[0]), **data[1])
|
|
|
|
assert gp.ner == ["O", "O", "O", "O", "B-MONEY", "L-MONEY", "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-09-15 23:31:31 +03:00
|
|
|
def test_roundtrip_docs_to_json():
|
|
|
|
text = "I flew to Silicon Valley via London."
|
2019-10-24 17:21:08 +03:00
|
|
|
tags = ["PRP", "VBD", "IN", "NNP", "NNP", "IN", "NNP", "."]
|
2019-10-23 17:01:44 +03:00
|
|
|
heads = [1, 1, 1, 4, 2, 1, 5, 1]
|
2019-10-24 17:21:08 +03:00
|
|
|
deps = ["nsubj", "ROOT", "prep", "compound", "pobj", "prep", "pobj", "punct"]
|
2019-10-23 17:01:44 +03:00
|
|
|
biluo_tags = ["O", "O", "O", "B-LOC", "L-LOC", "O", "U-GPE", "O"]
|
2019-09-15 23:31:31 +03:00
|
|
|
cats = {"TRAVEL": 1.0, "BAKING": 0.0}
|
|
|
|
nlp = English()
|
|
|
|
doc = nlp(text)
|
2019-10-23 17:01:44 +03:00
|
|
|
for i in range(len(tags)):
|
|
|
|
doc[i].tag_ = tags[i]
|
|
|
|
doc[i].dep_ = deps[i]
|
|
|
|
doc[i].head = doc[heads[i]]
|
|
|
|
doc.ents = spans_from_biluo_tags(doc, biluo_tags)
|
2019-09-15 23:31:31 +03:00
|
|
|
doc.cats = cats
|
2019-10-23 17:01:44 +03:00
|
|
|
doc.is_tagged = True
|
|
|
|
doc.is_parsed = True
|
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)])
|
|
|
|
goldcorpus = GoldCorpus(str(json_file), str(json_file))
|
|
|
|
|
|
|
|
reloaded_doc, goldparse = next(goldcorpus.train_docs(nlp))
|
|
|
|
|
|
|
|
assert len(doc) == goldcorpus.count_train()
|
|
|
|
assert text == reloaded_doc.text
|
2019-10-23 17:01:44 +03:00
|
|
|
assert tags == goldparse.tags
|
|
|
|
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))
|
|
|
|
|
|
|
|
reloaded_doc, goldparse = next(goldcorpus.train_docs(nlp))
|
|
|
|
|
|
|
|
assert len(doc) == goldcorpus.count_train()
|
|
|
|
assert text == reloaded_doc.text
|
|
|
|
assert tags == goldparse.tags
|
|
|
|
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
|
|
|
|
srsly.write_jsonl(jsonl_file, goldcorpus.train_tuples)
|
|
|
|
goldcorpus = GoldCorpus(str(jsonl_file), str(jsonl_file))
|
|
|
|
|
|
|
|
reloaded_doc, goldparse = next(goldcorpus.train_docs(nlp))
|
|
|
|
|
|
|
|
assert len(doc) == goldcorpus.count_train()
|
|
|
|
assert text == reloaded_doc.text
|
|
|
|
assert tags == goldparse.tags
|
|
|
|
assert deps == goldparse.labels
|
|
|
|
assert heads == goldparse.heads
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"tokens_a,tokens_b,expected",
|
|
|
|
[
|
|
|
|
(["a", "b", "c"], ["ab", "c"], (3, [-1, -1, 1], [-1, 2], {0: 0, 1: 0}, {})),
|
|
|
|
(
|
2020-04-21 20:31:03 +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"]
|