human
What is a Python code to fix this by extending the Text class to make our own class, called the SaneText class, to behave more sanely: given the following:
I am working on the file "Lestrygonians Part 1.ipynb".
The first task is:
Notice the woman-night and man-day association, and the woman-bull and man-nannygoat association. Some curious things here...
Because of the way the Text class is implemented, these are printed directly to output, instead of being returned as a list or something convenient. See the NLTK source for details. But we can fix this by extending the Text class to make our own class, called the SaneText class, to behave more sanely:
Can you write Python code for it?
assistant
#Here is Python code to fix this by extending the Text class to make our own class, called the SaneText class, to behave more sanely:
.

##################################################
# This block of code is extending an NLTK object
# to modify its behavior.
# 
# This creates a SaneText object, which modifies the similar() method
# to behave in a sane way (return a list of words, instead of printing it
# to the console).
#
# Source: NLTK source code. 
# Modified lines are indicated.
# http://www.nltk.org/_modules/nltk/text.html#ContextIndex.word_similarity_dict

from nltk.compat import Counter
from nltk.util import tokenwrap

class SaneText(nltk.Text):

    def similar(self, word, num=20):
        """
        This is copied and pasted from the NLTK source code,
        but with print statements replaced with return statements.
        """

        if '_word_context_index' not in self.__dict__:
            #print('Building word-context index...')
            self._word_context_index = nltk.ContextIndex(self.tokens,
                                                    filter=lambda x:x.isalpha(),
                                                    key=lambda s:s.lower())

#        words = self._word_context_index.similar_words(word, num)

        word = word.lower()
        wci = self._word_context_index._word_to_contexts
        if word in wci.conditions():
            contexts = set(wci[word])
            fd = Counter(w for w in wci.conditions() for c in wci[w]
                          if c in contexts and not w == word)
            words = [w for w, _ in fd.most_common(num)]
            
            #######
            # begin changed lines
            return tokenwrap(words)
        else:
            return u''
            # end changed lines
            ######
#
# Done defining custom class
################################################

sanetext = SaneText(tokens)

def similar_words(w):
    print "%32s : %s"%( w, sanetext.similar(w)...