def main(): # Create a grammar and add some rules to it grammar = RootGrammar() name = HiddenRule("name", AlternativeSet("john", "bob", "anna")) # greeting is either: 'hey', 'hey there' or 'hello' greeting = HiddenRule("greeting", AlternativeSet( Sequence("hey", OptionalGrouping("there")), "hello")) # parting_phrase is either: 'good bye' or 'see you' parting_phrase = HiddenRule("parting_phrase", AlternativeSet( "good bye", "see you")) # greet is a greeting followed by a name greet = PublicRule("greet", Sequence(RuleRef(greeting), RuleRef(name))) # goodbye is a parting phrase followed by a name goodbye = PublicRule("goodbye", Sequence( RuleRef(parting_phrase), RuleRef(name))) grammar.add_rules(name, greeting, parting_phrase, greet, goodbye) print("Root grammar compiles to the following:") print(grammar.compile()) # Try matching some speech strings print_matching(grammar, "hey john") print_matching(grammar, "hey there john") print_matching(grammar, "see you john") # Try matching some hidden rules print_matching(grammar, "bob") print_matching(grammar, "hey there") print_matching(grammar, "good bye")
def main(): # Define a open/close file rule. open, close = Literal("open"), Literal("close") open.tag, close.tag = "OPEN", "CLOSE" cmd = PublicRule("command", Sequence(AlternativeSet(open, close), "the file")) # Print the tags of the 'command' rule. print("Tags: %s\n" % cmd.tags) # Initialise a new grammar and add the rule to it. g = Grammar() g.add_rule(cmd) # Print the compiled grammar print("Compiled grammar is:\n%s" % g.compile()) # Find and print rules tagged with "OPEN" print("Tagged rules are:\n%s\n" % g.find_tagged_rules("OPEN")) # Matching tags can be retrieved using r.get_tags_matching # The Rule.matched_tags property can also be used if Rule.matches or # Grammar.find_matching_rules has been called first. speech = "open the file" print("Tags matching '%s' are: %s" % (speech, cmd.get_tags_matching(speech)))
def main(): # Create a simple rule using a Dictation expansion. rule = PublicRule("Hello_X", Sequence("hello", Dictation())) # Create a new DictationGrammar using the simple rule. grammar = DictationGrammar([rule]) # Print the compiled grammar print(grammar.compile()) # Match against some speech strings. # find_matching_rules has an optional second parameter for advancing to # the next part of the rule, which is set to False here. matching = grammar.find_matching_rules("hello", False) print("Matching rule: %s" % matching[0]) # first part of rule # Go to the next part of the rule. matching[0].set_next() # Match the dictation part. This can be anything. matching = grammar.find_matching_rules("world") print("Matching rule: %s" % matching[0]) # The entire match and the original rule's current_match value will both be 'hello world' print(matching[0].entire_match) print(rule.expansion.current_match)
def main(): # The Repeat expansion requires one or more matches for its child expansion. # The KleeneStar requires zero or more matches. The Sequence expansion # requires all of its children expansions to be spoken in sequence. # Create a public rule using a Repeat expansion and another using the # KleeneStar expansion. rule1 = PublicRule("repeat", Sequence(Repeat("please"), "don't crash")) rule2 = PublicRule("kleene", Sequence(KleeneStar("please"), "don't crash")) # Create a grammar and add the new rules to it grammar = Grammar("g") grammar.add_rules(rule1, rule2) # Compile the grammar using compile() print("Grammar '%s' compiles to:" % grammar.name) print(grammar.compile()) # Find rules in the grammar that match some speech strings print_matching(grammar, "don't crash") # only kleene will match print_matching(grammar, "please don't crash") # both will match print_matching(grammar, "please please don't crash") # both again
def main(): # Create a simple rule using a Dictation expansion. dictation = Dictation() dictation.tag = "dictation" # add a tag to the expansion rule = PublicRule("dictation", Sequence("hello", dictation)) # Print the compiled rule print("Compiled rule: %s" % rule.compile()) # Match a speech string against the rule. speech = "hello world" print("Rule matches '%s': %s." % (speech, rule.matches(speech))) # Print the rule's current_match values using map_expansion. def print_match(x): print("Match for %s: %s" % (x, x.current_match)) map_expansion(rule.expansion, print_match)
def main(): # The Sequence expansion requires all of its children expansions to be spoken # in sequence. The OptionalGrouping expansion optionally requires its child # expansion to be spoken. # Create a public rule using an optional expansion rule = PublicRule("greet", Sequence("hey", OptionalGrouping("there"))) # Create a grammar and add the new rule to it grammar = Grammar("g") grammar.add_rule(rule) # Compile the grammar using compile() print("Grammar '%s' compiles to:" % grammar.name) print(grammar.compile()) # Use or do not use the optional word 'there' print_matching(grammar, "hey") print_matching(grammar, "hey there")
def main(): # Create a new public rule using speech alternatives # Note that the Sequence expansion requires all of its children expansions # to be spoken in sequence rule = PublicRule("greet", Sequence(AlternativeSet("hello", "hey"), "there")) # Create a grammar and add the new rule to it grammar = Grammar("g") grammar.add_rule(rule) # Compile the grammar using compile() print("Grammar '%s' compiles to:" % grammar.name) print(grammar.compile()) # Find rules in the grammar that match some speech strings print_matching(grammar, "hello there") print_matching(grammar, "hey there") # 'hello hey there' will not match because only one alternative in an # AlternativeSet expansion can be matched print_matching(grammar, "hello hey there")
def main(): parser = argparse.ArgumentParser(prog="matching benchmark.py", description="pyjsgf matching benchmark") parser.add_argument("-r", "--rule-string", type=str, default="default", help=("Rule to use for benchmarking. " "Must be a valid JSGF rule ending with ';'.")) parser.add_argument("-n", "--n-speech-strings", type=int, default=100, dest="n", help="Number of speech strings to generate.") parser.add_argument( "-q", "--quiet", default=False, action="store_true", help="Suppress output of generated strings.", ) parser.add_argument( "-p", "--profile", default=False, action="store_true", help=( "Whether to run the benchmark through 'cProfile'. If the module is " "not available, then 'profile' will be used instead."), ) # Parse the arguments. args = parser.parse_args() # Set up rules for testing. if not args.rule_string or args.rule_string == 'default': word = Rule("word", False, AlternativeSet(*WORDS)) number = Rule("number", False, AlternativeSet(*NUMBERS)) rule = Rule( "series", True, Repeat(Sequence(RuleRef(word), OptionalGrouping(RuleRef(number))))) else: rule = parse_rule_string(args.rule_string) # Generate N speech strings to test how well the matching performs. strings = [] for _ in range(args.n): strings.append(rule.generate()) if args.profile: try: # Try 'cProfile'. import cProfile as profile_mod except ImportError: # Fallback on 'profile' (slower) if it isn't available. import profile as profile_mod # Run the benchmark via the imported module, passing locals and globals. now = time.time() profile_mod.runctx("do_benchmark(rule, strings, args)", {}, { "do_benchmark": do_benchmark, "rule": rule, "strings": strings, "args": args }) else: # Run the benchmark without profiling. now = time.time() do_benchmark(rule, strings, args) # Print the time it took to match N generated strings. after = time.time() print("Matched %d generated strings in %.3f seconds." % (args.n, after - now))