Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scfg_train_main.cc
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1996,1997 */
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 /* THE UNIVERSITY OF EDINBURGH 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 THE UNIVERSITY OF EDINBURGH 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 */
34 /* Date : October 1997 */
35 /*-----------------------------------------------------------------------*/
36 /* Train a stochastic context free grammar with respect to a given */
37 /* corpus. */
38 /* */
39 /* Only the inside/outside algorithm (with bracketing) is supported */
40 /* */
41 /* */
42 /*=======================================================================*/
43 #include <cstdlib>
44 #include <cstdio>
45 #include <iostream>
46 #include <fstream>
47 #include <cstring>
48 #include "EST_cmd_line.h"
49 #include "EST_SCFG.h"
50 #include "siod.h"
51 
52 static EST_String outfile = "-";
53 
54 
55 static int scfg_train_main(int argc, char **argv);
56 
57 
58 int main(int argc, char **argv)
59 {
60 
61  scfg_train_main(argc,argv);
62 
63  exit(0);
64  return 0;
65 }
66 
67 static int scfg_train_main(int argc, char **argv)
68 {
69  // Top level function generates a probabilistic grammar
70  EST_Option al;
71  EST_StrList files;
72  int spread;
73 
74  parse_command_line
75  (argc, argv,
76  EST_String("[options\n")+
77  "Summary: Train a stochastic context free grammar from a (bracketed) corpus\n"+
78  "-grammar <ifile> Grammar file, one rule per line.\n"+
79  "-corpus <ifile> Corpus file, one bracketed sentence per line.\n"+
80  "-method <string> {inout}\n"+
81  " Method for training: inout.\n"+
82  "-passes <int> {50}\n"+
83  " Number of training passes.\n"+
84  "-startpass <int> {0}\n"+
85  " Starting at pass N.\n"+
86  "-spread <int> Spread training data over N passes.\n"+
87  "-checkpoint <int> Save grammar every N passes\n"+
88  "-heap <int> {210000}\n"+
89  " Set size of Lisp heap, needed for large corpora\n"+
90  "-o <ofile> Output file for trained grammar.\n",
91  files, al);
92 
93  if (al.present("-o"))
94  outfile = al.val("-o");
95  else
96  outfile = "-";
97 
98  siod_init(al.ival("-heap"));
99 
100  EST_SCFG_traintest grammar;
101 
102  if (al.present("-grammar"))
103  {
104  grammar.load(al.val("-grammar"));
105  }
106  else
107  {
108  cerr << "scfg_train: no grammar specified" << endl;
109  exit(1);
110  }
111 
112  if (al.present("-corpus"))
113  {
114  grammar.load_corpus(al.val("-corpus"));
115  }
116  else
117  {
118  cerr << "scfg_train: no corpus specified" << endl;
119  exit(1);
120  }
121 
122  if (al.present("-spread"))
123  spread = al.ival("-spread");
124  else
125  spread = 0;
126 
127  if (al.val("-method") == "inout")
128  {
129  int checkpoint = -1;
130  if (al.present("-checkpoint"))
131  checkpoint = al.ival("-checkpoint");
132 
133  grammar.train_inout(al.ival("-passes"),
134  al.ival("-startpass"),
135  checkpoint,spread,outfile);
136  }
137  else
138  {
139  cerr << "scfg_train: unknown training method \"" <<
140  al.val("-method") << "\"" << endl;
141  exit(1);
142  }
143 
144  if (grammar.save(outfile) != write_ok)
145  {
146  cerr << "scfg_train: failed to write grammar to \"" <<
147  outfile << "\"" << endl;
148  exit(1);
149  }
150 
151  return 0;
152 }
EST_read_status load(const EST_String &filename)
Load grammar from named file.
Definition: EST_SCFG.cc:193
A class used to train (and test) SCFGs is an extension of EST_SCFG .
Definition: EST_SCFG.h:256
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:76
const int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145