There has to be a more elegant, and pythonic, way to do this, but none of my experiments with nested list comprehensions or with itertool’s chain
function worked.
What I started with is a function that creates a list of sentences, each of which is a list of words from a text (string):
def sentience (the_string):
sentences = [
[word.lower() for word in nltk.word_tokenize(sentence)]
for sentence in nltk.sent_tokenize(the_string)
]
return sentences
But in the current moment, I didn’t need all of a text, but only two sentences to examine with the NLTK’s part-of-speech tagger. nltk.pos_tag(text)
, however, only accepts a flat list of words. So I needed to flatten my lists of lists into one list, and I only needed, in this case, the first two sentences:
test = []
for i in range(len(text2[0:2])): #the main list
for j in range (len(text2[i])): #the sublists
test.append(text2[i][j])
I’d still like to make this a single line of code, a nested list comprehension, but, for now, this works.