Skip to main content

Generate sentences from a probabilistic context-free grammar.

Project description

pcfg

Documentation Status

Description

Implement the generate() method for NLTK's probabilistic context-free grammar to probabilistically generate valid sentences. (NLTK stands for Natural Language Toolkit.)

Installation

pip install pcfg

Documentation

Read the latest documentation for pcfg here.

Example usage

A PCFG can be initialized in the same way that an NLTK probabilistic context-free grammar is initialized:

>>> from pcfg import PCFG
>>> grammar = PCFG.fromstring("""
S -> Subject Action [1.0]
Subject -> "a cow" [0.7] | "some guy" [0.1] | "the woman" [0.2]
Action -> "eats lunch" [0.5] | "was here" [0.5]
""")

To generate sentences, simply use the generate() method:

>>> for sentence in grammar.generate(3):
...     print(sentence)

The output could be the following:

the woman eats lunch
the woman was here
a cow was here

Of course, your output may be different because the sentences are generated probabilistically.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pcfg-0.1.5.tar.gz (3.5 kB view hashes)

Uploaded Source

Built Distribution

pcfg-0.1.5-py3-none-any.whl (3.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page