You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB
Plaintext

.. Copyright (C) 2001-2020 NLTK Project
.. For license information, see LICENSE.TXT
===============================================
Generating sentences from context-free grammars
===============================================
An example grammar:
>>> from nltk.parse.generate import generate, demo_grammar
>>> from nltk import CFG
>>> grammar = CFG.fromstring(demo_grammar)
>>> print(grammar)
Grammar with 13 productions (start state = S)
S -> NP VP
NP -> Det N
PP -> P NP
VP -> 'slept'
VP -> 'saw' NP
VP -> 'walked' PP
Det -> 'the'
Det -> 'a'
N -> 'man'
N -> 'park'
N -> 'dog'
P -> 'in'
P -> 'with'
The first 10 generated sentences:
>>> for sentence in generate(grammar, n=10):
... print(' '.join(sentence))
the man slept
the man saw the man
the man saw the park
the man saw the dog
the man saw a man
the man saw a park
the man saw a dog
the man walked in the man
the man walked in the park
the man walked in the dog
All sentences of max depth 4:
>>> for sentence in generate(grammar, depth=4):
... print(' '.join(sentence))
the man slept
the park slept
the dog slept
a man slept
a park slept
a dog slept
The number of sentences of different max depths:
>>> len(list(generate(grammar, depth=3)))
0
>>> len(list(generate(grammar, depth=4)))
6
>>> len(list(generate(grammar, depth=5)))
42
>>> len(list(generate(grammar, depth=6)))
114
>>> len(list(generate(grammar)))
114