forked from k2-fsa/icefall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_graph_compiler.py
172 lines (141 loc) · 4.62 KB
/
test_graph_compiler.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
#!/usr/bin/env python3
# Copyright 2021 Xiaomi Corp. (authors: Fangjun Kuang)
#
# See ../../LICENSE for clarification regarding multiple authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import k2
import pytest
import torch
from icefall.graph_compiler import CtcTrainingGraphCompiler
from icefall.lexicon import Lexicon
from icefall.utils import get_texts
@pytest.fixture
def lexicon():
"""
We use the following test data:
lexicon.txt
foo f o o
bar b a r
baz b a z
<UNK> SPN
phones.txt
<eps> 0
a 1
b 2
f 3
o 4
r 5
z 6
SPN 7
words.txt:
<eps> 0
foo 1
bar 2
baz 3
<UNK> 4
"""
L = k2.Fsa.from_str(
"""
0 0 7 4 0
0 7 -1 -1 0
0 1 3 1 0
0 3 2 2 0
0 5 2 3 0
1 2 4 0 0
2 0 4 0 0
3 4 1 0 0
4 0 5 0 0
5 6 1 0 0
6 0 6 0 0
7
""",
num_aux_labels=1,
)
L.labels_sym = k2.SymbolTable.from_str(
"""
a 1
b 2
f 3
o 4
r 5
z 6
SPN 7
"""
)
L.aux_labels_sym = k2.SymbolTable.from_str(
"""
foo 1
bar 2
baz 3
<UNK> 4
"""
)
ans = Lexicon.__new__(Lexicon)
ans.token_table = L.labels_sym
ans.word_table = L.aux_labels_sym
ans.L_inv = k2.arc_sort(L.invert_())
ans.disambig_pattern = re.compile(r"^#\d+$")
return ans
@pytest.fixture
def compiler(lexicon):
return CtcTrainingGraphCompiler(lexicon, device=torch.device("cpu"))
class TestCtcTrainingGraphCompiler(object):
@staticmethod
def test_convert_transcript_to_fsa(compiler, lexicon):
texts = ["bar foo", "baz ok"]
fsa = compiler.convert_transcript_to_fsa(texts)
labels0 = fsa[0].labels[:-1].tolist()
aux_labels0 = fsa[0].aux_labels[:-1]
aux_labels0 = aux_labels0[aux_labels0 != 0].tolist()
labels1 = fsa[1].labels[:-1].tolist()
aux_labels1 = fsa[1].aux_labels[:-1]
aux_labels1 = aux_labels1[aux_labels1 != 0].tolist()
labels0 = [lexicon.token_table[i] for i in labels0]
labels1 = [lexicon.token_table[i] for i in labels1]
aux_labels0 = [lexicon.word_table[i] for i in aux_labels0]
aux_labels1 = [lexicon.word_table[i] for i in aux_labels1]
assert labels0 == ["b", "a", "r", "f", "o", "o"]
assert aux_labels0 == ["bar", "foo"]
assert labels1 == ["b", "a", "z", "SPN"]
assert aux_labels1 == ["baz", "<UNK>"]
@staticmethod
def test_compile(compiler, lexicon):
texts = ["bar foo", "baz ok"]
decoding_graph = compiler.compile(texts)
input1 = ["b", "b", "<blk>", "<blk>", "a", "a", "r", "<blk>", "<blk>"]
input1 += ["f", "f", "<blk>", "<blk>", "o", "o", "<blk>", "o", "o"]
input2 = ["b", "b", "a", "a", "a", "<blk>", "<blk>", "z", "z"]
input2 += ["<blk>", "<blk>", "SPN", "SPN", "<blk>", "<blk>"]
lexicon.token_table._id2sym[0] == "<blk>"
lexicon.token_table._sym2id["<blk>"] = 0
input1 = [lexicon.token_table[i] for i in input1]
input2 = [lexicon.token_table[i] for i in input2]
fsa1 = k2.linear_fsa(input1)
fsa2 = k2.linear_fsa(input2)
fsas = k2.Fsa.from_fsas([fsa1, fsa2])
decoding_graph = k2.arc_sort(decoding_graph)
lattice = k2.intersect(decoding_graph, fsas, treat_epsilons_specially=False)
lattice = k2.connect(lattice)
aux_labels0 = lattice[0].aux_labels[:-1]
aux_labels0 = aux_labels0[aux_labels0 != 0].tolist()
aux_labels0 = [lexicon.word_table[i] for i in aux_labels0]
assert aux_labels0 == ["bar", "foo"]
aux_labels1 = lattice[1].aux_labels[:-1]
aux_labels1 = aux_labels1[aux_labels1 != 0].tolist()
aux_labels1 = [lexicon.word_table[i] for i in aux_labels1]
assert aux_labels1 == ["baz", "<UNK>"]
texts = get_texts(lattice)
texts = [[lexicon.word_table[i] for i in words] for words in texts]
assert texts == [["bar", "foo"], ["baz", "<UNK>"]]