def test_optional_with_alt_set(self): e1 = AS("a", "b", Seq("c", Opt(Seq("d", Dict())))) self.assertListEqual( expand_dictation_expansion(e1), [AS("a", "b", Seq("c")), AS("a", "b"), Seq("c", Seq("d", Dict()))]) e2 = AS("a", "b", Seq("c", KleeneStar(Seq("d", Dict())))) self.assertListEqual(expand_dictation_expansion(e2), [ AS("a", "b", Seq("c")), AS("a", "b"), Seq("c", Rep(Seq("d", Dict()))) ]) e3 = AS("a", "b", Seq(Opt("c"), Seq("d", Dict()))) self.assertListEqual( expand_dictation_expansion(e3), [AS("a", "b"), Seq(Seq("d", Dict())), Seq("c", Seq("d", Dict()))]) e4 = AS("a", "b", Seq(KleeneStar("c"), Seq("d", Dict()))) self.assertListEqual(expand_dictation_expansion(e4), [ AS("a", "b"), Seq(Seq("d", Dict())), Seq(Rep("c"), Seq("d", Dict())) ])
def test_mutually_exclusive_dictation(self): e1 = AS(Seq("a", Dict()), Seq(Dict(), "b")) self.assertListEqual( expand_dictation_expansion(e1), [Seq("a", Dict()), Seq(Dict(), "b")]) e2 = AS(Seq("a", Dict()), Seq("b", Dict())) self.assertListEqual( expand_dictation_expansion(e2), [Seq("a", Dict()), Seq("b", Dict())]) e3 = AS(Seq(Dict(), "a"), Seq(Dict(), "b")) self.assertListEqual( expand_dictation_expansion(e3), [Seq(Dict(), "a"), Seq(Dict(), "b")]) # Also test with a JSGF only alternative e4 = AS(Seq("a", Dict()), Seq("b", Dict()), "c") self.assertListEqual( expand_dictation_expansion(e4), [ Literal( "c" ), # JSGF alternatives just happen to be processed first Seq("a", Dict()), Seq("b", Dict()) ])
def test_optional(self): e1 = Seq("hey", Opt(Dict())) self.assertListEqual(expand_dictation_expansion(e1), [Seq("hey"), Seq("hey", Dict())]) e2 = Seq("the", Opt(Dict()), "thing") self.assertListEqual( expand_dictation_expansion(e2), [Seq("the", "thing"), Seq("the", Dict(), "thing")]) e3 = Seq("the", KleeneStar(Dict()), "thing") self.assertListEqual( expand_dictation_expansion(e3), [Seq("the", "thing"), Seq("the", Rep(Dict()), "thing")]) e4 = Seq(Opt("hey"), Dict()) self.assertListEqual(expand_dictation_expansion(e4), [Seq(Dict()), Seq("hey", Dict())]) e5 = Seq(Opt("hey"), Dict(), Opt("hey")) self.assertListEqual(expand_dictation_expansion(e5), [ Seq(Dict()), Seq(Dict(), "hey"), Seq("hey", Dict()), Seq("hey", Dict(), "hey") ])
def test_copying(self): """Original expansions are not used in output expansions""" # Note that JSGF only expansions are not expected to pass this test; # expand_dictation_expansion(e) returns exactly e. def assert_no_identical_expansions(original_e, expanded_list): """ Recursively check if any expanded expansion is identical to one in the original expansion tree. Only the immediate tree is checked (shallow traversals). :type original_e: Expansion :type expanded_list: list """ original_expansions = flat_map_expansion(original_e, shallow=True) def f(x): for o in original_expansions: self.assertIsNot(x, o) for expanded in expanded_list: map_expansion(expanded, f, shallow=True) # Test with a relatively simple expansion e = AS("a", "b", Seq("c", Dict())) result = expand_dictation_expansion(e) self.assertListEqual(result, [ AS("a", "b"), Seq("c", Dict()) ]) assert_no_identical_expansions(e, result) # Test with an expansion using RuleRefs n = Rule("n", False, AS("one", "two", "three")) e = AS(Seq("backward", RuleRef(n)), "forward", Seq(Dict(), RuleRef(n))) result = expand_dictation_expansion(e) self.assertListEqual(result, [ AS(Seq("backward", RuleRef(n)), "forward"), Seq(Dict(), RuleRef(n)) ]) assert_no_identical_expansions(e, result) # Test with an expansion using optionals e = AS("a", "b", Seq("c", Opt(Dict()))) result = expand_dictation_expansion(e) self.assertListEqual(result, [ AS("a", "b", Seq("c")), AS("a", "b"), Seq("c", Dict()) ]) assert_no_identical_expansions(e, result) # And again instead using KleeneStar e = AS("a", "b", Seq("c", KleeneStar(Dict()))) result = expand_dictation_expansion(e) self.assertListEqual(result, [ AS("a", "b", Seq("c")), AS("a", "b"), Seq("c", Repeat(Dict())) ]) assert_no_identical_expansions(e, result)
def test_no_dictation(self): e1 = AS("hi", "hello") self.assertListEqual(expand_dictation_expansion(e1), [e1], "Dictation-free expansions should remain untouched") e2 = Seq(AS("hi", "hello"), "there") self.assertListEqual(expand_dictation_expansion(e2), [e2])
def test_one_jsgf_only_alternative(self): e1 = AS("a", Dict()) self.assertListEqual(expand_dictation_expansion(e1), [Literal("a"), Dict()]) e2 = AS("a") self.assertListEqual( expand_dictation_expansion(e2), [AS("a")], "Dictation free AlternativeSets should remain untouched")
def test_multiple_dictation(self): e1 = AS("hi", "hello", Seq("hey", Dict()), Dict()) self.assertListEqual( expand_dictation_expansion(e1), [AS("hi", "hello"), Seq("hey", Dict()), Dict()]) e2 = Seq(AS("hi", "hello", Seq("hey", Dict())), Dict()) self.assertListEqual( expand_dictation_expansion(e2), [Seq(AS("hi", "hello"), Dict()), Seq(Seq("hey", Dict()), Dict())])
def test_one_dictation(self): e1 = AS("hi", "hello", Dict()) self.assertListEqual(expand_dictation_expansion(e1), [AS("hi", "hello"), Dict()]) e2 = AS("hi", "hello", Seq("hey", Dict())) self.assertListEqual( expand_dictation_expansion(e2), [AS("hi", "hello"), Seq("hey", Dict())]) e3 = Seq(AS("hi", "hello", Dict()), "there") self.assertListEqual( expand_dictation_expansion(e3), [Seq(AS("hi", "hello"), "there"), Seq(Dict(), "there")])
def test_multiple_dictation_alt_sets(self): e1 = Seq(AS("a", "b", Dict()), "c", AS("d", Dict())) self.assertListEqual(expand_dictation_expansion(e1), [ Seq(AS("a", "b"), "c", "d"), Seq(AS("a", "b"), "c", Dict()), Seq(Dict(), "c", "d"), Seq(Dict(), "c", Dict()) ]) e2 = Seq(AS("a", "b", Dict()), "c", AS("d", Dict()), "e") self.assertListEqual(expand_dictation_expansion(e2), [ Seq(AS("a", "b"), "c", "d", "e"), Seq(AS("a", "b"), "c", Dict(), "e"), Seq(Dict(), "c", "d", "e"), Seq(Dict(), "c", Dict(), "e") ])
def test_no_alt_sets(self): e2 = Seq("hi", "hello") self.assertListEqual(expand_dictation_expansion(e2), [e2]) e1 = Seq("hi", "hello", Dict()) self.assertListEqual(expand_dictation_expansion(e1), [e1])