Sub topic 7
Sentence boundaries from the parse
Prerequisites and the working model
This chapter assumes basic Python, a working spaCy installation, and familiarity with nouns, verbs, and subjects. You should also know that a spaCy pipeline is a sequence of components that analyzes text. A Doc is the processed document, and a Token represents one token in it.
Sentence segmentation is the process of deciding where one sentence ends and the next begins. In a standard English pipeline, the dependency parser usually predicts these boundaries while building the dependency structure. spaCy records the result on each token with token.is_sent_start.
A sentence is available as a Span, a view over part of a Doc. You can iterate over these spans with doc.sents.
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The parser found one sentence. It found another one.")
for sentence in doc.sents:
print(sentence.text)
print("tokens:", [token.text for token in sentence])The output contains two sentence spans. The first token of each span has is_sent_start == True. The first token in the document is also a sentence start.
Inspecting sentence-start flags
You can inspect the boundary decision directly:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Dr. Lee arrived at 9 a.m. She started the meeting.")
for token in doc:
print(f"{token.text:6} sentence_start={token.is_sent_start}")Abbreviations such as Dr. and a.m. show why sentence segmentation is difficult. A period does not always end a sentence. The statistical model uses surrounding words, token attributes, and syntactic evidence rather than treating every period as a boundary.
The main sentence-related attributes are:
| Attribute | Meaning |
|---|---|
| `token.is_sent_start` | `True` when the token begins a sentence, `False` when it does not |
| `doc.sents` | An iterator over sentence spans |
| `sentence.start` and `sentence.end` | Token indexes defining the span |
A sentence's end index is exclusive, so doc[sentence.start:sentence.end] gives the same span.
Parser boundaries and rule-based boundaries
The dependency parser is useful when sentence boundaries depend on syntax. However, it is not the only option. The sentencizer is a rule-based component that marks boundaries using punctuation. It is useful when you need predictable splitting or when you do not need dependency parsing.
Do not add a sentencizer casually to a pipeline that already has a parser. Both components may try to set the same boundary information. Choose the component that matches your task.
How boundaries affect dependencies
Dependency relations normally stay within a sentence. A token's head should belong to the same sentence, except when a model makes an error. This makes sentence spans useful for later extraction:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Researchers published the dataset. Editors reviewed the paper.")
for sentence in doc.sents:
print("\nSENTENCE:", sentence.text)
for token in sentence:
print(token.text, "->", token.head.text, token.dep_)If boundaries are wrong, later operations can also be wrong. A subject may attach to a verb in another supposed sentence, or a relation extractor may combine words that should remain separate.
Checking and improving results
Test segmentation with representative examples instead of one short sentence. Include abbreviations, decimal numbers, quotations, and line breaks. Compare doc.sents with boundaries that a person expects.
If you disable the parser, doc.sents may not be available unless another component sets sentence boundaries. For example, a pipeline can use sentencizer when sentence spans are needed but dependency relations are unnecessary.
Exercise
What does token.is_sent_start tell you, and why should you inspect it before extracting relations from sentences?
Show answer
It tells you whether a token begins a sentence. Incorrect boundaries can cause later dependency-based extraction to group words from different sentences or split one sentence incorrectly.