forked from Zhenzi-Weng/DeepSC-ST_demonstration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.py
36 lines (28 loc) · 1.05 KB
/
decoder.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
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 24 18:28:36 2022
@author: Zhenzi Weng
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import itertools
import numpy as np
class DeepSpeechDecoder(object):
def __init__(self, labels, blank_index=28):
self.labels = labels
self.blank_index = blank_index
self.int_to_char = dict([(i, c) for (i, c) in enumerate(labels)])
def convert_to_string(self, sequence):
return "".join([self.int_to_char[i] for i in sequence])
def decode(self, logits):
# choose the class with maximimum probability
best = list(np.argmax(logits, axis=1))
# merge repeated chars
merge = [k for k, _ in itertools.groupby(best)]
# remove the blank index in the decoded sequence
merge_remove_blank = []
for k in merge:
if k != self.blank_index:
merge_remove_blank.append(k)
return self.convert_to_string(merge_remove_blank)