-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_house_vcontroller.py
More file actions
62 lines (49 loc) · 1.64 KB
/
Copy pathrun_house_vcontroller.py
File metadata and controls
62 lines (49 loc) · 1.64 KB
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
from juliuspy import Julius
from camphor_house import House
import re
import json
from logging import getLogger,StreamHandler,DEBUG
logger = getLogger(__name__)
handler = StreamHandler()
handler.setLevel(DEBUG)
logger.setLevel(DEBUG)
logger.addHandler(handler)
logger.propagate = False
class KeywordLister():
def __init__(self):
self.__julius = Julius(host='localhost', port=10500)
self.__julius.connect()
def __del__(self):
self.__julius.disconenct()
@staticmethod
def filter_recognout(stream):
reg = re.compile(r'WHYPO WORD="(.*)" CLASSID="(.*)" PHONE="(.*)" CM="(\d\.\d{3})"')
for msg in stream:
match = re.search(reg, msg)
if match:
word, cid, phone, cm = match.groups(1)
yield json.dumps({'word': word, 'cid': cid, 'phone':phone, 'cm': cm}, ensure_ascii=False)
@staticmethod
def filter_by_cm(stream, threshold):
for msg in stream:
j = json.loads(msg)
if float(j['cm']) > threshold:
yield msg
def listen(self, cm_threshold):
stream = self.__julius.get_stream()
recognout = self.filter_recognout(stream)
return self.filter_by_cm(recognout, cm_threshold)
def main():
house = House()
k_listener = KeywordLister()
try:
for k_msg in k_listener.listen(cm_threshold=0.5):
logger.debug(k_msg)
j = json.loads(k_msg)
house.listen(j['word'])
except KeyboardInterrupt:
logger.debug('---- KeyboardInterrupt ---- ')
except Exception as e:
logger.error(e.message)
if __name__ == '__main__':
main()