数据准备:http://www.statmt.org/europarl/
srilm工具包下载:https://github.com/BitSpeech/SRILM.git
对于srilm的安装可以搜索其他教程,网上有很多教程。
1、生成n-gram计数文件
bin/i686-m64/ngram-count -text /home/hml/github/bg-en/europarl-v7.bg-en.en -order 3 -write /home/hml/github/bg-en/europarl-v7.bg-en.en.counts
note:-text表示要读取的文件,就是我们的所有语音对应的文本文件,最好就是提前做一个数据清理(去掉标点符号,只保留纯文本)
-order 表示生成的是n-gram模型,词的最大长度为3
-write 将计数写入到某个文件中
下面是生成的counts文件中的几行:
Uncooperative 1 # 表示该词在文件中出现了一次
Uncooperative countries 1
Uncooperative countries of 1
propagate 6 # 表示该词在文件中出现了六次
propagate transplantation 1
propagate tarnsplantation and 1
propagate here 1
2、利用生成的n-gram计数文件,生成语言模型
bin/i686-m64/ngram-count -read /home/hml/github/bg-en/europarl-v7.bg-en.en.counts -order 3 -lm /home/hml/github/bg-en/europarl-v7.bg-en.en.lm -interpolate -kndiscount
note:-read表示读入计数文件
-lm是需要生成导出的lm文件
-interpolate 表示插值平滑
-kndiscout 表示kneser-ney打折
下面是生成的lm文件中的前几行:
\data\
ngram 1=115075 # 表示一元词的个数
ngram 2=1477406 # 表示二元词的个数
ngram 3=885946
\1-grams:
-5.880136 "A -0.1279694
-6.049498 "Adapting -0.1279694
-6.049498 "America -0.1279694
-6.049498 "BRICS” -0.1279694
-6.049498 "Competitiveness -0.1279694
-6.049498 "County -0.1279694
-6.049498 "Don't -0.1279694
-6.049498 "Ecobales” -0.1279694
-5.880136 "European -0.1279694
-6.049498 "Garden -0.1279694
-6.049498 "Health -0.1279694
-6.049498 "How -0.1279694
上面的每行都是三列,第一列是指 "A 出现的概率(注意这个地方没有去掉标点符号,词与词之间以空格进行分割),取以10为底的对数
这里可以验证一下,"A 在counts中出现的次数为2,一元词的总共次数为每一个词出现的次数的total,math.log10(2/total) 就是第一列的结果。
第三列表示回退权重(backoff weight)w,也是math.log10(w)
3、利用上一步生成的语言模型计算测试集的困惑度:
bin/i686-m64/ngram -ppl /home/hml/github/bg-en/europarl-v7.bg-en.en -order 3 -lm /home/hml/github/bg-en/europarl-v7.bg-en.en.lm > /home/hml/github/bg-en/europarl-v7.bg-en.en.ppl
下面是生成europarl-v7.bg-en.en.ppl文件的内容:
file /home/hml/github/bg-en/europarl-v7.bg-en.en: 406879 sentences, 9886401 words, 0 OOVs
0 zeroprobs, logprob= -1.76278e+07 ppl= 51.58865 ppl1= 60.67853
看以看到我们提供的文本文件中,406879 sentences, 9886401 words, 0 OOVs(out of vocabulary)
0 zeroprobs 没有0概率,代表没有句子没有出现过
logprob,log10(P(T)),P(T)所有句子的概率乘积
ppl:考虑句子和词
ppl1:仅考虑词
(ppl与ppl1均为困惑度)
europarl-v7.bg-en.en——训练数据集
europarl-v7.bg-en.en.counts——训练数据集三元词组词频统计文件
europarl-v7.bg-en.en.lm——三元语言模型
europarl-v7.bg-en.en.ppl——三元语言模型训练数据集困惑度测试结果
留言与评论(共有 0 条评论) “” |