1 | initial version |
from nltk.corpus import reuters
from nltk import sent_tokenize
import pandas as pd
sentences = reuters.sents()
sent_tokenize
to create a list of sentences:sentences = [sent_tokenize(" ".join(sentence)) for sentence in sentences]
sentences
list:df = pd.DataFrame({'text': [item for sublist in sentences for item in sublist]})
corpus_segment
to add a column of the corresponding categories for each sentence:df['category'] = reuters.categories(fileids=[idx for idx, _ in reuters.fileids() if _ in df['text'].tolist()])
This will result in a DataFrame with two columns: text
containing the sentences and category
containing the corresponding categories.