Sentencizer does not process empty text
How to reproduce the behaviour
Faced this error trying to process a bunch of texts, some of them appear to be empty (or consist of whitespaces)
import spacy
nlp = spacy.load("en_core_web_md-2.1.0")
nlp.add_pipe(nlp.create_pipe('sentencizer'), before='parser')
for doc in nlp.pipe(texts, disable=['parser']):
for sent in doc.sents:
do something
produces the following error when trying to iterate over sentences:
ValueError: [E030] Sentence boundaries unset. You can add the 'sentencizer' component to the pipeline with: nlp.add_pipe(nlp.create_pipe('sentencizer')) Alternatively, add the dependency parser, or set sentence boundaries by setting doc[i].is_sent_start.
I am not sure, if it is a bug or was done intentionally, although from my perspective, if sentencizer is enabled, we should not expect such an exception, even if text is empty.
Thanks!
Your Environment
- Operating System: Ubuntu
- Python Version Used: 3.6
- spaCy Version Used: 2.1.3
- Environment Information:
I think this mostly comes down to a sentence consisting o only one token.
With the current implementation of the sentencizer and the is_sentenced property, this is expected behaviour at the moment. Token.is_sent_start defaults to True for the first token in a sentence – which makes sense, because we know that it's always a start. The Doc.is_sentenced property uses the is_sent_start attribute of its token to determine whether sentence boundaries have been applied. For multi-token documents, it can check whether any tokens after the first one have is_sent_start set to True or False (as opposed to None). But if there's only one token, thats's ambiguous.
As a workaround, you could add a check for len(doc) > 1 and exclude single-token docs (or treat them separately).
Hello, Thank you very much for explanation. Yes, I will apply suggested workaround.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.