Debian Codegraph @

lang/usenglish/us_ffeatures.c

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