一、环境
- kaldi
- openfst1.6.4 http://www.openfst.org/twiki/bin/view/FST/FstDownload
- thrax1.2.4 http://openfst.cs.nyu.edu/twiki/bin/view/GRM/ThraxDownload
- 其他工具:有far2fst等 http://cs.jhu.edu/~jason/465/hw-ofst
二、流程
1.编写.grm文件
例如,编写一个简单中文颜色转英文单词的例子
import 'byte.grm' as b;
seven_color = (("绿色":"green") |
("红色":"red") |
("黄色":"yellow") |
("蓝色":"blue") |
("粉红色":"pink") |
("黑色":"black") |
("白色":"white")
);
export COLOR = CDRewrite[seven_color, "", "", b.kBytes*];
2.使用thraxmakedep生成thrax语法依赖关系,产生Makefile文件,对.grm语法进行集中make编译。
color.far: color.grm byte.far
thraxcompiler --input_grammar=$< --output_far=$@
byte.far: byte.grm
thraxcompiler --input_grammar=$< --output_far=$@
clean:
rm -f byte.far
3.在同.grm和MakeFile路径下使用make编译。
make
4.编译以后,会产生.far文件,即fst archieve文件。
5.接下来可以对产生的.far文件进行测试,测试使用thraxrewrite-tester。
- 一个个测试
thraxrewrite-tester --far=color.far --rules=COLOR
–rules是export导出的规则,该规则其实为一个fst对象。
Input string: 红色
Output string: red
Input string: 黑色
Output string: black
Input string: 蓝色
Output string: blue
Input string: 白色
Output string: white
按ctrl+D可以结束测试。
上面测试是一个个测试的,也可以集中测试
- 集中测试
cat testcase.txt | thraxrewrite-tester --far=color.far
- 可以使用grmtest、grmtest-with-symbols等进行集中编译、测试,这些工具位于【一、环境】下的bin/目录下工具
6.far2fst
使用far2fst转fst文件即可,可以使用
far2fst color.far
可以用fstinfo,fstdraw,fstprint等命令查看fst信息。
也可使用thrax的LoadFst函数去加载G.fst等fst文件,使用thraxrewrite-tester对输入输出进行测试。
7.得到.fst文件后,进行组合、优化、确定化等操作,得到HCLG.fst。
上述过程中的命令具体使用及可选参数可以查看相应官网或–help,其实整个流程也可以编写一个shell文件进行自动化操作。