mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-25 17:36:30 +03:00
Modernise and merge parser tests and don't depend on models
This commit is contained in:
parent
178c147612
commit
69778924c8
|
@ -1,41 +0,0 @@
|
|||
from __future__ import unicode_literals
|
||||
import pytest
|
||||
|
||||
|
||||
# @pytest.mark.models
|
||||
# def test_nsubj(EN):
|
||||
# sent = EN(u'A base phrase should be recognized.')
|
||||
# base_nps = list(sent.noun_chunks)
|
||||
# assert len(base_nps) == 1
|
||||
# assert base_nps[0].string == 'A base phrase '
|
||||
|
||||
|
||||
# @pytest.mark.models
|
||||
# def test_coord(EN):
|
||||
# sent = EN(u'A base phrase and a good phrase are often the same.')
|
||||
# base_nps = list(sent.noun_chunks)
|
||||
# assert len(base_nps) == 2
|
||||
# assert base_nps[0].string == 'A base phrase '
|
||||
# assert base_nps[1].string == 'a good phrase '
|
||||
|
||||
|
||||
# @pytest.mark.models
|
||||
# def test_pp(EN):
|
||||
# sent = EN(u'A phrase with another phrase occurs')
|
||||
# base_nps = list(sent.noun_chunks)
|
||||
# assert len(base_nps) == 2
|
||||
# assert base_nps[0].string == 'A phrase '
|
||||
# assert base_nps[1].string == 'another phrase '
|
||||
|
||||
|
||||
@pytest.mark.models
|
||||
def test_merge_pp(EN):
|
||||
sent = EN(u'A phrase with another phrase occurs')
|
||||
nps = [(np[0].idx, np[-1].idx + len(np[-1]), np.lemma_, np[0].ent_type_) for np in sent.noun_chunks]
|
||||
|
||||
for start, end, lemma, ent_type in nps:
|
||||
sent.merge(start, end, u'NP', lemma, ent_type)
|
||||
assert sent[0].string == 'A phrase '
|
||||
assert sent[1].string == 'with '
|
||||
assert sent[2].string == 'another phrase '
|
||||
assert sent[3].string == 'occurs'
|
|
@ -1,16 +0,0 @@
|
|||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.models
|
||||
def test_initial(EN):
|
||||
doc = EN.tokenizer(u'I ate the pizza with anchovies.')
|
||||
EN.tagger(doc)
|
||||
with EN.parser.step_through(doc) as stepwise:
|
||||
stepwise.transition('L-nsubj')
|
||||
stepwise.transition('S')
|
||||
stepwise.transition('L-det')
|
||||
assert doc[0].head.i == 1
|
||||
assert doc[1].head.i == 1
|
||||
assert doc[2].head.i == 3
|
||||
assert doc[3].head.i == 3
|
||||
assert doc
|
|
@ -27,6 +27,55 @@ def test_parser_parse_one_word_sentence(en_tokenizer, en_parser, text):
|
|||
assert doc[0].dep != 0
|
||||
|
||||
|
||||
def test_parser_initial(en_tokenizer, en_parser):
|
||||
text = "I ate the pizza with anchovies."
|
||||
heads = [1, 0, 1, -2, -3, -1, -5]
|
||||
transition = ['L-nsubj', 'S', 'L-det']
|
||||
|
||||
tokens = en_tokenizer(text)
|
||||
apply_transition_sequence(en_parser, tokens, transition)
|
||||
|
||||
assert tokens[0].head.i == 1
|
||||
assert tokens[1].head.i == 1
|
||||
assert tokens[2].head.i == 3
|
||||
assert tokens[3].head.i == 3
|
||||
|
||||
|
||||
def test_parser_parse_subtrees(en_tokenizer, en_parser):
|
||||
text = "The four wheels on the bus turned quickly"
|
||||
heads = [2, 1, 4, -1, 1, -2, 0, -1]
|
||||
tokens = en_tokenizer(text)
|
||||
doc = get_doc(tokens.vocab, [t.text for t in tokens], heads=heads)
|
||||
|
||||
assert len(list(doc[2].lefts)) == 2
|
||||
assert len(list(doc[2].rights)) == 1
|
||||
assert len(list(doc[2].children)) == 3
|
||||
assert len(list(doc[5].lefts)) == 1
|
||||
assert len(list(doc[5].rights)) == 0
|
||||
assert len(list(doc[5].children)) == 1
|
||||
assert len(list(doc[2].subtree)) == 6
|
||||
|
||||
|
||||
def test_parser_merge_pp(en_tokenizer):
|
||||
text = "A phrase with another phrase occurs"
|
||||
heads = [1, 4, -1, 1, -2, 0]
|
||||
deps = ['det', 'nsubj', 'prep', 'det', 'pobj', 'ROOT']
|
||||
tags = ['DT', 'NN', 'IN', 'DT', 'NN', 'VBZ']
|
||||
|
||||
tokens = en_tokenizer(text)
|
||||
doc = get_doc(tokens.vocab, [t.text for t in tokens], deps=deps, heads=heads)
|
||||
for token in doc:
|
||||
token.tag_ = tags[token.i]
|
||||
nps = [(np[0].idx, np[-1].idx + len(np[-1]), np.lemma_) for np in doc.noun_chunks]
|
||||
|
||||
for start, end, lemma in nps:
|
||||
doc.merge(start, end, label='NP', lemma=lemma)
|
||||
assert doc[0].text == 'A phrase'
|
||||
assert doc[1].text == 'with'
|
||||
assert doc[2].text == 'another phrase'
|
||||
assert doc[3].text == 'occurs'
|
||||
|
||||
|
||||
def test_parser_arc_eager_finalize_state(en_tokenizer, en_parser):
|
||||
text = "a b c d e"
|
||||
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
from __future__ import unicode_literals
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.models
|
||||
def test_subtrees(EN):
|
||||
sent = EN('The four wheels on the bus turned quickly')
|
||||
wheels = sent[2]
|
||||
bus = sent[5]
|
||||
assert len(list(wheels.lefts)) == 2
|
||||
assert len(list(wheels.rights)) == 1
|
||||
assert len(list(wheels.children)) == 3
|
||||
assert len(list(bus.lefts)) == 1
|
||||
assert len(list(bus.rights)) == 0
|
||||
assert len(list(bus.children)) == 1
|
||||
|
||||
assert len(list(wheels.subtree)) == 6
|
Loading…
Reference in New Issue
Block a user