grammars — Extension grammar classes module

This module contains extension grammar classes.

Classes

class jsgf.ext.grammars.DictationGrammar(rules=None, name='default', case_sensitive=False)

Grammar subclass that processes rules using Dictation expansions so they can be compiled, matched and used with normal JSGF rules with utterance breaks.

Parameters:
  • rules – list
  • name – str
  • case_sensitive – bool
add_rule(rule)

Add a rule to the grammar.

This method will override the new rule’s case_sensitive value with the grammar’s case_sensitive value.

Parameters:rule – Rule
Raises:GrammarError
compile()

Compile this grammar’s header, imports and rules into a string that can be recognised by a JSGF parser.

Returns:str
compile_as_root_grammar()

Compile this grammar with one public “root” rule containing rule references in an alternative set to every other rule as such:

public <root> = (<rule1>|<rule2>|..|<ruleN>);
<rule1> = ...;
<rule2> = ...;
.
.
.
<ruleN> = ...;

This is useful if you are using JSGF grammars with CMU Pocket Sphinx.

Returns:str
find_matching_rules(speech, advance_sequence_rules=True)

Find each visible rule passed to the grammar that matches the speech string. Also set matches for the original rule.

Parameters:
  • speech – str
  • advance_sequence_rules – whether to call set_next() for successful sequence rule matches.
Returns:

list

get_generated_rules(rule)

Get the rules generated from a rule added to this grammar.

Parameters:rule – Rule
Returns:generator
get_original_rule(rule)

Get the original rule from a generated rule.

Parameters:rule – Rule
Returns:Rule
match_rules

The rules that the find_matching_rules method will match against.

Returns:list
rearrange_rules()

Move each SequenceRule in this grammar between the dictation rules list and the internal grammar used for JSGF only rules depending on whether a SequenceRule’s current expansion is dictation-only or not.

remove_rule(rule, ignore_dependent=False)

Remove a rule from this grammar.

Parameters:
  • rule – Rule object or the name of a rule in this grammar
  • ignore_dependent – whether to check if the rule has dependent rules
Raises:

GrammarError

reset_sequence_rules()

Reset each SequenceRule in this grammar so that they can accept matches again.

rules

The rules in this grammar.

This includes internal generated rules as well as original rules.

Returns:list