44 #include "EST_cutils.h"
46 ostream &operator << (ostream &s,
const EST_WFST &w)
49 return s <<
"<<EST_WFST>>";
54 #if defined(INSTANTIATE_TEMPLATES)
55 #include "../base_class/EST_TList.cc"
61 static LISP expand_fp(
const EST_String p,LISP fp);
62 static LISP find_feasible_pairs(LISP rules);
63 static LISP all_but(LISP rulepair,LISP fp);
64 static LISP expand_sets(LISP sets,LISP fp);
65 static LISP inline_sets(LISP l, LISP sets);
66 static void full_kkcompile(LISP inalpha,LISP outalpha,
67 LISP fp, LISP rules, LISP sets,
70 void kkcompile(LISP ruleset,
EST_WFST &all_wfst)
78 LISP inalpha = siod_nth(1,siod_assoc_str(
"Alphabets",cdr(cdr(ruleset))));
79 LISP outalpha = siod_nth(2,siod_assoc_str(
"Alphabets",cdr(cdr(ruleset))));
80 LISP sets = cdr(siod_assoc_str(
"Sets",ruleset));
81 LISP rules = cdr(siod_assoc_str(
"Rules",ruleset));
83 fp = find_feasible_pairs(rules);
84 sets = expand_sets(sets,fp);
86 full_kkcompile(inalpha,outalpha,fp,rules,sets,all_wfst);
89 static void full_kkcompile(LISP inalpha,LISP outalpha,
90 LISP fp, LISP rules, LISP sets,
96 for (r=rules; r != NIL; r=cdr(r))
101 cout <<
"Rule: " << siod_llength(rules)-siod_llength(r) << endl;
103 base_wfst.kkrule_compile(inalpha,outalpha,fp,car(r),sets);
104 cout <<
" base " << base_wfst.summary() << endl;
106 cout <<
" determinized " << det_wfst.summary() << endl;
108 cout <<
" minimized " << rr_wfst.summary() << endl;
111 cout <<
"WFST: intersecting " << rulelist.length() <<
" rules" << endl;
114 for (i=0,p=rulelist.head(); p->next() != 0; p=nnp)
120 cout <<
"intersecting " << i <<
" and " << i+1 <<
" " <<
121 rulelist.length()-2 <<
" left" << endl;
122 cout <<
" " << rulelist(p).summary() <<
" and " << endl;
123 cout <<
" " << rulelist(p->next()).summary() <<
" becomes " << endl;
125 cout <<
" " << mmm.summary() <<
" minimizes to " << endl;
127 cout <<
" " << rr_wfst.summary() << endl;
128 nnp=p->next()->next();
130 rulelist.
remove(p->next());
134 all_wfst = rulelist.
first();
138 static LISP expand_sets(LISP sets,LISP fp)
145 for (s=sets; s != NIL; s=cdr(s))
147 for (ne=NIL,e=cdr(car(s)); e != NIL; e=cdr(e))
151 ne = cons(car(e),ne);
153 ne = append(expand_fp(ss,fp),ne);
157 cerr <<
"WFST: kkcompile: set " << get_c_string(car(car(s))) <<
158 " has no feasible pairs" << endl;
161 else if (siod_llength(ne) == 1)
162 es = cons(cons(car(car(s)),ne),es);
164 es = cons(cons(car(car(s)),
165 cons(cons(rintern(
"or"),reverse(ne)),
172 static LISP expand_fp(
const EST_String p,LISP fp)
178 for (f=fp; f != NIL; f=cdr(f))
181 if ((p == ss) || (ss.
matches(rg)))
187 static LISP find_feasible_pairs(LISP rules)
194 for (r=rules; r != NIL; r=cdr(r))
196 if (siod_member_str(get_c_string(siod_nth(0,car(r))),fp) == NIL)
197 fp = cons(siod_nth(0,car(r)),fp);
202 static int surface_coercion(LISP rt)
204 return (streq(
"<=",get_c_string(rt)));
207 static int context_restriction(LISP rt)
209 return (streq(
"=>",get_c_string(rt)));
212 static int composite(LISP rt)
214 return (streq(
"<=>",get_c_string(rt)));
217 static LISP inline_sets(LISP l, LISP sets)
224 return cons(inline_sets(car(l),sets),inline_sets(cdr(l),sets));
225 else if ((s=siod_assoc_str(get_c_string(l),sets)) != NIL)
231 void EST_WFST::kkrule_compile(LISP inalpha, LISP outalpha, LISP fp,
236 LISP leftcontext = inline_sets(siod_nth(2,rule),sets);
237 LISP rulepair = siod_nth(0,rule);
238 LISP ruletype = siod_nth(1,rule);
239 LISP rightcontext = inline_sets(siod_nth(4,rule),sets);
242 int end_LC,end_RP,end_NOTRP,end_RC,err_state;
245 init(inalpha,outalpha);
249 for (p=fp; p != NIL; p=cdr(p))
250 if ((!equal(rulepair,car(p))) ||
251 (surface_coercion(ruletype)))
252 build_wfst(p_start_state,p_start_state,car(p));
260 for (i=end_LC; i < p_num_states; i++)
263 p_states[i]->set_type(wfst_final);
267 end_LC = p_start_state;
270 if (composite(ruletype) || context_restriction(ruletype))
277 build_wfst(end_RP,p_start_state,rightcontext);
279 for (i=end_RP; i < err_state; i++)
286 for (p=fp; p != NIL; p=cdr(p))
287 if (
transition(i,get_c_string(car(p))) == WFST_ERROR_STATE)
290 p_states[i]->set_type(wfst_licence);
298 if (composite(ruletype) || surface_coercion(ruletype))
300 LISP abrp = all_but(rulepair,fp);
312 for (i=end_NOTRP; i < p_num_states; i++)
315 p_states[i]->set_type(wfst_final);
327 static LISP all_but(LISP rulepair,LISP fp)
334 p = get_c_string(rulepair);
346 for (r=fp; r != NIL; r = cdr(r))
348 rr = get_c_string(car(r));
359 if ((l != lr) && (s == sr))
360 notrp = cons(car(r),notrp);
363 if (siod_llength(notrp) > 1)
364 notrp = cons(strintern(
"or"),notrp);