rules — Rule classes module¶
This module contains classes for compiling and matching JSpeech Grammar Format rules.
Rule(name, visible, expansion, case_sensitive=False)¶
Base class for JSGF rules.
Rule names can be a single word containing one or more alphanumeric Unicode characters and/or any of the following special characters: +-:;,=|/()@#%!^&~$
For example, the following are valid rule names:
There are two reserved rule names: NULL and VOID. These reserved names cannot be used as rule names. You can however change the case to ‘null’ or ‘void’ to use them, as names are case-sensitive.
- name – str
- visible – bool
- expansion – a string or Expansion object
- case_sensitive – whether rule literals should be case sensitive (default False).
Whether this rule is enabled or not. If it is, the rule can be matched and compiled, otherwise the
matchesmethods will return “” and False respectively.
Case sensitivity used when matching and compiling
This property can be
False. Matching and compilation will be case-sensitive if
Trueand case-insensitive if
False. The default value is
Setting this property will override the
case_sensitivevalue for each
Literalin the rule and in referenced rules.
Return type: bool Returns: literal case sensitivity
Compile this rule’s expansion tree and return the result. Set ignore_tags to True to not include expansion tags in the result.
Parameters: ignore_tags – bool Returns: str
The set of rules which this rule directly and indirectly references.
The set of rules in this rule’s grammar that reference this rule. Returns an empty set if this rule is not in a grammar.
Stop this rule from producing compile output or from matching speech strings.
Allow this rule to produce compile output and to match speech strings.
This rule’s expansion.
Searches for a part of speech that matches this rule and returns it.
If no part matches or the rule is disabled, return None.
Parameters: speech – str Returns: str | None
This rule’s fully qualified name.
Fully-qualified rule names are the grammar name plus the rule name. For example, if
"com.example.grammar"is the grammar name and
"rule"is the rule name, then
"com.example.grammar.rule"is the fully-qualified name.
Generate a string matching this rule.
Return type: str
Match a speech string and return a list of any matching tags in this rule and in any referenced rules.
Parameters: speech – str Returns: list
Check whether there are expansions in this rule or referenced rules that use a given JSGF tag.
Parameters: tag – str Returns: bool
A list of JSGF tags whose expansions have been matched. The returned list will be in the order in which tags appear in the compiled rule.
This includes matching tags in referenced rules.
Whether speech matches this rule.
Matching ambiguous rule expansions is not supported because it not worth the performance hit. Ambiguous rule expansions are defined as some optional literal x followed by a required literal x. For example, successfully matching
'test'for the following rule is not supported:
<rule> = [test] test;
Parameters: speech – str Returns: bool
This rule’s qualified name.
Qualified rule names are the last part of the grammar name plus the rule name. For example, if
"com.example.grammar"is the full grammar name and
"rule"is the rule name, then
"grammar.rule"is the qualified name.
The number of dependent rules.
A list of JSGF tags used by this rule and any referenced rules. The returned list will be in the order in which tags appear in the compiled rule.
Whether this rule matched last time the
matchesmethod was called.
PublicRule(name, expansion, case_sensitive=False)¶
Rule subclass with
visibleset to True.