57 static int scfg_make_main(
int argc,
char **argv);
61 static LISP assign_probs(LISP rules,
const EST_String &domain,
63 static LISP make_all_rules(
const EST_StrList &NonTerminals,
65 static void generate_probs(
double *probs,
int num);
70 int main(
int argc,
char **argv)
73 scfg_make_main(argc,argv);
79 static int scfg_make_main(
int argc,
char **argv)
91 "Summary: Build a stochastic context free grammar\n"+
92 "-nonterms <string> Number of nonterminals or file containing them\n"+
93 "-terms <string> Number of terminals or file containing them\n"+
94 "-domain <string> {nlogp}\n"+
95 " Values to be nlogp (negative log probabilities)\n"+
96 " or prob (probabilities)\n"+
97 "-values <string> {equal}\n"+
98 " General initial scores on rules as equal or\n" 100 "-heap <int> {500000}\n"+
101 " Set size of Lisp heap, only needed for large grammars\n"+
102 "-o <ofile> File to save grammar (default stdout)\n",
106 outfile = al.
val(
"-o");
112 if (al.
val(
"-domain") ==
"nlogp")
114 else if (al.
val(
"-domain") ==
"prob")
118 cerr <<
"scfg_make: domain must be nlogp or prob" << endl;
125 if (al.
val(
"-values") ==
"equal")
127 else if (al.
val(
"-values") ==
"random")
131 cerr <<
"scfg_make: values must be equal or random" << endl;
139 make_symbols(NonTerminals,al.
ival(
"-nonterms"),
"NT");
141 load_symbols(NonTerminals,al.
val(
"-nonterms"));
145 cerr <<
"scfg_make: no nonterminals specified" << endl;
152 make_symbols(Terminals,al.
ival(
"-terms"),
"T");
154 load_symbols(Terminals,al.
val(
"-terms"));
158 cerr <<
"scfg_make: no terminals specified" << endl;
164 rules = make_all_rules(NonTerminals,Terminals);
165 rules = assign_probs(rules,domain,values);
171 if ((fd=fopen(outfile,
"w")) ==
NULL)
173 cerr <<
"scfg_make: failed to open file \"" << outfile <<
174 "\" for writing" << endl;
179 for (r=rules; r !=
NIL; r=
cdr(r))
189 static LISP make_all_rules(
const EST_StrList &NonTerminals,
197 for (p=NonTerminals.
head(); p != 0; p=p->
next())
199 int num_rules_nt = (NonTerminals.
length()*NonTerminals.
length())+
201 double *probs =
new double[num_rules_nt];
202 generate_probs(probs,num_rules_nt);
204 for (q=NonTerminals.
head(); q != 0; q=q->
next())
205 for (r=NonTerminals.
head(); r != 0; r=r->
next(),i++)
211 for (q=Terminals.
head(); q != 0; q=q->
next(),i++)
222 static void generate_probs(
double *probs,
int num)
227 if (values ==
"equal")
229 double defp = 1.0/(float)num;
230 for (i=0; i < num; i++)
233 else if (values ==
"random")
237 for (i=0; i < num; i++)
239 probs[i] = (double)abs(rand())/(
double)0x7fff;
242 for (i=0; i < num; i++)
249 cerr <<
"scfg_make: unknown value for probability distribution" 255 static LISP assign_probs(LISP rules,
const EST_String &domain,
262 if (domain ==
"nlogp")
263 for (r=rules; r !=
NIL; r =
cdr(r))
280 for (magnitude=0,t=n; t > 0; t=t/10)
285 sprintf(skel,
"%s%%%02dd",(
const char *)prefix,magnitude);
287 for (i=0; i < n; i++)
289 sprintf(name,skel,i);
int main(int argc, char **argv)
#define walloc(TYPE, SIZE)
float get_c_float(LISP x)
int ival(const EST_String &rkey, int m=1) const
EST_Regex RXint("-?[0-9]+")
Integer.
LISP cons(LISP x, LISP y)
int matches(const char *e, ssize_t pos=0) const
Exactly match this string?
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
void append(const T &item)
add item onto end of list
void pprint_to_fd(FILE *fd, LISP exp)
size_t length(void) const
Length of string ({not} length of underlying chunk)
LISP rintern(const char *name)
int present(const K &rkey) const
Returns true if key is present.
int siod_init(int heap_size=DEFAULT_HEAP_SIZE)
float sum(const EST_FMatrix &a)
sum of elements
EST_read_status load_StrList(EST_String filename, EST_StrList &l)
Load tokens from a file and return them in a EST_StrList.
void reverse(EST_Wave &sig)
int parse_command_line(int argc, char *argv[], const EST_String &usage, EST_StrList &files, EST_Option &al, int make_stdio=1)