spaCy/spacy/tests/spans/test_span.py

52 lines
1.4 KiB
Python
Raw Normal View History

2015-04-07 05:52:25 +03:00
from __future__ import unicode_literals
2016-01-16 18:19:09 +03:00
from spacy.attrs import HEAD
from spacy.en import English
import numpy as np
2015-04-07 05:52:25 +03:00
import pytest
@pytest.fixture
2015-07-09 18:30:58 +03:00
def doc(EN):
return EN('This is a sentence. This is another sentence. And a third.')
2015-04-07 05:52:25 +03:00
2015-07-23 10:26:43 +03:00
@pytest.mark.models
2015-04-07 05:52:25 +03:00
def test_sent_spans(doc):
sents = list(doc.sents)
assert sents[0].start == 0
assert sents[0].end == 5
assert len(sents) == 3
assert sum(len(sent) for sent in sents) == len(doc)
2015-07-09 18:30:58 +03:00
2015-07-23 10:26:43 +03:00
@pytest.mark.models
2015-07-09 18:30:58 +03:00
def test_root(doc):
np = doc[2:4]
assert len(np) == 2
assert np.orth_ == 'a sentence'
assert np.root.orth_ == 'sentence'
2015-07-14 01:08:50 +03:00
assert np.root.head.orth_ == 'is'
2016-01-16 18:19:09 +03:00
2016-10-15 15:44:37 +03:00
def test_root2(EN):
2016-01-16 18:19:09 +03:00
text = 'through North and South Carolina'
doc = EN(text)
heads = np.asarray([[0, 3, -1, -2, -4]], dtype='int32')
doc.from_array([HEAD], heads.T)
south_carolina = doc[-2:]
assert south_carolina.root.text == 'Carolina'
def test_sent(doc):
'''Test new span.sent property'''
#return EN('This is a sentence. This is another sentence. And a third.')
heads = np.asarray([[1, 0, -1, -1, -1, 1, 0, -1, -1, -1, 2, 1, 0, -1]], dtype='int32')
doc.from_array([HEAD], heads.T)
assert len(list(doc.sents))
span = doc[:2]
assert span.sent.root.text == 'is'
assert span.sent.text == 'This is a sentence.'
span = doc[6:7]
assert span.sent.root.left_edge.text == 'This'