Generate sentences from a probabilistic context-free grammar.
Project description
pcfg
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
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
Built Distribution
File details
Details for the file pcfg-0.1.5.tar.gz
.
File metadata
- Download URL: pcfg-0.1.5.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.5 CPython/3.9.1 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05eb48c8995af8e29a14c6c0e969dfb73c1c50a1b952f854322648f7930a8d0a |
|
MD5 | f081d1a83b2159c41066ef1674a325fb |
|
BLAKE2b-256 | cd10ea43a0c90a93195cdc598586d761bfc2a600926e53582c056f62f2a90aea |
File details
Details for the file pcfg-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: pcfg-0.1.5-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.5 CPython/3.9.1 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a522990093d3bc0b917a547f2b0cf0ed80b07e1c82dd9bc2a95e5c30f5884ebf |
|
MD5 | 4c74d7f021e10e81a0db7402e5df501b |
|
BLAKE2b-256 | 45e911e680357bf0c86c7ded3a5db33cad6cc7bca12e619d617c074126eb5feb |