55static int scfg_make_main(
int argc,
char **
argv);
63static void generate_probs(
double *
probs,
int num);
109static int scfg_make_main(
int argc,
char **
argv)
121 "Summary: Build a stochastic context free grammar\n"+
122 "-nonterms <string> Number of nonterminals or file containing them\n"+
123 "-terms <string> Number of terminals or file containing them\n"+
124 "-domain <string> {nlogp}\n"+
125 " Values to be nlogp (negative log probabilities)\n"+
126 " or prob (probabilities)\n"+
127 "-values <string> {equal}\n"+
128 " General initial scores on rules as equal or\n"
130 "-heap <int> {500000}\n"+
131 " Set size of Lisp heap, only needed for large grammars\n"+
132 "-o <ofile> File to save grammar (default stdout)\n",
135 if (
al.present(
"-o"))
136 outfile =
al.val(
"-o");
140 if (
al.present(
"-domain"))
142 if (
al.val(
"-domain") ==
"nlogp")
144 else if (
al.val(
"-domain") ==
"prob")
148 cerr <<
"scfg_make: domain must be nlogp or prob" <<
endl;
153 if (
al.present(
"-values"))
155 if (
al.val(
"-values") ==
"equal")
157 else if (
al.val(
"-values") ==
"random")
161 cerr <<
"scfg_make: values must be equal or random" <<
endl;
166 if (
al.present(
"-nonterms"))
168 if (
al.val(
"-nonterms").matches(RXint))
175 cerr <<
"scfg_make: no nonterminals specified" <<
endl;
179 if (
al.present(
"-terms"))
181 if (
al.val(
"-terms").matches(RXint))
188 cerr <<
"scfg_make: no terminals specified" <<
endl;
192 siod_init(
al.ival(
"-heap"));
195 rules = assign_probs(rules,domain,values);
201 if ((fd=
fopen(outfile,
"w")) == NULL)
203 cerr <<
"scfg_make: failed to open file \"" << outfile <<
204 "\" for writing" <<
endl;
209 for (r=rules; r != NIL; r=cdr(r))
210 pprint_to_fd(fd,car(r));
236 rules = cons(cons(flocons(
probs[i]),
242 rules = cons(cons(flocons(
probs[i]),
249 return reverse(rules);
252static void generate_probs(
double *
probs,
int num)
257 if (values ==
"equal")
260 for (i=0; i <
num; i++)
263 else if (values ==
"random")
267 for (i=0; i <
num; i++)
272 for (i=0; i <
num; i++)
279 cerr <<
"scfg_make: unknown value for probability distribution"
292 if (domain ==
"nlogp")
293 for (r=rules; r != NIL; r = cdr(r))
295 if (get_c_float(car(car(r))) == 0)
296 CAR(car(r)) = flocons(40);
298 CAR(car(r)) = flocons(-
log(get_c_float(car(car(r)))));
317 for (i=0; i < n; i++)
333 load_StrList(filename,
syms);