Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
scfg_parse_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 /* Parse a list of sentences with a given stochastic context free */
37 /* grammar */
38 /* */
39 /*=======================================================================*/
40 #include <cstdlib>
41 #include <cstdio>
42 #include <iostream>
43 #include <fstream>
44 #include <cstring>
45 #include "EST.h"
46 #include "EST_SCFG.h"
47 #include "EST_SCFG_Chart.h"
48 #include "siod.h"
49 
50 static EST_String outfile = "-";
51 
52 static int scfg_parse_main(int argc, char **argv);
53 
54 
55 int main(int argc, char **argv)
56 {
57 
58  scfg_parse_main(argc,argv);
59 
60  exit(0);
61  return 0;
62 }
63 
64 static int scfg_parse_main(int argc, char **argv)
65 {
66  // Top level function generates a probabilistic grammar
67  EST_Option al;
68  EST_StrList files;
69  EST_SCFG_Chart chart;
70  LISP rules,s,parse;
71  FILE *corpus,*output;
72  int i;
73 
74  parse_command_line
75  (argc, argv,
76  EST_String("[options]\n")+
77  "Summary: Parse a corpus with a stochastic context free grammar\n"+
78  "-grammar <ifile> Grammar file, one rule per line.\n"+
79  "-corpus <ifile> Corpus file, one bracketed sentence per line.\n"+
80  "-brackets Output bracketing only.\n"+
81  "-o <ofile> Output file for parsed sentences.\n",
82  files, al);
83 
84  if (al.present("-o"))
85  outfile = al.val("-o");
86  else
87  outfile = "-";
88 
89  siod_init();
90 
91  if (al.present("-grammar"))
92  {
93  rules = vload(al.val("-grammar"),1);
94  gc_protect(&rules);
95  }
96  else
97  {
98  cerr << "scfg_parse: no grammar specified" << endl;
99  exit(1);
100  }
101 
102  if (al.present("-corpus"))
103  {
104  if ((corpus = fopen(al.val("-corpus"),"r")) == NULL)
105  {
106  cerr << "scfg_parse: can't open corpus file \"" <<
107  al.val("-corpus") << "\" for reading " << endl;
108  exit(1);
109  }
110  }
111  else
112  {
113  cerr << "scfg_parse: no corpus specified" << endl;
114  exit(1);
115  }
116 
117  if (al.present("-o"))
118  {
119  if ((output=fopen(al.val("-o"),"w")) == NULL)
120  {
121  cerr << "scfg_parse: can't open output file \"" <<
122  al.val("-o") << "\" for writing " << endl;
123  exit(1);
124  }
125  }
126  else
127  output = stdout;
128 
129  gc_protect(&s);
130  gc_protect(&parse);
131  for (i=0; ((s=lreadf(corpus)) != get_eof_val()); i++)
132  {
133  parse = scfg_parse(s,rules);
134  if (al.present("-brackets"))
135  {
136  LISP bparse = scfg_bracketing_only(parse);
137  if (bparse == NIL)
138  bparse = s;
139  pprint_to_fd(output,bparse);
140  }
141  else
142  pprint_to_fd(output,parse);
143  if (i%100 == 99)
144  user_gc(NIL);
145  }
146 
147  if (output != stdout)
148  fclose(output);
149  gc_unprotect(&s);
150  gc_unprotect(&parse);
151  gc_unprotect(&rules);
152 
153  return 0;
154 }
155 
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
A class for parsing with a probabilistic grammars.