OpenFst Example Operator Overloading

Quick snippet to drop into programs using OpenFst that overloads the * and + operators to perform the Times and Plus. I find this more readable when adding/multiplying many times.

inline LogArc::Weight operator+(const LogArc::Weight& a, const LogArc::Weight& b)

{

    return Plus(a,b);

}; 

 

inline LogArc::Weight operator*(const LogArc::Weight& a, const LogArc::Weight& b)

{

    return Times(a,b);

}  

OpenFst Example: Using User Defined Flags

In this OpenFst sample I will show you how to create a tool in the style of the library tools and add a custom command option (user define flag) that does not require the compilation of the whole library.

The below testfst.cpp file calls parses the command options and calls a main function that essentially does nothing. The DEFINE_double macros is used to define our flag names test_flag in this case.

 

#include "test-main.h"

 

namespace fst

{

  REGISTER_FST_MAIN(TestMain, StdArc); 

  REGISTER_FST_MAIN(TestMain, LogArc);

 

DEFINE_double(myflag, 1234, "My Flag");

using fst::CallFstMain;

int main(int argc, char **argv)

{

  string usage = "Test flags \n\n  Usage: ";

  usage += argv[0];

  usage += "  Flags: myflag\n";

  std::set_new_handler(FailedNewHandler);

  SetFlags(usage.c_str(), &argc, &argv, true);

  if (argc !=1)

  {

    ShowUsage();

    return 1;

  }

  return CallFstMain("TestMain", argc, argv, FLAGS_arc_type);

}

The corresponding test-main.h files is the actual main function that just echo value of myflag a corresponding DECLAR_double macro.

#ifndef TEST_MAIN_H__

#define TEST_MAIN_H__

 

#include <fst/main.h>

DECLARE_double(myflag);

DECLARE_string(arc_type);

 

namespace fst

{

 

  template <class Arc>

  int TestMain(int argc, char **argv, istream&, const FstReadOptions&)

  {

 

    cout << "myflag=" << FLAGS_myflag << endl;

      return 0;

  }

#endif 

Next compile with:

g++ fsttest.cpp -lfstmain -lfst -ldl -o fsttest

Running the help option we see our new flag in the help options.

fsttest –help

..

..

--delta: type = double, default = 0.000976562
      Comparison/quantization delta
    --height: type = double, default = 11
      Set height
    --myflag: type = double, default = 1234
      My Flag
    --nodesep: type = double, default = 0.25
      Set minimum separation between nodes (see dot documentation)
    --ranksep: type = double, default = 0.4

..

And double check for a few values

$/fsttest --myflag=10
myflag=10

$ ./fsttest --myflag=-2.2134234
myflag=-2.21342

My two feature request are the shorthand versions, for example –i for –isymbols and that a tool only displays it relevant options.

Fst types avaliable in OpenFst

In OpenFst the command fstconvert is used to convert the fst type, either based on a stl vector container or const type. In version 1.1 a potentially very cool compact fst type was introduced.

Displaying the help by fstconvert –help displays the

Converts an FST to another type.

  Usage: fstconvert [in.fst [out.fst]]
  Flags: fst_type

  ……

  --fst_type: type = string, default = "vector"
      Output FST type

This wasn’t so helpful, so I added a quick Function to the FstRegister in include/fst/register.h

  void PrintFstNames()

  {

    for(typename map<string, Entry>::const_iterator it = fst_table_.begin(); it!=fst_table_.end(); ++it)

    {

      cout << it->first << endl;

    }

 

  }

The created a quick app to create the FstRegister  and print out the types.

 

#include <iostream>

#include <fst/main.h>

 

using namespace std;

using namespace fst;

 

 

int main()

{

        FstRegister<StdArc>* r = FstRegister<StdArc>::GetRegister();

        r->PrintFstNames();

        return 0;

}

Here are the list of the fst types for the StdArc (tropical semiring) should be the same for LogArc types. Unfortunately, no compact transducer though. The acceptor types might be useful for large language models.

compact16_acceptor
compact16_string
compact16_unweighted
compact16_unweighted_acceptor
compact16_weighted_string
compact64_acceptor
compact64_string
compact64_unweighted
compact64_unweighted_acceptor
compact64_weighted_string
compact8_acceptor
compact8_string
compact8_unweighted
compact8_unweighted_acceptor
compact8_weighted_string
compact_acceptor
compact_string
compact_unweighted
compact_unweighted_acceptor
compact_weighted_string
const
const16
const64
const8
vector