Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
wfst_run_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 : December 1997 */
35 /*-----------------------------------------------------------------------*/
36 /* Run a WFST on some data, either as a recognizer or as a transducer */
37 /* */
38 /*=======================================================================*/
39 #include <cstdlib>
40 #include <cstdio>
41 #include <iostream>
42 #include <fstream>
43 #include <cstring>
44 #include "EST.h"
45 #include "EST_simplestats.h"
46 #include "EST_WFST.h"
47 
48 static int wfst_run_main(int argc, char **argv);
49 
50 /** @name <command>wfst_run</command> <emphasis>Run a weighted finite-state transducer</emphasis>
51  @id wfst-run-manual
52  * @toc
53  */
54 
55 //@{
56 
57 
58 /**@name Synopsis
59  */
60 //@{
61 
62 //@synopsis
63 
64 /**
65 This program runs a WFST on some given data. It works in either
66 recognize mode where both inputs and output are specified, but also
67 in transduction mode where an input is transduced to the output.
68 
69  */
70 
71 //@}
72 
73 /**@name OPTIONS
74  */
75 //@{
76 
77 //@options
78 
79 //@}
80 
81 
82 int main(int argc, char **argv)
83 {
84 
85  wfst_run_main(argc,argv);
86 
87  exit(0);
88  return 0;
89 }
90 
91 static int wfst_run_main(int argc, char **argv)
92 {
93  // recognize/transduce
94  EST_Option al;
95  EST_StrList files;
96  EST_Litem *f;
97  EST_String wfstfile;
98  FILE *ofd;
99  int r;
100  EST_SuffStats R;
101  float sumlogp=0,isumlogp;
102  float count=0,icount;
103 
104  parse_command_line
105  (argc, argv,
106  EST_String("[WFSTFILE] [input file0] ... [-o output file]\n")+
107  "Summary: Recognize/transduce using a WFST on data\n"+
108  "-wfst <ifile> The WFST to use\n"+
109  "-transduce Transduce input to output (default)\n"+
110  "-recog Recognize input consists of pairs\n"+
111  "-cumulate_into <ofile>\n"+
112  " Cumulate transitions to give new weights\n"+
113  " save new WFST into ofile\n"+
114  "-itype <string> char or token\n"+
115  "-quiet No extraneous messages\n"+
116  "-perplexity Calculate perplexity on given data set\n"+
117  "-heap <int> {210000}\n"+
118  " Set size of Lisp heap, needed for large wfsts\n"+
119  "-o <ofile> Output file for transduced forms\n",
120  files, al);
121 
122  if (al.present("-o"))
123  {
124  if ((ofd=fopen(al.val("-o"),"w")) == NULL)
125  EST_error("can't open output file for writing \"%s\"",
126  (const char *)al.val("-o"));
127  }
128  else
129  ofd = stdout;
130 
131  if (al.present("-wfst"))
132  wfstfile = al.val("-wfst");
133  else
134  EST_error("no WFST specified");
135 
136  siod_init(al.ival("-heap"));
137 
138  EST_WFST wfst;
139  EST_TokenStream ts;
140 
141  if (wfst.load(wfstfile) != format_ok)
142  EST_error("failed to read WFST from \"%s\"",
143  (const char *)wfstfile);
144 
145  if (al.present("-cumulate_into"))
146  wfst.start_cumulate();
147 
148  for (f=files.head(); f != 0; f=f->next())
149  {
150  if (files(f) == "-")
151  ts.open(stdin,FALSE);
152  else
153  if (ts.open(files(f)) != 0)
154  EST_error("failed to read WFST data file from \"%s\"",
155  (const char *)files(f));
156 
157  // Not the best way to input things but will do the the present
158  while(!ts.eof())
159  {
160  EST_StrList ostrs,istrs;
161  do
162  istrs.append(ts.get());
163  while((!ts.eof()) && (!ts.eoln()));
164 
165  if (al.present("-recog"))
166  {
167  if (al.present("-perplexity"))
168  {
169  r = recognize_for_perplexity(wfst,istrs,
170  al.present("-quiet"),
171  icount,
172  isumlogp);
173  if (r)
174  {
175  count += icount;
176  sumlogp += isumlogp;
177  }
178  }
179  else
180  r = recognize(wfst,istrs,al.present("-quiet"));
181  }
182  else
183  {
184  r = transduce(wfst,istrs,ostrs);
185  if (r)
186  {
187  cout << ostrs;
188  cout << endl;
189  }
190  }
191  R += r;
192 
193  if (!al.present("-quiet"))
194  {
195  if (r)
196  cout << "OK." << endl;
197  else
198  cout << "failed." << endl;
199  }
200  }
201  ts.close();
202  }
203 
204  if (al.present("-cumulate_into"))
205  {
206  wfst.stop_cumulate();
207  if (wfst.save(al.val("-cumulate_into")) != write_ok)
208  EST_error("failed to write cumulated WFST to \"%s\"",
209  (const char *)al.val("-cumulate_into"));
210  }
211 
212  printf("total %d OK %f%% failed %f%%\n",
213  (int)R.samples(),R.mean()*100,(1-R.mean())*100);
214  if (al.present("-perplexity"))
215  {
216  printf("perplexity is %f\n", pow(float(2.0),float(-1.0 * (sumlogp/count))));
217  }
218 
219  if (ofd != stdout)
220  fclose(ofd);
221 
222  if (R.mean() == 1) // true is *all* files were recognized
223  return 0;
224  else
225  return -1;
226 }
227