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
|
# Copyright (c) 2007 Carnegie Mellon University
#
# You may copy and modify this freely under the same terms as
# Sphinx-III
"""
Sphinx-III format hypothesis segmentation files.
"""
__author__ = "David Huggins-Daines <dhdaines@gmail.com>"
__version__ = "$Revision$"
import io
def open(filename):
"""
Open a Sphinx-III format hypothesis segmentation file for reading.
@param filename Name of file to read
@type filename string
"""
return S3HypSeg(filename)
class S3HypSegEntry(object):
"""
Entry in a Sphinx-III format hypothesis segmentation file.
@ivar uttid: Utterance ID.
@type uttid: string
@ivar scale: Acoustic scaling factor.
@type scale: int
@ivar score: Viterbi path score.
@type score: int
@ivar ascr: Total acoustic score.
@type ascr: int
@ivar lscr: Total language model score.
@type lscr: int
@ivar segs: List of segmentations (name, start_frame, end_frame, ascr, lscr)
@type segs: (string, int, int, int, int)
"""
__fields__ = ['uttid', 'scale', 'score', 'ascr', 'lscr', 'segs']
def __init__(self, line):
fields = line.rstrip().split()
# Strip S * T * A * L * sf
self.uttid = fields[0]
self.scale = int(fields[2])
self.score = int(fields[4])
self.ascr = int(fields[6])
self.lscr = int(fields[8])
fields[0:9] = ()
sf = fields.pop(0)
self.segs = []
while fields:
ascr, lscr, name, ef = fields[0:4]
fields[0:4] = ()
self.segs.append((name, int(sf), int(ef), int(ascr), int(lscr)))
sf = ef
class S3HypSeg:
"""
Class for reading Sphinx-III format hypothesis segmentation files.
"""
def __init__(self, filename):
self.fh = io.open(filename)
def __del__(self):
self.fh.close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
self.fh.close()
return False
def __iter__(self):
return self
def __next__(self):
spam = self.fh.readline()
if spam:
return S3HypSegEntry(spam)
else:
raise StopIteration
|