make_lex.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 (awb@cs.cmu.edu) ;;;
  34. ;;; Date: December 1999 ;;;
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ;;; ;;;
  37. ;;; Generate a C compilable lexicon file from a Festival lexicon ;;;
  38. ;;; ;;;
  39. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  40. (define (lextoC name ifile odir)
  41. "(lextoc name ifile ofile)
  42. Convert a (Festival) compiled lexicon file to a format suitable
  43. For C to compile."
  44. (let
  45. (
  46. ;; The general tables and indices
  47. (ofde (fopen (path-append odir (string-append name "_lex_entries.c")) "w"))
  48. ;; The lexical entries (words and pronunciations) for later compression
  49. (ofdsd (fopen (path-append odir (string-append name "_lex_data")) "w"))
  50. ;; number of bytes in data (that may get reset by compression)
  51. (ofdi (fopen (path-append odir (string-append name "_lex_num_bytes.c")) "w"))
  52. ;; data file for non-compression case
  53. (ofddc (fopen (path-append odir (string-append name "_lex_data.c")) "w"))
  54. (ofddrc (fopen (path-append odir (string-append name "_lex_data_raw.c")) "w"))
  55. (ifd (fopen ifile "r"))
  56. (phone_table))
  57. (format ofde "/*******************************************************/\n")
  58. (format ofde "/** generated lexicon entry file from %s */\n" name)
  59. (format ofde "/*******************************************************/\n")
  60. (format ofde "\n")
  61. (format ofde "#include \"cst_string.h\"\n")
  62. (format ofde "#include \"cst_lexicon.h\"\n")
  63. (format ofde "\n")
  64. (format ofde "const int %s_lex_num_bytes = \n" name)
  65. (format ofde "#include \"%s_lex_num_bytes.c\"\n" name)
  66. (format ofde ";\n")
  67. (format ofde "\n")
  68. (format ofddc "/*******************************************************/\n")
  69. (format ofddc "/** generated lexicon data file from %s */\n" name)
  70. (format ofddc "/*******************************************************/\n")
  71. (format ofddc "\n")
  72. (format ofddc "const unsigned char %s_lex_data[] = \n" name)
  73. (format ofddc "{\n")
  74. (format ofddc " 0,\n")
  75. (format ofddc "#include \"%s_lex_data_raw.c\"\n" name)
  76. (format ofddc"};\n")
  77. (format ofddc "\n")
  78. (format ofddrc "/** generated lexicon data file from %s uncompressed */\n" name)
  79. (set! phone_table (l2C_dump_entries ifd ofdsd ofddrc ofdi))
  80. ;; Number of entries
  81. (format ofde "const int %s_lex_num_entries = %d;\n" name lex_num_entries)
  82. (format ofde "\n")
  83. ;; The phone table (bytes to phone names)
  84. (format ofde "const char * const %s_lex_phone_table[%d] = \n"
  85. name (+ 1 (length phone_table)))
  86. (format ofde "{\n")
  87. (mapcar (lambda (p) (format ofde " \"%s\",\n" p)) phone_table)
  88. (format ofde " NULL\n")
  89. (format ofde "};\n")
  90. (format ofde "\n")
  91. (format ofde "const char * const %s_lex_phones_huff_table[%d] = \n"
  92. name 257)
  93. (format ofde "{\n")
  94. (format ofde " NULL, /* reserved */\n")
  95. (format ofde "#include \"%s_lex_phones_huff_table.c\"\n" name)
  96. (format ofde " NULL\n")
  97. (format ofde "};\n")
  98. (format ofde "\n")
  99. (format ofde "const char * const %s_lex_entries_huff_table[%d] = \n"
  100. name 257)
  101. (format ofde "{\n")
  102. (format ofde " NULL, /* reserved */\n")
  103. (format ofde "#include \"%s_lex_entries_huff_table.c\"\n" name)
  104. (format ofde " NULL\n")
  105. (format ofde "};\n")
  106. ; ;; The register function -- now hand coded else where
  107. ; (format ofde "\n")
  108. ; (format ofde "void register_lex_%s()\n" name)
  109. ; (format ofde "{\n")
  110. ; (format ofde " lexicon *lex = new_lexicon();\n")
  111. ; (format ofde " lex->name = cst_strdup(\"%s\");\n" name)
  112. ; (format ofde " lex->num_entries = %s_num_entries;\n" name)
  113. ; (format ofde " lex->entry_index = %s_lex_entry;\n" name)
  114. ; (format ofde " lex->phones = %s_lex_phones;\n" name)
  115. ; (format ofde " lex->phone_table = %s_phone_table;\n" name)
  116. ; (format ofde " lexicon_register(lex);\n")
  117. ; (format ofde "}\n")
  118. (format ofde "\n")
  119. (fclose ofde)
  120. (fclose ofdsd)
  121. (fclose ofdi)
  122. (fclose ofddc)
  123. (fclose ofddrc)
  124. ))
  125. (define (enoctal x)
  126. "(enoctal x)
  127. Return number as three digit octal"
  128. (let (units eights sixtyfours)
  129. (set! units (% x 8))
  130. (set! eights (% (/ (- x units) 8) 8))
  131. (set! sixtyfours (% (/ (- (/ (- x units) 8) eights) 8) 8))
  132. (format nil "%d%d%d" sixtyfours eights units)))
  133. (define (l2C_dump_entries ifd ofdsd ofddrc ofdi)
  134. "(l2C_dump_entries ifd ofde)
  135. We dump the entries and pronunciation in simple strings to ofdsd which
  136. is used for compression later, and also dump the entries to ofddrc
  137. with their index to ofdi in the format they will be used in. For
  138. compressions sake we dump the prunciations in reverse (sharing the
  139. null terminator with the previous entry and the entry itself forward.
  140. The index points at the start of the entry (so you have to go backwards to
  141. get the prunciation."
  142. (let ((phone_table (list "_epsilon_" ))
  143. (entry)
  144. (entry_count 0)
  145. (pos)
  146. (pcount 0))
  147. ; (if (not (string-equal "MNCL" (readfp ifd)))
  148. ; (error "L2C: input file is not a compiled lexicon\n"))
  149. (set! pcount (+ 1 pcount)) ;; the initial 0
  150. (while (not (equal? (set! entry (readfp ifd)) (eof-val)))
  151. (if (not (car (cdr entry)))
  152. (set! pos "0")
  153. (set! pos (substring
  154. (string-append (car (cdr entry))) 0 1)))
  155. (format t "entry: %l\n" entry)
  156. (if (or (car (cdr entry)) ;; hmm not sure of these conditions
  157. ; (< (length (car entry)) 5) ;; awb 28/12/2004
  158. (not (equal? (car (cdr (cdr (cdr entry))))
  159. (l2C_phonetize (car (cdr (cdr entry)))))))
  160. (begin
  161. ;; Lexical entry
  162. (set! entry_count (+ 1 entry_count))
  163. (if (consp (car entry))
  164. (set! simple_entry
  165. (format nil
  166. "%s%s"
  167. pos
  168. (apply string-append (car entry))))
  169. (set! simple_entry (format nil "%s%s" pos (car entry))))
  170. (set! phone_list (l2C_phonetize (car (cdr (cdr entry)))))
  171. ;; To the data.c file
  172. (format ofddrc " ")
  173. (mapcar
  174. (lambda (p)
  175. (format ofddrc "%d," (l2C_phone_index p 0 phone_table))
  176. (set! pcount (+ 1 pcount)))
  177. (reverse phone_list))
  178. (set! pcount (+ 1 pcount))
  179. (format ofddrc " 255, /* %s %d */ " simple_entry pcount)
  180. (mapcar
  181. (lambda (l)
  182. (if (string-equal l "'")
  183. (format ofddrc "'\\%s'," l) ;; unpacked is "readable"
  184. (format ofddrc "'%s'," l))) ;; unpacked is "readable"
  185. (symbolexplode simple_entry))
  186. (format ofddrc "0,\n") ;; terminator for entry
  187. (set! pcount (+ 1 pcount))
  188. ;; to the data file for potential other compression
  189. (format ofdsd "%s " simple_entry)
  190. (mapcar
  191. (lambda (p)
  192. (format ofdsd "\\%s"
  193. (enoctal (l2C_phone_index p 0 phone_table))))
  194. phone_list)
  195. (format ofdsd "\n")
  196. )
  197. (format t " skipped\n")))
  198. (format ofdi " %d\n" pcount)
  199. (set! lex_num_entries entry_count) ;; shouldn't be a global
  200. phone_table))
  201. (define (l2C_phone_index p n table)
  202. (cond
  203. ((string-equal p (car table))
  204. n)
  205. ((not (cdr table)) ;; new p
  206. (set-cdr! table (list p))
  207. (+ 1 n))
  208. (t
  209. (l2C_phone_index p (+ 1 n) (cdr table)))))
  210. ;; Should be a better way to do this
  211. (set! vowels
  212. '(
  213. ;; radio (CMULEX)
  214. aa ae ah ao aw ax axr ay eh el em en er ey ih iy ow oy uh uw
  215. ;; mrpa (OALD)
  216. uh e a o i u ii uu oo aa @@ ai ei oi au ou e@ i@ u@ @
  217. ;; ogi_worldbet
  218. i: I E @ u U ^ & > A 3r ei aI >i iU aU oU
  219. ))
  220. (define (is_a_vowel p)
  221. ; (if (> (car (cdr festival_version_number)) 4)
  222. ; (string-equal "+" (Param.get (format nil "phoneset.%s.vc" p)))
  223. ; (string-equal "+"
  224. ; (car (cdr
  225. ; (assoc_string p
  226. ; (car (cdr
  227. ; (assoc 'phones
  228. ; (PhoneSet.description '(phones)))))))))
  229. (member_string p vowels)
  230. ; )
  231. )
  232. (define (l2C_phonetize syls)
  233. "(l2C_phonetize syls)
  234. Return simple list of atomic phone/stress values"
  235. (if (consp (car syls))
  236. ;; syllabified entries
  237. (apply
  238. append
  239. (mapcar
  240. (lambda (syl)
  241. (mapcar
  242. (lambda (p)
  243. (if (is_a_vowel p)
  244. (intern (string-append p (car (cdr syl))))
  245. p))
  246. (car syl)))
  247. syls))
  248. ;; Flat entries (from lts_test maybe)
  249. syls))
  250. (define (L2C_make_phone_index_tree name ifile ofile)
  251. "(L2C_make_phone_index_tree name ifile ofile)
  252. Build a regular grammar for the words in this lexicon in order
  253. to build a more efficent representation of the pronunciations."
  254. (let ((ifd (fopen ifile "r"))
  255. (ofd (fopen ofile "w"))
  256. entry)
  257. (if (not (string-equal "MNCL" (readfp ifd)))
  258. (error "L2C: input file is not a compiled lexicon\n"))
  259. (format ofd "(TreeLexicon\n")
  260. (format ofd " %s\n" name)
  261. (format ofd " nil\n")
  262. (format ofd " (\n")
  263. (while (not (equal? (set! entry (readfp ifd)) (eof-val)))
  264. (format ofd " ( ")
  265. (mapcar
  266. (lambda (l)
  267. (format ofd "%s " l))
  268. (reverse (symbolexplode (car entry))))
  269. (if (car (cdr entry))
  270. (format ofd "pos_%s " (car (cdr entry)))
  271. (format ofd "0 "))
  272. (mapcar
  273. (lambda (l)
  274. (format ofd "%s " l))
  275. (l2C_phonetize (car (cdr (cdr entry)))))
  276. (format ofd " -> W 1.0)\n"))
  277. (format ofd " ))\n")
  278. ))
  279. (define (remove_predictable_entries infile outfile ltsfile)
  280. "(remove_predictable_entries infile outfile ltsfile)
  281. Check each entry in infile against the lts rules. Output those entries that
  282. are predicable and not homographs."
  283. (let ((fd (fopen infile "r"))
  284. (ofd (fopen outfile "w"))
  285. (entry)
  286. (wordcount 0)
  287. (correctwords 0))
  288. ;; Use a new lexicon and not get confused with any installed ont
  289. (lex.create "g0006")
  290. (lex.set.compile.file infile)
  291. (lex.set.phoneset "radio")
  292. (lex.set.lts.method 'cmu_lts_function)
  293. (lex.select "g0006")
  294. (load ltsfile)
  295. (set! cmu_lts_rules lex_lts_rules)
  296. (while (not (equal? (set! entry (readfp fd)) (eof-val)))
  297. (if (consp entry)
  298. (let ((lts (cmu_lts_function (car entry) (car (cdr entry))))
  299. (lexentries (lex.lookup_all (car entry))))
  300. (if (or (> (length lexentries) 1)
  301. (not (equal? (nth 2 entry) (nth 2 lts))))
  302. (begin
  303. (format ofd "( \"%s\" %l (" (car entry) (cadr entry))
  304. (mapcar
  305. (lambda (syl)
  306. (mapcar
  307. (lambda (seg)
  308. (cond
  309. ((string-matches seg "[aeiouAEIOU@].*")
  310. (format ofd "%s " (string-append seg (cadr syl))))
  311. (t
  312. (format ofd "%s " seg))))
  313. (car syl)))
  314. (car (cddr entry)))
  315. (format ofd "))\n"))
  316. (set! correctwords (+ 1 correctwords)))
  317. (set! wordcount (+ 1 wordcount))
  318. )
  319. )
  320. )
  321. (format t "From %d words %d correct %d exceptions\n"
  322. wordcount correctwords (- wordcount correctwords))
  323. )
  324. )
  325. (define (utf8entries infile outfile)
  326. (let (ifd ofd entry)
  327. (set! ifd (fopen infile "r"))
  328. (set! ofd (fopen outfile "w"))
  329. (while (not (equal? (set! entry (readfp ifd)) (eof-val)))
  330. (mapcar
  331. (lambda (l) (format ofd "%s " l))
  332. (utf8explode entry))
  333. (format ofd "\n"))
  334. (fclose ifd)
  335. (fclose ifd)))
  336. (provide 'make_lex)