Debian Codegraph @

tools/build_lex

raw
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
41 if [ "x$FLITEDIR" = "x" ]
42 then
43 FLITEDIR=`pwd`/../..
44 fi
45
46 ## Change these two lines
47 LEXNAME=cmu_iq
48 LEXFILE=cmu_iq_lex.out
49
50 if [ $# = 0 ]
51 then
52 $0 setup
53 $0 lts
54 $0 lts2c
55 $0 lex
56 $0 compresslex
57 $0 install
58 exit
59 fi
60
61 if [ "$1" = "setup" ]
62 then
63 $FESTVOXDIR/src/lts/build_lts setup $LEXFILE utf8
64 mkdir c
65 mkdir wfst
66 cp -p $FESTVOXDIR/src/lts/build_lts .
67 cp -p $FESTVOXDIR/src/lts/build_lts_rules .
68 fi
69
70 if [ "$1" = "lts" ]
71 then
72 # Let's assume you have your allowables.scm already
73 ./build_lts make_allowables_smt
74 ./build_lts cummulate
75 ./build_lts align
76 ./build_lts build
77 ./build_lts merge
78 ./build_lts test
79 fi
80
81 if [ "$1" = "lts2c" ]
82 then
83 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/")';
84 festival --heap 10000000 -b $FLITEDIR/tools/make_lts.scm lts_scratch/lex_lts_rules.scm '(ltsregextoC "'$LEXNAME'" lex_lts_rules "wfst/" "c")';
85 fi
86
87 if [ "$1" = "lex" ]
88 then
89 # Find the words to prune from the built lexicon
90 # We will prune words that aren't homographs and the LTS gets correct
91 # Use the lts_test.log to find the failed entries and only
92 # include them in the list
93 # festival -b cmulex.scm ~/projects/flite/tools/make_lex.scm '(remove_predictable_entries "cmudict-0.4.out" "pruned_lex.scm" "lex_lts_rules.scm")'
94 # festival --heap 10000000 -b '(lex.compile "pruned_lex.scm" "pruned_lex.out")'
95 festival --heap 10000000 -b $FLITEDIR/tools/make_lex.scm '(lextoC "'$LEXNAME'" "lts_scratch/lex_entries.out" "c")' ;
96
97 fi
98
99 if [ "$1" = "lex2" ]
100 then
101 # experiment: put Letter_Phone(s) together as things to compress
102 # does give much smaller result, though needs to be less than 256 symbols
103 # not fully implemented
104 ( cd festival/lib/dicts/cmu;
105 # Use the lts_test.log to find the failed entries and only
106 # include them in the list
107 # this needs festival-1.96 or later to get the pos from the lts_test.log
108 grep "^failed " lts_test.log |
109 sed 's/(/( /g;s/)/ )/g' |
110 awk '{printf("( \"");
111 for (i=3; $i != ")"; i++)
112 printf("%s",$i);
113 i++;
114 printf("\" %s ( ",$i);
115 i++; i++
116 for ( ; $i != ")"; i++)
117 if ($i != "#")
118 printf("%s ",$i);
119 printf("))\n");
120 }' |
121 tr -d '()"' |
122 awk '{if ($2 == "nil")
123 printf("0_start ");
124 else
125 printf("%s_start ",$2);
126 for (i=1; i<=length($1); i++)
127 printf("%s_%s ",substr($1,i,1),$(i+2));
128 printf("\n");}' >pruned_lex2.data
129 # festival --heap 10000000 -b $FLITEDIR/tools/make_lex.scm '(lextoC "cmu" "pruned_lex.out" "c")' ;
130 )
131 fi
132
133 if [ "$1" = "compresslex" ]
134 then
135 # Compress the entries and phone strings by finding best ngrams
136 ( cd c;
137 $FLITEDIR/tools/huff_table phones ${LEXNAME}_lex_data ${LEXNAME}_lex_phones_huff_table.c
138 $FLITEDIR/tools/huff_table entries ${LEXNAME}_lex_data ${LEXNAME}_lex_entries_huff_table.c
139 paste huff.entries.compressed huff.phones.compressed huff.tmp.corpus |
140 tr -d " " |
141 awk 'BEGIN {pcount = 1;
142 printf("/* index to compressed data */\n");
143 }
144 function unenoctal(x)
145 {
146 y = ((substr(x,1,1)+0)*64) + ((substr(x,2,1)+0)*8) + (substr(x,3,1)+0);
147 return y;
148 }
149 {printf(" ");
150 for (i=length($2)-3; i>0; i-=4)
151 {
152 printf("%d,",unenoctal(substr($2,i+1,3)));
153 pcount++;
154 }
155 pcount++;
156 printf(" 255, /* %d %s */ ",pcount,$3);
157 for (i=1; i<length($1); i+=4)
158 {
159 printf("%d,",unenoctal(substr($1,i+1,3)));
160 pcount++;
161 }
162 printf("0,\n");
163 pcount++;
164 }
165 END { printf("/* num_bytes = %d */\n",pcount);}' >${LEXNAME}_lex_data_compressed.c
166 grep "num_bytes = " ${LEXNAME}_lex_data_compressed.c |
167 awk '{print $4}' >${LEXNAME}_lex_num_bytes_compressed.c
168 )
169 fi
170
171 if [ "$1" = "install" ]
172 then
173 cp -p c/${LEXNAME}_lex_data.c .
174 cp -p c/${LEXNAME}_lex_data_compressed.c ./${LEXNAME}_lex_data_raw.c
175 cp -p c/${LEXNAME}_lex_num_bytes_compressed.c ./${LEXNAME}_lex_num_bytes.c
176 cp -p c/${LEXNAME}_lex_entries.c .
177 cp -p c/${LEXNAME}_lex_phones_huff_table.c .
178 cp -p c/${LEXNAME}_lex_entries_huff_table.c .
179 cp -p c/${LEXNAME}_lts_model.c .
180 cp -p c/${LEXNAME}_lts_model.h .
181 cp -p c/${LEXNAME}_lts_rules.c .
182 fi
183
184