How to save nltk Text.similar() with variable
Question: I'm a noob in nltk and python.Recently I got a problem,I want to save what
text.similar() show in terminal in a variable,but I failed many times.How
could I save it?

    
    
    text = nltk.Text(word.lower() for word in nltk.corpus.brown.words())
    save = []
    for word in nltk.word_tokenize("i want to slove this problem"):
        save.append(text.similar(word))
    

Sadly,I found there is nothing in save[].Then I test code "word =
text.similar('women')",I also found there is nothing in "word". I realized it
seams I couldn't save text.similar(). What should I do to save it?


Answer: 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](http://nltk.github.com/api/nltk.html#nltk.text.Text),
specifically the `similar` method, where it references
`nltk.text.ContextIndex`

