0

Being new at programming in general, and new with Python in particular, I'm having some beginner's troubles.

I'm trying out a function from NLTK called generate:

string.generate()

It returns what seems like a string. However, if I write:

stringvariable = string.generate()

or

stringvariable = str(string.generate())

… the stringvariable is always Empty.

So I guess I'm missing something here. Can the text output generated, that I see on the screen, be something else than a string output? And if so, is there any way for me to grab that output and put it into a variable?

Briefly put, how to I get what comes out of string.generate() into stringvariable, if not as described above?

Paolo
  • 2,161
  • 5
  • 25
  • 32
  • There's not much information here to go on, so here are a few questions which would be helpful to know: (1) is it possible your `string.generate()` function is printing but not returning the string you're seeing? (2) Is there *something* in `stringvariable`? What do you see if you do `type(stringvariable)`? – BillyBBone Apr 29 '14 at 23:32
  • First, `string` is a bad variable name because it shadows a builtin module. Second, most likely your sample corpus is too small. – Paulo Scardine Apr 29 '14 at 23:34
  • It turns out the function prints the string rather than returning it. Not sure how to capture it. – Paolo Apr 30 '14 at 07:23

2 Answers2

1

you can rewrite generate. The only disadvantage is that it can change and your code might not be updated to reflect these changes:

from nltk.util import tokenwrap
def generate_no_stdout(self, length=100):
    if '_trigram_model' not in self.__dict__:
        estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)
        self._trigram_model = NgramModel(3, self, estimator=estimator)
    text = self._trigram_model.generate(length)
    return tokenwrap(text)

then "a.generate()" becomes "generate_no_stdout(a)"

Giuseppe Scrivano
  • 1,385
  • 10
  • 13
0

generate() prints its output rather than returning a string, so you need to capture it.

Community
  • 1
  • 1
Emre
  • 5,976
  • 7
  • 29
  • 42