Skip to content

Instantly share code, notes, and snippets.

@edobashira
Created August 18, 2014 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edobashira/8183f3ef2e7431345241 to your computer and use it in GitHub Desktop.
Save edobashira/8183f3ef2e7431345241 to your computer and use it in GitHub Desktop.
farcompose
#include<iostream>
#include <fst/compose.h>
#include <fst/shortest-path.h>
#include <fst/vector-fst.h>
#include <fst/extensions/far/far.h>
using namespace std;
using namespace fst;
int main(int argc, char **argv) {
string usage = "Apply an fst to far archieve.\n\n Usage: ";
usage += argv[0];
usage += "model.fst in.far [out.far]\n";
std::set_new_handler(FailedNewHandler);
SET_FLAGS(usage.c_str(), &argc, &argv, true);
if (argc < 3 || argc > 4) {
ShowUsage();
return 1;
}
StdFst *model = StdFst::Read(argv[1]);
if (!model)
LOG(FATAL) << "Failed to read model from : " << argv[1];
FarReader<StdArc> *reader = FarReader<StdArc>::Open(argv[2]);
if (!reader)
LOG(FATAL) << "Failed ro read FAR arhieve from : " << argv[2];
FarWriter<StdArc> *writer =
FarWriter<StdArc>::Create(argc == 4 ? argv[3] : "");
for (; !reader->Done(); reader->Next()) {
const StdFst &fst = reader->GetFst();
StdVectorFst cfst;
Compose(fst, *model, &cfst);
StdVectorFst ofst;
ShortestPath(cfst, &ofst);
writer->Add(reader->GetKey(), ofst);
}
delete model;
delete reader;
delete writer;
return 0;
}
all: farcompose
CXXFLAGS=-O2 -std=c++11
LDFLAGS=-L/usr/local/lib -L/usr/local/lib/fst
farcompose: farcompose.o
$(CXX) $^ -o $@ $(LDFLAGS) $(LDLIBS) -lfst -lfstfar
clean:
rm -rf *.o farcompose
%.o:%.cc ${includes}
$(CXX) $(CXXFLAGS) -c $<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment