2018-03-15 02:18:51 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import pytest
|
2018-07-25 00:38:44 +03:00
|
|
|
from spacy.language import Language
|
|
|
|
from spacy.tokens import Span
|
2018-03-15 02:18:51 +03:00
|
|
|
|
|
|
|
from ..util import get_doc
|
2018-07-25 00:38:44 +03:00
|
|
|
|
2018-03-15 02:18:51 +03:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def doc(en_tokenizer):
|
2018-11-27 03:09:36 +03:00
|
|
|
text = "I like New York in Autumn."
|
2018-03-15 02:18:51 +03:00
|
|
|
heads = [1, 0, 1, -2, -3, -1, -5]
|
2018-11-27 03:09:36 +03:00
|
|
|
tags = ["PRP", "IN", "NNP", "NNP", "IN", "NNP", "."]
|
|
|
|
pos = ["PRON", "VERB", "PROPN", "PROPN", "ADP", "PROPN", "PUNCT"]
|
|
|
|
deps = ["ROOT", "prep", "compound", "pobj", "prep", "pobj", "punct"]
|
2018-03-15 02:18:51 +03:00
|
|
|
tokens = en_tokenizer(text)
|
2018-11-27 03:09:36 +03:00
|
|
|
doc = get_doc(
|
|
|
|
tokens.vocab,
|
|
|
|
words=[t.text for t in tokens],
|
|
|
|
heads=heads,
|
|
|
|
tags=tags,
|
|
|
|
pos=pos,
|
|
|
|
deps=deps,
|
|
|
|
)
|
|
|
|
doc.ents = [Span(doc, 2, 4, doc.vocab.strings["GPE"])]
|
2018-03-15 02:18:51 +03:00
|
|
|
doc.is_parsed = True
|
|
|
|
doc.is_tagged = True
|
|
|
|
return doc
|
|
|
|
|
|
|
|
|
|
|
|
def test_factories_merge_noun_chunks(doc):
|
|
|
|
assert len(doc) == 7
|
|
|
|
nlp = Language()
|
2018-11-27 03:09:36 +03:00
|
|
|
merge_noun_chunks = nlp.create_pipe("merge_noun_chunks")
|
2018-03-15 02:18:51 +03:00
|
|
|
merge_noun_chunks(doc)
|
|
|
|
assert len(doc) == 6
|
2018-11-27 03:09:36 +03:00
|
|
|
assert doc[2].text == "New York"
|
2018-03-15 02:18:51 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_factories_merge_ents(doc):
|
|
|
|
assert len(doc) == 7
|
|
|
|
assert len(list(doc.ents)) == 1
|
|
|
|
nlp = Language()
|
2018-11-27 03:09:36 +03:00
|
|
|
merge_entities = nlp.create_pipe("merge_entities")
|
2018-03-15 02:18:51 +03:00
|
|
|
merge_entities(doc)
|
|
|
|
assert len(doc) == 6
|
|
|
|
assert len(list(doc.ents)) == 1
|
2018-11-27 03:09:36 +03:00
|
|
|
assert doc[2].text == "New York"
|