-
Notifications
You must be signed in to change notification settings - Fork 15
/
grammar.py
57 lines (42 loc) · 1.7 KB
/
grammar.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
import numpy as np
class Grammar(object):
# @context: tuple containing the previous label indices
# @label: the current label index
# @return: the log probability of label given context p(label|context)
def score(self, context, label): # score is a log probability
return 0.0
# @return: the number of classes
def n_classes(self):
return 0
# @return sequence start symbol
def start_symbol(self):
return -1
# @return sequence end symbol
def end_symbol(self):
return -2
# @context: tuple containing the previous label indices
# @return: list of all possible successor labels for the given context
def possible_successors(context):
return set()
# grammar that generates only a single transcript
# use during training to align frames to transcript
class SingleTranscriptGrammar(Grammar):
def __init__(self, transcript, next_idx_range, n_classes):
self.num_classes = n_classes
transcript = transcript + [self.end_symbol()]
self.transcript = transcript
self.successors = dict()
self.next_idx_range = dict()
for i in range(len(transcript)):
context = (self.start_symbol(),) + tuple(transcript[0:i])
self.successors[context] = set([transcript[i]]).union( self.successors.get(context, set()) )
self.next_idx_range[context] = next_idx_range[i]
def n_classes(self):
return self.num_classes
def possible_successors(self, context):
return self.successors.get(context, set())
def score(self, context, label):
if label in self.possible_successors(context):
return 0.0
else:
return -np.inf