OpenFst的创建和读取

来源:互联网 发布:mac系统多大 编辑:程序博客网 时间:2024/05/17 21:49

http://www.openfst.org/twiki/bin/view/FST/FstQuickTour#CreatingFsts

一、可以通过两种方法创建, 一种是使用C++代码中定义的结构体和方法创建, 另一种是通过shell命令创建

1、使用C++代码:

StdVectorFst fst;fst.AddState();                           //第一个状态为0fst.SetStart(0);                          //0是状态的id//增加状态0的转移fst.AddArc(0, StdArc(1, 1, 0.5, 1));      //第一个参数是状态idfst.AddArc(0, StdArc(2, 2, 1.5, 1));//增加状态1和它的转移fst.AddState();fst.AddArc(1, StdArc(3, 3, 2.5, 2));//增加状态2和它的转移fst.AddState();fst.SetFinal(2, 3.5);                     //第一个参数是状态id, 第二个参数时权重fst.Write('binary.fst');                  //保存fst

2、通过shell命令方式创建

# cat > text.fst << EOF0 1 a x .50 1 b y 1.51 2 c z 2.52 3.5EOF#生成输入符号对应数字文件$ cat >isyms.txt <<EOF<eps> 0a 1b 2c 3EOF#生成输出符号对应数字文件  数字零对应的是fst中的epsilon符号$ cat >osyms.txt <<EOF<eps> 0x 1y 2z 3EOF#从三个文本文件中生成binary.fst$ fstcompile --isymbols=isyms.txt --osymbols=osyms.txt text.fst binary.fst#将symbol保留在fst中$ fstcompile --isymbols=isyms.txt --osymbols=osyms.txt --keep_isymbols --keep_osymbols text.fst binary.fst

生成的FST是这个样子
这里写图片描述

二、FST的读取同样可以使用这两种方式

1、C++代码读取

StdFst *fst = StdFst::Read("binary.fst")//Here is the standard representation of an arc:struct StdArc { typedef int Label; typedef TropicalWeight Weight; // see "FST Weights" below typedef int StateId; Label ilabel; Label olabel; Weight weight; StateId nextstate;};

2、shell方式读取FST

$ fstprint --isymbols=isyms.txt --osymbols=osyms.txt binary.fst text.fst# Draw FST using symbol table files and Graphviz dot:$ fstdraw --isymbols=isyms.txt --osymbols=osyms.txt binary.fst binary.dot$ dot -Tps binary.dot >binary.ps#查看fst信息$ fstinfo binary.fstfst type vectorarc type standardinput symbol table isyms.txtoutput symbol table osyms.txt# of states 3# of arcs 3initial state 0# of final states 1# of input/output epsilons 0# of input epsilons 0# of output epsilons 0# of accessible states 3# of coaccessible states 3# of connected states 3# of connected components 1# of strongly conn components 3input matcher youtput matcher yexpanded ymutable yacceptor yinput deterministic youtput deterministic yinput/output epsilons ninput epsilons noutput epsilons ninput label sorted youtput label sorted yweighted ycyclic ncyclic at initial state ntop sorted yaccessible ycoaccessible ystring n

未完待续。。。

原创粉丝点击