-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasr.py
65 lines (47 loc) · 1.74 KB
/
asr.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
from math import *
from alex_asr.utils import lattice_to_nbest, wst2dict
from alex_asr import Decoder
from asr_utils import lattice_calibration
def create_asr():
import config
recogniser = Decoder(config.model_path)
return ASR(recogniser)
class ASR:
def __init__(self, recogniser):
self.recogniser = recogniser
self.decoded_frames = 0
self.callbacks = []
def add_callback(self, callback):
self.callbacks.append(callback)
def recognize_chunk(self, chunk):
self.recogniser.frame_in(chunk)
dec_t = self.recogniser.decode(max_frames=10)
while dec_t > 0:
self.call_callbacks()
self.decoded_frames += dec_t
dec_t = self.recogniser.decode(max_frames=10)
if self.decoded_frames == 0:
return (1.0, '')
else:
p, interim_result = self.recogniser.get_best_path()
return p, self._tokens_to_words(interim_result)
def _tokens_to_words(self, tokens):
print tokens
return " ".join([self.recogniser.get_word(x).decode('utf8') for x in tokens])
def get_final_hypothesis(self):
if self.decoded_frames == 0:
return [(1.0, '')]
self.recogniser.finalize_decoding()
utt_lik, lat = self.recogniser.get_lattice()
self.reset()
return self._to_nbest(lat, 10)
def _to_nbest(self, lattice, n):
return [(exp(-prob), self._tokens_to_words(path)) for (prob, path) in lattice_to_nbest(lattice_calibration(lattice), n=n)]
def change_lm(self, lm):
pass
def reset(self):
self.decoded_frames = 0
self.recogniser.reset()
def call_callbacks(self):
for callback in self.callbacks:
callback()