| 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 ([email protected]) ;;; |
| 34 |
;;; Date: January 2000 ;;; |
| 35 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 36 |
;;; ;;; |
| 37 |
;;; Generate C compilable CART trees. ;;; |
| 38 |
;;; ;;; |
| 39 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 40 |
|
| 41 |
(defvar val_table_length 0) |
| 42 |
|
| 43 |
(define (carttoC name tree odir) |
| 44 |
"(carttoC NAME TREE ODIR) |
| 45 |
Coverts a CART tree to a single C file called ODIR/NAME_cart.c." |
| 46 |
(let |
| 47 |
((ofdc (fopen (path-append odir (string-append name "_cart.c")) "w")) |
| 48 |
(ofdh(fopen (path-append odir (string-append name "_cart.h")) "w"))) |
| 49 |
(format ofdc "/*******************************************************/\n") |
| 50 |
(format ofdc "/** Autogenerated cart trees for %s */\n" name) |
| 51 |
(format ofdc "/*******************************************************/\n") |
| 52 |
(format ofdc "\n") |
| 53 |
(format ofdc "#include \"cst_string.h\"\n") |
| 54 |
(format ofdc "#include \"cst_cart.h\"\n") |
| 55 |
(format ofdc "#include \"cst_regex.h\"\n") |
| 56 |
(format ofdc "#include \"%s_cart.h\"\n" name) |
| 57 |
|
| 58 |
(format ofdh "/*******************************************************/\n") |
| 59 |
(format ofdh "/** Autogenerated cart tree for %s */\n" name) |
| 60 |
(format ofdh "/** from %s */\n" odir) |
| 61 |
(format ofdh "/*******************************************************/\n") |
| 62 |
(format ofdh "\n") |
| 63 |
|
| 64 |
(set! current_node -1) |
| 65 |
(set! val_table nil) |
| 66 |
(set! feat_nums nil) |
| 67 |
|
| 68 |
(do_carttoC ofdc ofdh name tree) |
| 69 |
|
| 70 |
(fclose ofdc) |
| 71 |
(fclose ofdh) |
| 72 |
)) |
| 73 |
|
| 74 |
(define (do_carttoC ofdc ofdh name tree) |
| 75 |
"(do_carttoC ofdc ofdh name tree) |
| 76 |
Do the tree dump, this section is split off for cases when we |
| 77 |
want multiple trees in the same file." |
| 78 |
|
| 79 |
(set! cart_name name) |
| 80 |
(format ofdc "\n") |
| 81 |
(format ofdc "static const cst_cart_node %s_cart_nodes[] = {\n" name) |
| 82 |
(carttoC_tree_nodes tree ofdc ofdh) |
| 83 |
(format ofdc "{ 255, CST_CART_OP_NONE, 0, 0}};\n\n") |
| 84 |
|
| 85 |
;; definitions are in the .h file |
| 86 |
; (mapcar |
| 87 |
; (lambda (f) |
| 88 |
; (format ofdc "%s" (caddr f))) |
| 89 |
; (reverse val_table)) |
| 90 |
(format ofdc "\n") |
| 91 |
|
| 92 |
(format ofdc "static const char * const %s_feat_table[] = {\n" name) |
| 93 |
(mapcar |
| 94 |
(lambda (f) |
| 95 |
(if (string-equal "string" (typeof (car f))) |
| 96 |
(format ofdc "%s,\n" (car f)) |
| 97 |
(format ofdc "\"%s\",\n" (car f)))) |
| 98 |
(reverse feat_nums)) |
| 99 |
(format ofdc "NULL };\n\n") |
| 100 |
|
| 101 |
(format ofdc "const cst_cart %s_cart = {\n" name) |
| 102 |
(format ofdc " %s_cart_nodes,\n" name) |
| 103 |
(format ofdc " %s_feat_table\n" name) |
| 104 |
(format ofdc "};\n") |
| 105 |
|
| 106 |
t |
| 107 |
) |
| 108 |
|
| 109 |
(defvar cart_operators |
| 110 |
'(("is" "CST_CART_OP_IS") |
| 111 |
("in" "CST_CART_OP_IN") |
| 112 |
("<" "CST_CART_OP_LESS") |
| 113 |
(">" "CST_CART_OP_GREATER") |
| 114 |
("matches" "CST_CART_OP_MATCHES") |
| 115 |
("=" "CST_CART_OP_EQUALS"))) |
| 116 |
|
| 117 |
(define (carttoC_feat_num f) |
| 118 |
(let ((fn (assoc_string f feat_nums))) |
| 119 |
(cond |
| 120 |
(fn |
| 121 |
(cadr fn)) |
| 122 |
(t |
| 123 |
(set! feat_nums |
| 124 |
(cons (list f (length feat_nums)) |
| 125 |
feat_nums)) |
| 126 |
(carttoC_feat_num f))))) |
| 127 |
|
| 128 |
(define (carttoC_val_table ofdh f operator) |
| 129 |
(let ((fn (if (number? f) |
| 130 |
nil ;; if its a number don't try to share it |
| 131 |
(assoc_string |
| 132 |
(if (string-equal operator "is") |
| 133 |
(format nil "is_%s" f) |
| 134 |
f) |
| 135 |
val_table)))) |
| 136 |
(if (null val_table) |
| 137 |
(begin |
| 138 |
(set! val_table_length 0) |
| 139 |
(set! val_table (cons (list "_xxx" "_xxx" nil))))) |
| 140 |
(cond |
| 141 |
(fn |
| 142 |
; (format t "awb_debug reused_val %l\n" fn) |
| 143 |
(cadr fn)) |
| 144 |
(t |
| 145 |
(let ((nname (format nil "val_%04d" val_table_length))) |
| 146 |
(set! val_table_length (+ 1 val_table_length)) |
| 147 |
(set! val_table_key |
| 148 |
(if (string-equal operator "is") |
| 149 |
(format nil "is_%s" f) |
| 150 |
f)) |
| 151 |
; (format ofdh "static const cst_val %s;\n" nname) |
| 152 |
(cond |
| 153 |
((string-equal operator "is") |
| 154 |
(set! val_table |
| 155 |
(cons (list val_table_key nname |
| 156 |
(format ofdh "DEF_STATIC_CONST_VAL_STRING(%s,\"%s\");\n" |
| 157 |
nname f)) |
| 158 |
val_table))) |
| 159 |
((string-equal "matches" operator) |
| 160 |
(set! val_table |
| 161 |
(cons (list val_table_key nname |
| 162 |
(format ofdh "DEF_STATIC_CONST_VAL_INT(%s,CST_RX_%s_NUM);\n" |
| 163 |
nname f)) |
| 164 |
val_table))) |
| 165 |
((number? f) |
| 166 |
(format ofdh "DEF_STATIC_CONST_VAL_FLOAT(%s,%f);\n" |
| 167 |
nname f)) |
| 168 |
((consp f) |
| 169 |
(format stderr "list vals not supported here yet\n") |
| 170 |
(error f)) |
| 171 |
(t |
| 172 |
(set! val_table |
| 173 |
(cons (list val_table_key nname |
| 174 |
(format ofdh "DEF_STATIC_CONST_VAL_STRING(%s,\"%s\");\n" |
| 175 |
nname f)) |
| 176 |
val_table)))) |
| 177 |
; (format t "awb_debug new_val %l\n" (car val_table)) |
| 178 |
nname |
| 179 |
))))) |
| 180 |
|
| 181 |
(define (carttoC_tree_nodes tree ofdc ofdh) |
| 182 |
"(carttoC_tree_nodes tree ofdc ofdh) |
| 183 |
Dump the nodes in the tree." |
| 184 |
(let ((this_node (set! current_node (+ 1 current_node)))) |
| 185 |
(cond |
| 186 |
((cdr tree) ;; a question node |
| 187 |
(format ofdc "{ %d, %s, %s, (cst_val *)&%s},\n" |
| 188 |
(carttoC_feat_num (caar tree)) ;; the feature |
| 189 |
(cadr (assoc_string ;; operator |
| 190 |
(cadr (car tree)) |
| 191 |
cart_operators)) |
| 192 |
(format nil "CTNODE_%s_NO_%04d" cart_name this_node); the no node |
| 193 |
(carttoC_val_table ofdh |
| 194 |
(nth 2 (car tree)) |
| 195 |
(cadr (car tree)))) |
| 196 |
(carttoC_tree_nodes (car (cdr tree)) ofdc ofdh) |
| 197 |
(format ofdh "#define CTNODE_%s_NO_%04d %d\n" cart_name |
| 198 |
this_node (+ 1 current_node)) |
| 199 |
(carttoC_tree_nodes (car (cdr (cdr tree))) ofdc ofdh)) |
| 200 |
(t ;; a leaf node |
| 201 |
(format ofdc |
| 202 |
"{ 255, CST_CART_OP_NONE, 0, (cst_val *)&%s },\n" |
| 203 |
(carttoC_extract_answer ofdh tree)))))) |
| 204 |
|
| 205 |
(define (carttoC_extract_answer ofdh tree) |
| 206 |
"(carttoC_extract_answer tree) |
| 207 |
Get answer from leaf node. (this can be redefined if you want different |
| 208 |
behaviour)." |
| 209 |
(carttoC_val_table ofdh |
| 210 |
(car (last (car tree))) |
| 211 |
'none)) |
| 212 |
(set! basic_carttoC_extract_answer carttoC_extract_answer) |
| 213 |
|
| 214 |
(provide 'make_cart) |