Created
June 19, 2013 05:53
-
-
Save edobashira/5811963 to your computer and use it in GitHub Desktop.
Simple tool to convert Kaldi lattice to HTK SLF format using Kaldi and OpenLat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//lattice-to-htk.cc | |
//Just a hacked up version of latbin/lattice-to-fst.cc | |
//Modified to write htk slf instead of a table of StdFsts | |
//using openlat library | |
//https://github.com/valabau/openlat | |
// Copyright 2009-2011 Microsoft Corporation | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | |
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | |
// MERCHANTABLITY OR NON-INFRINGEMENT. | |
// See the Apache 2 License for the specific language governing permissions and | |
// limitations under the License. | |
#include "base/kaldi-common.h" | |
#include "util/common-utils.h" | |
#include "fstext/fstext-lib.h" | |
#include "lat/kaldi-lattice.h" | |
#include <openlat/htk-printer.h> | |
int main(int argc, char *argv[]) { | |
try { | |
using namespace kaldi; | |
typedef kaldi::int32 int32; | |
typedef kaldi::int64 int64; | |
using fst::SymbolTable; | |
using fst::VectorFst; | |
using fst::StdArc; | |
using std::vector; | |
using std::ofstream; | |
using std::stringstream; | |
BaseFloat acoustic_scale = 0.0; | |
BaseFloat lm_scale = 0.0; | |
string word_syms; | |
const char *usage = | |
"Turn lattices into text SLFS instead of normal FSTs, retaining only the word labels\n" | |
"By default, removes all weights and also epsilons (configure with\n" | |
"with --acoustic-scale, --lm-scale and --rm-eps)\n" | |
"Usage: lattice-to-fst [options] lattice-rspecifier slf.filename\n" | |
" e.g.: lattice-to-fst ark:1.lats lats.slf\n"; | |
ParseOptions po(usage); | |
po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods"); | |
po.Register("lm-scale", &lm_scale, "Scaling factor for graph/lm costs"); | |
po.Register("word-syms", &word_syms, ""); | |
po.Read(argc, argv); | |
if (po.NumArgs() != 2) { | |
po.PrintUsage(); | |
exit(1); | |
} | |
vector<vector<double> > scale = fst::LatticeScale(lm_scale, acoustic_scale); | |
std::string lats_rspecifier = po.GetArg(1), | |
slfs_wspecifier = po.GetArg(2); | |
SequentialCompactLatticeReader lattice_reader(lats_rspecifier); | |
ofstream ofs(slfs_wspecifier.c_str()); | |
if (!ofs.is_open()) { | |
KALDI_ERR << "Failed to open output file : " << slfs_wspecifier; | |
} | |
//Probably should error check this | |
SymbolTable* syms = 0; | |
if (!word_syms.empty()) { | |
syms = SymbolTable::ReadText(word_syms); | |
if (!syms) | |
KALDI_ERR << "Failed to read word symbols files"; | |
} | |
int32 n_done = 0; // there is no failure mode, barring a crash. | |
for (; !lattice_reader.Done(); lattice_reader.Next()) { | |
std::string key = lattice_reader.Key(); | |
CompactLattice clat = lattice_reader.Value(); | |
lattice_reader.FreeCurrent(); | |
ScaleLattice(scale, &clat); // typically scales to zero. | |
RemoveAlignmentsFromCompactLattice(&clat); // remove the alignments... | |
fst::VectorFst<StdArc> fst; | |
{ | |
Lattice lat; | |
ConvertLattice(clat, &lat); // convert to non-compact form.. won't introduce | |
// extra states because already removed alignments. | |
ConvertLattice(lat, &fst); // this adds up the (lm,acoustic) costs to get | |
// the normal (tropical) costs. | |
Project(&fst, fst::PROJECT_OUTPUT); // Because in the standard Lattice format, | |
// the words are on the output, and we want the word labels. | |
} | |
RemoveEpsLocal(&fst); | |
//Or maybe use the standard RmEpsilon function from OpenFst | |
fst.SetInputSymbols(syms); | |
stringstream ss; | |
openlat::PrintHtk(fst, ss); | |
ofs << "#!" << key << "!#" << endl; | |
ofs << ss.str(); | |
n_done++; | |
} | |
KALDI_LOG << "Done converting " << n_done << " lattices to HTK SLF format"; | |
return (n_done != 0 ? 0 : 1); | |
} catch(const std::exception& e) { | |
std::cerr << e.what(); | |
return -1; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KALDI_ROOT= | |
OPENLAT_ROOT= | |
all: lattice-to-htk | |
ifndef KALDI_ROOT | |
$(error KALDI_ROOT is not set) | |
endif | |
ifndef OPENLAT_ROOT | |
$(error OPENLAT_ROOT is not set) | |
endif | |
include $(KALDI_ROOT)/src/kaldi.mk | |
CXXFLAGS+=-I${KALDI_ROOT}/src/ -I${OPENLAT_ROOT}/src/include/ | |
lattice-to-htk: lattice-to-htk.o | |
$(CXX) $^ -o $@ $(LDFLAGS) $(LDLIBS) $(KALDI_ROOT)/src/lat/kaldi-lat.a \ | |
$(KALDI_ROOT)/src/base/kaldi-base.a $(KALDI_ROOT)/src/util/kaldi-util.a | |
clean: | |
rm -rf *.o lattice-to-htk | |
%.o:%.cc | |
$(CXX) $(CXXFLAGS) -c $< | |
%.o:%.cpp | |
$(CXX) $(CXXFLAGS) -c $< |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following is the standard lattice
VERSION=1.0
UTTERANCE=session0_seg37.plp
lmname=....\sys_env\parse\lvcsr_standard/scripts/../plp_pitch_hlda_fmpe_mpe/input.decoding.lm/trigram.bin
lmscale=9.00 wdpenalty=-5.00
acscale=1.00
base=2.718282
vocab=....\sys_env\parse\lvcsr_standard/scripts/../plp_pitch_hlda_fmpe_mpe/input.lexicon/863.pdb
hmms=....\sys_env\parse\lvcsr_standard/scripts/../plp_pitch_hlda_fmpe_mpe/output.chunk/chunk_base.file
N=69 L=100
I=0 t=0.00 W=!NULL
I=1 t=0.72 W= v=0
I=2 t=1.08 W=nian2 v=0
I=3 t=1.24 W=ji2 v=0
I=4 t=1.62 W=da4 v=0
I=5 t=1.79 W= v=0
I=6 t=1.97 W=wo3 v=0
I=7 t=1.97 W=wo3 v=0
I=8 t=2.23 W=zhi2 v=0
I=9 t=2.24 W=chi1 v=0
I=10 t=2.54 W=dao4 v=0
I=11 t=2.54 W=dao4 v=0
I=12 t=2.57 W= v=0
I=13 t=2.57 W= v=0
I=14 t=2.57 W= v=0
I=15 t=2.57 W= v=0
I=16 t=2.57 W= v=0
I=17 t=2.57 W= v=0
I=18 t=2.57 W= v=0
I=19 t=2.57 W= v=0
I=20 t=2.57 W= v=0
I=21 t=2.57 W= v=0
I=22 t=2.57 W= v=0
I=23 t=2.57 W= v=0
I=24 t=2.57 W= v=0
I=25 t=2.57 W= v=0
I=26 t=2.57 W= v=0
I=27 t=2.57 W= v=0
I=28 t=2.57 W= v=0
I=29 t=2.57 W= v=0
I=30 t=2.57 W= v=0
I=31 t=2.57 W= v=0
I=32 t=2.57 W= v=0
I=33 t=2.57 W= v=0
I=34 t=2.57 W= v=0
I=35 t=2.57 W= v=0
I=36 t=2.57 W= v=0
I=37 t=2.57 W=da3 v=0
I=38 t=2.57 W=da4 v=0
I=39 t=2.57 W=da4 v=0
I=40 t=2.57 W=da4 v=0
I=41 t=2.57 W=da4 v=0
I=42 t=2.57 W=da4 v=0
I=43 t=2.57 W=da4 v=0
I=44 t=2.57 W=da4 v=0
I=45 t=2.57 W=da4 v=0
I=46 t=2.57 W=da4 v=0
I=47 t=2.57 W=dao3 v=0
I=48 t=2.57 W=dao3 v=0
I=49 t=2.57 W=dao4 v=0
I=50 t=2.57 W=dao4 v=0
I=51 t=2.57 W=dao4 v=0
I=52 t=2.57 W=dao4 v=0
I=53 t=2.57 W=dao4 v=0
I=54 t=2.57 W=dao4 v=0
I=55 t=2.57 W=dao4 v=0
I=56 t=2.57 W=dao4 v=0
I=57 t=2.57 W=dao4 v=0
I=58 t=2.57 W=dao4 v=0
I=59 t=2.57 W=dao4 v=0
I=60 t=2.57 W=dao4 v=0
I=61 t=2.57 W=dao4 v=0
I=62 t=2.57 W=dao4 v=0
I=63 t=2.57 W=dao4 v=0
I=64 t=2.57 W=dao4 v=0
I=65 t=2.57 W=dao4 v=0
I=66 t=2.57 W=dao4 v=0
I=67 t=2.57 W=dao4 v=0
I=68 t=2.57 W=!NULL
J=0 S=0 E=1 a=-3905.67 l=0.000
J=1 S=1 E=2 a=-2183.72 l=-6.650 d=:sil-n+ian2,10:n-ian2+j,10:
J=2 S=2 E=3 a=-968.65 l=-2.965 d=:ian2-j+i2,10:j-i2+d,10:
J=3 S=3 E=4 a=-2261.57 l=-2.544 d=:i2-d+a4,10:d-a4+sil,10:
J=4 S=4 E=5 a=-1066.94 l=-3.210
J=5 S=5 E=6 a=-1227.08 l=-6.677 d=:sil-uu+uo3,10:uu-uo3+zh,10:
J=6 S=5 E=7 a=-1233.81 l=-6.677 d=:sil-uu+uo3,10:uu-uo3+ch,10:
J=7 S=6 E=8 a=-1571.21 l=-4.123 d=:uo3-zh+ix2,10:zh-ix2+d,10:
J=8 S=7 E=9 a=-1634.01 l=-6.636 d=:uo3-ch+ix1,10:ch-ix1+d,10:
J=9 S=8 E=10 a=-1682.32 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=10 S=9 E=11 a=-1597.82 l=-5.891 d=:ix1-d+ao4,10:d-ao4+sil,10:
J=11 S=49 E=12 a=0.00 l=-3.570
J=12 S=38 E=13 a=0.00 l=-4.709
J=13 S=50 E=14 a=0.00 l=-3.570
J=14 S=51 E=15 a=0.00 l=-3.570
J=15 S=52 E=16 a=0.00 l=-3.570
J=16 S=53 E=17 a=0.00 l=-3.570
J=17 S=37 E=18 a=0.00 l=-3.499
J=18 S=39 E=19 a=0.00 l=-3.957
J=19 S=40 E=19 a=0.00 l=-4.709
J=20 S=41 E=20 a=0.00 l=-4.709
J=21 S=42 E=21 a=0.00 l=-3.957
J=22 S=43 E=21 a=0.00 l=-4.709
J=23 S=54 E=22 a=0.00 l=-3.570
J=24 S=55 E=23 a=0.00 l=-3.570
J=25 S=56 E=24 a=0.00 l=-3.570
J=26 S=47 E=25 a=0.00 l=-2.059
J=27 S=10 E=26 a=-253.45 l=-3.570
J=28 S=11 E=26 a=-253.45 l=-2.817
J=29 S=57 E=27 a=0.00 l=-3.570
J=30 S=58 E=27 a=0.00 l=-2.817
J=31 S=59 E=28 a=0.00 l=-3.570
J=32 S=44 E=29 a=0.00 l=-4.709
J=33 S=45 E=30 a=0.00 l=-3.957
J=34 S=46 E=30 a=0.00 l=-4.709
J=35 S=60 E=31 a=0.00 l=-3.570
J=36 S=61 E=32 a=0.00 l=-3.570
J=37 S=62 E=33 a=0.00 l=-3.570
J=38 S=63 E=33 a=0.00 l=-2.817
J=39 S=64 E=34 a=0.00 l=-3.570
J=40 S=65 E=34 a=0.00 l=-2.817
J=41 S=48 E=35 a=0.00 l=-2.059
J=42 S=66 E=36 a=0.00 l=-3.570
J=43 S=67 E=36 a=0.00 l=-2.817
J=44 S=8 E=37 a=-2024.56 l=-6.954 d=:ix2-d+a3,10:d-a3+sil,10:
J=45 S=9 E=38 a=-1950.14 l=-5.164 d=:ix1-d+a4,10:d-a4+sil,10:
J=46 S=8 E=39 a=-2019.83 l=-8.298 d=:ix2-d+a4,10:d-a4+sil,10:
J=47 S=9 E=40 a=-1946.52 l=-5.164 d=:ix1-d+a4,10:d-a4+sil,10:
J=48 S=9 E=41 a=-1953.20 l=-5.164 d=:ix1-d+a4,10:d-a4+sil,10:
J=49 S=8 E=42 a=-2017.16 l=-8.298 d=:ix2-d+a4,10:d-a4+sil,10:
J=50 S=9 E=43 a=-1943.11 l=-5.164 d=:ix1-d+a4,10:d-a4+sil,10:
J=51 S=9 E=44 a=-1920.14 l=-5.164 d=:ix1-d+a4,10:d-a4+sil,10:
J=52 S=8 E=45 a=-1919.38 l=-8.298 d=:ix2-d+a4,10:d-a4+sil,10:
J=53 S=9 E=46 a=-1846.06 l=-5.164 d=:ix1-d+a4,10:d-a4+sil,10:
J=54 S=8 E=47 a=-2036.09 l=-4.515 d=:ix2-d+ao3,10:d-ao3+sil,10:
J=55 S=8 E=48 a=-2049.56 l=-4.515 d=:ix2-d+ao3,10:d-ao3+sil,10:
J=56 S=8 E=49 a=-2030.92 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=57 S=8 E=50 a=-2028.20 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=58 S=8 E=51 a=-2015.47 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=59 S=8 E=52 a=-2060.80 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=60 S=8 E=53 a=-2024.13 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=61 S=8 E=54 a=-2013.79 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=62 S=8 E=55 a=-2026.69 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=63 S=8 E=56 a=-2058.83 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=64 S=8 E=57 a=-1971.04 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=65 S=9 E=58 a=-1891.54 l=-5.891 d=:ix1-d+ao4,10:d-ao4+sil,10:
J=66 S=8 E=59 a=-2025.64 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:sp,10:
J=67 S=8 E=60 a=-2053.55 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:sp,10:
J=68 S=8 E=61 a=-2022.72 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=69 S=8 E=62 a=-1991.95 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:sp,10:
J=70 S=9 E=63 a=-1913.86 l=-5.891 d=:ix1-d+ao4,10:d-ao4+sil,10:sp,10:
J=71 S=8 E=64 a=-1992.36 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:sp,10:
J=72 S=9 E=65 a=-1940.64 l=-5.891 d=:ix1-d+ao4,10:d-ao4+sil,10:
J=73 S=8 E=66 a=-1905.99 l=-4.042 d=:ix2-d+ao4,10:d-ao4+sil,10:
J=74 S=9 E=67 a=-1821.49 l=-5.891 d=:ix1-d+ao4,10:d-ao4+sil,10:
J=75 S=12 E=68 a=0.00 l=0.000
J=76 S=13 E=68 a=0.00 l=0.000
J=77 S=14 E=68 a=0.00 l=0.000
J=78 S=15 E=68 a=0.00 l=0.000
J=79 S=16 E=68 a=0.00 l=0.000
J=80 S=17 E=68 a=0.00 l=0.000
J=81 S=18 E=68 a=0.00 l=0.000
J=82 S=19 E=68 a=0.00 l=0.000
J=83 S=20 E=68 a=0.00 l=0.000
J=84 S=21 E=68 a=0.00 l=0.000
J=85 S=22 E=68 a=0.00 l=0.000
J=86 S=23 E=68 a=0.00 l=0.000
J=87 S=24 E=68 a=0.00 l=0.000
J=88 S=25 E=68 a=0.00 l=0.000
J=89 S=26 E=68 a=0.00 l=0.000
J=90 S=27 E=68 a=0.00 l=0.000
J=91 S=28 E=68 a=0.00 l=0.000
J=92 S=29 E=68 a=0.00 l=0.000
J=93 S=30 E=68 a=0.00 l=0.000
J=94 S=31 E=68 a=0.00 l=0.000
J=95 S=32 E=68 a=0.00 l=0.000
J=96 S=33 E=68 a=0.00 l=0.000
J=97 S=34 E=68 a=0.00 l=0.000
J=98 S=35 E=68 a=0.00 l=0.000
J=99 S=36 E=68 a=0.00 l=0.000