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-02-27 14:06:32 +03:00
|
|
|
from spacy.gold import spans_from_biluo_tags, GoldParse
|
2018-07-25 00:38:44 +03:00
|
|
|
from spacy.tokens import Doc
|
2019-08-15 19:13:32 +03:00
|
|
|
import pytest
|
2016-10-15 22:50:43 +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-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)
|
|
|
|
entities = [(len("I flew to "), len("I flew to San Francisco Valley"), "LOC"),
|
|
|
|
(len("I flew to "), len("I flew to San Francisco"), "LOC")]
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
tags = 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
|