make_cart.scm 8.6 KB

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