Only run noun chunks iterator in Span if available (closes #3199)

This commit is contained in:
Ines Montani 2019-02-08 18:33:16 +01:00
parent ff36b14cb2
commit ea07f3022e
2 changed files with 18 additions and 2 deletions

View File

@ -0,0 +1,15 @@
# coding: utf8
from __future__ import unicode_literals
from spacy.tokens import Doc
from spacy.vocab import Vocab
def test_issue3199():
"""Test that Span.noun_chunks works correctly if no noun chunks iterator
is available. To make this test future-proof, we're constructing a Doc
with a new Vocab here and setting is_parsed to make sure the noun chunks run.
"""
doc = Doc(Vocab(), words=["This", "is", "a", "sentence"])
doc.is_parsed = True
assert list(doc[0:3].noun_chunks) == []

View File

@ -403,8 +403,9 @@ cdef class Span:
# objects. See Issue #375
spans = []
cdef attr_t label
for start, end, label in self.doc.noun_chunks_iterator(self):
spans.append(Span(self.doc, start, end, label=label))
if self.doc.noun_chunks_iterator is not None:
for start, end, label in self.doc.noun_chunks_iterator(self):
spans.append(Span(self.doc, start, end, label=label))
for span in spans:
yield span