-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtakk.py
executable file
·103 lines (85 loc) · 2.99 KB
/
takk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
from __future__ import print_function
from __armando__ import Armando
import json, re, traceback
###
Armando.initialize()
###
from audiosource import AudioSource
from speechrecognition import SpeechRecognition, SpeechRecognitionError
from hue import Hue
from mpd import MPD
from config import Config
from logger import Logger
from rules import Rules
class Takk(object):
__config = Config.get_config()
__logger = Logger.get_logger(__name__)
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)
if __name__ == '__main__':
try:
Takk()
except Exception as e:
tb = traceback.format_exc()
Logger().error({
'msg_type' : 'Uncaught exception, exiting',
'exception' : tb,
})
raise e
# vim:sw=4:ts=4:et: