1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*************************************************************************/
/* */
/* Centre for Speech Technology Research */
/* University of Edinburgh, UK */
/* Copyright (c) 1997 */
/* All Rights Reserved. */
/* */
/* Permission is hereby granted, free of charge, to use and distribute */
/* this software and its documentation without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of this work, and to */
/* permit persons to whom this work is furnished to do so, subject to */
/* the following conditions: */
/* 1. The code must retain the above copyright notice, this list of */
/* conditions and the following disclaimer. */
/* 2. Any modifications must be clearly marked as such. */
/* 3. Original authors' names are not deleted. */
/* 4. The authors' names are not used to endorse or promote products */
/* derived from this software without specific prior written */
/* permission. */
/* */
/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
/* THIS SOFTWARE. */
/* */
/*************************************************************************/
/* Author : Alan W Black */
/* Date : October 1997 */
/*-----------------------------------------------------------------------*/
/* */
/* A class for representing Stochastic Context Free Grammars */
/* */
/*=======================================================================*/
#include <iostream>
#include "EST_Pathname.h"
#include "EST_SCFG.h"
EST_SCFG_Rule::EST_SCFG_Rule(double prob,int p, int m)
{
set_rule(prob,p,m);
}
EST_SCFG_Rule::EST_SCFG_Rule(double prob,int p, int q, int r)
{
set_rule(prob,p,q,r);
}
void EST_SCFG_Rule::set_rule(double prob,int p, int m)
{
p_prob = prob;
p_mother = p;
p_daughter1 = m;
p_type = est_scfg_unary_rule;
}
void EST_SCFG_Rule::set_rule(double prob,int p, int q, int r)
{
p_prob = prob;
p_mother = p;
p_daughter1 = q;
p_daughter2 = r;
p_type = est_scfg_binary_rule;
}
EST_SCFG::EST_SCFG()
{
p_prob_B=0;
p_prob_U=0;
}
EST_SCFG::EST_SCFG(LISP rs)
{
p_prob_B=0;
p_prob_U=0;
set_rules(rs);
}
EST_SCFG::~EST_SCFG(void)
{
delete_rule_prob_cache();
}
void EST_SCFG::find_terms_nonterms(EST_StrList &nt, EST_StrList &t,LISP rs)
{
// Cummulate the nonterminals and terminals
LISP r;
for (r=rs; r != NIL; r=cdr(r))
{
LISP p = car(cdr(car(r)));
if (!strlist_member(nt,get_c_string(p)))
nt.append(get_c_string(p));
if (siod_llength(car(r)) == 3) // unary rule
{
LISP d = car(cdr(cdr(car(r))));
if (!strlist_member(t,get_c_string(d)))
t.append(get_c_string(d));
}
else // binary rules
{
LISP d1 = car(cdr(cdr(car(r))));
LISP d2 = car(cdr(cdr(cdr(car(r)))));
if (!strlist_member(nt,get_c_string(d1)))
nt.append(get_c_string(d1));
if (!strlist_member(nt,get_c_string(d2)))
nt.append(get_c_string(d2));
}
}
}
void EST_SCFG::set_rules(LISP lrules)
{
// Initialise rule base from Lisp form
LISP r;
EST_StrList nt_list, term_list;
rules.clear();
delete_rule_prob_cache();
find_terms_nonterms(nt_list,term_list,lrules);
nonterminals.init(nt_list);
terminals.init(term_list);
if (!consp(car(cdr(car(lrules)))))
p_distinguished_symbol =
nonterminal(get_c_string(car(cdr(car(lrules)))));
else
cerr << "SCFG: no distinguished symbol" << endl;
for (r=lrules; r != NIL; r=cdr(r))
{
if ((siod_llength(car(r)) < 3) ||
(siod_llength(car(r)) > 4) ||
(!numberp(car(car(r)))))
cerr << "SCFG rule is malformed" << endl;
// est_error("SCFG rule is malformed\n");
else
{
EST_SCFG_Rule rule;
if (siod_llength(car(r)) == 3)
{
int m = nonterminal(get_c_string(car(cdr(car(r)))));
int d = terminal(get_c_string(car(cdr(cdr(car(r))))));
rule.set_rule(get_c_float(car(car(r))),m,d);
}
else
{
int p = nonterminal(get_c_string(car(cdr(car(r)))));
int d1=nonterminal(get_c_string(car(cdr(cdr(car(r))))));
int d2 = nonterminal(get_c_string(car(cdr(cdr(cdr(car(r)))))));
rule.set_rule(get_c_float(car(car(r))),p,d1,d2);
}
rules.append(rule);
}
}
rule_prob_cache();
}
LISP EST_SCFG::get_rules()
{
// Return LISP form of rules
EST_Litem *p;
LISP r;
for (r=NIL,p=rules.head(); p != 0; p=next(p))
{
if (rules(p).type() == est_scfg_unary_rule)
r = cons(cons(flocons(rules(p).prob()),
cons(rintern(nonterminal(rules(p).mother())),
cons(rintern(terminal(rules(p).daughter1())),NIL))),
r);
else if (rules(p).type() == est_scfg_binary_rule)
r = cons(cons(flocons(rules(p).prob()),
cons(rintern(nonterminal(rules(p).mother())),
cons(rintern(nonterminal(rules(p).daughter1())),
cons(rintern(nonterminal(rules(p).daughter2())),
NIL)))),
r);
}
return reverse(r);
}
EST_read_status EST_SCFG::load(const EST_String &filename)
{
LISP rs;
rs = vload(filename,1);
set_rules(rs);
return format_ok;
}
EST_write_status EST_SCFG::save(const EST_String &filename)
{
EST_Pathname outfile(filename);
FILE *fd;
LISP r;
if (outfile == "-")
fd = stdout;
else
{
if ((fd=fopen(outfile,"w")) == NULL)
{
cerr << "scfg_train: failed to open file \"" << outfile <<
"\" for writing" << endl;
return misc_write_error;
}
}
for (r=get_rules(); r != NIL; r=cdr(r))
pprint_to_fd(fd,car(r));
if (fd != stdout)
fclose(fd);
return write_ok;
}
void EST_SCFG::rule_prob_cache()
{
// Build access cache for the probabilities of binary rules
// This will have to made much more efficient
int i,j;
p_prob_B = new double**[num_nonterminals()];
p_prob_U = new double*[num_nonterminals()];
for (i=0; i < num_nonterminals(); i++)
{
p_prob_B[i] = new double*[num_nonterminals()];
p_prob_U[i] = new double[num_terminals()];
memset(p_prob_U[i],0,sizeof(double)*num_terminals());
for (j=0; j < num_nonterminals(); j++)
{
p_prob_B[i][j] = new double[num_nonterminals()];
memset(p_prob_B[i][j],0,sizeof(double)*num_nonterminals());
}
}
set_rule_prob_cache();
}
void EST_SCFG::set_rule_prob_cache()
{
EST_Litem *pp;
for (pp=rules.head(); pp != 0; pp = next(pp))
{
if (rules(pp).type() == est_scfg_binary_rule)
{
int p = rules(pp).mother();
int q = rules(pp).daughter1();
int r = rules(pp).daughter2();
p_prob_B[p][q][r] = rules(pp).prob();
}
else if (rules(pp).type() == est_scfg_unary_rule)
{
int p = rules(pp).mother();
int m = rules(pp).daughter1();
p_prob_U[p][m] = rules(pp).prob();
}
}
}
void EST_SCFG::delete_rule_prob_cache()
{
int i,j;
if (p_prob_B == 0)
return;
for (i=0; i < num_nonterminals(); i++)
{
for (j=0; j < num_nonterminals(); j++)
delete [] p_prob_B[i][j];
delete [] p_prob_B[i];
delete [] p_prob_U[i];
}
delete [] p_prob_B;
delete [] p_prob_U;
p_prob_B = 0;
p_prob_U = 0;
}
ostream &operator << (ostream &s, const EST_SCFG_Rule &rule)
{
(void)rule;
return s << "<<EST_SCFG_Rule>>";
}
Declare_TList(EST_SCFG_Rule)
#if defined(INSTANTIATE_TEMPLATES)
#include "../base_class/EST_TList.cc"
#include "../base_class/EST_TSortable.cc"
Instantiate_TList(EST_SCFG_Rule)
#endif
|