Debian Codegraph @

src/lexicon/cst_lts_rewrites.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2002 */
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: August 2002 */
35 /*************************************************************************/
36 /* */
37 /* Letter to sound rule for Festival's hand writable rule set */
38 /* */
39 /* Rules are ordered list of */
40 /* LC [ A ] RC => B */
41 /* */
42 /*************************************************************************/
43
44 #include "cst_string.h"
45 #include "cst_val.h"
46 #include "cst_lts_rewrites.h"
47
48 static int item_match(const cst_val *PATT, const cst_val *THING,
49 const cst_val *sets)
50 {
51 const cst_val *sss;
52
53 if (cst_streq(val_string(PATT),val_string(THING)))
54 return TRUE;
55
56 for (sss=sets; sss; sss=val_cdr(sss))
57 {
58 if (cst_streq(val_string(val_car(val_car(sss))),val_string(PATT)))
59 { /* Its a set not a letter */
60 if (val_member_string(val_string(THING),val_cdr(val_car(sss))))
61 {
62 return TRUE;
63 }
64 else
65 {
66 return FALSE;
67 }
68 }
69 }
70 return FALSE;
71 }
72
73 static int context_match(const cst_val *PATTERN, const cst_val *STRING,
74 const cst_val *sets)
75 {
76 int r,s,t;
77 /* printf("PATTERN: "); val_print(stdout, PATTERN); printf("\n"); */
78 /* printf("STRING: "); val_print(stdout, STRING); printf("\n"); */
79 if (!PATTERN)
80 r = TRUE;
81 else if (!STRING)
82 r = FALSE;
83 else if (val_cdr(PATTERN) &&
84 (cst_streq("*",val_string(val_car(PATTERN)))))
85 {
86 r = context_match(val_cdr(val_cdr(PATTERN)),STRING,sets);
87 s = context_match(val_cdr(PATTERN),STRING,sets);
88 t = item_match(val_car(val_cdr(PATTERN)),val_car(STRING),sets) &&
89 context_match(PATTERN, val_cdr(STRING),sets);
90 r = r || s || t;
91 }
92 #if 0
93 else if (val_cdr(PATTERN) &&
94 (cst_streq("+",val_string(val_car(PATTERN)))))
95 return context_match(val_cdr(PATTERN),STRING,sets) || /* last match */
96 (item_match(val_car(val_cdr(PATTERN)),val_car(STRING),sets) &&
97 context_match(val_cdr(val_cdr(PATTERN)), /* loop match */
98 val_cdr(STRING),sets));
99 #endif
100 else if (item_match(val_car(PATTERN),val_car(STRING),sets))
101 r = context_match(val_cdr(PATTERN),val_cdr(STRING),sets);
102 else
103 r = FALSE;
104 /* printf("R = %s\n",(r ? "TRUE" : "FALSE")); */
105 return r;
106 }
107
108 static int rule_matches(const cst_val *LC, const cst_val *RC,
109 const cst_val *RLC, const cst_val *RA,
110 const cst_val *RRC,
111 const cst_val *sets)
112 {
113 const cst_val *rc, *ra;
114
115 /* Check [ X ] bit */
116 for (rc=RC,ra=RA; ra; ra=val_cdr(ra),rc=val_cdr(rc))
117 {
118 if (!rc) return FALSE;
119 if (!cst_streq(val_string(val_car(ra)),
120 val_string(val_car(rc))))
121 return FALSE;
122 }
123
124 /* Check LC bit: LC may have some limited regex stuff */
125 if (context_match(RLC,LC,sets) && context_match(RRC,rc,sets))
126 return TRUE;
127 else
128 return FALSE;
129 }
130
131 static const cst_val *find_rewrite_rule(const cst_val *LC,
132 const cst_val *RC,
133 const cst_lts_rewrites *r)
134 {
135 /* Search through rewrite rules to find matching one */
136 const cst_val *i, *RLC, *RA, *RRC;
137
138 for (i=r->rules; i; i=val_cdr(i))
139 {
140 /* val_print(stdout, val_car(i)); printf("\n"); */
141 RLC = val_car(val_car(i));
142 RA = val_car(val_cdr(val_car(i)));
143 RRC = val_car(val_cdr(val_cdr(val_car(i))));
144 if (rule_matches(LC,RC,RLC,RA,RRC,r->sets))
145 return val_car(i);
146 }
147
148 #if 0
149 fprintf(stderr,"LTS_REWRITES: unable to find a matching rules for:\n");
150 fprintf(stderr,"CL: ");
151 val_print(stderr,LC);
152 fprintf(stderr,"\n");
153 fprintf(stderr,"RC: ");
154 val_print(stderr,RC);
155 fprintf(stderr,"\n");
156 #endif
157
158 return NULL;
159 }
160
161 cst_val *lts_rewrites_word(const char *word, const cst_lts_rewrites *r)
162 {
163 cst_val *w, *p;
164 char x[2];
165 int i;
166
167 x[1] = '\0';
168 w = cons_val(string_val("#"),NULL);
169 for (i=0; word[i]; i++)
170 {
171 x[0] = word[i];
172 w = cons_val(string_val(x),w);
173 }
174 w = cons_val(string_val("#"),w);
175
176 w = val_reverse(w);
177
178 p = lts_rewrites(w,r);
179
180 delete_val(w);
181
182 return p;
183 }
184
185 cst_val *lts_rewrites(const cst_val *itape, const cst_lts_rewrites *r)
186 {
187 /* Returns list of rewritten "letters" to "phones" by r */
188 cst_val *LC;
189 const cst_val *RC, *i;
190 const cst_val *rule;
191 cst_val *otape;
192
193 LC = cons_val(val_car(itape),NULL);
194 RC = val_cdr(itape);
195 otape = NULL;
196
197 while (val_cdr(RC))
198 {
199 rule = find_rewrite_rule(LC,RC,r);
200
201 if (!rule)
202 break;
203 /* val_print(stdout,rule);
204 printf("\n"); */
205
206 /* Shift itape head */
207 for (i=val_car(val_cdr(rule)); i; i=val_cdr(i))
208 {
209 LC = cons_val(val_car(RC),LC);
210 RC = val_cdr(RC);
211 }
212
213 /* Output things to otape */
214 for (i=val_car(val_cdr(val_cdr(val_cdr(rule)))); i; i=val_cdr(i))
215 otape = cons_val(val_car(i),otape);
216 }
217
218 delete_val(LC);
219
220 return val_reverse(otape);
221 }
222