Debian Codegraph @

tools/make_lex.scm

raw
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;; ;;;
3 ;;; Language Technologies Institute ;;;
4 ;;; Carnegie Mellon University ;;;
5 ;;; Copyright (c) 1999 ;;;
6 ;;; All Rights Reserved. ;;;
7 ;;; ;;;
8 ;;; Permission is hereby granted, free of charge, to use and distribute ;;;
9 ;;; this software and its documentation without restriction, including ;;;
10 ;;; without limitation the rights to use, copy, modify, merge, publish, ;;;
11 ;;; distribute, sublicense, and/or sell copies of this work, and to ;;;
12 ;;; permit persons to whom this work is furnished to do so, subject to ;;;
13 ;;; the following conditions: ;;;
14 ;;; 1. The code must retain the above copyright notice, this list of ;;;
15 ;;; conditions and the following disclaimer. ;;;
16 ;;; 2. Any modifications must be clearly marked as such. ;;;
17 ;;; 3. Original authors' names are not deleted. ;;;
18 ;;; 4. The authors' names are not used to endorse or promote products ;;;
19 ;;; derived from this software without specific prior written ;;;
20 ;;; permission. ;;;
21 ;;; ;;;
22 ;;; CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK ;;;
23 ;;; DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ;;;
24 ;;; ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT ;;;
25 ;;; SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE ;;;
26 ;;; FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ;;;
27 ;;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN ;;;
28 ;;; AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ;;;
29 ;;; ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF ;;;
30 ;;; THIS SOFTWARE. ;;;
31 ;;; ;;;
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ;;; Author: Alan W Black ([email protected]) ;;;
34 ;;; Date: December 1999 ;;;
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 ;;; ;;;
37 ;;; Generate a C compilable lexicon file from a Festival lexicon ;;;
38 ;;; ;;;
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40
41 (define (lextoC name ifile odir)
42 "(lextoc name ifile ofile)
43 Convert a (Festival) compiled lexicon file to a format suitable
44 For C to compile."
45 (let
46 (
47 ;; The general tables and indices
48 (ofde (fopen (path-append odir (string-append name "_lex_entries.c")) "w"))
49 ;; The lexical entries (words and pronunciations) for later compression
50 (ofdsd (fopen (path-append odir (string-append name "_lex_data")) "w"))
51 ;; number of bytes in data (that may get reset by compression)
52 (ofdi (fopen (path-append odir (string-append name "_lex_num_bytes.c")) "w"))
53 ;; data file for non-compression case
54 (ofddc (fopen (path-append odir (string-append name "_lex_data.c")) "w"))
55 (ofddrc (fopen (path-append odir (string-append name "_lex_data_raw.c")) "w"))
56 (ifd (fopen ifile "r"))
57 (phone_table))
58 (format ofde "/*******************************************************/\n")
59 (format ofde "/** generated lexicon entry file from %s */\n" name)
60 (format ofde "/*******************************************************/\n")
61 (format ofde "\n")
62 (format ofde "#include \"cst_string.h\"\n")
63 (format ofde "#include \"cst_lexicon.h\"\n")
64 (format ofde "\n")
65
66 (format ofde "const int %s_lex_num_bytes = \n" name)
67 (format ofde "#include \"%s_lex_num_bytes.c\"\n" name)
68 (format ofde ";\n")
69 (format ofde "\n")
70
71 (format ofddc "/*******************************************************/\n")
72 (format ofddc "/** generated lexicon data file from %s */\n" name)
73 (format ofddc "/*******************************************************/\n")
74 (format ofddc "\n")
75 (format ofddc "const unsigned char %s_lex_data[] = \n" name)
76 (format ofddc "{\n")
77 (format ofddc " 0,\n")
78 (format ofddc "#include \"%s_lex_data_raw.c\"\n" name)
79 (format ofddc"};\n")
80 (format ofddc "\n")
81
82 (format ofddrc "/** generated lexicon data file from %s uncompressed */\n" name)
83
84 (set! phone_table (l2C_dump_entries ifd ofdsd ofddrc ofdi))
85
86 ;; Number of entries
87 (format ofde "const int %s_lex_num_entries = %d;\n" name lex_num_entries)
88 (format ofde "\n")
89
90 ;; The phone table (bytes to phone names)
91 (format ofde "const char * const %s_lex_phone_table[%d] = \n"
92 name (+ 1 (length phone_table)))
93 (format ofde "{\n")
94 (mapcar (lambda (p) (format ofde " \"%s\",\n" p)) phone_table)
95 (format ofde " NULL\n")
96 (format ofde "};\n")
97 (format ofde "\n")
98
99 (format ofde "const char * const %s_lex_phones_huff_table[%d] = \n"
100 name 257)
101 (format ofde "{\n")
102 (format ofde " NULL, /* reserved */\n")
103 (format ofde "#include \"%s_lex_phones_huff_table.c\"\n" name)
104 (format ofde " NULL\n")
105 (format ofde "};\n")
106 (format ofde "\n")
107
108 (format ofde "const char * const %s_lex_entries_huff_table[%d] = \n"
109 name 257)
110 (format ofde "{\n")
111 (format ofde " NULL, /* reserved */\n")
112 (format ofde "#include \"%s_lex_entries_huff_table.c\"\n" name)
113 (format ofde " NULL\n")
114 (format ofde "};\n")
115
116 ; ;; The register function -- now hand coded else where
117 ; (format ofde "\n")
118 ; (format ofde "void register_lex_%s()\n" name)
119 ; (format ofde "{\n")
120 ; (format ofde " lexicon *lex = new_lexicon();\n")
121 ; (format ofde " lex->name = cst_strdup(\"%s\");\n" name)
122 ; (format ofde " lex->num_entries = %s_num_entries;\n" name)
123 ; (format ofde " lex->entry_index = %s_lex_entry;\n" name)
124 ; (format ofde " lex->phones = %s_lex_phones;\n" name)
125 ; (format ofde " lex->phone_table = %s_phone_table;\n" name)
126 ; (format ofde " lexicon_register(lex);\n")
127 ; (format ofde "}\n")
128 (format ofde "\n")
129
130 (fclose ofde)
131 (fclose ofdsd)
132 (fclose ofdi)
133 (fclose ofddc)
134 (fclose ofddrc)
135 ))
136
137 (define (enoctal x)
138 "(enoctal x)
139 Return number as three digit octal"
140 (let (units eights sixtyfours)
141 (set! units (% x 8))
142 (set! eights (% (/ (- x units) 8) 8))
143 (set! sixtyfours (% (/ (- (/ (- x units) 8) eights) 8) 8))
144 (format nil "%d%d%d" sixtyfours eights units)))
145
146 (define (l2C_dump_entries ifd ofdsd ofddrc ofdi)
147 "(l2C_dump_entries ifd ofde)
148 We dump the entries and pronunciation in simple strings to ofdsd which
149 is used for compression later, and also dump the entries to ofddrc
150 with their index to ofdi in the format they will be used in. For
151 compressions sake we dump the prunciations in reverse (sharing the
152 null terminator with the previous entry and the entry itself forward.
153 The index points at the start of the entry (so you have to go backwards to
154 get the prunciation."
155 (let ((phone_table (list "_epsilon_" ))
156 (entry)
157 (entry_count 0)
158 (pos)
159 (pcount 0))
160 ; (if (not (string-equal "MNCL" (readfp ifd)))
161 ; (error "L2C: input file is not a compiled lexicon\n"))
162 (set! pcount (+ 1 pcount)) ;; the initial 0
163 (while (not (equal? (set! entry (readfp ifd)) (eof-val)))
164 (if (not (car (cdr entry)))
165 (set! pos "0")
166 (set! pos (substring
167 (string-append (car (cdr entry))) 0 1)))
168 (format t "entry: %l\n" entry)
169 (if (or (car (cdr entry)) ;; hmm not sure of these conditions
170 ; (< (length (car entry)) 5) ;; awb 28/12/2004
171 (not (equal? (car (cdr (cdr (cdr entry))))
172 (l2C_phonetize (car (cdr (cdr entry)))))))
173 (begin
174 ;; Lexical entry
175 (set! entry_count (+ 1 entry_count))
176 (if (consp (car entry))
177 (set! simple_entry
178 (format nil
179 "%s%s"
180 pos
181 (apply string-append (car entry))))
182 (set! simple_entry (format nil "%s%s" pos (car entry))))
183 (set! phone_list (l2C_phonetize (car (cdr (cdr entry)))))
184
185 ;; To the data.c file
186 (format ofddrc " ")
187 (mapcar
188 (lambda (p)
189 (format ofddrc "%d," (l2C_phone_index p 0 phone_table))
190 (set! pcount (+ 1 pcount)))
191 (reverse phone_list))
192 (set! pcount (+ 1 pcount))
193 (format ofddrc " 255, /* %s %d */ " simple_entry pcount)
194 (mapcar
195 (lambda (l)
196 (if (string-equal l "'")
197 (format ofddrc "'\\%s'," l) ;; unpacked is "readable"
198 (format ofddrc "'%s'," l))) ;; unpacked is "readable"
199 (symbolexplode simple_entry))
200 (format ofddrc "0,\n") ;; terminator for entry
201 (set! pcount (+ 1 pcount))
202
203 ;; to the data file for potential other compression
204 (format ofdsd "%s " simple_entry)
205 (mapcar
206 (lambda (p)
207 (format ofdsd "\\%s"
208 (enoctal (l2C_phone_index p 0 phone_table))))
209 phone_list)
210 (format ofdsd "\n")
211 )
212 (format t " skipped\n")))
213 (format ofdi " %d\n" pcount)
214 (set! lex_num_entries entry_count) ;; shouldn't be a global
215 phone_table))
216
217 (define (l2C_phone_index p n table)
218 (cond
219 ((string-equal p (car table))
220 n)
221 ((not (cdr table)) ;; new p
222 (set-cdr! table (list p))
223 (+ 1 n))
224 (t
225 (l2C_phone_index p (+ 1 n) (cdr table)))))
226
227 ;; Should be a better way to do this
228 (set! vowels
229 '(
230 ;; radio (CMULEX)
231 aa ae ah ao aw ax axr ay eh el em en er ey ih iy ow oy uh uw
232 ;; mrpa (OALD)
233 uh e a o i u ii uu oo aa @@ ai ei oi au ou e@ i@ u@ @
234 ;; ogi_worldbet
235 i: I E @ u U ^ & > A 3r ei aI >i iU aU oU
236 ))
237
238 (define (is_a_vowel p)
239 ; (if (> (car (cdr festival_version_number)) 4)
240 ; (string-equal "+" (Param.get (format nil "phoneset.%s.vc" p)))
241 ; (string-equal "+"
242 ; (car (cdr
243 ; (assoc_string p
244 ; (car (cdr
245 ; (assoc 'phones
246 ; (PhoneSet.description '(phones)))))))))
247 (member_string p vowels)
248 ; )
249 )
250
251 (define (l2C_phonetize syls)
252 "(l2C_phonetize syls)
253 Return simple list of atomic phone/stress values"
254 (if (consp (car syls))
255 ;; syllabified entries
256 (apply
257 append
258 (mapcar
259 (lambda (syl)
260 (mapcar
261 (lambda (p)
262 (if (is_a_vowel p)
263 (intern (string-append p (car (cdr syl))))
264 p))
265 (car syl)))
266 syls))
267 ;; Flat entries (from lts_test maybe)
268 syls))
269
270 (define (L2C_make_phone_index_tree name ifile ofile)
271 "(L2C_make_phone_index_tree name ifile ofile)
272 Build a regular grammar for the words in this lexicon in order
273 to build a more efficent representation of the pronunciations."
274 (let ((ifd (fopen ifile "r"))
275 (ofd (fopen ofile "w"))
276 entry)
277 (if (not (string-equal "MNCL" (readfp ifd)))
278 (error "L2C: input file is not a compiled lexicon\n"))
279 (format ofd "(TreeLexicon\n")
280 (format ofd " %s\n" name)
281 (format ofd " nil\n")
282 (format ofd " (\n")
283 (while (not (equal? (set! entry (readfp ifd)) (eof-val)))
284 (format ofd " ( ")
285 (mapcar
286 (lambda (l)
287 (format ofd "%s " l))
288 (reverse (symbolexplode (car entry))))
289 (if (car (cdr entry))
290 (format ofd "pos_%s " (car (cdr entry)))
291 (format ofd "0 "))
292 (mapcar
293 (lambda (l)
294 (format ofd "%s " l))
295 (l2C_phonetize (car (cdr (cdr entry)))))
296 (format ofd " -> W 1.0)\n"))
297 (format ofd " ))\n")
298 ))
299
300 (define (remove_predictable_entries infile outfile ltsfile)
301 "(remove_predictable_entries infile outfile ltsfile)
302 Check each entry in infile against the lts rules. Output those entries that
303 are predicable and not homographs."
304 (let ((fd (fopen infile "r"))
305 (ofd (fopen outfile "w"))
306 (entry)
307 (wordcount 0)
308 (correctwords 0))
309 ;; Use a new lexicon and not get confused with any installed ont
310 (lex.create "g0006")
311 (lex.set.compile.file infile)
312 (lex.set.phoneset "radio")
313 (lex.set.lts.method 'cmu_lts_function)
314 (lex.select "g0006")
315 (load ltsfile)
316 (set! cmu_lts_rules lex_lts_rules)
317
318 (while (not (equal? (set! entry (readfp fd)) (eof-val)))
319 (if (consp entry)
320 (let ((lts (cmu_lts_function (car entry) (car (cdr entry))))
321 (lexentries (lex.lookup_all (car entry))))
322 (if (or (> (length lexentries) 1)
323 (not (equal? (nth 2 entry) (nth 2 lts))))
324 (begin
325 (format ofd "( \"%s\" %l (" (car entry) (cadr entry))
326 (mapcar
327 (lambda (syl)
328 (mapcar
329 (lambda (seg)
330 (cond
331 ((string-matches seg "[aeiouAEIOU@].*")
332 (format ofd "%s " (string-append seg (cadr syl))))
333 (t
334 (format ofd "%s " seg))))
335 (car syl)))
336 (car (cddr entry)))
337 (format ofd "))\n"))
338 (set! correctwords (+ 1 correctwords)))
339 (set! wordcount (+ 1 wordcount))
340 )
341 )
342 )
343 (format t "From %d words %d correct %d exceptions\n"
344 wordcount correctwords (- wordcount correctwords))
345 )
346 )
347
348 (define (utf8entries infile outfile)
349 (let (ifd ofd entry)
350 (set! ifd (fopen infile "r"))
351 (set! ofd (fopen outfile "w"))
352 (while (not (equal? (set! entry (readfp ifd)) (eof-val)))
353 (mapcar
354 (lambda (l) (format ofd "%s " l))
355 (utf8explode entry))
356 (format ofd "\n"))
357 (fclose ifd)
358 (fclose ifd)))
359
360 (provide 'make_lex)
361
362
363
364