From 69778924c8eace4eb0187f1692a007603930dda5 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Thu, 12 Jan 2017 01:07:29 +0100 Subject: [PATCH] Modernise and merge parser tests and don't depend on models --- spacy/tests/parser/test_base_nps.py | 41 ---------------- .../parser/test_initial_actions_parse.py | 16 ------ spacy/tests/parser/test_parse.py | 49 +++++++++++++++++++ spacy/tests/parser/test_subtree.py | 17 ------- 4 files changed, 49 insertions(+), 74 deletions(-) delete mode 100644 spacy/tests/parser/test_base_nps.py delete mode 100644 spacy/tests/parser/test_initial_actions_parse.py delete mode 100644 spacy/tests/parser/test_subtree.py diff --git a/spacy/tests/parser/test_base_nps.py b/spacy/tests/parser/test_base_nps.py deleted file mode 100644 index b598158d0..000000000 --- a/spacy/tests/parser/test_base_nps.py +++ /dev/null @@ -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' diff --git a/spacy/tests/parser/test_initial_actions_parse.py b/spacy/tests/parser/test_initial_actions_parse.py deleted file mode 100644 index a4a57e5b2..000000000 --- a/spacy/tests/parser/test_initial_actions_parse.py +++ /dev/null @@ -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 diff --git a/spacy/tests/parser/test_parse.py b/spacy/tests/parser/test_parse.py index ae6c10021..25d265e09 100644 --- a/spacy/tests/parser/test_parse.py +++ b/spacy/tests/parser/test_parse.py @@ -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" diff --git a/spacy/tests/parser/test_subtree.py b/spacy/tests/parser/test_subtree.py deleted file mode 100644 index 8502a0ac9..000000000 --- a/spacy/tests/parser/test_subtree.py +++ /dev/null @@ -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