Skip to content

Instantly share code, notes, and snippets.

@edobashira
Created Mar 26, 2014
Embed
What would you like to do?
far-to-lattice utility for converting OpenFst FAR files to Kaldi tables.
/// far-to-lattice.cc
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author Paul Dixon (paul.r.dixon@mgmail.com)
#include <fst/script/arg-packs.h>
#include <fst/script/script-impl.h>
#include <fst/extensions/far/far.h>
#include <fst/extensions/far/main.h>
#include <fst/extensions/far/farscript.h>
#include "base/kaldi-common.h"
#include "lat/kaldi-lattice.h"
using namespace std;
using kaldi::LatticeWriter;
using kaldi::LatticeArc;
using kaldi::Lattice;
using kaldi::LatticeWeight;
namespace fst {
template<>
struct WeightConvert<TropicalWeight, LatticeWeight> {
LatticeWeight operator()(TropicalWeight w) const {
return w == TropicalWeight::Zero() ? LatticeWeight::Zero() :
LatticeWeight(w.Value(), 0);
}
};
namespace script {
typedef args::Package<const string&, const string&, const string&>
ToKaldiLatticeArgs;
template<class Arc>
void ToKaldiLattice(ToKaldiLatticeArgs* args) {
typedef typename Arc::StateId StateId;
typedef typename Arc::Label Label;
typedef typename Arc::Weight Weight;
FarReader<Arc>* reader = FarReader<Arc>::Open(args->arg1);
if (!reader) return;
WeightConvertMapper<Arc, LatticeArc> mapper;
LatticeWriter writer(args->arg3);
for (; !reader->Done(); reader->Next()) {
string key = reader->GetKey();
vector<string> fields;
kaldi::SplitStringToVector(key, "_", true, &fields);
const Fst<Arc> &fst = reader->GetFst();
Lattice lattice;
ArcMap(fst, &lattice, mapper);
writer.Write(fields.back(), lattice);
}
delete reader;
}
void ToKaldiLattice(const string& ifilename, const string& arc_type,
const string& ofilename) {
ToKaldiLatticeArgs args(ifilename, arc_type, ofilename);
Apply<Operation<ToKaldiLatticeArgs> >("ToKaldiLattice", arc_type, &args);
}
REGISTER_FST_OPERATION(ToKaldiLattice, StdArc, ToKaldiLatticeArgs);
REGISTER_FST_OPERATION(ToKaldiLattice, LatticeArc, ToKaldiLatticeArgs);
} // namespace script
REGISTER_FST(VectorFst, LatticeArc);
REGISTER_FST(ConstFst, LatticeArc);
} // namespace fst
using namespace fst;
int main(int argc, char **argv) {
namespace s = fst::script;
string usage = "Convert OpenFst far file to Kaldi Lattice table.\n\n Usage: ";
usage += argv[0];
usage += " in.fst [out.text]\n";
std::set_new_handler(FailedNewHandler);
SetFlags(usage.c_str(), &argc, &argv, true);
if (argc != 3) {
ShowUsage();
return 1;
}
string ifilename = (strcmp(argv[1], "-") != 0) ? argv[1] : "";
string ofilename = argv[2];
s::ToKaldiLattice(ifilename, fst::LoadArcTypeFromFar(ifilename), ofilename);
return 0;
}
all:
EXTRA_CXXFLAGS = -Wno-mismatched-tags -Wno-sign-compare \
-L../../tools/openfst-1.3.2/src/extensions/far/.libs \
-lfstfar -lfstfarscript
include ../kaldi.mk
BINFILES = far-to-lattice
OBJFILES =
TESTFILES =
ADDLIBS = ../lat/kaldi-lat.a ../hmm/kaldi-hmm.a ../tree/kaldi-tree.a \
../util/kaldi-util.a ../matrix/kaldi-matrix.a ../thread/kaldi-thread.a \
../base/kaldi-base.a
include ../makefiles/default_rules.mk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment