利用Kaldi做语言模型的自适应

2019-01-03 Lu Huang 更多博文 » 博客 » GitHub »

Kaldi LM

原文链接 https://hlthu.github.io/2019/01/03/Kaldi-LM-Adaption.html
注:以下为加速网络访问所做的原文缓存,经过重新格式化,可能存在格式方面的问题,或偶有遗漏信息,请以原文为准。


最近需要做一个语言模型的自适应,所谓自适应,是指我已经有了一个通用的语言模型,比如 librispeech 的 tgsmall,但是我现在的任务大部分是朗读一段内容,我希望提高语言模型在解码中的比重,于是想利用新的语料训练一个新的语言模型,然后和之前的通用的融合。

1. 生成一个 arpa 语言模型

将下面的语料存为 corpus.txt:

I'D LIKE TO GIVE YOU SOME ADVICE ON MAKING FRIENDS IN SCHOOLS
FIRST JOIN A CLUB
THERE ARE MANY STUDENT CLUBS
IF YOU LIKE READING YOU MAY JOIN A BOOK CLUB
YOU WILL FIND PEOPLE LOVE READING BOOKS JUST LIKE YOU THERE
IF YOU LIKE SPORTS LET'S SAY BASKETBALL YOU CAN JOIN A BASKETBALL CLUB
YOU PLAY BASKETBALL WITH THE MEMBERS AND MAKE FRIENDS WITH THEM
IT'S EASY TO MAKE FRIENDS WITH SAME HOBBIES IN THE CLUBS
SECOND TRY TO BE POLITE AND HELPFUL ALL THE TIME NO ONE LIKES RUDE PEOPLE
YOU'D BETTER SAY PLEASE AND THANK YOU WHEN YOU SHOULD
AND MAKE SURE NOT TO TALK BAD THINGS ABOUT PEOPLE BEHIND THEM
THIS BAD HABIT WILL TURN PEOPLE AWAY FROM YOU WHEN SOMEBODY NEEDS YOUR HELP
PLEASE TRY YOUR BEST TO HELP HIM
THIS WAY YOU WILL BE SURPRISED TO SEE HOW MANY FRIENDS YOU WILL MAKE
I'D LIKE TO GIVE YOU SOME ADVICE ON MAKING FRIENDS IN SCHOOLS FIRST JOIN A CLUB THERE ARE MANY STUDENT CLUBS
IF YOU LIKE READING YOU MAY JOIN A BOOK CLUB YOU WILL FIND PEOPLE LOVE READING BOOKS JUST LIKE YOU THERE
IF YOU LIKE SPORTS LET'S SAY BASKETBALL YOU CAN JOIN A BASKETBALL CLUB YOU PLAY BASKETBALL WITH THE MEMBERS AND MAKE FRIENDS WITH THEM
IT'S EASY TO MAKE FRIENDS WITH SAME HOBBIES IN THE CLUBS SECOND TRY TO BE POLITE AND HELPFUL ALL THE TIME
NO ONE LIKES RUDE PEOPLE YOU'D BETTER SAY PLEASE AND THANK YOU WHEN YOU SHOULD AND MAKE SURE NOT TO TALK BAD THINGS ABOUT PEOPLE BEHIND THEM
THIS BAD HABIT WILL TURN PEOPLE AWAY FROM YOU WHEN SOMEBODY NEEDS YOUR HELP PLEASE TRY YOUR BEST TO HELP HIM
THIS WAY YOU WILL BE SURPRISED TO SEE HOW MANY FRIENDS YOU WILL MAKE
I'D LIKE TO GIVE YOU SOME ADVICE ON MAKING FRIENDS IN SCHOOLS
FIRST JOIN A CLUB THERE ARE MANY STUDENT CLUBS IF YOU LIKE READING YOU MAY JOIN A BOOK CLUB
YOU WILL FIND PEOPLE LOVE READING BOOKS JUST LIKE YOU THERE IF YOU LIKE SPORTS LET'S SAY BASKETBALL YOU CAN JOIN A BASKETBALL CLUB
YOU PLAY BASKETBALL WITH THE MEMBERS AND MAKE FRIENDS WITH THEM IT'S EASY TO MAKE FRIENDS WITH SAME HOBBIES IN THE CLUBS
SECOND TRY TO BE POLITE AND HELPFUL ALL THE TIME NO ONE LIKES RUDE PEOPLE YOU'D BETTER SAY PLEASE AND THANK YOU WHEN YOU SHOULD
AND MAKE SURE NOT TO TALK BAD THINGS ABOUT PEOPLE BEHIND THEM THIS BAD HABIT WILL TURN PEOPLE AWAY FROM YOU
WHEN SOMEBODY NEEDS YOUR HELP PLEASE TRY YOUR BEST TO HELP HIM THIS WAY YOU WILL BE SURPRISED TO SEE HOW MANY FRIENDS YOU WILL MAKE

然后生成一个 arpa 语言模型,并将这个语言模型转成 FST。

local=data/local
mkdir $local/tmp
ngram-count -order $lm_order -write-vocab $local/tmp/vocab-full.txt \
            -wbdiscount -text $local/corpus.txt -lm $local/tmp/lm.arpa
lang=data/lang_new
arpa2fst --disambig-symbol=#0 --read-symbol-table=$lang/words.txt \
                          $local/tmp/lm.arpa $lang/G.fst

2. 生成新的解码图

utils/mkgraph.sh data/lang_new exp/tri1 exp/tri1/graph_new
cp -r exp/tri1/graph_new exp/tri1/graph_fstunion
fstunion exp/tri1/graph_new/HCLG.fst exp/tri1/graph_old/HCLG.fst exp/tri1/graph_fstunion/HCLG.fst

然后就可以利用 graph_fstunion 解码了。