/OpenEars/CMULibraries/flite-1.4-release/tools/make_lts_wfst.scm

https://github.com/jackieju/irobot_app · Scheme · 156 lines · 87 code · 10 blank · 59 comment · 1 complexity · eac2ae02158ec248f542bfd28ce7f9ed MD5 · raw file

  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. ;;; Convert the LTS cart trees to regular grammars and build wfsts
  38. ;;;
  39. ;;; Technique described in the Flite paper
  40. ;;; http://www.cs.cmu.edu/~awb/papers/ISCA01/flite/flite.html
  41. ;;;
  42. ;;;
  43. ;;; call as:
  44. ;;; festival $FLITEDIR/tools/make_lts_wfst.scm cmulex_lts_rules.scm \
  45. ;;; '(lts_to_rg_to_wfst cmulex_lts_rules ".")'
  46. ;;;
  47. ;;; will make a bunch of *.tree.wfst files in "."
  48. ;;; use make_lts.scm:(ltsregextoC "cmulex" "." ".")
  49. ;;; to convert them to scheme
  50. ;;;
  51. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  52. (define (lts_to_rg_to_wfst lts_rules odir)
  53. (mapcar
  54. (lambda (a)
  55. (format t "%s\n" (car a))
  56. (savergtree (car (cdr a))
  57. (format nil "%s/%s.tree.rg" odir (car a)))
  58. (system
  59. (format nil
  60. "$ESTDIR/bin/wfst_build -heap 10000000 -type rg -detmin -o %s/%s.tree.wfst %s/%s.tree.rg"
  61. odir (car a)
  62. odir (car a)))
  63. t)
  64. lts_rules)
  65. t)
  66. (define (torg tree)
  67. "(torg tree)
  68. Convert a CART tree to a regular grammar. This will only
  69. reasonably work for classification trees, not regression
  70. trees."
  71. (set! torg_state 1)
  72. (torgrule tree (intern (format nil "s%d" torg_state))))
  73. (define (ensymbolize l)
  74. (let ((ss "_"))
  75. (mapcar
  76. (lambda (a)
  77. (set! ss (string-append ss a "_")))
  78. l)
  79. ss))
  80. (define (cart_tree_node_count tree)
  81. "(tree_node_count tree)
  82. Count the number nodes (questions and leafs) in the given CART tree."
  83. (cond
  84. ((cdr tree)
  85. (+ 1
  86. (cart_tree_node_count (car (cdr tree)))
  87. (cart_tree_node_count (car (cdr (cdr tree))))))
  88. (t
  89. 1)))
  90. (define (torgrule tree state)
  91. (cond
  92. ((cdr tree)
  93. (let ((leftstate (intern (format nil "s%d" (+ 1 torg_state))))
  94. (rightstate (intern (format nil "s%d" (+ 2 torg_state)))))
  95. (set! torg_state (+ 2 torg_state))
  96. (append
  97. (list
  98. (list state '-> (ensymbolize (car tree)) leftstate)
  99. (list state '-> (ensymbolize (cons 'not (car tree))) rightstate))
  100. (torgrule (car (cdr tree)) leftstate)
  101. (torgrule (car (cdr (cdr tree))) rightstate))))
  102. (t
  103. (let ((ss (car (last (car tree)))))
  104. (list
  105. (list state '->
  106. (if (string-equal "_epsilon_" ss)
  107. 'epsilon
  108. ss)))))))
  109. (define (torg2 tree)
  110. (torgrule2 tree nil))
  111. (define (torgrule2 tree p)
  112. (cond
  113. ((cdr tree)
  114. (append
  115. (torgrule2 (car (cdr tree)) (cons (ensymbolize (car tree)) p))
  116. (torgrule2 (car (cdr (cdr tree)))
  117. (cons (ensymbolize (cons 'not (car tree))) p))))
  118. (t
  119. (let ((ss (car (last (car tree)))))
  120. (list
  121. (cons 'S (cons '->
  122. (reverse
  123. (mapcar (lambda (a) a)
  124. (cons
  125. (if (string-equal "_epsilon_" ss)
  126. 'epsilon
  127. ss)
  128. p))))))))))
  129. (define (savergtree tree fname)
  130. (let ((fd (fopen fname "w")))
  131. ; (format fd "(RegularGrammar\n")
  132. ; (format fd " name\n")
  133. ; (format fd " ()\n")
  134. ; (format fd "(\n")
  135. ; (mapcar
  136. ; (lambda (a) (format fd "%l\n" a))
  137. ; (torg2 tree))
  138. ; (format fd "))\n")
  139. (pprintf
  140. (list
  141. 'RegularGrammar
  142. 'name
  143. ()
  144. (torg tree))
  145. fd)
  146. (fclose fd)))