0

I want to generate sentences randomly from a given context-free grammar.

Randomly is the important part because my grammar is quite large, and NLTK generates all the possible utterances which falls short on recursions (i.e. E -> A E) and takes too long to generate "interesting" utterances in short time (interesting being unlike the other utterances preceding the current one).

Are there any Python libraries for that? Thanks!

Bora M. Alper
  • 3,538
  • 1
  • 24
  • 35

1 Answers1

0

NLTK doesn't provide a method to generate random sentences from a grammar, although as indicated in this related SO question, How to use NLTK to generate sentences from an induced grammar?, it can generate random sentences from trigrams.

If you want to write your own Python function, you might be interested in this 1997 paper by Bruce Mackenzie, Generating Strings at Random from a Context Free Grammar. (I found the link in this answer to a different SO question.) The algorithm involves precomputing weights in an O(N2) preprocessing step, and requires that the grammar have no epsilon productions (productions that expand to the empty string).

rici
  • 234,347
  • 28
  • 237
  • 341
  • "NLTK doesn't provide a method to generate random sentences from a grammar" See [Generating sentences from context-free grammars - nltk.org](http://www.nltk.org/howto/generate.html) – Bora M. Alper Oct 03 '19 at 17:00
  • 1
    @bora: generating *all* sentences and generating *random* sentences are different problems. You yourself emphasize that in your question. – rici Oct 03 '19 at 17:47
  • Oh you meant it that way. Yes sure, my bad! – Bora M. Alper Oct 03 '19 at 21:09