Using OpenFst on Linux

The new version of OpenFst is awesome (reading the source code is highly recommended). The *nix installer makes it very simple to add use the library in your own code and building new tools in the style of the existing fst tools is also straightforward.

Using OpenFst Library

Download and install openfst-1.1: OpenFst-1.1 is essential as there was small bug in the openfst-1.0 installer

Make sure the libraries can be found: Assuming the default installation as root, the OpenFst libraries will be put in /usr/local/lib. To add these to the LD_LIBRARY_PATH either run the below command from the shell or in my case I add it to my .bash_profile.export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH

Build a simple application:

Lets use the code from the Windows walkthrough describe in this post. Create a file for example named openfstest.cpp and paste in the following code:

#include <fst/vector-fst.h>
using namespace fst;
int main()
{
  typedef StdVectorFst::StateId StateId;
  typedef StdVectorFst::Weight Weight;
  StdVectorFst ofst;
  StateId q = ofst.AddState();
  ofst.SetStart(q);
  for(int i=0; i!=10;++i)
  {
      StateId n = ofst.AddState();
      ofst.AddArc(q, StdArc(i+1, i+1, Weight::One(), n));
      q = n;
  }
  ofst.SetFinal(q,Weight::One());
  ofst.Write("test.ofst");
  return 0;
}


Next compile with the following command:



g++ openfsttest.cpp -o openfsttest -lfst



To use openfst we just need to add a reference to libfst by using the -lfst option.



Test the application:



If everything went correctly g++ should execute without any warnings or errors. Next invoke the application



./openfsttest



There should be a file named test.ofst which contains a simple machine. We can view the machine in text format using



fstprint test.ofst



Which should display the following:



0  1  1  1
1  2  2  2
2  3  3  3
3  4  4  4
4  5  5  5
5  6  6  6
6  7  7  7
7  8  8  8
8  9  9  9
9  10  10  10
10


The machine information can be displayed using


fstinfo test.ofst


Which should give the following:


fst type                                          vector
arc type                                          standard
input symbol table                                none
output symbol table                               none
# of states                                       11
# of arcs                                         10
initial state                                     0
# of final states                                 1
# of input/output epsilons                        0
# of input epsilons                               0
# of output epsilons                              0
# of accessible states                            11
# of coaccessible states                          11
# of connected states                             11
# of strongly conn components                     11
expanded                                          y
mutable                                           y
acceptor                                          y
input deterministic                               y
output deterministic                              y
input/output epsilons                             n
input epsilons                                    n
output epsilons                                   n
input label sorted                                y
output label sorted                               y
weighted                                          n
cyclic                                            n
cyclic at initial state                           n
top sorted                                        y
accessible                                        y
coaccessible                                      y
string                                            y


This entry was posted by Edobashira. Bookmark the permalink.

4 thoughts on “Using OpenFst on Linux”

Leave a Reply

Note: only a member of this blog may post a comment.