File: lat_rescore.py

package info (click to toggle)
sphinxtrain 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,572 kB
  • sloc: ansic: 94,052; perl: 8,939; python: 6,702; cpp: 2,044; makefile: 6
file content (35 lines) | stat: -rw-r--r-- 933 bytes parent folder | download
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))