AI Hints
Home›NLP Tutorial›Language Patterns
NLP · Lesson 78 of 106

Splitting Sentences

Harder than it looks.

⏱ 2 min read▶ Video at 2:14:22🎓 Free
Watch this lesson in the NLP courseWatch on YouTube ↗Full NLP course

Split a text at every full stop. That's easy, isn't it? Then Dr. Khan arrives, and pays twelve dollars fifty for Volume two, from the UK depot. Suddenly your chunker has cut sentences in half, and your RAG system starts retrieving nonsense. Small details at this level ripple through everything.

Splitting Sentences
From the video: Splitting Sentences
  • Split at every full stop?
  • Dr. Khan arrives
  • Small details ripple outward

Handling abbreviations

The naive split breaks on every full stop followed by a space. Run it. Seven pieces. Dr has been cut off from Khan, and Vol from two. The better rule uses look behind patterns. Split after sentence punctuation, but not if it follows Dr or Vol, and only if a capital letter comes next. Now we get four clean sentences. Notice that UK depot stayed together too, because a lowercase word follows it. A short list of abbreviations goes a long way. Libraries such as spaCy learn this from data, and for real work, you should use those.

sentences.pypython
import re

text = "Dr. Khan paid $12.50 for Vol. 2. It came late! Was it the U.K. depot? Nobody knows."

naive = re.split(r"[.!?]\s+", text)
print(len(naive), naive[:3])

smart = re.split(r"(?<=[.!?])(?<!Dr\.)(?<!Vol\.)\s+(?=[A-Z])", text)
print(len(smart))
for s in smart:
    print("-", s)
Output
7 ['Dr', 'Khan paid $12.50 for Vol', '2']
4
- Dr. Khan paid $12.50 for Vol. 2.
- It came late!
- Was it the U.K. depot?
- Nobody knows.

Summary

Basic steps deserve testing. Next, why simple sentiment scoring fails on one small word.

Key takeaways
  • Full stops aren't always the end
  • Use lookbehind for exceptions
  • Use a trained splitter in real work
Next up: Handling Negation — one small word flips everything