Edinburgh Speech Tools  2.1-release
scfg_train_main.cc
Go to the documentation of this file.
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 using namespace std;
53 
54 static EST_String outfile = "-";
55 
56 
57 static int scfg_train_main(int argc, char **argv);
58 
59 
60 int main(int argc, char **argv)
61 {
62 
63  scfg_train_main(argc,argv);
64 
65  exit(0);
66  return 0;
67 }
68 
69 static int scfg_train_main(int argc, char **argv)
70 {
71  // Top level function generates a probabilistic grammar
72  EST_Option al;
73  EST_StrList files;
74  int spread;
75 
77  (argc, argv,
78  EST_String("[options\n")+
79  "Summary: Train a stochastic context free grammar from a (bracketed) corpus\n"+
80  "-grammar <ifile> Grammar file, one rule per line.\n"+
81  "-corpus <ifile> Corpus file, one bracketed sentence per line.\n"+
82  "-method <string> {inout}\n"+
83  " Method for training: inout.\n"+
84  "-passes <int> {50}\n"+
85  " Number of training passes.\n"+
86  "-startpass <int> {0}\n"+
87  " Starting at pass N.\n"+
88  "-spread <int> Spread training data over N passes.\n"+
89  "-checkpoint <int> Save grammar every N passes\n"+
90  "-heap <int> {210000}\n"+
91  " Set size of Lisp heap, needed for large corpora\n"+
92  "-o <ofile> Output file for trained grammar.\n",
93  files, al);
94 
95  if (al.present("-o"))
96  outfile = al.val("-o");
97  else
98  outfile = "-";
99 
100  siod_init(al.ival("-heap"));
101 
102  EST_SCFG_traintest grammar;
103 
104  if (al.present("-grammar"))
105  {
106  grammar.load(al.val("-grammar"));
107  }
108  else
109  {
110  cerr << "scfg_train: no grammar specified" << endl;
111  exit(1);
112  }
113 
114  if (al.present("-corpus"))
115  {
116  grammar.load_corpus(al.val("-corpus"));
117  }
118  else
119  {
120  cerr << "scfg_train: no corpus specified" << endl;
121  exit(1);
122  }
123 
124  if (al.present("-spread"))
125  spread = al.ival("-spread");
126  else
127  spread = 0;
128 
129  if (al.val("-method") == "inout")
130  {
131  int checkpoint = -1;
132  if (al.present("-checkpoint"))
133  checkpoint = al.ival("-checkpoint");
134 
135  grammar.train_inout(al.ival("-passes"),
136  al.ival("-startpass"),
137  checkpoint,spread,outfile);
138  }
139  else
140  {
141  cerr << "scfg_train: unknown training method \"" <<
142  al.val("-method") << "\"" << endl;
143  exit(1);
144  }
145 
146  if (grammar.save(outfile) != write_ok)
147  {
148  cerr << "scfg_train: failed to write grammar to \"" <<
149  outfile << "\"" << endl;
150  exit(1);
151  }
152 
153  return 0;
154 }
EST_read_status load(const EST_String &filename)
Load grammar from named file.
Definition: EST_SCFG.cc:197
A class used to train (and test) SCFGs is an extension of EST_SCFG.
Definition: EST_SCFG.h:259
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:82
int main(int argc, char **argv)
The file was written successfully.
EST_String outfile
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
int siod_init(int heap_size=DEFAULT_HEAP_SIZE)
Definition: siod.cc:58
EST_String
int parse_command_line(int argc, char *argv[], const EST_String &usage, EST_StrList &files, EST_Option &al, int make_stdio=1)
Definition: cmd_line.cc:101