make_decode_graph.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. #!/bin/bash
  2. #
  3. if [ -f path.sh ]; then . path.sh; fi
  4. lm_dir=$1
  5. tgt_lang=$2
  6. arpa_lm=${lm_dir}/lm.arpa
  7. [ ! -f $arpa_lm ] && echo No such file $arpa_lm && exit 1;
  8. # Compose the language model to FST
  9. cat "$arpa_lm" | \
  10. grep -v '<s> <s>' | \
  11. grep -v '</s> <s>' | \
  12. grep -v '</s> </s>' | \
  13. grep -v -i '<unk>' | \
  14. arpa2fst --read-symbol-table=$tgt_lang/words.txt --keep-symbols=true - | fstprint | \
  15. fst/eps2disambig.pl | fst/s2eps.pl | fstcompile --isymbols=$tgt_lang/words.txt \
  16. --osymbols=$tgt_lang/words.txt --keep_isymbols=false --keep_osymbols=false | \
  17. fstrmepsilon | fstarcsort --sort_type=ilabel > $tgt_lang/G.fst
  18. echo "Checking how stochastic G is (the first of these numbers should be small):"
  19. fstisstochastic $tgt_lang/G.fst
  20. # Compose the token, lexicon and language-model FST into the final decoding graph
  21. fsttablecompose $tgt_lang/L.fst $tgt_lang/G.fst | fstdeterminizestar --use-log=true | \
  22. fstminimizeencoded | fstarcsort --sort_type=ilabel > $tgt_lang/LG.fst || exit 1;
  23. fsttablecompose $tgt_lang/T.fst $tgt_lang/LG.fst > $tgt_lang/TLG.fst || exit 1;
  24. echo "Composing decoding graph TLG.fst succeeded"
  25. rm -r $tgt_lang/LG.fst # We don't need to keep this intermediate FST