build_lex 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/bin/sh
  2. ###########################################################################
  3. ## ##
  4. ## Language Technologies Institute ##
  5. ## Carnegie Mellon University ##
  6. ## Copyright (c) 2004-2012 ##
  7. ## All Rights Reserved. ##
  8. ## ##
  9. ## Permission is hereby granted, free of charge, to use and distribute ##
  10. ## this software and its documentation without restriction, including ##
  11. ## without limitation the rights to use, copy, modify, merge, publish, ##
  12. ## distribute, sublicense, and/or sell copies of this work, and to ##
  13. ## permit persons to whom this work is furnished to do so, subject to ##
  14. ## the following conditions: ##
  15. ## 1. The code must retain the above copyright notice, this list of ##
  16. ## conditions and the following disclaimer. ##
  17. ## 2. Any modifications must be clearly marked as such. ##
  18. ## 3. Original authors' names are not deleted. ##
  19. ## 4. The authors' names are not used to endorse or promote products ##
  20. ## derived from this software without specific prior written ##
  21. ## permission. ##
  22. ## ##
  23. ## CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK ##
  24. ## DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ##
  25. ## ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT ##
  26. ## SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE ##
  27. ## FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ##
  28. ## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN ##
  29. ## AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ##
  30. ## ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF ##
  31. ## THIS SOFTWARE. ##
  32. ## ##
  33. ###########################################################################
  34. ## ##
  35. ## Make a flite lexicon from a Festival compiled lexicon ##
  36. ## ##
  37. ## This is offered as an example, though it's not robust ##
  38. ## ##
  39. ###########################################################################
  40. if [ "x$FLITEDIR" = "x" ]
  41. then
  42. FLITEDIR=`pwd`/../..
  43. fi
  44. ## Change these two lines
  45. LEXNAME=cmu_iq
  46. LEXFILE=cmu_iq_lex.out
  47. if [ $# = 0 ]
  48. then
  49. $0 setup
  50. $0 lts
  51. $0 lts2c
  52. $0 lex
  53. $0 compresslex
  54. $0 install
  55. exit
  56. fi
  57. if [ "$1" = "setup" ]
  58. then
  59. $FESTVOXDIR/src/lts/build_lts setup $LEXFILE utf8
  60. mkdir c
  61. mkdir wfst
  62. cp -p $FESTVOXDIR/src/lts/build_lts .
  63. cp -p $FESTVOXDIR/src/lts/build_lts_rules .
  64. fi
  65. if [ "$1" = "lts" ]
  66. then
  67. # Let's assume you have your allowables.scm already
  68. ./build_lts make_allowables_smt
  69. ./build_lts cummulate
  70. ./build_lts align
  71. ./build_lts build
  72. ./build_lts merge
  73. ./build_lts test
  74. fi
  75. if [ "$1" = "lts2c" ]
  76. then
  77. festival --heap 10000000 -b $FLITEDIR/tools/make_lts_wfst.scm lts_scratch/lex_lts_rules.scm '(lts_to_rg_to_wfst lex_lts_rules "wfst/")';
  78. festival --heap 10000000 -b $FLITEDIR/tools/make_lts.scm lts_scratch/lex_lts_rules.scm '(ltsregextoC "'$LEXNAME'" lex_lts_rules "wfst/" "c")';
  79. fi
  80. if [ "$1" = "lex" ]
  81. then
  82. # Find the words to prune from the built lexicon
  83. # We will prune words that aren't homographs and the LTS gets correct
  84. # Use the lts_test.log to find the failed entries and only
  85. # include them in the list
  86. # festival -b cmulex.scm ~/projects/flite/tools/make_lex.scm '(remove_predictable_entries "cmudict-0.4.out" "pruned_lex.scm" "lex_lts_rules.scm")'
  87. # festival --heap 10000000 -b '(lex.compile "pruned_lex.scm" "pruned_lex.out")'
  88. festival --heap 10000000 -b $FLITEDIR/tools/make_lex.scm '(lextoC "'$LEXNAME'" "lts_scratch/lex_entries.out" "c")' ;
  89. fi
  90. if [ "$1" = "lex2" ]
  91. then
  92. # experiment: put Letter_Phone(s) together as things to compress
  93. # does give much smaller result, though needs to be less than 256 symbols
  94. # not fully implemented
  95. ( cd festival/lib/dicts/cmu;
  96. # Use the lts_test.log to find the failed entries and only
  97. # include them in the list
  98. # this needs festival-1.96 or later to get the pos from the lts_test.log
  99. grep "^failed " lts_test.log |
  100. sed 's/(/( /g;s/)/ )/g' |
  101. awk '{printf("( \"");
  102. for (i=3; $i != ")"; i++)
  103. printf("%s",$i);
  104. i++;
  105. printf("\" %s ( ",$i);
  106. i++; i++
  107. for ( ; $i != ")"; i++)
  108. if ($i != "#")
  109. printf("%s ",$i);
  110. printf("))\n");
  111. }' |
  112. tr -d '()"' |
  113. awk '{if ($2 == "nil")
  114. printf("0_start ");
  115. else
  116. printf("%s_start ",$2);
  117. for (i=1; i<=length($1); i++)
  118. printf("%s_%s ",substr($1,i,1),$(i+2));
  119. printf("\n");}' >pruned_lex2.data
  120. # festival --heap 10000000 -b $FLITEDIR/tools/make_lex.scm '(lextoC "cmu" "pruned_lex.out" "c")' ;
  121. )
  122. fi
  123. if [ "$1" = "compresslex" ]
  124. then
  125. # Compress the entries and phone strings by finding best ngrams
  126. ( cd c;
  127. $FLITEDIR/tools/huff_table phones ${LEXNAME}_lex_data ${LEXNAME}_lex_phones_huff_table.c
  128. $FLITEDIR/tools/huff_table entries ${LEXNAME}_lex_data ${LEXNAME}_lex_entries_huff_table.c
  129. paste huff.entries.compressed huff.phones.compressed huff.tmp.corpus |
  130. tr -d " " |
  131. awk 'BEGIN {pcount = 1;
  132. printf("/* index to compressed data */\n");
  133. }
  134. function unenoctal(x)
  135. {
  136. y = ((substr(x,1,1)+0)*64) + ((substr(x,2,1)+0)*8) + (substr(x,3,1)+0);
  137. return y;
  138. }
  139. {printf(" ");
  140. for (i=length($2)-3; i>0; i-=4)
  141. {
  142. printf("%d,",unenoctal(substr($2,i+1,3)));
  143. pcount++;
  144. }
  145. pcount++;
  146. printf(" 255, /* %d %s */ ",pcount,$3);
  147. for (i=1; i<length($1); i+=4)
  148. {
  149. printf("%d,",unenoctal(substr($1,i+1,3)));
  150. pcount++;
  151. }
  152. printf("0,\n");
  153. pcount++;
  154. }
  155. END { printf("/* num_bytes = %d */\n",pcount);}' >${LEXNAME}_lex_data_compressed.c
  156. grep "num_bytes = " ${LEXNAME}_lex_data_compressed.c |
  157. awk '{print $4}' >${LEXNAME}_lex_num_bytes_compressed.c
  158. )
  159. fi
  160. if [ "$1" = "install" ]
  161. then
  162. cp -p c/${LEXNAME}_lex_data.c .
  163. cp -p c/${LEXNAME}_lex_data_compressed.c ./${LEXNAME}_lex_data_raw.c
  164. cp -p c/${LEXNAME}_lex_num_bytes_compressed.c ./${LEXNAME}_lex_num_bytes.c
  165. cp -p c/${LEXNAME}_lex_entries.c .
  166. cp -p c/${LEXNAME}_lex_phones_huff_table.c .
  167. cp -p c/${LEXNAME}_lex_entries_huff_table.c .
  168. cp -p c/${LEXNAME}_lts_model.c .
  169. cp -p c/${LEXNAME}_lts_model.h .
  170. cp -p c/${LEXNAME}_lts_rules.c .
  171. fi