Instances of nltk.Text
are really meant only for interactive exploration. It dumps a lot of stuff to the console, but really doesn't return any constructed objects from it's functions.
What you want in this case is the nltk.ContextIndex
class. Using this class...
import nltk
import nltk.text
import nltk.corpus
idx = nltk.text.ContextIndex([word.lower( ) for word in nltk.corpus.brown.words( )])
save = [ ]
for word in nltk.word_tokenize("i want to solve this problem"):
save.append(idx.similar_words(word))
When done, save
will be a nest list of the most frequent words in the contexts of "i", "want", "to", etc.
Take a look at the online nltk.text.Text documentation, specifically the similar
method, where it references nltk.text.ContextIndex
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…