Debian Codegraph @

tools/make_lts_wfst.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 ;;; 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
53
54 (define (lts_to_rg_to_wfst lts_rules odir)
55 (mapcar
56 (lambda (a)
57 (format t "%s\n" (car a))
58 (savergtree (car (cdr a))
59 (format nil "%s/%s.tree.rg" odir (car a)))
60 (system
61 (format nil
62 "$ESTDIR/bin/wfst_build -heap 10000000 -type rg -detmin -o %s/%s.tree.wfst %s/%s.tree.rg"
63 odir (car a)
64 odir (car a)))
65 t)
66 lts_rules)
67 t)
68
69 (define (torg tree)
70 "(torg tree)
71 Convert a CART tree to a regular grammar. This will only
72 reasonably work for classification trees, not regression
73 trees."
74 (set! torg_state 1)
75 (torgrule tree (intern (format nil "s%d" torg_state))))
76
77 (define (ensymbolize l)
78 (let ((ss "_"))
79 (mapcar
80 (lambda (a)
81 (set! ss (string-append ss a "_")))
82 l)
83 ss))
84
85 (define (cart_tree_node_count tree)
86 "(tree_node_count tree)
87 Count the number nodes (questions and leafs) in the given CART tree."
88 (cond
89 ((cdr tree)
90 (+ 1
91 (cart_tree_node_count (car (cdr tree)))
92 (cart_tree_node_count (car (cdr (cdr tree))))))
93 (t
94 1)))
95
96 (define (torgrule tree state)
97 (cond
98 ((cdr tree)
99 (let ((leftstate (intern (format nil "s%d" (+ 1 torg_state))))
100 (rightstate (intern (format nil "s%d" (+ 2 torg_state)))))
101 (set! torg_state (+ 2 torg_state))
102 (append
103 (list
104 (list state '-> (ensymbolize (car tree)) leftstate)
105 (list state '-> (ensymbolize (cons 'not (car tree))) rightstate))
106 (torgrule (car (cdr tree)) leftstate)
107 (torgrule (car (cdr (cdr tree))) rightstate))))
108 (t
109 (let ((ss (car (last (car tree)))))
110 (list
111 (list state '->
112 (if (string-equal "_epsilon_" ss)
113 'epsilon
114 ss)))))))
115
116 (define (torg2 tree)
117 (torgrule2 tree nil))
118
119 (define (torgrule2 tree p)
120 (cond
121 ((cdr tree)
122 (append
123 (torgrule2 (car (cdr tree)) (cons (ensymbolize (car tree)) p))
124 (torgrule2 (car (cdr (cdr tree)))
125 (cons (ensymbolize (cons 'not (car tree))) p))))
126 (t
127 (let ((ss (car (last (car tree)))))
128 (list
129 (cons 'S (cons '->
130 (reverse
131 (mapcar (lambda (a) a)
132 (cons
133 (if (string-equal "_epsilon_" ss)
134 'epsilon
135 ss)
136 p))))))))))
137
138
139 (define (savergtree tree fname)
140 (let ((fd (fopen fname "w")))
141 ; (format fd "(RegularGrammar\n")
142 ; (format fd " name\n")
143 ; (format fd " ()\n")
144 ; (format fd "(\n")
145 ; (mapcar
146 ; (lambda (a) (format fd "%l\n" a))
147 ; (torg2 tree))
148 ; (format fd "))\n")
149 (pprintf
150 (list
151 'RegularGrammar
152 'name
153 ()
154 (torg tree))
155 fd)
156 (fclose fd)))