| 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 |
/* CART tree support */ |
| 38 |
/* */ |
| 39 |
/*************************************************************************/ |
| 40 |
|
| 41 |
#include "cst_regex.h" |
| 42 |
#include "cst_cart.h" |
| 43 |
|
| 44 |
CST_VAL_REGISTER_TYPE_NODEL(cart,cst_cart) |
| 45 |
|
| 46 |
/* Make this 1 if you want to debug some cart calls */ |
| 47 |
#define CART_DEBUG 0 |
| 48 |
|
| 49 |
#define cst_cart_node_n(P,TREE) ((TREE)->rule_table[P]) |
| 50 |
|
| 51 |
void delete_cart(cst_cart *cart) |
| 52 |
{ |
| 53 |
/* have to compensate for previous over-zealous consting */ |
| 54 |
/* It is assume that given carts, can be freed (i.e. they aren't in */ |
| 55 |
/* in the data segment */ |
| 56 |
int i; |
| 57 |
|
| 58 |
if (cart == NULL) |
| 59 |
return; |
| 60 |
|
| 61 |
for (i=0; cart->rule_table[i].val; i++) |
| 62 |
delete_val((cst_val *)(void *)cart->rule_table[i].val); |
| 63 |
cst_free((void *)cart->rule_table); |
| 64 |
|
| 65 |
for (i=0; cart->feat_table[i]; i++) |
| 66 |
cst_free((void *)cart->feat_table[i]); |
| 67 |
cst_free((void *)cart->feat_table); |
| 68 |
|
| 69 |
cst_free(cart); |
| 70 |
|
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
#define cst_cart_node_val(n,tree) (cst_cart_node_n(n,tree).val) |
| 75 |
#define cst_cart_node_op(n,tree) (cst_cart_node_n(n,tree).op) |
| 76 |
#define cst_cart_node_feat(n,tree) (tree->feat_table[cst_cart_node_n(n,tree).feat]) |
| 77 |
#define cst_cart_node_yes(n,tree) (n+1) |
| 78 |
#define cst_cart_node_no(n,tree) (cst_cart_node_n(n,tree).no_node) |
| 79 |
|
| 80 |
#if CART_DEBUG |
| 81 |
static void cart_print_node(int n, const cst_cart *tree) |
| 82 |
{ |
| 83 |
printf("%s ",cst_cart_node_feat(n,tree)); |
| 84 |
if (cst_cart_node_op(n,tree) == CST_CART_OP_IS) |
| 85 |
printf("IS "); |
| 86 |
else if (cst_cart_node_op(n,tree) == CST_CART_OP_LESS) |
| 87 |
printf("< "); |
| 88 |
else if (cst_cart_node_op(n,tree) == CST_CART_OP_GREATER) |
| 89 |
printf("> "); |
| 90 |
else if (cst_cart_node_op(n,tree) == CST_CART_OP_IN) |
| 91 |
printf("IN "); |
| 92 |
else if (cst_cart_node_op(n,tree) == CST_CART_OP_MATCHES) |
| 93 |
printf("MATCHES "); |
| 94 |
else |
| 95 |
printf("*%d* ",cst_cart_node_op(n,tree)); |
| 96 |
val_print(stdout,cst_cart_node_val(n,tree)); |
| 97 |
printf("\n"); |
| 98 |
} |
| 99 |
#endif |
| 100 |
|
| 101 |
const cst_val *cart_interpret(cst_item *item, const cst_cart *tree) |
| 102 |
{ |
| 103 |
/* Tree interpretation */ |
| 104 |
const cst_val *v=0; |
| 105 |
const cst_val *tree_val; |
| 106 |
const char *tree_feat = ""; |
| 107 |
cst_features *fcache; |
| 108 |
int r=0; |
| 109 |
int node=0; |
| 110 |
|
| 111 |
fcache = new_features_local(item_utt(item)->ctx); |
| 112 |
|
| 113 |
while (cst_cart_node_op(node,tree) != CST_CART_OP_LEAF) |
| 114 |
{ |
| 115 |
#if CART_DEBUG |
| 116 |
cart_print_node(node,tree); |
| 117 |
#endif |
| 118 |
tree_feat = cst_cart_node_feat(node,tree); |
| 119 |
|
| 120 |
v = get_param_val(fcache,tree_feat,0); |
| 121 |
if (v == 0) |
| 122 |
{ |
| 123 |
v = ffeature(item,tree_feat); |
| 124 |
feat_set(fcache,tree_feat,v); |
| 125 |
} |
| 126 |
|
| 127 |
#if CART_DEBUG |
| 128 |
val_print(stdout,v); printf("\n"); |
| 129 |
#endif |
| 130 |
tree_val = cst_cart_node_val(node,tree); |
| 131 |
if (cst_cart_node_op(node,tree) == CST_CART_OP_IS) |
| 132 |
{ |
| 133 |
/* printf("awb_debug %d %d\n",CST_VAL_TYPE(v),CST_VAL_TYPE(tree_val));*/ |
| 134 |
r = val_equal(v,tree_val); |
| 135 |
} |
| 136 |
else if (cst_cart_node_op(node,tree) == CST_CART_OP_LESS) |
| 137 |
r = val_less(v,tree_val); |
| 138 |
else if (cst_cart_node_op(node,tree) == CST_CART_OP_GREATER) |
| 139 |
r = val_greater(v,tree_val); |
| 140 |
else if (cst_cart_node_op(node,tree) == CST_CART_OP_IN) |
| 141 |
r = val_member(v,tree_val); |
| 142 |
else if (cst_cart_node_op(node,tree) == CST_CART_OP_MATCHES) |
| 143 |
r = cst_regex_match(cst_regex_table[val_int(tree_val)], |
| 144 |
val_string(v)); |
| 145 |
else |
| 146 |
{ |
| 147 |
cst_errmsg("cart_interpret_question: unknown op type %d\n", |
| 148 |
cst_cart_node_op(node,tree)); |
| 149 |
cst_error(); |
| 150 |
} |
| 151 |
|
| 152 |
if (r) |
| 153 |
{ /* Oh yes it is */ |
| 154 |
#if CART_DEBUG |
| 155 |
printf(" YES\n"); |
| 156 |
#endif |
| 157 |
node = cst_cart_node_yes(node,tree); |
| 158 |
} |
| 159 |
else |
| 160 |
{ /* Oh no it isn't */ |
| 161 |
#if CART_DEBUG |
| 162 |
printf(" NO\n"); |
| 163 |
#endif |
| 164 |
node = cst_cart_node_no(node,tree); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
delete_features(fcache); |
| 169 |
|
| 170 |
return cst_cart_node_val(node,tree); |
| 171 |
|
| 172 |
} |