make_phoneset.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;; ;;;
  3. ;;; Language Technologies Institute ;;;
  4. ;;; Carnegie Mellon University ;;;
  5. ;;; Copyright (c) 2000 ;;;
  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 2000 ;;;
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ;;; ;;;
  37. ;;; Convert a phoneset to a C compilable structure ;;;
  38. ;;; ;;;
  39. ;;; (phonesettoC "cmu_klingon" (car (load "cmu_klingon_worf_phoneset.scm" t)) "pau" ".")
  40. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  41. (defvar featval_ids nil)
  42. (define (phonesettoC name phonesetdef silence odir)
  43. "(phonesettoC name phonesetdef ODIR)
  44. Convert a Phoneset definition to a C structure in ODIR/NAME_phoneset.c."
  45. (let
  46. ((ofdc (fopen (path-append odir (string-append name "_phoneset.c")) "w"))
  47. (i 0))
  48. (format ofdc "/*******************************************************/\n")
  49. (format ofdc "/** Autogenerated phoneset trees for %s */\n" name)
  50. (format ofdc "/*******************************************************/\n")
  51. (format ofdc "\n")
  52. (format ofdc "#include \"cst_string.h\"\n")
  53. (format ofdc "#include \"cst_phoneset.h\"\n")
  54. (format ofdc "extern const cst_phoneset %s_phoneset;\n" name)
  55. (set! featval_ids nil)
  56. (format ofdc "\n\n")
  57. ;;; feature names
  58. (format ofdc "static const char * const %s_featnames[] = {\n" name)
  59. (mapcar
  60. (lambda (fn) (format ofdc " \"%s\",\n" fn))
  61. (mapcar car (caddr phonesetdef)))
  62. (format ofdc " NULL };\n\n")
  63. ;;; Phone names
  64. (format ofdc "static const char * const %s_phonenames[] = {\n" name)
  65. (mapcar
  66. (lambda (pn) (format ofdc " \"%s\",\n" pn))
  67. (mapcar car (caddr (cdr phonesetdef))))
  68. (format ofdc " NULL };\n\n")
  69. (set! num_phones (length (caddr (cdr phonesetdef))))
  70. ;;; Feature table (do values afterwards)
  71. (set! i 0)
  72. (mapcar
  73. (lambda (pdef)
  74. (format ofdc "static const int %s_fv_%03d[] = { " name i)
  75. (mapcar
  76. (lambda (fv)
  77. (format ofdc "%d, " (phonesettoC_featval_id fv)))
  78. (cdr pdef))
  79. (set! i (+ i 1))
  80. (format ofdc " -1 };\n"))
  81. (caddr (cdr phonesetdef)))
  82. (format ofdc "static const int %s_fv_%03d[] = { 0 };\n\n" name i)
  83. (set! i 0)
  84. (format ofdc "static const int * const %s_fvtable[] = {\n" name )
  85. (mapcar
  86. (lambda (pdef)
  87. (format ofdc " %s_fv_%03d, \n" name i)
  88. (set! i (+ i 1)))
  89. (caddr (cdr phonesetdef)))
  90. (format ofdc " %s_fv_%03d };\n\n" name i)
  91. ;;; Feature values
  92. (set! i 0)
  93. (mapcar
  94. (lambda (fv)
  95. (format ofdc "DEF_STATIC_CONST_VAL_STRING(featval_%d,\"%s\");\n" i fv)
  96. (set! i (+ i 1)))
  97. (mapcar car (reverse featval_ids)))
  98. (format ofdc "\n")
  99. (format ofdc "static const cst_val * const %s_featvals[] = {\n" name)
  100. (set! i 0)
  101. (mapcar
  102. (lambda (fv)
  103. (format ofdc " (cst_val *)&featval_%d,\n" i)
  104. (set! i (+ i 1)))
  105. (mapcar car (reverse featval_ids)))
  106. (format ofdc " NULL };\n\n")
  107. (format ofdc "const cst_phoneset %s_phoneset = {\n" name)
  108. (format ofdc " \"%s\",\n" name)
  109. (format ofdc " %s_featnames,\n" name)
  110. (format ofdc " %s_featvals,\n" name)
  111. (format ofdc " %s_phonenames,\n" name)
  112. (format ofdc " \"%s\",\n" silence)
  113. (format ofdc " %d,\n" num_phones)
  114. (format ofdc " %s_fvtable,\n" name)
  115. (format ofdc " 0 /* not freeable */\n")
  116. (format ofdc "};\n")
  117. (fclose ofdc)
  118. ))
  119. (define (phonesettoC_featval_id f)
  120. (let ((fn (assoc_string f featval_ids)))
  121. (cond
  122. (fn
  123. (cadr fn))
  124. (t
  125. (set! featval_ids
  126. (cons (list f (length featval_ids))
  127. featval_ids))
  128. (phonesettoC_featval_id f)))))
  129. (provide 'make_phoneset)