*Notice: Parts of this article may be outdated. There are many changes to its API and performance improvements recently in the java fst framework. Please refer to recent articles in Java FST Framework category for the latest info.
*

(originally posted at http://cmusphinx.sourceforge.net/2012/05/porting-openfst-to-java-part-2/)

**Foreword**

This article, the second in a series regarding, porting openFST to java, briefly presents some additional base classes and raise some issues regarding the java fst architecture in general and its compatibility with the original openFST binary format for saving models.

**1. FST java library base architecture**

The first article in the series [1] introduced the `Weight<T>`

class, the semiring operations related classes and interfaces and finally a solid class implementation for the tropical semiring based on float weights. In the last svn commit (revision 11363) [2] includes the `edu.cmu.sphinx.fst.weight.LogSemiring`

class which implements a logarithmic semiring based on Double weight values.

Furthermore revision 11363 includes the `edu.cmu.sphinx.fst.state.State<W extends Weight<?>>`

, `edu.cmu.sphinx.fst.arc.Arc<W extends Weight<?>>`

and `edu.cmu.sphinx.fst.fst.Fst<W extends Weight<?>>`

classes which implement the state, arc and fst functionality, respectively.

**2. Architecture design issues**

**2.1. Java generics support**

As described in the first part [1], the `edu.cmu.sphinx.fst.weight.Weight<T>`

acts basically as a wrapper for the weight’s value. The current implementation of State, Arc and Fst classes take as a type parameter any class that extends the Weight base class. Although this approach provides a great flexibility on building custom types of FSTs, the implementations can be greatly simplified if we assume only the base Weight class and modify the State, Arc and Fst classes definition to simply use a type parameter.

As an example the Arc class definition would be simplified to

1 2 3 4 5 6 7 8 | public class Arc<T> implements Serializable{ private static final long serialVersionUID = -7996802366816336109L; // Arc's weight protected Weight<T> weight; // Rest of the code..... } |

instead of its current definition

1 2 3 4 5 6 7 8 | public class Arc<W extends Weight<?>> implements Serializable{ private static final long serialVersionUID = -7996802366816336109L; // Arc's weight protected W weight; // Rest of the code..... } |

The proposed modification can be applied also to State and Fst classes and provide an easier to use api. In that case the construction of a basic FST in the class `edu.cmu.sphinx.fst.demos.basic.FstTest`

would be simplified as follows

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // ... Fst<Double> fst = new Fst<Double>(); // State 0 State<Double> s = new State<Double>(); s.AddArc(new Arc<Double>(new Weight<Double>(0.5), 1, 1, 1)); s.AddArc(new Arc<Double>(new Weight<Double>(1.5), 2, 2, 1)); fst.AddState(s); // State 1 s = new State<Double>(); s.AddArc(new Arc<Double>(new Weight<Double>(2.5), 3, 3, 2)); fst.AddState(s); // State 2 (final) s = new State<Double>(new Weight<Double>(3.5)); fst.AddState(s); // ... |

The code could be further simplified by completely dropping generics support in State, Arc and Fst classes by just providing solid implementations based on Weight weights.

**2.2. Compatibility with the original openFST binary format**

A second issue is the compatibility of the serialized binary format with the original openFST format. A compatible java library that is able to load/save openFST models, would provide us the ability to share trained models between various applications. As an example, in the case of ASR applications, trained models could be easily shared between between sphinx4 and kaldi [3] which is written in C++ and already uses the openFST library.

**References**

[1] J. Salatas, “Porting openFST to java: Part 1”, ICT Research Blog, May 2012.

[2] CMUSphinx g2p SVN repository

[3] Kaldi Speech recognition research toolkit , last accessed: 18/05/2012.

[4] C. Allauzen, M. Riley, J. Schalkwyk, W. Skut, M. Mohri, “OpenFst: a general and efficient weighted finite-state transducer library”, Proceedings of the 12th International Conference on Implementation and Application of Automata (CIAA 2007), pp. 11–23, Prague, Czech Republic, July 2007.