当前位置: 首页>>代码示例>>Python>>正文


Python Rules.pattern_match方法代码示例

本文整理汇总了Python中rules.Rules.pattern_match方法的典型用法代码示例。如果您正苦于以下问题:Python Rules.pattern_match方法的具体用法?Python Rules.pattern_match怎么用?Python Rules.pattern_match使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rules.Rules的用法示例。


在下文中一共展示了Rules.pattern_match方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestTakkRules

# 需要导入模块: from rules import Rules [as 别名]
# 或者: from rules.Rules import pattern_match [as 别名]
class TestTakkRules(unittest.TestCase):
    __dummy_file = 'test_action_dummy_file'

    def setUp(self):
        Config.get_config('conf/main.test.conf')
        from rules import Rules
        self.rules = Rules('conf/speech.test.xml')

    def test_pattern_matched(self):
        patterns = self.rules.pattern_match('play some music artist Led Zeppelin')
        self.assertGreater(len(patterns), 0)
        self.assertEqual(patterns[0]['id'], 'play-music')
        self.assertEqual(patterns[0]['attributes']['artist'], 'Led Zeppelin')

    def test_pattern_not_matched(self):
        patterns = self.rules.pattern_match('this will never be matched by any of my rules')
        self.assertEqual(len(patterns), 0)

    def test_non_existing_action(self):
        self.assertRaises(KeyError, self.rules.run_action, 'non-existing-action')

    def test_get_rules_by_pattern(self):
        rules = self.rules.get_rules_by_patterns(['create-file'])
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0], 'create-test-file-shell-on-create-file')

        action_ids = self.rules.get_actions_by_rule(rules[0])
        self.assertEqual(len(action_ids), 1)
        self.assertEqual(action_ids[0], 'create-test-file-shell')

    def test_get_rules_by_multiple_patterns(self):
        rules = self.rules.get_rules_by_patterns(['create-file', 'remove-file'])
        self.assertEqual(len(rules), 1)
        self.assertEqual(rules[0], 'create-and-remove-test-file-shell-on-double-command')

        action_ids = self.rules.get_actions_by_rule(rules[0])
        self.assertEqual(len(action_ids), 2)
        self.assertEqual(action_ids[0], 'create-test-file-shell')
        self.assertEqual(action_ids[1], 'remove-test-file-shell')

    def test_get_rules_by_non_existing_pattern(self):
        rules = self.rules.get_rules_by_patterns(['i-dont-exist'])
        self.assertEqual(len(rules), 0)

    def test_shell_action(self):
        self.rules.run_action('create-test-file-shell',
            {'filename': self.__dummy_file }
        )

        self.assertTrue(os.path.isfile(self.__dummy_file))

        self.rules.run_action('remove-test-file-shell',
            {'filename': self.__dummy_file }
        )

        self.assertFalse(os.path.isfile(self.__dummy_file))

    def test_python_action(self):
        self.rules.run_action('create-test-file-python',
            {'filename': self.__dummy_file }
        )

        self.assertTrue(os.path.isfile(self.__dummy_file))

        self.rules.run_action('remove-test-file-python',
            {'filename': self.__dummy_file }
        )

        self.assertFalse(os.path.isfile(self.__dummy_file))

    def tearDown(self):
        if os.path.isfile(self.__dummy_file):
            os.remove(self.__dummy_file)
开发者ID:BlackLight,项目名称:Armando,代码行数:75,代码来源:testspeechrules.py

示例2: __init__

# 需要导入模块: from rules import Rules [as 别名]
# 或者: from rules.Rules import pattern_match [as 别名]
    def __init__(self):
        self.__logger.info({
            'msg_type': 'Application started',
            'config': self.__config.dump(),
        })

        audio = AudioSource()
        audio.record_to_flac()

        speech = SpeechRecognition()
        text = None
        confidence = None

        try:
            text, confidence = speech.recognize_speech_from_file()
            self.__logger.info({
                'msg_type': 'Speech recognized',
                'text': text,
                'confidence': confidence,
            })

        except SpeechRecognitionError as e:
            # TODO Properly manage the raised exception with a retry mechanism, see #13
            self.__logger.info({
                'msg_type': 'Speech not recognized',
            })

        rules = Rules('rules.xml')
        patterns = rules.pattern_match(text.strip())

        if len(patterns) == 0:
            self.__logger.info({
                'msg_type': 'No pattern matched',
                'text': text,
            })
        else:
            self.__logger.info({
                'msg_type': 'Pattern matched',
                'text': text,
                'patterns': json.dumps(patterns),
            })

            pattern_ids = list(map(lambda _: _['id'], patterns))
            matched_rules = rules.get_rules_by_patterns(pattern_ids)

            if len(matched_rules) == 0:
                self.__logger.info({
                    'msg_type': 'No rules associated to the matched patterns',
                    'patterns': json.dumps(patterns),
                })
            else:
                self.__logger.info({
                    'msg_type': 'Rules found',
                    'patterns': json.dumps(patterns),
                    'rules': json.dumps(matched_rules),
                })

                # TODO We only pick up the first rule for now.
                # Eventually we should build a map of all the
                # actions associated to the matched patterns
                # according to the provided rules and establish
                # a priority for executing all of them.
                rule = matched_rules[0]
                actions = rules.get_actions_by_rule(rule)
                for action in actions:
                    rules.run_action(action)
开发者ID:BlackLight,项目名称:Takk,代码行数:68,代码来源:takk.py


注:本文中的rules.Rules.pattern_match方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。