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
|
#!/usr/bin/env python3
# Copyright (c) 2010 Carnegie Mellon University
#
# You may copy and modify this freely under the same terms as
# Sphinx-III
"""
Rescore a lattice using a language model directly
"""
__author__ = "David Huggins-Daines <dhdaines@gmail.com>"
__version__ = "$Revision $"
import sphinxbase
from cmusphinx import lattice
import sys
import os
def lat_rescore(latfile, lmfst):
"""
Rescore a lattice using a language model.
"""
dag = lattice.Dag(latfile)
end = dag.bestpath(lm)
return [lattice.baseword(x.sym) for x in dag.backtrace(end)], end.score
if __name__ == '__main__':
ctlfile, latdir, lmfile = sys.argv[1:]
lm = sphinxbase.NGramModel(lmfile, wip=1.0, lw=9.5)
for spam in open(ctlfile):
latfile = os.path.join(latdir, spam.strip() + ".lat.gz")
words, score = lat_rescore(latfile, lm)
print(" ".join(words), "(%s %f)" % (spam.strip(), score))
|