How to convert text to sentence with python and nltk

We can easily convert text to sentence in python with nltk by tokenize sentence..

First install nltk

pip install nltk

Download nltk data .

NLTK requires data files for tokenization. run the code in a python program to download the data.


import nltk
nltk.download('punkt')


After download the necessary data


from nltk import tokenize

text="In the symphony of existence, we discover our interconnectedness and the boundless spiritual power within us. Together, we build a world where empathy thrives, sparking hope and lighting the way to a future filled with harmony and peace."

sentence_list=tokenize.sent_tokenize(text)
print(sentence_list)


#for sentence in sentence_list:
#	print(sentence)

for i, sentence in enumerate(sentence_list, start=1):
    print(f"Sentence {i}: {sentence}")


this will print the the result like this

['In the symphony of existence, we discover our interconnectedness and the boundless spiritual power within us.', 'Together, we build a world where empathy thrives, sparking hope and lighting the way to a future filled with harmony and peace.']
Sentence 1: In the symphony of existence, we discover our interconnectedness and the boundless spiritual power within us.
Sentence 2: Together, we build a world where empathy thrives, sparking hope and lighting the way to a future filled with harmony and peace.