Python pyjsgf

From wikinotes

python library to interact with JSGF files.

Documentation

official docs https://pyjsgf.readthedocs.io/en/latest/introduction.html#installation
github https://github.com/Danesprite/pyjsgf

Usage

Compose

import jsgf

# add rules to a grammar
grammar = jsgf.Grammar()
rule = jsgf.PublicRule('direction', jsgf.AlternativeSet('up', 'down', 'left', 'right'))
grammar.add_rule(rule)

# compile grammar into JSGF file.
grammar.compile()
#> '#JSGF V1.0;\ngrammar default;\npublic <direction> = ((up|down|right|left));\n'

Parser

import jsgf

# list all rules within grammar-file
grammar = jsgf.parse_grammar_string('''
#JSGF V1.0;
grammar nav_commands;
public <direction> = (up|down|right|left);
public <multiplier> = (one|two|three|four|five|six|seven|eight|nine|ten);
public <repeat_command> = <multiplier><direction>
''') 
grammar.name        #> 'nav_commands'
grammar.rule_names  #> ['direction', 'multiplier', 'repeat_command']
grammar.rules       #> [Rule(..), Rule(..), Rule(..)]