mirror of
https://github.com/explosion/spaCy.git
synced 2025-02-06 06:30:35 +03:00
additional tests for new get_aligned function
This commit is contained in:
parent
41d29983a7
commit
68986a252e
|
@ -23,7 +23,6 @@ cpdef Doc annotations2doc(vocab, tok_annot, doc_annot):
|
||||||
cdef class Example:
|
cdef class Example:
|
||||||
def __init__(self, Doc predicted, Doc reference, *, Alignment alignment=None):
|
def __init__(self, Doc predicted, Doc reference, *, Alignment alignment=None):
|
||||||
""" Doc can either be text, or an actual Doc """
|
""" Doc can either be text, or an actual Doc """
|
||||||
assert predicted.vocab is reference.vocab
|
|
||||||
msg = "Example.__init__ got None for '{arg}'. Requires Doc."
|
msg = "Example.__init__ got None for '{arg}'. Requires Doc."
|
||||||
if predicted is None:
|
if predicted is None:
|
||||||
raise TypeError(msg.format(arg="predicted"))
|
raise TypeError(msg.format(arg="predicted"))
|
||||||
|
@ -89,11 +88,12 @@ cdef class Example:
|
||||||
output.append(None)
|
output.append(None)
|
||||||
elif gold_i is None:
|
elif gold_i is None:
|
||||||
if i in i2j_multi:
|
if i in i2j_multi:
|
||||||
output.append(vocab.strings[gold_values[i2j_multi[i]]])
|
output.append(gold_values[i2j_multi[i]])
|
||||||
else:
|
else:
|
||||||
output.append(None)
|
output.append(None)
|
||||||
else:
|
else:
|
||||||
output.append(vocab.strings[gold_values[gold_i]])
|
output.append([gold_values[gold_i]])
|
||||||
|
output = [vocab.strings[o] for o in output]
|
||||||
return output
|
return output
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
|
|
|
@ -32,11 +32,11 @@ def test_Example_from_dict_invalid(annots):
|
||||||
Example.from_dict(predicted, annots)
|
Example.from_dict(predicted, annots)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("gold_words", [["ice", "cream"], ["icecream"], ["i", "ce", "cream"]])
|
@pytest.mark.parametrize("pred_words", [["ice", "cream"], ["icecream"], ["i", "ce", "cream"]])
|
||||||
@pytest.mark.parametrize("annots", [{"words": ["icecream"], "tags": ["NN"]}])
|
@pytest.mark.parametrize("annots", [{"words": ["icecream"], "tags": ["NN"]}])
|
||||||
def test_Example_from_dict_with_tags(gold_words, annots):
|
def test_Example_from_dict_with_tags(pred_words, annots):
|
||||||
vocab = Vocab()
|
vocab = Vocab()
|
||||||
predicted = Doc(vocab, words=gold_words)
|
predicted = Doc(vocab, words=pred_words)
|
||||||
example = Example.from_dict(predicted, annots)
|
example = Example.from_dict(predicted, annots)
|
||||||
for i, token in enumerate(example.reference):
|
for i, token in enumerate(example.reference):
|
||||||
assert token.tag_ == annots["tags"][i]
|
assert token.tag_ == annots["tags"][i]
|
||||||
|
@ -44,6 +44,30 @@ def test_Example_from_dict_with_tags(gold_words, annots):
|
||||||
assert aligned_tags == ["NN" for _ in predicted]
|
assert aligned_tags == ["NN" for _ in predicted]
|
||||||
|
|
||||||
|
|
||||||
|
def test_aligned_tags():
|
||||||
|
pred_words = ["Apply", "some", "sunscreen", "unless", "you", "can", "not"]
|
||||||
|
gold_words = ["Apply", "some", "sun", "screen", "unless", "you", "cannot"]
|
||||||
|
gold_tags = ["VERB", "DET", "NOUN", "NOUN", "SCONJ", "PRON", "VERB"]
|
||||||
|
annots = {"words": gold_words, "tags": gold_tags}
|
||||||
|
vocab = Vocab()
|
||||||
|
predicted = Doc(vocab, words=pred_words)
|
||||||
|
example = Example.from_dict(predicted, annots)
|
||||||
|
aligned_tags = example.get_aligned("tag")
|
||||||
|
assert aligned_tags == ["VERB", "DET", None, "SCONJ", "PRON", "VERB", "VERB"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_aligned_tags_multi():
|
||||||
|
pred_words = ["Applysome", "sunscreen", "unless", "you", "can", "not"]
|
||||||
|
gold_words = ["Apply", "somesun", "screen", "unless", "you", "cannot"]
|
||||||
|
gold_tags = ["VERB", "DET", "NOUN", "SCONJ", "PRON", "VERB"]
|
||||||
|
annots = {"words": gold_words, "tags": gold_tags}
|
||||||
|
vocab = Vocab()
|
||||||
|
predicted = Doc(vocab, words=pred_words)
|
||||||
|
example = Example.from_dict(predicted, annots)
|
||||||
|
aligned_tags = example.get_aligned("tag")
|
||||||
|
assert aligned_tags == [None, None, "SCONJ", "PRON", "VERB", "VERB"]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"annots",
|
"annots",
|
||||||
[
|
[
|
||||||
|
|
Loading…
Reference in New Issue
Block a user