-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandEngine.py
185 lines (158 loc) · 6.62 KB
/
CommandEngine.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import plivo
from threading import Thread
from SpeechRecognition import SpeechRecognizer
from nltk.stem import WordNetLemmatizer
from nltk.stem import PorterStemmer
from Bot import Bot
from queue import Queue
import logging
import time
import subprocess
import sys
from subprocess import call
import polly as polly
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
class CommandEngine(object):
"""docstring for CommandEngine"""
def __init__(self, configParser):
super(CommandEngine, self).__init__()
self.initialize_logger()
self.config_parser = configParser
self.config = dict(self.config_parser.items('command-engine'))
self.client = plivo.RestClient(auth_id=self.config['client_auth_id'], auth_token=self.config['client_auth_token'])
self.conference = {}
self.polling_thread = Thread(target=self.find_conference)
self.polling_thread.daemon = True
self.raw_text_queue = Queue()
self.bot = Bot(dict(self.config_parser.items('bot')))
# thread for recognition
self.recognition_thread = Thread(target=self.speech_recognition)
self.recognition_thread.daemon = True
self.recognition_thread_started = False
self.polling = True
self.polling_thread.start()
# self.find_conference()
# join the polling thread in the end
self.commander()
# self.polling_thread.join()
def initialize_logger(self):
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.DEBUG)
# stream to stdout
log_handler_std = logging.StreamHandler()
log_handler_std.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log_handler_std.setFormatter(formatter)
self.logger.addHandler(log_handler_std)
self.logger.info('logger initialized')
def speech_recognition(self):
self.sr = SpeechRecognizer('pulse_monitor', self.config_parser, self.raw_text_queue)
self.sr.startRecognition()
# start the bot if a conference is underway
def start_bot(self):
bot_present = False
for member in self.conference['members']:
if 'confBot' in member['caller_name']:
# self.logger.debug(member)
bot_present = True
break
if bot_present:
self.logger.debug('bot found')
# if bot is the only member present then hangup the bot
if len(self.conference['members']) == 1:
self.logger.debug('hangup the bot')
self.bot.remove()
# stop processing the audio
try:
self.sr.stopRecognition()
self.recognition_thread.join()
except Exception:
self.logger.error('recognition not active')
else:
self.logger.debug('bot not found in conference, add the bot')
self.bot.add()
# start processing the audio
self.logger.debug('start audio processing')
if self.recognition_thread_started == False:
self.recognition_thread_started = True
self.recognition_thread.start()
# replace polling with proper response from conference application
def find_conference(self):
try:
while self.polling:
conferences = self.client.conferences.list().conferences
if conferences == []:
self.logger.debug('no conferences found')
# clear the conference object
self.conference = {}
else:
# if self.conference == {}:
# take the first conference
self.conference = self.client.conferences.get(conference_name=conferences[0])
self.logger.debug('conference going on with ' + str(len(self.conference['members'])) + ' members')
self.start_bot()
time.sleep(int(self.config['polling_in_seconds']))
except KeyboardInterrupt: # allow Ctrl + C to shut down the program
self.logger.info('bot interrupted with Ctrl + C')
self.sr.stopRecognition()
self.bot.remove()
def matcher(self, words, phrase):
match_cnt = 0
for word in words:
if word in phrase:
match_cnt += 1
# if match_cnt == len(words):
if match_cnt >= 2:
return True
return False
def executioner(self, phrase):
if self.matcher(['hello', 'there'], phrase):
self.logger.debug('hello there')
self.bot.play_text('hello there, how may I help you')
return True
if self.matcher(['start', 'recording'], phrase):
self.logger.debug('start recording')
self.bot.play_text('sure thing')
# do something to record the pulseaudio sink
return True
if self.matcher(['call', 'Sam'], phrase):
self.logger.debug('call sam')
self.bot.play_text('sure, calling Sam and adding him to the conference.')
self.client.calls.create(
from_='10000',
to_='919999999999',
answer_url='https://s3.amazonaws.com/plivosamplexml/conference_url.xml',
answer_method='GET'
)
return True
if len(phrase) > 50:
self.logger.debug('phrase too long, resetting')
return True
return False
def commander(self):
self.logger.info('printing the raw results')
phrase = ''
self.stemmer = PorterStemmer()
self.lemmatizer = WordNetLemmatizer()
try:
while True:
result = self.raw_text_queue.get()
self.raw_text_queue.task_done()
with open("raw_text", "a") as myfile:
myfile.write(result + ' ')
# we get None when recognition is stopped
# recognition is stopped when we get into keyboard interrupt
# so for now it will never get None
if result is None:
break
print(result, end=' ')
sys.stdout.flush()
phrase += result
exec_result = self.executioner(phrase)
if exec_result:
phrase = ''
except KeyboardInterrupt: # allow Ctrl + C to shut down the program
self.logger.info('bot interrupted with Ctrl + C')
self.bot.remove()
self.sr.stopRecognition()