import os, sys ## go to the output directory, ## find all the hyp files ## get rdir, odir, filenames and other parameters (keep the default to 0, no changes) ## generate ehyp and eventually tse files. # ## write the output files # def write_ofiles(fname_a, scores_a, rdir_a, odir_a, ofile_ext_a): ## create output file name with its destination # op_f_name = ".".join([os.path.splitext(os.path.basename(fname_a))[0], \ ofile_ext_a]) op_dir = os.path.dirname(fname_a).replace(rdir_a, odir_a) abs_ofile = os.path.join(op_dir, op_f_name) create_dirtree(op_dir) if ofile_ext_a == "hyp": ## open the file and pass the pointer when writing # with open(abs_ofile, 'wb') as fout: write_hyp(fout, scores_a) fout.close() ## TODO: This file format is obsolete so we are not ## updating it right now. If needed in future, update it from ## nist utils. # elif ofile_ext_a == "ehyp": ## open the file and pass the pointer when writing # with open(abs_ofile, 'wb') as fout: write_ehyp(fout, scores_a) fout.close() elif: ofile_ext_a == "tse": ## open the file and pass the pointer when writing # with open(abs_ofile, 'wb') as fout: write_tse(fout, scores_a) fout.close() else: print "Error (%s:%s): Invalide output file extension. (%s)" \ %(sys.argv[0], __name__, ofile_ext_a) exit(-1) ## end of write_ofile method # ## This method writes the hyp file # def write_hyp(fp_a, scores_a): ## loop through all the indices representing a second ## and update the hyp file # for i in range(len(scores_a)): fp_a.write( ",".join( [str(i * HTK_TIME_SCALE), str( (i+1) * HTK_TIME_SCALE), \ str(1.0 - scores_a[i]), str(scores_a[i]) ] ) ) fp_a.write("\n") ## return gracefully # ## end of write_hyp method # def create_dirtree(dirtree_a): if not os.path.exists(dirtree_a): os.makedirs(dirtree_a)