def test_alt_set(self): # Test that ordering doesn't matter for hashing. self.assertEqual(hash(AlternativeSet("hello", "hi")), hash(AlternativeSet("hi", "hello"))) # Test more complex expansions self.assertEqual(hash(Sequence(AlternativeSet("a", "b"), "c")), hash(Sequence(AlternativeSet("b", "a"), "c"))) self.assertEqual( hash(Sequence(AlternativeSet("a", Dictation(), "b"), "c")), hash(Sequence(AlternativeSet("b", "a", Dictation()), "c"))) # Test that different sets produce different hashes self.assertNotEqual(hash(AlternativeSet("a", "b")), hash(AlternativeSet("a", "b", "c"))) # Test weights. e1 = AlternativeSet("hello", "hi") e1.weights = {"hello": 1, "hi": 2} self.assertNotEqual(hash(e1), hash(AlternativeSet("hi", "hello"))) e2 = AlternativeSet("hello", "hi") e2.weights = {"hello": 2, "hi": 3} self.assertNotEqual(hash(e1), hash(e2)) e2.weights = {"hi": 2, "hello": 1} self.assertEqual(hash(e1), hash(e2))
def test_alt_set(self): # Test that ordering doesn't matter for hashing. self.assertEqual(hash(AlternativeSet("hello", "hi")), hash(AlternativeSet("hi", "hello"))) # Test more complex expansions self.assertEqual(hash(Sequence(AlternativeSet("a", "b"), "c")), hash(Sequence(AlternativeSet("b", "a"), "c"))) self.assertEqual( hash(Sequence(AlternativeSet("a", Dictation(), "b"), "c")), hash(Sequence(AlternativeSet("b", "a", Dictation()), "c"))) # Test that different sets produce different hashes self.assertNotEqual(hash(AlternativeSet("a", "b")), hash(AlternativeSet("a", "b", "c")))
def test_literals(self): self.assert_copy_works(Literal("test")) self.assert_copy_works(Dictation()) # Check that regex patterns are not copied e1 = Literal("test") e2 = Dictation() # Initialise patterns - they are initialised lazily _ = e1.matching_regex_pattern _ = e2.matching_regex_pattern # Value of internal member '_pattern' should be None for copies self.assertIsNone(e1.copy()._pattern) self.assertIsNone(e2.copy()._pattern)
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 test_invalid_rules(self): # Literals with text == "" raise CompilationErrors self.assertRaises(CompilationError, PublicRule("test", "").compile) self.assertRaises(CompilationError, PublicRule("test", Literal("")).compile) # Dictation doesn't raise errors on compilation. self.assertEqual(PublicRule("test", Dictation()).compile(), "public <test> = <DICTATION>;")
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 test_literals(self): self.assert_copy_works(Literal("test")) self.assert_copy_works(Dictation())
def test_dictation(self): """Dictation Expansions match Unicode strings.""" self.assertTrue(PublicRule(u"всё", Dictation().matches(u"это кофе")))