forked from RuABraun/fst-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_lca.py
133 lines (117 loc) · 4.02 KB
/
create_lca.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
from wrappedfst import WrappedFst
import os
def create_multiple_lca_txt(inf, outf, dct_isyms, write_syms_f, index_offset, cost):
paths = []
syms = {}
syms['<eps>'] = 0
with open(inf) as fh:
for line in fh:
path = line.split()
for w in path:
if w not in syms:
syms[w] = len(syms) + index_offset
if dct_isyms:
path = [dct_isyms[w] for w in path]
paths.append(path)
cost /= len(paths)
output_lines = []
start_state = 0
end_state = 1
state = 2
for path in paths:
assert len(path)
if len(path) >= 3:
s = f'{start_state} {state} {path[0]} {path[0]} {cost}'
state += 1
output_lines.append(s)
for sym in path[1:-1]:
s = f'{state - 1} {state} {sym} {sym}'
output_lines.append(s)
state += 1
s = f'{state - 1} {end_state} {path[-1]} {path[-1]}'
output_lines.append(s)
elif len(path) == 1:
s = f'{start_state} {end_state} {path[0]} {path[0]} {cost}'
output_lines.append(s)
elif len(path) == 2:
s = f'{start_state} {state} {path[0]} {path[0]} {cost}'
output_lines.append(s)
state += 1
s = f'{state - 1} {end_state} {path[1]} {path[1]}'
output_lines.append(s)
else:
print(path)
raise RuntimeError
with open(outf, 'w') as fh:
for line in output_lines:
fh.write(f'{line}\n')
fh.write(f'{end_state}\n')
if write_syms_f:
with open(write_syms_f, 'w') as fh:
for sym, i in syms.items():
fh.write(f'{sym} {i}\n')
def create_lca(syms, dct_isyms):
fst = WrappedFst()
start = fst.add_state()
fst.set_start(start)
state = start
for sym in syms:
newstate = fst.add_state()
if not dct_isyms:
sym = int(sym)
fst.add_arc(state, newstate, sym, sym, 0.)
else:
if sym in dct_isyms:
fst.add_arc(state, newstate, dct_isyms[sym], dct_isyms[sym], 0.)
else:
unkid = dct_isyms['<unk>']
fst.add_arc(state, newstate, unkid, unkid, 0.)
state = newstate
fst.set_final(state)
return fst
def create_one_lca(inf, outf, dct_isyms):
syms = []
with open(inf) as fh:
for line in fh:
if line.strip():
syms.extend(line.split())
fst = create_lca(syms, dct_isyms)
fst.write(outf)
def create_ark_lca(inf, outf, dct_isyms):
with open(inf) as fh:
for line in fh:
uttid, *syms = line.split()
fst = create_lca(syms, dct_isyms)
fst.write_ark_entry(uttid, outf)
def main(inf: "Path to file with input symbols",
outf: "Path to fst file to create.",
read_syms_f: ('', 'option', None),
write_syms_f: ('', 'option', None),
isark: ('', 'flag', None) = False,
ismultiple: ('Multiple paths in FST', 'flag', None) = False,
index_offset: ('', 'option', None, int) = 0,
cost: ('', 'option', None, float) = 0.):
""" Create linear chain automata
Three modes:
1. Creates a single LCA from a single sequence.
2. Creates a "comb", multiple arc sequences from a single start and end state.
3. Creates multiple LCAs from an ark file, one for each ark entry.
"""
dct_isyms = {}
if read_syms_f:
with open(read_syms_f) as fh:
for line in fh:
w, i = line.split()
i = int(i)
dct_isyms[w] = i
dct_isyms[i] = w
if ismultiple:
create_multiple_lca_txt(inf, outf, dct_isyms, write_syms_f, index_offset, cost)
elif not isark:
create_one_lca(inf, outf, dct_isyms)
else:
if os.path.isfile(outf): os.remove(outf)
create_ark_lca(inf, outf, dct_isyms)
if __name__ == "__main__":
import plac
plac.call(main)