-
Notifications
You must be signed in to change notification settings - Fork 274
/
prepare_lang_fst.py
executable file
·219 lines (169 loc) · 5.67 KB
/
prepare_lang_fst.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
#!/usr/bin/env python3
# Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
"""
This script takes as input lang_dir containing lexicon_disambig.txt,
tokens.txt, and words.txt and generates the following files:
- H.fst
- HL.fst
- HLG.fst
Note that saved files are in OpenFst binary format.
Usage:
./local/prepare_lang_fst.py \
--lang-dir ./data/lang_phone \
--has-silence 1
Or
./local/prepare_lang_fst.py \
--lang-dir ./data/lang_bpe_500
"""
import argparse
import logging
from pathlib import Path
import kaldifst
from icefall.ctc import (
Lexicon,
add_disambig_self_loops,
add_one,
build_standard_ctc_topo,
make_lexicon_fst_no_silence,
make_lexicon_fst_with_silence,
)
from icefall.utils import str2bool
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--lang-dir",
type=str,
help="""Input and output directory.
""",
)
parser.add_argument(
"--has-silence",
type=str2bool,
default=False,
help="True if the lexicon has silence.",
)
parser.add_argument(
"--ngram-G",
type=str,
help="""If not empty, it is the filename of G used to build HLG.
For instance, --ngram-G=./data/lm/G_3_fst.txt
""",
)
return parser.parse_args()
def build_HL(
H: kaldifst.StdVectorFst,
L: kaldifst.StdVectorFst,
has_silence: bool,
lexicon: Lexicon,
) -> kaldifst.StdVectorFst:
if has_silence:
# We also need to change the input labels of L
add_one(L, treat_ilabel_zero_specially=True, update_olabel=False)
else:
add_one(L, treat_ilabel_zero_specially=False, update_olabel=False)
# Invoke add_disambig_self_loops() so that it eats the disambig symbols
# from L after composition
add_disambig_self_loops(
H,
start=lexicon.token2id["#0"] + 1,
end=lexicon.max_disambig_id + 1,
)
kaldifst.arcsort(H, sort_type="olabel")
kaldifst.arcsort(L, sort_type="ilabel")
HL = kaldifst.compose(H, L)
kaldifst.determinize_star(HL)
disambig0 = lexicon.token2id["#0"] + 1
max_disambig = lexicon.max_disambig_id + 1
for state in kaldifst.StateIterator(HL):
for arc in kaldifst.ArcIterator(HL, state):
# If treat_ilabel_zero_specially is False, we always change it
# Otherwise, we only change non-zero input labels
if disambig0 <= arc.ilabel <= max_disambig:
arc.ilabel = 0
# Note: We are not composing L with G, so there is no need to add
# self-loops to L to handle #0
return HL
def build_HLG(
H: kaldifst.StdVectorFst,
L: kaldifst.StdVectorFst,
G: kaldifst.StdVectorFst,
has_silence: bool,
lexicon: Lexicon,
) -> kaldifst.StdVectorFst:
if has_silence:
# We also need to change the input labels of L
add_one(L, treat_ilabel_zero_specially=True, update_olabel=False)
else:
add_one(L, treat_ilabel_zero_specially=False, update_olabel=False)
# add-self-loops
token_disambig0 = lexicon.token2id["#0"] + 1
word_disambig0 = lexicon.word2id["#0"]
kaldifst.add_self_loops(L, isyms=[token_disambig0], osyms=[word_disambig0])
kaldifst.arcsort(L, sort_type="olabel")
kaldifst.arcsort(G, sort_type="ilabel")
LG = kaldifst.compose(L, G)
kaldifst.determinize_star(LG)
kaldifst.minimize_encoded(LG)
kaldifst.arcsort(LG, sort_type="ilabel")
# Invoke add_disambig_self_loops() so that it eats the disambig symbols
# from L after composition
add_disambig_self_loops(
H,
start=lexicon.token2id["#0"] + 1,
end=lexicon.max_disambig_id + 1,
)
kaldifst.arcsort(H, sort_type="olabel")
HLG = kaldifst.compose(H, LG)
kaldifst.determinize_star(HLG)
disambig0 = lexicon.token2id["#0"] + 1
max_disambig = lexicon.max_disambig_id + 1
for state in kaldifst.StateIterator(HLG):
for arc in kaldifst.ArcIterator(HLG, state):
# If treat_ilabel_zero_specially is False, we always change it
# Otherwise, we only change non-zero input labels
if disambig0 <= arc.ilabel <= max_disambig:
arc.ilabel = 0
return HLG
def copy_fst(fst):
# Please don't use fst.copy()
return kaldifst.StdVectorFst(fst)
def main():
args = get_args()
lang_dir = args.lang_dir
lexicon = Lexicon(lang_dir)
logging.info("Building standard CTC topology")
max_token_id = max(lexicon.tokens)
H = build_standard_ctc_topo(max_token_id=max_token_id)
# We need to add one to all tokens since we want to use ID 0
# for epsilon
add_one(H, treat_ilabel_zero_specially=False, update_olabel=True)
H.write(f"{lang_dir}/H.fst")
logging.info("Building L")
# Now for HL
if args.has_silence:
L = make_lexicon_fst_with_silence(lexicon, attach_symbol_table=False)
else:
L = make_lexicon_fst_no_silence(lexicon, attach_symbol_table=False)
logging.info("Building HL")
HL = build_HL(
H=copy_fst(H),
L=copy_fst(L),
has_silence=args.has_silence,
lexicon=lexicon,
)
HL.write(f"{lang_dir}/HL.fst")
if not args.ngram_G:
logging.info("Skip building HLG")
return
logging.info("Building HLG")
with open(args.ngram_G) as f:
G = kaldifst.compile(
s=f.read(),
acceptor=False,
)
HLG = build_HLG(H=H, L=L, G=G, has_silence=args.has_silence, lexicon=lexicon)
HLG.write(f"{lang_dir}/HLG.fst")
if __name__ == "__main__":
formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
logging.basicConfig(format=formatter, level=logging.INFO)
main()