us_ffeatures.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*************************************************************************/
  2. /* */
  3. /* Language Technologies Institute */
  4. /* Carnegie Mellon University */
  5. /* Copyright (c) 2001 */
  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 2001 */
  35. /*************************************************************************/
  36. /* Feature functions used by various cart trees etc */
  37. /* These have been create as needed, and as some of the trees are */
  38. /* from University of Edinburgh's Festival system their names and */
  39. /* semantics follow them */
  40. /*************************************************************************/
  41. #include "cst_hrg.h"
  42. #include "cst_phoneset.h"
  43. #include "cst_regex.h"
  44. #include "cst_ffeatures.h"
  45. #include "us_ffeatures.h"
  46. static const cst_val *gpos(const cst_item *word);
  47. DEF_STATIC_CONST_VAL_STRING(val_string_numeric,"numeric");
  48. DEF_STATIC_CONST_VAL_STRING(val_string_number,"number");
  49. DEF_STATIC_CONST_VAL_STRING(val_string_month,"month");
  50. DEF_STATIC_CONST_VAL_STRING(val_string_day,"day");
  51. DEF_STATIC_CONST_VAL_STRING(val_string_other,"_other_");
  52. DEF_STATIC_CONST_VAL_STRING(val_string_a,"a");
  53. DEF_STATIC_CONST_VAL_STRING(val_string_flight,"flight");
  54. DEF_STATIC_CONST_VAL_STRING(val_string_to,"to");
  55. DEF_STATIC_CONST_VAL_STRING(val_string_content,"content");
  56. static const cst_val *gpos(const cst_item *word)
  57. {
  58. /* Guess at part of speech (function/content) */
  59. const char *w;
  60. int s,t;
  61. w = item_feat_string(word,"name");
  62. for (s=0; us_gpos[s]; s++)
  63. {
  64. for (t=1; us_gpos[s][t]; t++)
  65. if (cst_streq(w,val_string(us_gpos[s][t])))
  66. return us_gpos[s][0];
  67. }
  68. return (cst_val *)&val_string_content;
  69. }
  70. static const cst_val *num_digits(const cst_item *token)
  71. {
  72. const char *name = item_feat_string(token,"name");
  73. return val_int_n(cst_strlen(name));
  74. }
  75. static const cst_val *month_range(const cst_item *token)
  76. {
  77. int v = item_feat_int(token,"name");
  78. if ((v > 0) && ( v < 32))
  79. return VAL_STRING_1;
  80. else
  81. return VAL_STRING_0;
  82. }
  83. static const cst_val *token_pos_guess(const cst_item *token)
  84. {
  85. const char *name = item_feat_string(token,"name");
  86. char *dc = cst_downcase(name);
  87. const cst_val *r;
  88. if (cst_regex_match(cst_rx_digits,dc))
  89. r = &val_string_numeric;
  90. else if ((cst_regex_match(cst_rx_double,dc)) ||
  91. (cst_regex_match(cst_rx_double,dc)))
  92. r = &val_string_number;
  93. else if (cst_streq(dc,"jan") ||
  94. cst_streq(dc,"january") ||
  95. cst_streq(dc,"feb") ||
  96. cst_streq(dc,"february") ||
  97. cst_streq(dc,"mar") ||
  98. cst_streq(dc,"march") ||
  99. cst_streq(dc,"apr") ||
  100. cst_streq(dc,"april") ||
  101. cst_streq(dc,"may") ||
  102. cst_streq(dc,"jun") ||
  103. cst_streq(dc,"june") ||
  104. cst_streq(dc,"jul") ||
  105. cst_streq(dc,"july") ||
  106. cst_streq(dc,"aug") ||
  107. cst_streq(dc,"august") ||
  108. cst_streq(dc,"sep") ||
  109. cst_streq(dc,"sept") ||
  110. cst_streq(dc,"september") ||
  111. cst_streq(dc,"oct") ||
  112. cst_streq(dc,"october") ||
  113. cst_streq(dc,"nov") ||
  114. cst_streq(dc,"november") ||
  115. cst_streq(dc,"dec") ||
  116. cst_streq(dc,"december"))
  117. r = &val_string_month;
  118. else if (cst_streq(dc,"sun") ||
  119. cst_streq(dc,"sunday") ||
  120. cst_streq(dc,"mon") ||
  121. cst_streq(dc,"monday") ||
  122. cst_streq(dc,"tue") ||
  123. cst_streq(dc,"tues") ||
  124. cst_streq(dc,"tuesday") ||
  125. cst_streq(dc,"wed") ||
  126. cst_streq(dc,"wednesday") ||
  127. cst_streq(dc,"thu") ||
  128. cst_streq(dc,"thurs") ||
  129. cst_streq(dc,"thursday") ||
  130. cst_streq(dc,"fri") ||
  131. cst_streq(dc,"friday") ||
  132. cst_streq(dc,"sat") ||
  133. cst_streq(dc,"saturday"))
  134. r = &val_string_day;
  135. /* ignoring the "token_most_common" condition, does get used */
  136. else if (cst_streq(dc,"a"))
  137. r = &val_string_a;
  138. else if (cst_streq(dc,"flight"))
  139. r = &val_string_flight;
  140. else if (cst_streq(dc,"to"))
  141. r = &val_string_to;
  142. else
  143. r = &val_string_other;
  144. cst_free(dc);
  145. return r;
  146. }
  147. const cst_val *content_words_in(const cst_item *p)
  148. {
  149. const cst_item *s;
  150. int i=0;
  151. p=item_as(p,"Word");
  152. s=item_as(path_to_item(p,"R:SylStructure.R:Phrase.parent.daughter1"),"Word");
  153. for (;s && !item_equal(p,s);s=item_next(s))
  154. {
  155. if (!strcmp(ffeature_string(s,"gpos"),"content"))
  156. {i++;}
  157. }
  158. // if(!strcmp(ffeature_string(p,"gpos"), "content")){i++;}
  159. return val_string_n(i);
  160. }
  161. const cst_val *content_words_out(const cst_item *p)
  162. {
  163. const cst_item *s;
  164. int i=0;
  165. p=item_as(p,"Word");
  166. s=item_as(path_to_item(p,"R:SylStructure.R:Phrase.parent.daughtern"),"Word");
  167. #if 1 /* fix by uratec */
  168. for (;s && !item_equal(p,s);s=item_prev(s))
  169. {
  170. if (!strcmp(ffeature_string(s,"gpos"),"content"))
  171. {i++;}
  172. }
  173. #else
  174. for (;s && !item_equal(p,s);p=item_next(p))
  175. {
  176. if (!strcmp(ffeature_string(p,"gpos"),"content"))
  177. {i++;}
  178. }
  179. if(!strcmp(ffeature_string(s,"gpos"), "content")){i++;}
  180. #endif
  181. return val_string_n(i);
  182. }
  183. const cst_val *cg_content_words_in_phrase(const cst_item *p)
  184. {
  185. return float_val(ffeature_float(p,"R:SylStructure.parent.parent.R:Word.content_words_in") + ffeature_float(p,"R:SylStructure.parent.parent.R:Word.content_words_out")) ;//- (strcmp(ffeature_string(p,"R:SylStructure.parent.parent.R:Word.gpos"),"content")==0?1:0));
  186. }
  187. void us_ff_register(cst_features *ffunctions)
  188. {
  189. /* The language independent ones */
  190. basic_ff_register(ffunctions);
  191. ff_register(ffunctions, "gpos",gpos);
  192. ff_register(ffunctions, "num_digits",num_digits);
  193. ff_register(ffunctions, "month_range",month_range);
  194. ff_register(ffunctions, "token_pos_guess",token_pos_guess);
  195. ff_register(ffunctions, "content_words_in",content_words_in);
  196. ff_register(ffunctions, "content_words_out",content_words_out);
  197. ff_register(ffunctions, "lisp_cg_content_words_in_phrase",cg_content_words_in_phrase);
  198. }