2020-07-09 17:43:39 +00:00
|
|
|
import numpy
|
2018-07-24 21:38:44 +00:00
|
|
|
from spacy.gold import biluo_tags_from_offsets, offsets_from_biluo_tags
|
2020-07-06 15:39:31 +00:00
|
|
|
from spacy.gold import spans_from_biluo_tags, iob_to_biluo
|
2020-06-26 17:34:12 +00:00
|
|
|
from spacy.gold import Corpus, docs_to_json
|
|
|
|
from spacy.gold.example import Example
|
|
|
|
from spacy.gold.converters import json2docs
|
2019-09-15 20:31:31 +00:00
|
|
|
from spacy.lang.en import English
|
2020-07-07 16:46:00 +00:00
|
|
|
from spacy.pipeline import EntityRuler
|
2020-06-26 17:34:12 +00:00
|
|
|
from spacy.tokens import Doc, DocBin
|
2020-07-06 11:06:25 +00:00
|
|
|
from spacy.util import get_words_and_spaces, minibatch
|
|
|
|
from thinc.api import compounding
|
2019-08-15 16:13:32 +00:00
|
|
|
import pytest
|
2019-09-15 20:31:31 +00:00
|
|
|
import srsly
|
2016-10-15 19:50:43 +00:00
|
|
|
|
2020-05-21 16:39:06 +00:00
|
|
|
from .util import make_tempdir
|
2020-06-26 17:34:12 +00:00
|
|
|
from ..gold.augment import make_orth_variants_example
|
2020-05-21 16:39:06 +00:00
|
|
|
|
2019-12-22 00:53:56 +00:00
|
|
|
|
2019-11-23 13:32:15 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def doc():
|
|
|
|
text = "Sarah's sister flew to Silicon Valley via London."
|
2019-12-22 00:53:56 +00:00
|
|
|
tags = ["NNP", "POS", "NN", "VBD", "IN", "NNP", "NNP", "IN", "NNP", "."]
|
2020-01-28 10:36:29 +00: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 13:32:15 +00:00
|
|
|
# head of '.' is intentionally nonprojective for testing
|
|
|
|
heads = [2, 0, 3, 3, 3, 6, 4, 3, 7, 5]
|
2019-12-22 00:53:56 +00: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 13:32:15 +00: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 10:36:29 +00:00
|
|
|
doc[i].pos_ = pos[i]
|
|
|
|
doc[i].morph_ = morphs[i]
|
|
|
|
doc[i].lemma_ = lemmas[i]
|
2019-11-23 13:32:15 +00: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 13:09:16 +00:00
|
|
|
|
2019-11-25 15:03:28 +00: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 17:34:12 +00:00
|
|
|
"spaces": [True, True, True, True, True, True, False],
|
2019-11-25 15:03:28 +00:00
|
|
|
"tags": ["INTJ", "ADV", "PRON", "PRON", "AUX", "ADV", "PRON"],
|
2020-06-26 17:34:12 +00:00
|
|
|
"sent_starts": [1, 0, 0, 1, 0, 0, 0],
|
2019-12-22 00:53:56 +00:00
|
|
|
}
|
2019-11-25 15:03:28 +00:00
|
|
|
|
2019-08-18 13:09:16 +00:00
|
|
|
|
2020-06-26 17:34:12 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def vocab():
|
|
|
|
nlp = English()
|
|
|
|
return nlp.vocab
|
|
|
|
|
|
|
|
|
2017-01-12 22:39:18 +00:00
|
|
|
def test_gold_biluo_U(en_vocab):
|
2018-11-27 00:09:36 +00: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 19:50:43 +00:00
|
|
|
tags = biluo_tags_from_offsets(doc, entities)
|
2018-11-27 00:09:36 +00:00
|
|
|
assert tags == ["O", "O", "O", "U-LOC", "O"]
|
2016-10-15 19:50:43 +00:00
|
|
|
|
|
|
|
|
2017-01-12 22:39:18 +00:00
|
|
|
def test_gold_biluo_BL(en_vocab):
|
2018-11-27 00:09:36 +00: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 19:50:43 +00:00
|
|
|
tags = biluo_tags_from_offsets(doc, entities)
|
2018-11-27 00:09:36 +00:00
|
|
|
assert tags == ["O", "O", "O", "B-LOC", "L-LOC", "O"]
|
2016-10-15 19:50:43 +00:00
|
|
|
|
|
|
|
|
2017-01-12 22:39:18 +00:00
|
|
|
def test_gold_biluo_BIL(en_vocab):
|
2018-11-27 00:09:36 +00: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 19:50:43 +00:00
|
|
|
tags = biluo_tags_from_offsets(doc, entities)
|
2018-11-27 00:09:36 +00:00
|
|
|
assert tags == ["O", "O", "O", "B-LOC", "I-LOC", "L-LOC", "O"]
|
2016-10-15 19:50:43 +00:00
|
|
|
|
2019-08-18 13:09:16 +00:00
|
|
|
|
2019-08-15 16:13:32 +00: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 13:09:16 +00: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 16:13:32 +00:00
|
|
|
with pytest.raises(ValueError):
|
2019-08-18 13:09:16 +00:00
|
|
|
biluo_tags_from_offsets(doc, entities)
|
|
|
|
|
2016-10-15 19:50:43 +00:00
|
|
|
|
2017-01-12 22:39:18 +00:00
|
|
|
def test_gold_biluo_misalign(en_vocab):
|
2018-11-27 00:09:36 +00: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 14:01:18 +00:00
|
|
|
with pytest.warns(UserWarning):
|
|
|
|
tags = biluo_tags_from_offsets(doc, entities)
|
2018-11-27 00:09:36 +00:00
|
|
|
assert tags == ["O", "O", "O", "-", "-", "-"]
|
2017-11-26 15:38:01 +00:00
|
|
|
|
|
|
|
|
2020-07-09 17:43:39 +00: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 17:34:12 +00: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 14:25:34 +00:00
|
|
|
|
2020-06-26 17:34:12 +00: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 14:25:34 +00:00
|
|
|
predicted, {"words": words, "entities": ["U-LOC", None, None, None]}
|
2020-06-26 17:34:12 +00:00
|
|
|
)
|
|
|
|
ner_tags = example.get_aligned_ner()
|
|
|
|
assert ner_tags == ["U-LOC", None, None, None]
|
|
|
|
|
|
|
|
|
|
|
|
def test_json2docs_no_ner(en_vocab):
|
2020-07-04 14:25:34 +00: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 17:34:12 +00:00
|
|
|
]
|
2020-07-04 14:25:34 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
]
|
2020-06-26 17:34:12 +00:00
|
|
|
docs = json2docs(data)
|
|
|
|
assert len(docs) == 1
|
|
|
|
for doc in docs:
|
|
|
|
assert not doc.is_nered
|
|
|
|
for token in doc:
|
|
|
|
assert token.ent_iob == 0
|
|
|
|
eg = Example(
|
|
|
|
Doc(
|
|
|
|
doc.vocab,
|
|
|
|
words=[w.text for w in doc],
|
2020-06-29 11:59:17 +00:00
|
|
|
spaces=[bool(w.whitespace_) for w in doc],
|
2020-06-26 17:34:12 +00:00
|
|
|
),
|
2020-06-29 11:59:17 +00:00
|
|
|
doc,
|
2020-06-26 17:34:12 +00:00
|
|
|
)
|
|
|
|
ner_tags = eg.get_aligned_ner()
|
|
|
|
assert ner_tags == [None, None, None, None, None]
|
|
|
|
|
|
|
|
|
|
|
|
def test_split_sentences(en_vocab):
|
|
|
|
words = ["I", "flew", "to", "San Francisco Valley", "had", "loads of fun"]
|
|
|
|
doc = Doc(en_vocab, words=words)
|
|
|
|
gold_words = [
|
|
|
|
"I",
|
|
|
|
"flew",
|
|
|
|
"to",
|
|
|
|
"San",
|
|
|
|
"Francisco",
|
|
|
|
"Valley",
|
|
|
|
"had",
|
|
|
|
"loads",
|
|
|
|
"of",
|
|
|
|
"fun",
|
|
|
|
]
|
|
|
|
sent_starts = [True, False, False, False, False, False, True, False, False, False]
|
|
|
|
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 "
|
|
|
|
|
|
|
|
words = ["I", "flew", "to", "San", "Francisco", "Valley", "had", "loads", "of fun"]
|
|
|
|
doc = Doc(en_vocab, words=words)
|
|
|
|
gold_words = [
|
|
|
|
"I",
|
|
|
|
"flew",
|
|
|
|
"to",
|
|
|
|
"San Francisco",
|
|
|
|
"Valley",
|
|
|
|
"had",
|
|
|
|
"loads of",
|
|
|
|
"fun",
|
|
|
|
]
|
|
|
|
sent_starts = [True, False, False, False, False, True, False, False]
|
|
|
|
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 11:59:17 +00:00
|
|
|
def test_gold_biluo_one_to_many(en_vocab, en_tokenizer):
|
2020-07-07 16:46:00 +00:00
|
|
|
words = ["Mr and ", "Mrs Smith", "flew to", "San Francisco Valley", "."]
|
2020-07-06 15:39:31 +00:00
|
|
|
spaces = [True, True, True, False, False]
|
2020-04-23 14:58:23 +00:00
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
2020-07-07 16:46:00 +00:00
|
|
|
prefix = "Mr and Mrs Smith flew to "
|
2020-07-06 15:39:31 +00:00
|
|
|
entities = [(len(prefix), len(prefix + "San Francisco Valley"), "LOC")]
|
2020-07-07 16:46:00 +00:00
|
|
|
gold_words = ["Mr and Mrs Smith", "flew", "to", "San", "Francisco", "Valley", "."]
|
2020-06-26 17:34:12 +00:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 15:39:31 +00:00
|
|
|
assert ner_tags == ["O", "O", "O", "U-LOC", "O"]
|
2020-06-29 11:59:17 +00:00
|
|
|
|
|
|
|
entities = [
|
2020-07-07 16:46:00 +00:00
|
|
|
(len("Mr and "), len("Mr and Mrs Smith"), "PERSON"), # "Mrs Smith" is a PERSON
|
2020-07-06 15:39:31 +00:00
|
|
|
(len(prefix), len(prefix + "San Francisco Valley"), "LOC"),
|
2020-06-29 11:59:17 +00:00
|
|
|
]
|
2020-07-07 16:46:00 +00:00
|
|
|
gold_words = ["Mr and", "Mrs", "Smith", "flew", "to", "San", "Francisco", "Valley", "."]
|
2020-06-29 11:59:17 +00:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 15:39:31 +00:00
|
|
|
assert ner_tags == ["O", "U-PERSON", "O", "U-LOC", "O"]
|
2020-06-29 11:59:17 +00:00
|
|
|
|
|
|
|
entities = [
|
2020-07-07 16:46:00 +00:00
|
|
|
(len("Mr and "), len("Mr and Mrs"), "PERSON"), # "Mrs" is a Person
|
2020-07-06 15:39:31 +00:00
|
|
|
(len(prefix), len(prefix + "San Francisco Valley"), "LOC"),
|
2020-06-29 11:59:17 +00:00
|
|
|
]
|
2020-07-07 16:46:00 +00:00
|
|
|
gold_words = ["Mr and", "Mrs", "Smith", "flew", "to", "San", "Francisco", "Valley", "."]
|
2020-06-29 11:59:17 +00:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 15:39:31 +00:00
|
|
|
assert ner_tags == ["O", None, "O", "U-LOC", "O"]
|
2020-06-29 11:59:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_gold_biluo_many_to_one(en_vocab, en_tokenizer):
|
2020-07-07 16:46:00 +00:00
|
|
|
words = ["Mr and", "Mrs", "Smith", "flew", "to", "San", "Francisco", "Valley", "."]
|
2020-07-06 15:39:31 +00:00
|
|
|
spaces = [True, True, True, True, True, True, True, False, False]
|
2020-04-23 14:58:23 +00:00
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
2020-07-07 16:46:00 +00:00
|
|
|
prefix = "Mr and Mrs Smith flew to "
|
2020-07-06 15:39:31 +00:00
|
|
|
entities = [(len(prefix), len(prefix + "San Francisco Valley"), "LOC")]
|
2020-07-07 16:46:00 +00:00
|
|
|
gold_words = ["Mr and Mrs Smith", "flew to", "San Francisco Valley", "."]
|
2020-06-26 17:34:12 +00:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 15:39:31 +00:00
|
|
|
assert ner_tags == ["O", "O", "O", "O", "O", "B-LOC", "I-LOC", "L-LOC", "O"]
|
2020-04-23 14:58:23 +00:00
|
|
|
|
2020-06-29 11:59:17 +00:00
|
|
|
entities = [
|
2020-07-07 16:46:00 +00:00
|
|
|
(len("Mr and "), len("Mr and Mrs Smith"), "PERSON"), # "Mrs Smith" is a PERSON
|
2020-07-06 15:39:31 +00:00
|
|
|
(len(prefix), len(prefix + "San Francisco Valley"), "LOC"),
|
2020-06-29 11:59:17 +00:00
|
|
|
]
|
2020-07-07 16:46:00 +00:00
|
|
|
gold_words = ["Mr and", "Mrs Smith", "flew to", "San Francisco Valley", "."]
|
2020-06-29 11:59:17 +00:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 15:39:31 +00:00
|
|
|
assert ner_tags == ["O", "B-PERSON", "L-PERSON", "O", "O", "B-LOC", "I-LOC", "L-LOC", "O"]
|
2020-06-29 11:59:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_gold_biluo_misaligned(en_vocab, en_tokenizer):
|
2020-07-07 16:46:00 +00:00
|
|
|
words = ["Mr and Mrs", "Smith", "flew", "to", "San Francisco", "Valley", "."]
|
2020-07-06 15:39:31 +00:00
|
|
|
spaces = [True, True, True, True, True, False, False]
|
2020-04-23 14:58:23 +00:00
|
|
|
doc = Doc(en_vocab, words=words, spaces=spaces)
|
2020-07-07 16:46:00 +00:00
|
|
|
prefix = "Mr and Mrs Smith flew to "
|
2020-07-06 15:39:31 +00:00
|
|
|
entities = [(len(prefix), len(prefix + "San Francisco Valley"), "LOC")]
|
2020-07-07 16:46:00 +00:00
|
|
|
gold_words = ["Mr", "and Mrs Smith", "flew to", "San", "Francisco Valley", "."]
|
2020-06-29 11:59:17 +00:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
2020-06-26 17:34:12 +00:00
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 15:39:31 +00:00
|
|
|
assert ner_tags == ["O", "O", "O", "O", "B-LOC", "L-LOC", "O"]
|
2020-04-23 14:58:23 +00:00
|
|
|
|
2020-06-29 11:59:17 +00:00
|
|
|
entities = [
|
2020-07-07 16:46:00 +00:00
|
|
|
(len("Mr and "), len("Mr and Mrs Smith"), "PERSON"), # "Mrs Smith" is a PERSON
|
2020-07-06 15:39:31 +00:00
|
|
|
(len(prefix), len(prefix + "San Francisco Valley"), "LOC"),
|
2020-06-29 11:59:17 +00:00
|
|
|
]
|
2020-07-07 16:46:00 +00:00
|
|
|
gold_words = ["Mr and", "Mrs Smith", "flew to", "San", "Francisco Valley", "."]
|
2020-06-29 11:59:17 +00:00
|
|
|
example = Example.from_dict(doc, {"words": gold_words, "entities": entities})
|
|
|
|
ner_tags = example.get_aligned_ner()
|
2020-07-06 15:39:31 +00:00
|
|
|
assert ner_tags == [None, None, "O", "O", "B-LOC", "L-LOC", "O"]
|
2020-06-29 11:59:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_gold_biluo_additional_whitespace(en_vocab, en_tokenizer):
|
2020-04-23 14:58:23 +00: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 15:39:31 +00:00
|
|
|
prefix = "I flew to "
|
|
|
|
entities = [(len(prefix), len(prefix + "San Francisco Valley"), "LOC")]
|
2020-06-26 17:34:12 +00: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 14:58:23 +00:00
|
|
|
)
|
2020-06-26 17:34:12 +00:00
|
|
|
ner_tags = example.get_aligned_ner()
|
|
|
|
assert ner_tags == ["O", "O", "O", "O", "B-LOC", "L-LOC", "O"]
|
2020-04-23 14:58:23 +00:00
|
|
|
|
2020-06-29 11:59:17 +00:00
|
|
|
|
|
|
|
def test_gold_biluo_4791(en_vocab, en_tokenizer):
|
2020-06-26 17:34:12 +00:00
|
|
|
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 14:58:23 +00:00
|
|
|
)
|
2020-06-26 17:34:12 +00: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 14:58:23 +00:00
|
|
|
)
|
2020-06-26 17:34:12 +00:00
|
|
|
ner_tags = example.get_aligned_ner()
|
|
|
|
assert ner_tags == ["O", "O", "O", "O", "B-MONEY", "L-MONEY", "O"]
|
2020-04-23 14:58:23 +00:00
|
|
|
|
|
|
|
|
2017-11-26 15:38:01 +00:00
|
|
|
def test_roundtrip_offsets_biluo_conversion(en_tokenizer):
|
|
|
|
text = "I flew to Silicon Valley via London."
|
2018-11-27 00:09:36 +00:00
|
|
|
biluo_tags = ["O", "O", "O", "B-LOC", "L-LOC", "O", "U-GPE", "O"]
|
|
|
|
offsets = [(10, 24, "LOC"), (29, 35, "GPE")]
|
2017-11-26 15:38:01 +00: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)
|
2020-06-26 17:34:12 +00:00
|
|
|
offsets_converted = [ent for ent in offsets if ent[2]]
|
2017-11-26 15:38:01 +00:00
|
|
|
assert offsets_converted == offsets
|
2019-02-06 10:50:26 +00: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)
|
2020-06-26 17:34:12 +00:00
|
|
|
spans = [span for span in spans if span.label_]
|
2019-02-06 10:50:26 +00: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 11:06:32 +00:00
|
|
|
|
2019-02-27 13:24:55 +00:00
|
|
|
|
2020-07-07 16:46:00 +00: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"),
|
|
|
|
]
|
|
|
|
tokens_ref = ["Mr", "and", "Mrs", "Smith", "flew", "to", "San", "Francisco", "Valley", "."]
|
|
|
|
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()
|
|
|
|
ruler = EntityRuler(nlp)
|
|
|
|
patterns = [{"label": "PERSON", "pattern": "Mr and Mrs Smith"},
|
|
|
|
{"label": "LOC", "pattern": "San Francisco Valley"}]
|
|
|
|
ruler.add_patterns(patterns)
|
|
|
|
nlp.add_pipe(ruler)
|
|
|
|
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)]
|
|
|
|
|
|
|
|
|
2019-02-27 11:06:32 +00: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 17:34:12 +00: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 20:31:31 +00:00
|
|
|
|
|
|
|
|
2020-07-07 16:46:00 +00:00
|
|
|
def test_projectivize(en_tokenizer):
|
|
|
|
doc = en_tokenizer("He pretty quickly walks away")
|
|
|
|
heads = [3, 2, 3, 0, 2]
|
|
|
|
example = Example.from_dict(doc, {"heads": heads})
|
|
|
|
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 10:20:28 +00: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 14:21:08 +00:00
|
|
|
bad_iob = ["O", "O", '"', "B-LOC", "I-LOC"]
|
2019-10-21 10:20:28 +00: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 17:34:12 +00:00
|
|
|
def test_roundtrip_docs_to_docbin(doc):
|
2019-09-15 20:31:31 +00:00
|
|
|
nlp = English()
|
2019-11-23 13:32:15 +00:00
|
|
|
text = doc.text
|
2020-06-26 17:34:12 +00:00
|
|
|
idx = [t.idx for t in doc]
|
2019-11-23 13:32:15 +00:00
|
|
|
tags = [t.tag_ for t in doc]
|
2020-01-28 10:36:29 +00: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 13:32:15 +00:00
|
|
|
deps = [t.dep_ for t in doc]
|
|
|
|
heads = [t.head.i for t in doc]
|
|
|
|
cats = doc.cats
|
2020-06-26 17:34:12 +00:00
|
|
|
ents = [(e.start_char, e.end_char, e.label_) for e in doc.ents]
|
2019-09-15 20:31:31 +00:00
|
|
|
|
2020-06-26 17:34:12 +00:00
|
|
|
# roundtrip to DocBin
|
2019-09-15 20:31:31 +00:00
|
|
|
with make_tempdir() as tmpdir:
|
2020-07-14 12:07:35 +00:00
|
|
|
# use a separate vocab to test that all labels are added
|
|
|
|
reloaded_nlp = English()
|
2019-09-15 20:31:31 +00:00
|
|
|
json_file = tmpdir / "roundtrip.json"
|
|
|
|
srsly.write_json(json_file, [docs_to_json(doc)])
|
2020-06-26 17:34:12 +00:00
|
|
|
goldcorpus = Corpus(str(json_file), str(json_file))
|
|
|
|
output_file = tmpdir / "roundtrip.spacy"
|
|
|
|
data = DocBin(docs=[doc]).to_bytes()
|
|
|
|
with output_file.open("wb") as file_:
|
|
|
|
file_.write(data)
|
|
|
|
goldcorpus = Corpus(train_loc=str(output_file), dev_loc=str(output_file))
|
2020-07-14 12:07:35 +00:00
|
|
|
reloaded_example = next(goldcorpus.dev_dataset(nlp=reloaded_nlp))
|
|
|
|
assert len(doc) == goldcorpus.count_train(reloaded_nlp)
|
2020-06-26 17:34:12 +00: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]
|
|
|
|
assert morphs == [t.morph_ for t in reloaded_example.reference]
|
|
|
|
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 13:32:15 +00:00
|
|
|
|
|
|
|
|
2019-11-25 15:03:28 +00:00
|
|
|
def test_make_orth_variants(doc):
|
|
|
|
nlp = English()
|
|
|
|
with make_tempdir() as tmpdir:
|
2020-06-26 17:34:12 +00:00
|
|
|
output_file = tmpdir / "roundtrip.spacy"
|
|
|
|
data = DocBin(docs=[doc]).to_bytes()
|
|
|
|
with output_file.open("wb") as file_:
|
|
|
|
file_.write(data)
|
|
|
|
goldcorpus = Corpus(train_loc=str(output_file), dev_loc=str(output_file))
|
2019-11-25 15:03:28 +00:00
|
|
|
|
2020-06-26 17:34:12 +00:00
|
|
|
# due to randomness, test only that this runs with no errors for now
|
|
|
|
train_example = next(goldcorpus.train_dataset(nlp))
|
2020-07-06 11:06:25 +00:00
|
|
|
make_orth_variants_example(nlp, train_example, orth_variant_level=0.2)
|
2019-11-23 13:32:15 +00:00
|
|
|
|
|
|
|
|
2020-07-06 15:39:31 +00:00
|
|
|
@pytest.mark.skip("Outdated")
|
2019-10-27 12:38:04 +00: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 17:31:03 +00:00
|
|
|
["a", "b", '"', "c"],
|
2019-10-27 12:38:04 +00: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 14:44:28 +00:00
|
|
|
([" ", "a"], ["a"], (1, [-1, 0], [1], {}, {})),
|
2019-10-27 12:38:04 +00:00
|
|
|
],
|
|
|
|
)
|
2020-07-06 15:39:31 +00:00
|
|
|
def test_align(tokens_a, tokens_b, expected): # noqa
|
|
|
|
cost, a2b, b2a, a2b_multi, b2a_multi = align(tokens_a, tokens_b) # noqa
|
|
|
|
assert (cost, list(a2b), list(b2a), a2b_multi, b2a_multi) == expected # noqa
|
2019-10-27 12:38:04 +00:00
|
|
|
# check symmetry
|
2020-07-06 15:39:31 +00:00
|
|
|
cost, a2b, b2a, a2b_multi, b2a_multi = align(tokens_b, tokens_a) # noqa
|
|
|
|
assert (cost, list(b2a), list(a2b), b2a_multi, a2b_multi) == expected # noqa
|
2019-10-28 14:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_goldparse_startswith_space(en_tokenizer):
|
|
|
|
text = " a"
|
|
|
|
doc = en_tokenizer(text)
|
2020-06-26 17:34:12 +00: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 15:39:31 +00:00
|
|
|
assert ner_tags == ["O", "U-DATE"]
|
2020-06-26 17:34:12 +00:00
|
|
|
assert example.get_aligned("DEP", as_string=True) == [None, "ROOT"]
|
2019-11-11 16:35:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_gold_constructor():
|
2020-06-26 17:34:12 +00:00
|
|
|
"""Test that the Example constructor works fine"""
|
2019-11-11 16:35:27 +00:00
|
|
|
nlp = English()
|
|
|
|
doc = nlp("This is a sentence")
|
2020-06-26 17:34:12 +00: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 16:35:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_tuple_format_implicit():
|
2020-06-26 17:34:12 +00:00
|
|
|
"""Test tuple format"""
|
2019-11-11 16:35:27 +00:00
|
|
|
|
|
|
|
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")]}),
|
|
|
|
]
|
|
|
|
|
2020-07-06 11:02:36 +00:00
|
|
|
_train_tuples(train_data)
|
2019-11-11 16:35:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_tuple_format_implicit_invalid():
|
2020-06-26 17:34:12 +00:00
|
|
|
"""Test that an error is thrown for an implicit invalid field"""
|
2019-11-11 16:35:27 +00:00
|
|
|
|
|
|
|
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")]}),
|
|
|
|
]
|
|
|
|
|
2020-06-26 17:34:12 +00:00
|
|
|
with pytest.raises(KeyError):
|
2020-07-06 11:02:36 +00:00
|
|
|
_train_tuples(train_data)
|
2019-11-11 16:35:27 +00:00
|
|
|
|
|
|
|
|
2020-07-06 11:02:36 +00:00
|
|
|
def _train_tuples(train_data):
|
2019-11-11 16:35:27 +00:00
|
|
|
nlp = English()
|
|
|
|
ner = nlp.create_pipe("ner")
|
|
|
|
ner.add_label("ORG")
|
|
|
|
ner.add_label("LOC")
|
|
|
|
nlp.add_pipe(ner)
|
|
|
|
|
2020-07-06 11:02:36 +00:00
|
|
|
train_examples = []
|
|
|
|
for t in train_data:
|
|
|
|
train_examples.append(Example.from_dict(nlp.make_doc(t[0]), t[1]))
|
|
|
|
|
2019-11-11 16:35:27 +00:00
|
|
|
optimizer = nlp.begin_training()
|
|
|
|
for i in range(5):
|
|
|
|
losses = {}
|
2020-07-06 11:02:36 +00:00
|
|
|
batches = minibatch(train_examples, size=compounding(4.0, 32.0, 1.001))
|
2019-11-11 16:35:27 +00:00
|
|
|
for batch in batches:
|
|
|
|
nlp.update(batch, sgd=optimizer, losses=losses)
|
|
|
|
|
|
|
|
|
2019-11-25 15:03:28 +00:00
|
|
|
def test_split_sents(merged_dict):
|
2019-11-11 16:35:27 +00:00
|
|
|
nlp = English()
|
2020-06-26 17:34:12 +00: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-11 16:35:27 +00:00
|
|
|
|
2019-11-25 15:03:28 +00:00
|
|
|
split_examples = example.split_sents()
|
|
|
|
assert len(split_examples) == 2
|
2020-06-26 17:34:12 +00: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"]
|
|
|
|
assert token_annotation_1["words"] == ["Hi", "there", "everyone"]
|
|
|
|
assert token_annotation_1["tags"] == ["INTJ", "ADV", "PRON"]
|
|
|
|
assert token_annotation_1["sent_starts"] == [1, 0, 0]
|
|
|
|
|
|
|
|
token_annotation_2 = split_examples[1].to_dict()["token_annotation"]
|
|
|
|
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]
|