// 通用可变FST// A vector FST is a general mutable FST StdVectorFstfst;// 将状态0添加到最初为空的FST,使其成为初始状态// Adds state 0 to the initially empty FST and make it the start state. fst.AddState();// 1st state will be state 0 (returned by AddState) fst.SetStart(0);// arg is state ID// 添加状态0的两条弧// Adds two arcs exiting state 0.// Arc constructor args: ilabel, olabel, weight, dest state ID. fst.AddArc(0,StdArc(1,1,0.5,1));// 1st arg is src state ID fst.AddArc(0,StdArc(2,2,1.5,1));// 添加状态1和它的一条弧// Adds state 1 and its arc. fst.AddState();fst.AddArc(1,StdArc(3,3,2.5,2));// 添加状态2并设置最终权重// Adds state 2 and set its final weight. fst.AddState();fst.SetFinal(2,3.5);// 1st arg is state ID, 2nd arg weight// save this FST to a filefst.Write("binary.fst");
# arc format: src dest ilabel olabel [weight]# final state format: state [weight]# lines may occur in any order except initial state must be first line# unspecified weights default to 0.0 (for the library-default Weight type) $ cat >text.fst <<EOF0 1 a x .50 1 b y 1.51 2 c z 2.52 3.5EOF
# Creates binary Fst from text file. # The symbolic labels will be converted into integers using the symbol table files. $ fstcompile --isymbols=isyms.txt --osymbols=osyms.txt text.fst binary.fst
# As above but the symbol tables are stored with the FST. $ fstcompile --isymbols=isyms.txt --osymbols=osyms.txt --keep_isymbols --keep_osymbols text.fst binary.fst
typedefStdArc::StateIdStateId;# Gets the initial state; if == kNoState => empty FST. StateIdinitial_state=fst.Start();# Get state i's final weight; if == Weight::Zero() => non-final. Weightweight=fst.Final(i);# 迭代FST状态# Iterates over the FSTs states. for(StateIterator<StdFst>siter(fst);!siter.Done();siter.Next())StateIdstate_id=siter.Value();# 迭代状态i的弧# Iterates over state i's arcs. for(ArcIterator<StdFst>aiter(fst,i);!aiter.Done();aiter.Next())constStdArc&arc=aiter.Value();# 迭代状态i中输入标签为l的弧# Iterates over state i's arcs that have input label l (FST must support this# in the simplest cases, true when the input labels are sorted). Matcher<StdFst>matcher(fst,MATCH_INPUT);matcher.SetState(i);if(matcher.Find(l))for(;!matcher.Done();matcher.Next())constStdArc&arc=matcher.Value();
#include<fst/fstlib.h>namespacefst{// Reads in an input FST. StdVectorFst*input=StdVectorFst::Read("input.fst");// Reads in the transduction model. StdVectorFst*model=StdVectorFst::Read("model.fst");// FST需要按照复合的维度进行排序// The FSTs must be sorted along the dimensions they will be joined.// In fact, only one needs to be so sorted.// This could have instead been done for "model.fst" when it was created. ArcSort(input,StdOLabelCompare());ArcSort(model,StdILabelCompare());// Container for composition result. StdVectorFstresult;// Creates the composed FST. Compose(*input,*model,&result);// Just keeps the output labels. Project(&result,PROJECT_OUTPUT);// Writes the result FST to a file.result.Write("result.fst");}
# FST需要按照复合的维度进行排序# The FSTs must be sorted along the dimensions they will be joined.# In fact, only one needs to be so sorted.# This could have instead been done for "model.fst" when it was created. $ fstarcsort --sort_type=olabel input.fst input_sorted.fst
$ fstarcsort --sort_type=ilabel model.fst model_sorted.fst
# Creates the composed FST. $ fstcompose input_sorted.fst model_sorted.fst comp.fst
# Just keeps the output label $ fstproject --project_output comp.fst result.fst
# Do it all in a single command line. $ fstarcsort --sort_type=ilabel model.fst | fstcompose input.fst - | fstproject --project_output result.fst