-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
347 lines (275 loc) · 10.2 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import collections
import hashlib
import os
import re
import sys
import pprint
import numpy
from nltk.corpus import stopwords
from gensim.models import KeyedVectors
import pymorphy2
from synthesize_file import synthesize_text_file, synthesize_ssml_file
from transcribe_streaming_mic import recognize_microphone_stream
LANG='ru-RU'
TTS_CLIENT = None
synthesizer = synthesize_text_file
SOUND_DIR='sound_dir'
if os.name == 'nt':
import playsound
def play(filename):
playsound.playsound(filename)
else:
def play(filename):
os.system("mpg123 {}".format(filename))
SSML = """
<?xml version="1.0"?>
<speak version="1.1" xmlns="http://www.w3.org/2001/10/synthesis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/10/synthesis
http://www.w3.org/TR/speech-synthesis11/synthesis.xsd"
xml:lang="{lang}">
{TXT}
</speak>
"""
SSML = SSML.format(lang=LANG, TXT="{TXT}")
def synthesize_and_play(txt):
txt_hash = hashlib.md5(txt.encode('utf-8')).hexdigest()
filename = os.path.join(SOUND_DIR, '{}.mp3'.format(txt_hash))
synthesizer = synthesize_text_file
if txt[0] == '<':
synthesizer = synthesize_ssml_file
txt = SSML.format(TXT=txt)
print(txt)
if not os.path.isfile(filename):
with open(filename, "wb") as fh:
synthesizer(txt, TTS_CLIENT, fh, lang=LANG)
play(filename)
class ScriptReader(object):
def __init__(self, filename, callback, lang=LANG):
self.filename = filename
self.callback = callback
self.lang = LANG
self.update()
def save_script(self):
with open(self.filename + '.tmp', 'w', encoding='utf-8') as fh:
for q, a in self.script.items():
fh.write(q + '\n')
fh.write(a + '\n')
fh.write('\n')
os.unlink(self.filename)
os.rename(self.filename + '.tmp', self.filename)
@classmethod
def _text_process(cls, txt, is_input=False):
txt = txt.lower().strip()
txt = re.sub("^\* *", "", txt)
if not is_input:
return txt
txt = re.sub("ё", "е", txt)
return re.sub("[.,;:?!]", "", txt)
@classmethod
def read_script(cls, filename):
script = collections.OrderedDict()
with open(filename, encoding='utf-8') as fh:
it = iter(fh)
try:
while True:
k = cls._text_process(next(it), is_input=True)
if not k:
continue
v = cls._text_process(next(it))
if not v:
continue
script[k] = v
except StopIteration:
pass
return script
def update(self):
self.script = self.read_script(self.filename)
def add(self, key, value):
key = self._text_process(key, is_input=True)
value = self._text_process(value)
self.script[key] = value
def remove(self, key):
key = self._text_process(key, is_input=True)
if key in self.script:
del self.script[key]
def __call__(self, transcript, is_final=False):
transcript = self._text_process(transcript, is_input=True)
replica = self.script.get(transcript)
if not replica and is_final:
replica = self.script.get('ничего непонятно')
if not replica:
return
print("Got {}, respond with {}".format(transcript, replica))
self.callback(replica)
return True
class W2VScriptReader(ScriptReader):
stopwords = stopwords.words('russian')
pymorphy = pymorphy2.MorphAnalyzer()
grammar_map_POS_TAGS = {
'NOUN': '_NOUN',
'VERB': '_VERB', 'INFN': '_VERB', 'GRND': '_VERB', 'PRTF': '_VERB', 'PRTS': '_VERB',
'ADJF': '_ADJ', 'ADJS': '_ADJ',
'ADVB': '_ADV',
'PRED': '_ADP',
}
def __init__(self, *args, **kwargs):
print("Loading word2vec...")
self.w2v = KeyedVectors.load_word2vec_format('model/model.bin',
binary=True, encoding='utf-8')
print("done")
super(W2VScriptReader, self).__init__(*args, **kwargs)
@classmethod
def _text_process(cls, txt, is_input=False, filter_stopwords=False):
txt = txt.lower().strip()
txt = re.sub("^\* *", "", txt)
if not is_input:
return txt
txt = re.sub("ё", "е", txt)
txt = re.sub("[.,;:?!]", "", txt)
if filter_stopwords:
txt = " ".join(x for x in txt.split() if x not in cls.stopwords)
return txt
def update_vecs(self):
self.vectors = collections.OrderedDict()
for key, value in self.script.items():
self.add_vector_item(key, value)
def to_vector(self, txt):
total = numpy.zeros((self.w2v.vector_size,))
txt = self._text_process(txt, is_input=True, filter_stopwords=True)
words = txt.split()
nwords = 0
for word in words:
parse = self.pymorphy.parse(word)[0]
POS = parse.tag.POS
if POS is None:
print("dont know word", word)
continue
print(word, parse, POS)
POS = self.grammar_map_POS_TAGS.get(POS)
if POS is None:
print("can't map word", word)
continue
word = parse.normal_form
try:
vec = self.w2v[word + POS]
total += vec
nwords += 1
except KeyError:
print("no word ", word + POS)
return total / numpy.sqrt(numpy.dot(total, total) + 0.0001)
def add_vector_item(self, key, value):
if key == 'default':
return
vec = self.to_vector(key)
self.vectors[key] = (vec, key, value)
def lookup(self, key):
lookup = self.to_vector(key)
mindistance, element = float('+inf'), None
for i, (vec, q, a) in enumerate(self.vectors.values()):
d = lookup - vec
distance = numpy.sqrt(numpy.dot(d, d))
print(distance, q)
if distance < mindistance:
mindistance = distance
element = (vec, q, a)
print(mindistance, element[1], element[2])
return element
def update(self):
super(W2VScriptReader, self).update()
self.update_vecs()
def add(self, key, value):
key = self._text_process(key, is_input=True)
value = self._text_process(value)
self.script[key] = value
self.add_vector_item(key, value)
def remove(self, key):
key = self._text_process(key, is_input=True)
self.script.pop(key)
self.vectors.pop(key)
def __call__(self, transcript, is_final=False):
exact = self._text_process(transcript, is_input=True)
replica = self.script.get(exact)
if not replica and not is_final:
return
if is_final:
clear = self._text_process(transcript, is_input=True,
filter_stopwords=True)
replica = self.lookup(clear)
if replica:
replica = replica[2]
if not replica:
replica = self.script.get('default')
print("Got {}, respond with {}".format(transcript, replica))
self.callback(replica)
return True
class StopIt(Exception):
pass
class Listener(object):
def __init__(self, reader):
self.reader = reader
def __call__(self, responses):
for response in responses:
if not response.results:
continue
# The `results` list is consecutive. For streaming, we only care about
# the first result being considered, since once it's `is_final`, it
# moves on to considering the next utterance.
result = response.results[0]
if not result.alternatives:
continue
#print(result, result.alternatives[0].transcript, "is_final = ", result.is_final)
#if result.stability < 0.5:
#if result.is_final:
#return
#continue
for alternative in result.alternatives:
transcript = alternative.transcript
print(transcript, result.is_final)
if not re.search(r'василиса', transcript, re.I):
if self.reader(transcript, result.is_final):
# Force it to reconnect
return
continue
if re.search(r'\bумри\b', transcript, re.I):
print('Exiting..')
self.reader.save_script()
raise StopIt()
if re.search(r'\bсмени пластинку\b', transcript, re.I):
print('Updating..')
self.reader.update()
return
if re.search(r'\bпокажи сценарий\b', transcript, re.I):
pprint.pprint(self.reader.script)
return
match = re.search( r'\bдобавить(?P<phrase>.*)ответить(?P<response>.*)\b', transcript, re.I)
if result.is_final and match:
self.reader.add(match['phrase'], match['response'])
return
match = re.search(r'\bубрать команду(?P<phrase>.*)\b',
transcript, re.I)
if result.is_final and match:
self.reader.remove(match['phrase'])
return
if result.is_final:
return
def main():
global TTS_CLIENT
from google.cloud import texttospeech
TTS_CLIENT = texttospeech.TextToSpeechClient()
try:
filename = sys.argv[1]
except IndexError:
filename = 'script-%s.txt' % LANG
script_reader = W2VScriptReader(
filename,
synthesize_and_play,
lang=LANG)
listener = Listener(script_reader)
while True:
try:
recognize_microphone_stream(listener, lang=LANG, add_noise=100)
except StopIt:
break
if __name__ == '__main__':
main()