| 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 | /* ----------------------------------------------------------------- */ |
| 42 | /* The English TTS System "Flite+hts_engine" */ |
| 43 | /* developed by HTS Working Group */ |
| 44 | /* http://hts-engine.sourceforge.net/ */ |
| 45 | /* ----------------------------------------------------------------- */ |
| 46 | /* */ |
| 47 | /* Copyright (c) 2005-2013 Nagoya Institute of Technology */ |
| 48 | /* Department of Computer Science */ |
| 49 | /* */ |
| 50 | /* 2005-2008 Tokyo Institute of Technology */ |
| 51 | /* Interdisciplinary Graduate School of */ |
| 52 | /* Science and Engineering */ |
| 53 | /* */ |
| 54 | /* All rights reserved. */ |
| 55 | /* */ |
| 56 | /* Redistribution and use in source and binary forms, with or */ |
| 57 | /* without modification, are permitted provided that the following */ |
| 58 | /* conditions are met: */ |
| 59 | /* */ |
| 60 | /* - Redistributions of source code must retain the above copyright */ |
| 61 | /* notice, this list of conditions and the following disclaimer. */ |
| 62 | /* - Redistributions in binary form must reproduce the above */ |
| 63 | /* copyright notice, this list of conditions and the following */ |
| 64 | /* disclaimer in the documentation and/or other materials provided */ |
| 65 | /* with the distribution. */ |
| 66 | /* - Neither the name of the HTS working group nor the names of its */ |
| 67 | /* contributors may be used to endorse or promote products derived */ |
| 68 | /* from this software without specific prior written permission. */ |
| 69 | /* */ |
| 70 | /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */ |
| 71 | /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */ |
| 72 | /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ |
| 73 | /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ |
| 74 | /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */ |
| 75 | /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */ |
| 76 | /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */ |
| 77 | /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */ |
| 78 | /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ |
| 79 | /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */ |
| 80 | /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */ |
| 81 | /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */ |
| 82 | /* POSSIBILITY OF SUCH DAMAGE. */ |
| 83 | /* ----------------------------------------------------------------- */ |
| 84 | |
| 85 | #include "cst_regex.h" |
| 86 | #include "cst_cart.h" |
| 87 | |
| 88 | CST_VAL_REGISTER_TYPE_NODEL(cart,cst_cart) |
| 89 | |
| 90 | /* Make this 1 if you want to debug som cart calls */ |
| 91 | #define CART_DEBUG 0 |
| 92 | |
| 93 | #define cst_cart_node_n(P,TREE) ((TREE)->rule_table[P]) |
| 94 | |
| 95 | #ifndef FLITE_PLUS_HTS_ENGINE |
| 96 | void delete_cart(cst_cart *cart) |
| 97 | { |
| 98 | cst_errmsg("delete_cart function missing\n"); |
| 99 | return; |
| 100 | } |
| 101 | #endif /* !FLITE_PLUS_HTS_ENGINE */ |
| 102 | |
| 103 | #define cst_cart_node_val(n,tree) (cst_cart_node_n(n,tree).val) |
| 104 | #define cst_cart_node_op(n,tree) (cst_cart_node_n(n,tree).op) |
| 105 | #define cst_cart_node_feat(n,tree) (tree->feat_table[cst_cart_node_n(n,tree).feat]) |
| 106 | #define cst_cart_node_yes(n,tree) (n+1) |
| 107 | #define cst_cart_node_no(n,tree) (cst_cart_node_n(n,tree).no_node) |
| 108 | |
| 109 | #ifndef FLITE_PLUS_HTS_ENGINE |
| 110 | #if CART_DEBUG |
| 111 | static void cart_print_node(int n, const cst_cart *tree) |
| 112 | { |
| 113 | printf("%s ",cst_cart_node_feat(n,tree)); |
| 114 | if (cst_cart_node_op(n,tree) == CST_CART_OP_IS) |
| 115 | printf("IS "); |
| 116 | else if (cst_cart_node_op(n,tree) == CST_CART_OP_LESS) |
| 117 | printf("< "); |
| 118 | else if (cst_cart_node_op(n,tree) == CST_CART_OP_GREATER) |
| 119 | printf("> "); |
| 120 | else if (cst_cart_node_op(n,tree) == CST_CART_OP_IN) |
| 121 | printf("IN "); |
| 122 | else if (cst_cart_node_op(n,tree) == CST_CART_OP_MATCHES) |
| 123 | printf("MATCHES "); |
| 124 | else |
| 125 | printf("*%d* ",cst_cart_node_op(n,tree)); |
| 126 | val_print(stdout,cst_cart_node_val(n,tree)); |
| 127 | printf("\n"); |
| 128 | } |
| 129 | #endif |
| 130 | #endif /* !FLITE_PLUS_HTS_ENGINE */ |
| 131 | |
| 132 | const cst_val *cart_interpret(cst_item *item, const cst_cart *tree) |
| 133 | { |
| 134 | /* Tree interpretation */ |
| 135 | const cst_val *v=0; |
| 136 | const cst_val *tree_val; |
| 137 | const char *tree_feat = ""; |
| 138 | cst_features *fcache; |
| 139 | int r=0; |
| 140 | int node=0; |
| 141 | |
| 142 | fcache = new_features_local(item_utt(item)->ctx); |
| 143 | |
| 144 | while (cst_cart_node_op(node,tree) != CST_CART_OP_LEAF) |
| 145 | { |
| 146 | #if CART_DEBUG |
| 147 | cart_print_node(node,tree); |
| 148 | #endif |
| 149 | tree_feat = cst_cart_node_feat(node,tree); |
| 150 | |
| 151 | v = get_param_val(fcache,tree_feat,0); |
| 152 | if (v == 0) |
| 153 | { |
| 154 | v = ffeature(item,tree_feat); |
| 155 | feat_set(fcache,tree_feat,v); |
| 156 | } |
| 157 | |
| 158 | #if CART_DEBUG |
| 159 | val_print(stdout,v); printf("\n"); |
| 160 | #endif |
| 161 | tree_val = cst_cart_node_val(node,tree); |
| 162 | if (cst_cart_node_op(node,tree) == CST_CART_OP_IS) |
| 163 | r = val_equal(v,tree_val); |
| 164 | else if (cst_cart_node_op(node,tree) == CST_CART_OP_LESS) |
| 165 | r = val_less(v,tree_val); |
| 166 | else if (cst_cart_node_op(node,tree) == CST_CART_OP_GREATER) |
| 167 | r = val_greater(v,tree_val); |
| 168 | else if (cst_cart_node_op(node,tree) == CST_CART_OP_IN) |
| 169 | r = val_member(v,tree_val); |
| 170 | else if (cst_cart_node_op(node,tree) == CST_CART_OP_MATCHES) |
| 171 | r = cst_regex_match(cst_regex_table[val_int(tree_val)], |
| 172 | val_string(v)); |
| 173 | else |
| 174 | { |
| 175 | cst_errmsg("cart_interpret_question: unknown op type %d\n", |
| 176 | cst_cart_node_op(node,tree)); |
| 177 | cst_error(); |
| 178 | } |
| 179 | |
| 180 | if (r) |
| 181 | { /* Oh yes it is */ |
| 182 | #if CART_DEBUG |
| 183 | printf(" YES\n"); |
| 184 | #endif |
| 185 | node = cst_cart_node_yes(node,tree); |
| 186 | } |
| 187 | else |
| 188 | { /* Oh no it isn't */ |
| 189 | #if CART_DEBUG |
| 190 | printf(" NO\n"); |
| 191 | #endif |
| 192 | node = cst_cart_node_no(node,tree); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | delete_features(fcache); |
| 197 | |
| 198 | return cst_cart_node_val(node,tree); |
| 199 | |
| 200 | } |