|
1 | 1 | # JSGF Grammar Tools |
2 | 2 |
|
3 | | -This set of tools can be used primarily to generate strings from a JSGF |
4 | | -grammar, but it also provides an easy to use JSGFParser module which creates |
5 | | -abstract syntax trees for JSGF grammars. Developers can use these ASTs to |
6 | | -help create more tools for their purposes. For more detailed documentation, |
7 | | -refer to the Sphinx documentation located in docs/_build/html/index.html |
| 3 | +[](https://www.python.org/downloads/) |
| 4 | +[](https://opensource.org/licenses/MIT) |
8 | 5 |
|
9 | | -## Dependencies |
| 6 | +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. |
10 | 7 |
|
11 | | -- Python 2.7 |
12 | | -- PyParsing module (http://pyparsing.wikispaces.com/Download+and+Installation) |
| 8 | +## Features |
13 | 9 |
|
14 | | -## Instructions |
| 10 | +- **Parser**: Convert JSGF grammar files into abstract syntax trees |
| 11 | +- **Deterministic Generator**: Generate all possible strings from non-recursive grammars |
| 12 | +- **Probabilistic Generator**: Generate random strings using weights and probabilities |
| 13 | +- **Modern Python**: Full Python 3.7+ support with type hints and proper packaging |
| 14 | +- **Comprehensive Testing**: Full test suite with pytest |
15 | 15 |
|
16 | | -The two main Python scripts are DeterministicGenerator.py and |
17 | | -ProbabilisticGenerator.py. Both files require a grammar file as a command |
18 | | -line argument, and the latter also requires a number, which refers to the number |
19 | | -of sentences to generate. Importantly, DeterministicGenerator.py should not take |
20 | | -grammars with recursive rules as an argument. A recursive rule is of the form: |
| 16 | +## Installation |
21 | 17 |
|
22 | | -```<nonTerminal> = this (comes | goes) back to <nonTerminal>;``` |
| 18 | +### From Source |
| 19 | +```bash |
| 20 | +git clone https://github.com/syntactic/JSGFTools.git |
| 21 | +cd JSGFTools |
| 22 | +pip install -e . |
| 23 | +``` |
23 | 24 |
|
24 | | -There are two example grammars included with the scripts: Ideas.gram and |
25 | | -IdeasNonRecursive.gram. Ideas.gram is an example of a grammar with recursive |
26 | | -rules, though the recursion is not as direct as the above example. It's a good |
27 | | -idea to run these grammars with the generator scripts to see how the scripts |
28 | | -work: |
| 25 | +### Development Setup |
| 26 | +```bash |
| 27 | +git clone https://github.com/syntactic/JSGFTools.git |
| 28 | +cd JSGFTools |
| 29 | +pip install -r requirements-dev.txt |
| 30 | +``` |
29 | 31 |
|
30 | | -```> python DeterministicGenerator.py IdeasNonRecursive.gram``` |
| 32 | +## Quick Start |
31 | 33 |
|
32 | | -```> python ProbabilisticGenerator.py Ideas.gram 20``` |
| 34 | +### Command Line Usage |
33 | 35 |
|
34 | | -### Notes |
| 36 | +Generate all possible strings from a non-recursive grammar: |
| 37 | +```bash |
| 38 | +python DeterministicGenerator.py IdeasNonRecursive.gram |
| 39 | +``` |
35 | 40 |
|
36 | | -- Larger grammars take a longer time to parse, so if nothing seems to be generating, |
37 | | -wait a few seconds and the grammar should be parsed. |
| 41 | +Generate 20 random strings from a grammar (supports recursive rules): |
| 42 | +```bash |
| 43 | +python ProbabilisticGenerator.py Ideas.gram 20 |
| 44 | +``` |
38 | 45 |
|
39 | | -- Most of JSGF as described in http://www.w3.org/TR/2000/NOTE-jsgf-20000605/ is |
40 | | -supported, but there are a few things that have not been implemented by these |
41 | | -tools yet: |
42 | | - - Kleene operators |
43 | | - - Imports and Grammar Names |
44 | | - - Tags |
| 46 | +### Python API Usage |
| 47 | + |
| 48 | +```python |
| 49 | +import JSGFParser as parser |
| 50 | +import DeterministicGenerator as det_gen |
| 51 | +import ProbabilisticGenerator as prob_gen |
| 52 | +from io import StringIO |
| 53 | + |
| 54 | +# Parse a grammar |
| 55 | +grammar_text = """ |
| 56 | +public <greeting> = hello | hi; |
| 57 | +public <target> = world | there; |
| 58 | +public <start> = <greeting> <target>; |
| 59 | +""" |
| 60 | + |
| 61 | +with StringIO(grammar_text) as f: |
| 62 | + grammar = parser.getGrammarObject(f) |
| 63 | + |
| 64 | +# Generate all possibilities (deterministic) |
| 65 | +det_gen.grammar = grammar |
| 66 | +rule = grammar.publicRules[2] # <start> rule |
| 67 | +all_strings = det_gen.processRHS(rule.rhs) |
| 68 | +print("All possible strings:", all_strings) |
| 69 | + |
| 70 | +# Generate random string (probabilistic) |
| 71 | +prob_gen.grammar = grammar |
| 72 | +random_string = prob_gen.processRHS(rule.rhs) |
| 73 | +print("Random string:", random_string) |
| 74 | +``` |
| 75 | + |
| 76 | +## Grammar Format |
| 77 | + |
| 78 | +JSGFTools supports most of the JSGF specification: |
| 79 | + |
| 80 | +```jsgf |
| 81 | +// Comments are supported |
| 82 | +public <start> = <greeting> <target>; |
| 83 | +
|
| 84 | +// Alternatives with optional weights |
| 85 | +<greeting> = /5/ hello | /1/ hi | hey; |
| 86 | +
|
| 87 | +// Optional elements |
| 88 | +<polite> = [ please ]; |
| 89 | +
|
| 90 | +// Nonterminal references |
| 91 | +<target> = world | there; |
| 92 | +
|
| 93 | +// Recursive rules (use with ProbabilisticGenerator only) |
| 94 | +<recursive> = base | <recursive> more; |
| 95 | +``` |
| 96 | + |
| 97 | +### Supported Features |
| 98 | +- Rule definitions and nonterminal references |
| 99 | +- Alternatives (|) with optional weights (/weight/) |
| 100 | +- Optional elements ([...]) |
| 101 | +- Grouping with parentheses |
| 102 | +- Comments (// and /* */) |
| 103 | +- Public and private rules |
| 104 | + |
| 105 | +### Not Yet Supported |
| 106 | +- Kleene operators (* and +) |
| 107 | +- Import statements |
| 108 | +- Tags |
| 109 | + |
| 110 | +## Important Notes |
| 111 | + |
| 112 | +### Recursive vs Non-Recursive Grammars |
| 113 | + |
| 114 | +- **DeterministicGenerator**: Only use with non-recursive grammars to avoid infinite loops |
| 115 | +- **ProbabilisticGenerator**: Can safely handle recursive grammars through probabilistic termination |
| 116 | + |
| 117 | +**Example of recursive rule:** |
| 118 | +```jsgf |
| 119 | +<sentence> = <noun> <verb> | <sentence> and <sentence>; |
| 120 | +``` |
| 121 | + |
| 122 | +## Testing |
| 123 | + |
| 124 | +Run the test suite: |
| 125 | +```bash |
| 126 | +pytest test_jsgf_tools.py -v |
| 127 | +``` |
| 128 | + |
| 129 | +Run specific test categories: |
| 130 | +```bash |
| 131 | +pytest test_jsgf_tools.py::TestJSGFParser -v # Parser tests |
| 132 | +pytest test_jsgf_tools.py::TestIntegration -v # Integration tests |
| 133 | +``` |
| 134 | + |
| 135 | +## Documentation |
| 136 | + |
| 137 | +For detailed API documentation, build the Sphinx docs: |
| 138 | +```bash |
| 139 | +cd docs |
| 140 | +make html |
| 141 | +``` |
| 142 | + |
| 143 | +Then open `docs/_build/html/index.html` in your browser. |
| 144 | + |
| 145 | +## Example Files |
| 146 | + |
| 147 | +- `Ideas.gram`: Recursive grammar example (use with ProbabilisticGenerator) |
| 148 | +- `IdeasNonRecursive.gram`: Non-recursive grammar example (use with DeterministicGenerator) |
| 149 | + |
| 150 | +## Contributing |
| 151 | + |
| 152 | +1. Fork the repository |
| 153 | +2. Create a feature branch |
| 154 | +3. Make your changes |
| 155 | +4. Add tests for new functionality |
| 156 | +5. Run the test suite: `pytest` |
| 157 | +6. Submit a pull request |
| 158 | + |
| 159 | +## License |
| 160 | + |
| 161 | +MIT License. See [LICENSE](LICENSE) file for details. |
| 162 | + |
| 163 | +## Version History |
| 164 | + |
| 165 | +- **2.0.0**: Complete Python 3 modernization, added test suite, improved packaging |
| 166 | +- **1.x**: Original Python 2.7 version |
0 commit comments