I’m a Python noob, so bear with me.
I’m trying to work with the NLTK library, and in particular the ‘generate’ function. It looks like from the documentation this function simply prints its result (http://nltk.googlecode.com/svn/trunk/doc/api/nltk.text-pysrc.html). I would like to manipulate the resulting text prior to printing it to the screen, but I can’t seem to figure out how to get this function to return its text.
How would I go about getting the output of this function? Do I have to change the function to return the result instead of printing it?
UPDATE: I found this link which kinda does it, but it feels pretty darn hacky. http://northernplanets.blogspot.com/2006/07/capturing-output-of-print-in-python.html Is this the best I can hope for?
All
generateis doing is generating a trigram model if none exists, then callingand wrapping and printing it.
Just take the parts you want — possibly just the above line (with
selfreplaced by the instance name), or possibly the whole thing, as below, with the finalprintreplaced withreturn.And then you can just call it with a manually passed instance as the first argument.