使用NLTK库可以很容易地分割文本。下面是一种常见的方法:
import nltk
from nltk.tokenize import sent_tokenize
text = "Hello, my name is Alice. How are you doing today?"
sentences = sent_tokenize(text)
for sentence in sentences:
print(sentence)
from nltk.tokenize import word_tokenize
for sentence in sentences:
words = word_tokenize(sentence)
for word in words:
print(word)
通过这种方法,可以轻松地分割文本并对其进行进一步处理。NLTK库还提供了其他分割文本的方法,具体可以参考NLTK库的官方文档。