Sometimes, we want to split a text into sentences with Python.
In this article, we’ll look at how to split a text into sentences with Python.
How to split a text into sentences with Python?
To split a text into sentences with Python, we can use the Natural Language Toolkit.
We install it with
pip install --user -U nltk
Then we use it by writing
import nltk.data
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
fp = open("test.txt")
data = fp.read()
print '\n-----\n'.join(tokenizer.tokenize(data))
We call open
to open the test.txt file.
Then we call read
to read the file.
Then we have tokenizer.tokenize(data)
to split the file data
text into sentences.
Conclusion
To split a text into sentences with Python, we can use the Natural Language Toolkit.