Skip to main content

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

Metadata

Release files for pcfg 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pcfg 0.1.5
File Size Uploaded
pcfg-0.1.5.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pcfg 0.1.5
File Interpreter ABI Platform
pcfg-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 7.0 kB

Release files / pcfg-0.1.5.tar.gz

Download URL pcfg-0.1.5.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
05eb48c8995af8e29a14c6c0e969dfb73c1c50a1b952f854322648f7930a8d0a
BLAKE2b-256 checksum
How to use checksums
cd10ea43a0c90a93195cdc598586d761bfc2a600926e53582c056f62f2a90aea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.9.1 Darwin/20.6.0

Release files / pcfg-0.1.5-py3-none-any.whl

Download URL pcfg-0.1.5-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a522990093d3bc0b917a547f2b0cf0ed80b07e1c82dd9bc2a95e5c30f5884ebf
BLAKE2b-256 checksum
How to use checksums
45e911e680357bf0c86c7ded3a5db33cad6cc7bca12e619d617c074126eb5feb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.9.1 Darwin/20.6.0

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page