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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
#!/usr/bin/env python3
import sys
import math
from cmusphinx import lattice
import sphinxbase
def baseword(sym):
paren = sym.rfind('(')
if paren != -1:
return sym[0:paren]
else:
return sym
def load_lexicon(lexfile):
# load dict or filler file to get word to phone information
f = open(lexfile, 'r')
lines = f.readlines()
f.close()
lex = {}
for line in lines:
temp = line.split()
lex[temp[0]] = temp[1:len(temp)]
return lex
def load_denlat(latfile):
# Annotate the lattice with posterior probabilities
dag = lattice.Dag(latfile)
dag.remove_unreachable()
arc = []
for w in dag.nodes:
for l in w.exits:
if (w.entry, w.sym, l.dest.entry, l.dest.sym) not in arc:
arc.append((w.entry, w.sym, l.dest.entry, l.dest.sym))
lat = {}
for n in arc:
if n[1] != '<s>':
left = []
right = []
for m in arc:
if n[3] == '</s>' and (n[2], n[3], n[2]) not in right:
right.append((n[2], n[3], n[2]))
elif m[0] == n[2] and m[1] == n[3]:
if (m[0], m[1], m[2]) not in right:
right.append((m[0], m[1], m[2]))
if m[2] == n[0] and m[3] == n[1]:
if (m[0], m[1], m[2]) not in left:
left.append((m[0], m[1], m[2]))
if (n[0], n[1], n[2]) not in lat:
lat[(n[0], n[1], n[2])] = (left, right)
else:
for l in left:
if l not in lat[(n[0], n[1], n[2])][0]:
lat[(n[0], n[1], n[2])][0].append(l)
for r in right:
if r not in lat[(n[0], n[1], n[2])][1]:
lat[(n[0], n[1], n[2])][1].append(r)
return lat
def load_numlat(alignfile):
# read force alignment result which is numerator lattice
f = open(alignfile, 'r')
f.readline()
lines = f.readlines()
f.close()
wordseg = []
for line in lines:
temp = line.split()
if len(temp) == 4:
if '<s>' in temp:
wordseg.append((0, 1, '<s>'))
wordseg.append((1, int(temp[1]) + 2, '<sil>'))
elif '</s>' in temp:
wordseg.append((int(temp[0]) + 1, int(temp[1]) + 1, '<sil>'))
wordseg.append((int(temp[1]) + 1, int(temp[1]) + 1, '</s>'))
else:
wordseg.append((int(temp[0]) + 1, int(temp[1]) + 2, temp[3]))
if wordseg[1][2] == wordseg[2][2] and wordseg[1][2] == '<sil>' and wordseg[
1][1] == wordseg[2][0]:
sf = wordseg[1][0]
ef = wordseg[2][1]
wordseg.pop(1)
wordseg.pop(1)
wordseg.insert(1, (sf, ef, '<sil>'))
n = len(wordseg)
if wordseg[n - 2][2] == wordseg[n - 3][2] and wordseg[
n - 2][2] == '<sil>' and wordseg[n - 3][1] == wordseg[n - 2][0]:
sf = wordseg[n - 3][0]
ef = wordseg[n - 2][1]
x = wordseg.pop(n - 2)
y = wordseg.pop(n - 3)
wordseg.insert(n - 3, (sf, ef, '<sil>'))
numlat = {}
for i in range(1, len(wordseg) - 1):
sf = wordseg[i][0]
ef = wordseg[i][1]
word = wordseg[i][2]
left = []
right = []
left.append((wordseg[i - 1][0], wordseg[i - 1][2], wordseg[i - 1][1]))
right.append((wordseg[i + 1][0], wordseg[i + 1][2], wordseg[i + 1][1]))
numlat[(sf, word, ef)] = (left, right)
return numlat
def conv_lat_format(key, lat, filler, lm):
# add word segment IDs and
# use word IDs to present previsou and next word segments
# also assign unigram language model score to each word segment
newlat = []
i = 0
for sf, word, ef in key:
i = i + 1
if word in filler:
score = math.log(0.1)
else:
score = lm.prob([baseword(word)])
left = []
for context in lat[(sf, word, ef)][0]:
if '<s>' in context:
ID = 0
else:
ID = key.index(context) + 1
left.append(ID)
right = []
for context in lat[(sf, word, ef)][1]:
if '</s>' in context:
ID = 0
else:
ID = key.index(context) + 1
right.append(ID)
newlat.append((i, word, sf, ef, score, left, right))
return newlat
def add_numlat_into_denlat(numlat, denlat):
# add numerator lattice into denominator lattice
n_arc = len(denlat)
for i, word, sf, ef, score, left, right in numlat:
aid = i + n_arc
lids = []
for ids in left:
if ids == 0:
lids.append(ids)
else:
lids.append(ids + n_arc)
rids = []
for ids in right:
if ids == 0:
rids.append(ids)
else:
rids.append(ids + n_arc)
denlat.append((aid, word, sf, ef, score, lids, rids))
return denlat
def output_lattice(lat, n_true_arc, outlatfile):
# write lattice to a file
f = open(outlatfile, 'w')
f.write('Total arcs:\n')
f.write('%d\n' % len(lat))
f.write('True arcs:\n')
f.write('%d\n' % n_true_arc)
f.write(
'arc_id, arc_name, start frame, end frame, lmscore, number of preceding acrs, number of succeeding arcs, preceding arc_ids, succeeding arc_ids\n'
)
for ID, word, sf, ef, score, left, right in lat:
if score is None:
break
line = str(ID) + ' ' + word + ' ' + str(sf) + ' ' + str(
ef) + ' ' + str(score) + ' ' + str(len(left)) + ' ' + str(
len(right)) + ' <'
for lid in left:
line = line + ' ' + str(lid)
line = line + ' >'
for rid in right:
line = line + ' ' + str(rid)
f.write("%s\n" % line)
f.close()
def write_lat(lm, filler, filelist, filecount, fileoffset, denlatdir,
numlatdir, outdir):
# write lattice for mmi training
f = open(filelist, 'r')
files = f.readlines()
f.close()
start = int(fileoffset)
end = int(fileoffset) + int(filecount)
if end > len(files):
end = len(files)
for i in range(start, end):
line = files[i]
fileid = line.strip('\n')
alignfile = "%s/%s.wdseg" % (numlatdir, fileid)
latfile = "%s/%s.lat.gz" % (denlatdir, fileid)
print("process sent: %s" % fileid)
print("\t load numerator lattice %s ... " % alignfile)
try:
numlat = load_numlat(alignfile)
except IOError:
print("\t can't open numerator lattice %s to read" % alignfile)
continue
print("\t load denominator lattice %s ..." % latfile)
try:
den_wordseg = load_denlat(latfile)
except IOError:
print("\t can't open denominator lattice %s to read" % latfile)
continue
print("\t convert numerator lattice ...")
numkeys = list(numlat.keys())
numkeys.sort()
conv_numlat = conv_lat_format(numkeys, numlat, filler, lm)
print("\t convert denominator lattice ...")
denkeys = list(den_wordseg.keys())
denkeys.sort()
denlat = conv_lat_format(denkeys, den_wordseg, filler, lm)
print("\t add numerator lattice into denominator lattice ...")
conv_denlat = add_numlat_into_denlat(conv_numlat, denlat)
numlatfile = "%s/%s.numlat" % (outdir, fileid)
denlatfile = "%s/%s.denlat" % (outdir, fileid)
print("\t write numerator lattice to %s ..." % numlatfile)
try:
output_lattice(conv_numlat, len(conv_numlat), numlatfile)
except IOError:
print("\t can't write numerator lattice to %s" % numlatfile)
continue
print("\t write denominator lattice to %s ...\n" % denlatfile)
try:
output_lattice(conv_denlat, len(conv_numlat), denlatfile)
except IOError:
print("\t can't write denominator lattice to %s" % denlatfile)
print("ALL DONE\n")
# If this script is being run from the command-line, do this
if __name__ == '__main__':
if len(sys.argv) != 9:
sys.stderr.write(
"Usage: %s LMFILE FILLERFILE FILELIST FILECOUNT FILEOFFSET DENLATDIR NUMLATDIR OUTLATDIR\n"
% (sys.argv[0]))
sys.exit(1)
command = ''
for argv in sys.argv:
command += argv + ' '
print("%s\n" % command)
lmfile, fillerfile, filelist, filecount, fileoffset, \
denlatdir, numlatdir, outdir = sys.argv[1:]
# load the filler dictionary
filler = load_lexicon(fillerfile)
# load the language model to score the word hypothesis in a lattice
lm = sphinxbase.NGramModel(lmfile)
# convert lattice and output to a file
write_lat(lm, filler, filelist, filecount, fileoffset, denlatdir,
numlatdir, outdir)
|