def test_get_engine_sapi5_is_usable(self):
        """ Verify that the sapi5 engine is usable. """
        engine = get_engine()
        self.assertTrue(isinstance(engine, EngineBase))
        self.assertTrue(engine.name.startswith("sapi5"))

        engine.speak("testing WSR")
        from dragonfly import Literal, Sequence
        from dragonfly.test import ElementTester
        seq = Sequence([Literal("hello"), Literal("world")])
        tester = ElementTester(seq, engine=engine)
        results = tester.recognize("hello world")
        self.assertEqual([u"hello", u"world"], results)
def _get_dragonfly_rule_element(target, parser, depth=0):
    global RULES
    if target not in parser.rules:
        raise Exception("Target {} not in parser rules".format(target))

    # If already present in RULES, return it
    if target in RULES:
        return RULES[target]

    # Get the rule
    rule = parser.rules[target]

    # Iterate over all options
    option_alternative_list = []
    for opt in rule.options:

        # Iterate over all conjunctions
        conjunctions_list = []
        for conj in opt.conjuncts:
            # If the conjunction is already present
            if conj.name in RULES:
                conjunctions_list.append(RULES[conj.name])
                continue

            # If variable: go one level deeper
            if conj.is_variable:
                result = _get_dragonfly_rule_element(conj.name, parser,
                                                     depth + 1)
                if result:
                    conjunctions_list.append(result)
            else:
                # Add a new literal to the list
                RULES[conj.name] = Literal(conj.name)
                conjunctions_list.append(RULES[conj.name])
                logger.debug("Adding literal rule: %s", conj.name)

        # ToDo: apply caching?
        if len(conjunctions_list) == 1:
            option_alternative_list.append(conjunctions_list[0])
        else:
            option_alternative_list.append(Sequence(conjunctions_list))

    if len(option_alternative_list) == 1:
        RULES[target] = option_alternative_list[0]
    else:
        RULES[target] = Alternative(option_alternative_list)

    logger.debug("Adding alternative rule: %s", target)
    return RULES[target]
Exemple #3
0
def doSomethingToCommand(command):
    newCommand = Sequence(command)
    newCommand.execute()
 def value(self, node):
     return Sequence.value(self, node)[0]
 def __init__(self, name, child):
     Sequence.__init__(self, (child, ), name)
 def value(self, node):
     return self.delimiter.join(
         str(v) for v in Sequence.value(self, node) if v)
 def __init__(self, delimiter, *args, **kwargs):
     Sequence.__init__(self, *args, **kwargs)
     self.delimiter = delimiter
 def value(self, node):
     return Sequence.value(self, node)[0]
 def __init__(self, name, child):
     Sequence.__init__(self, (child, ), name)
 def value(self, node):
     return self.delimiter.join(str(v)
                                for v in Sequence.value(self, node)
                                if v)
 def __init__(self, delimiter, *args, **kwargs):
     Sequence.__init__(self, *args, **kwargs)
     self.delimiter = delimiter
 def test_sequence(self):
     check_parse_tree(
         " test <an_extra> [op]",
         Sequence([Literal(u"test"), extras["an_extra"], Optional(Literal(u"op"))]),
     )