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


Speeding up Compile Times with Linux PS3

The PS3 doesn't have a huge amount of memory. Recently, when compiling a template heavy C++ library the memory swapping was causing incredibly slow build times.

I managed to get speed-up by creating a swap on Flash Card, as Root:

/sbin/mkswap /dev/sdc1
/sbin/swapoff -a
/sbin/swapon /dev/sdc1

This might be useful for a quick speedup, however, because there limited read/write cycles on flash memory devices, a better solution would be to setup the toolchain on another machine

OpenFst For Windows Getting Started

Requirements

This version is known to work under 32bit versions of Microsoft Windows XP or Vista. The Compiler requirements are Visual Studio 2008 (SP1) this includes free C++ express edition available here.

Obtaining

From the contrib section of the OpenFst site download the openfstwin.zip file.

Build

Unzip openfstwin.zip in the desired location (for example C:\code ). The files should now be in c:\code\openfstwin\ and from there open the solution file openfstwin.sln in Visual Studio. Select either the Debug or Release build and from the build menu run the Build Solution.

Using The Command Line Tools

The command lines tools and library should now be located in C:\code\openfstwin\Release or C:\code\openfstwin\debug.

 

Creating a Simple Application

Open Visual Studio and create a new C++ Project. From the New Project Dialog select Project type Win32 and template type Win32 Console Application.

newproject

On the next dialog. Uncheck Precompiled header and check Empty Project and Application Type: Console Application.

wizard

Add a new C++ file to the project.

newitem

Add some code, for example just paste the below

#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;
}


Add include and library paths to the project.



Either From the main menu go to Properties -> ProjectName(openfsttest) in this example. Or right click the project node in the solution explorer.



Go to Configuration Properties -> C/C++ -> General and add an additional include directory to the openfst src include directory. Be sure to set Configuraion -> All Configurations in the top left combo box



includeproj



Next: go to Linker input and add additional dependency to openfstwinlib.lib again set this for all configurations



libname



Finally, Linker->General add Additional Library Directory. Set the correct paths for the Release and Debug Configurations.



libpath 



Now compile either the Release or Debug from the build menu. It everything went smoothly there should be openfsttest.exe in the Release and or Debug directory. After invoking openfsttest.exe a file names test.ofst should be written to the same directly.



Running the fstprint.exe from the Openfst tools should give the following output:



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

今日の練習

何時もより寝坊して(僕にしたは)5:30目が覚めて、急いで朝ごはんを食べたり、準備したりで6:00発。

一人で和田峠行ってきました。走者時間4時間半、往復距離は約120KM。(ちょっと短い)

以上