rules — Rule classes module

This module contains classes for compiling and matching JSpeech Grammar Format rules.

Classes

class jsgf.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:

  • hello
  • Zürich
  • user_test
  • $100
  • 1+2=3

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.

Parameters:
  • name – str
  • visible – bool
  • expansion – a string or Expansion object
  • case_sensitive – whether rule literals should be case sensitive (default False).
active

Whether this rule is enabled or not. If it is, the rule can be matched and compiled, otherwise the compile and matches methods will return “” and False respectively.

Returns:bool
case_sensitive

Case sensitivity used when matching and compiling Literal rule expansions.

This property can be True or False. Matching and compilation will be case-sensitive if True and case-insensitive if False. The default value is False.

Setting this property will override the case_sensitive value for each Literal in the rule and in referenced rules.

Return type:bool
Returns:literal case sensitivity
compile(ignore_tags=False)

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
dependencies

The set of rules which this rule directly and indirectly references.

Returns:set
dependent_rules

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.

Returns:set
disable()

Stop this rule from producing compile output or from matching speech strings.

enable()

Allow this rule to produce compile output and to match speech strings.

expansion

This rule’s expansion.

Returns:Expansion
find_matching_part(speech)

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
fully_qualified_name

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()

Generate a string matching this rule.

Return type:str
get_tags_matching(speech)

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
has_tag(tag)

Check whether there are expansions in this rule or referenced rules that use a given JSGF tag.

Parameters:tag – str
Returns:bool
matched_tags

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.

Returns:list
matches(speech)

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
qualified_name

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.

reference_count

The number of dependent rules.

Returns:int
tags

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.

Returns:list
was_matched

Whether this rule matched last time the matches method was called.

Returns:bool
class jsgf.rules.PublicRule(name, expansion, case_sensitive=False)

Rule subclass with visible set to True.

jsgf.rules.HiddenRule

alias of jsgf.rules.PrivateRule