Complete JSGF toolkit: parse, generate, and test speech grammars with Unicode support
Project description
JSGF Grammar Tools
A Python library for parsing and generating strings from JSGF (Java Speech Grammar Format) grammars. This modernized version supports Python 3.7+ and includes comprehensive testing.
Features
- Parser: Convert JSGF grammar files into abstract syntax trees
- Deterministic Generator: Generate all possible strings from non-recursive grammars
- Probabilistic Generator: Generate random strings using weights and probabilities
- Modern Python: Full Python 3.7+ support with type hints and proper packaging
- Comprehensive Testing: Full test suite with pytest
Installation
From Source
git clone https://github.com/syntactic/JSGFTools.git
cd JSGFTools
pip install -e .
Development Setup
git clone https://github.com/syntactic/JSGFTools.git
cd JSGFTools
pip install -r requirements-dev.txt
Quick Start
Command Line Usage
Generate all possible strings from a non-recursive grammar:
python DeterministicGenerator.py IdeasNonRecursive.gram
Generate 20 random strings from a grammar (supports recursive rules):
python ProbabilisticGenerator.py Ideas.gram 20
Python API Usage
import JSGFParser as parser
import DeterministicGenerator as det_gen
import ProbabilisticGenerator as prob_gen
from io import StringIO
# Parse a grammar
grammar_text = """
public <greeting> = hello | hi;
public <target> = world | there;
public <start> = <greeting> <target>;
"""
with StringIO(grammar_text) as f:
grammar = parser.getGrammarObject(f)
# Generate all possibilities (deterministic)
det_gen.grammar = grammar
rule = grammar.publicRules[2] # <start> rule
all_strings = det_gen.processRHS(rule.rhs)
print("All possible strings:", all_strings)
# Generate random string (probabilistic)
prob_gen.grammar = grammar
random_string = prob_gen.processRHS(rule.rhs)
print("Random string:", random_string)
Grammar Format
JSGFTools supports most of the JSGF specification:
// Comments are supported
public <start> = <greeting> <target>;
// Alternatives with optional weights
<greeting> = /5/ hello | /1/ hi | hey;
// Optional elements
<polite> = [ please ];
// Nonterminal references
<target> = world | there;
// Recursive rules (use with ProbabilisticGenerator only)
<recursive> = base | <recursive> more;
Supported Features
- Rule definitions and nonterminal references
- Alternatives (|) with optional weights (/weight/)
- Optional elements ([...])
- Grouping with parentheses
- Comments (// and /* */)
- Public and private rules
Not Yet Supported
- Kleene operators (* and +)
- Import statements
- Tags
Important Notes
Recursive vs Non-Recursive Grammars
- DeterministicGenerator: Only use with non-recursive grammars to avoid infinite loops
- ProbabilisticGenerator: Can safely handle recursive grammars through probabilistic termination
Example of recursive rule:
<sentence> = <noun> <verb> | <sentence> and <sentence>;
Testing
Run the test suite:
pytest test_jsgf_tools.py -v
Run specific test categories:
pytest test_jsgf_tools.py::TestJSGFParser -v # Parser tests
pytest test_jsgf_tools.py::TestIntegration -v # Integration tests
Documentation
For detailed API documentation, build the Sphinx docs:
cd docs
make html
Then open docs/_build/html/index.html in your browser.
Example Files
Ideas.gram: Recursive grammar example (use with ProbabilisticGenerator)IdeasNonRecursive.gram: Non-recursive grammar example (use with DeterministicGenerator)
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite:
pytest - Submit a pull request
License
MIT License. See LICENSE file for details.
Version History
- 2.0.0: Complete Python 3 modernization, added test suite, improved packaging
- 1.x: Original Python 2.7 version
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsgf_tools-2.1.0.tar.gz.
File metadata
- Download URL: jsgf_tools-2.1.0.tar.gz
- Upload date:
- Size: 25.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e716d3d7f888ab6bdb22bcf6643a1a902307e516d235b2b5cfeb0e2768479db
|
|
| MD5 |
76718c6960a50b5e863046d340df00a0
|
|
| BLAKE2b-256 |
4a7f34931cd5fd8ad4173e06053ad88dc0a64cbcd6c0611c78d18e84a8ef7177
|
File details
Details for the file jsgf_tools-2.1.0-py3-none-any.whl.
File metadata
- Download URL: jsgf_tools-2.1.0-py3-none-any.whl
- Upload date:
- Size: 28.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44753857a3169edb24a6db9af15157bf81cc4faf8a25622c9d0c0a5af09378a5
|
|
| MD5 |
63b3c8b49d932e0b559b500ab0d29a7b
|
|
| BLAKE2b-256 |
e1ce43f1fb72fb9c996899946020ccb2048b8be7cde38453e80d33d3201279f2
|