SphinxBase  0.6
main.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 2007 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
37 
38 #include <string.h>
39 
40 #include <sphinxbase/hash_table.h>
41 #include <sphinxbase/fsg_model.h>
42 #include <sphinxbase/jsgf.h>
43 #include <sphinxbase/err.h>
44 #include <sphinxbase/strfuncs.h>
45 
46 static const arg_t defn[] = {
47  { "-help",
49  "no",
50  "Shows the usage of the tool"},
51 
52  { "-jsgf",
54  NULL,
55  "Input grammar in jsgf format (required)"},
56 
57  { "-rule",
58  ARG_STRING,
59  NULL,
60  "Root rule name (optional)"},
61 
62  { "-fsg",
63  ARG_STRING,
64  NULL,
65  "Output grammar in fsg format"},
66 
67  { "-fsm",
68  ARG_STRING,
69  NULL,
70  "Output grammar in FSM format"},
71 
72  { "-symtab",
73  ARG_STRING,
74  NULL,
75  "Output symtab for grammar in FSM format"},
76 
77  { "-compile",
79  "no",
80  "Compute grammar closure to speedup loading"},
81 
82  { NULL, 0, NULL, NULL }
83 };
84 
85 
86 static void
87 usagemsg(char *pgm)
88 {
89  E_INFO("Usage: %s -jsgf <input.jsgf> -rule <rule name>\\\n", pgm);
90  E_INFOCONT("\t[-fsm yes/no] [-compile yes/no]\n");
91  E_INFOCONT("\t-fsg <output.fsg>\n");
92 
93  exit(0);
94 }
95 
96 static fsg_model_t *
97 get_fsg(jsgf_t *grammar, const char *name)
98 {
99  jsgf_rule_iter_t *itor;
100  logmath_t *lmath = logmath_init(1.0001, 0, 0);
101  fsg_model_t *fsg = NULL;
102 
103  for (itor = jsgf_rule_iter(grammar); itor;
104  itor = jsgf_rule_iter_next(itor)) {
105  jsgf_rule_t *rule = jsgf_rule_iter_rule(itor);
106  char const *rule_name = jsgf_rule_name(rule);
107 
108  if ((name == NULL && jsgf_rule_public(rule))
109  || (name && strlen(rule_name)-2 == strlen(name) &&
110  0 == strncmp(rule_name + 1, name, strlen(rule_name) - 2))) {
111  fsg = jsgf_build_fsg_raw(grammar, rule, logmath_retain(lmath), 1.0);
112  jsgf_rule_iter_free(itor);
113  break;
114  }
115  }
116 
117  logmath_free(lmath);
118  return fsg;
119 }
120 
121 int
122 main(int argc, char *argv[])
123 {
124  jsgf_t *jsgf;
125  fsg_model_t *fsg;
126  cmd_ln_t *config;
127 
128  if ((config = cmd_ln_parse_r(NULL, defn, argc, argv, TRUE)) == NULL)
129  return 1;
130 
131  if (cmd_ln_boolean_r(config, "-help")) {
132  usagemsg(argv[0]);
133  }
134 
135  jsgf = jsgf_parse_file(cmd_ln_str_r(config, "-jsgf"), NULL);
136  if (jsgf == NULL) {
137  return 1;
138  }
139 
140  fsg = get_fsg(jsgf, cmd_ln_str_r(config, "-rule") ? cmd_ln_str_r(config, "-rule") : NULL);
141 
142  if (cmd_ln_boolean_r(config, "-compile")) {
143  fsg_model_null_trans_closure(fsg, NULL);
144  }
145 
146 
147  if (cmd_ln_str_r(config, "-fsm")) {
148  const char* outfile = cmd_ln_str_r(config, "-fsm");
149  const char* symfile = cmd_ln_str_r(config, "-symtab");
150  if (outfile)
151  fsg_model_writefile_fsm(fsg, outfile);
152  else
153  fsg_model_write_fsm(fsg, stdout);
154  if (symfile)
155  fsg_model_writefile_symtab(fsg, symfile);
156  }
157  else {
158  const char *outfile = cmd_ln_str_r(config, "-fsg");
159  if (outfile)
160  fsg_model_writefile(fsg, outfile);
161  else
162  fsg_model_write(fsg, stdout);
163  }
164  fsg_model_free(fsg);
165  jsgf_grammar_free(jsgf);
166 
167  return 0;
168 }
169 
170 
171 #if defined(_WIN32_WCE)
172 #pragma comment(linker,"/entry:mainWCRTStartup")
173 #include <windows.h>
174 
175 //Windows Mobile has the Unicode main only
176 int wmain(int32 argc, wchar_t *wargv[]) {
177  char** argv;
178  size_t wlen;
179  size_t len;
180  int i;
181 
182  argv = malloc(argc*sizeof(char*));
183  for (i=0; i<argc; i++){
184  wlen = lstrlenW(wargv[i]);
185  len = wcstombs(NULL, wargv[i], wlen);
186  argv[i] = malloc(len+1);
187  wcstombs(argv[i], wargv[i], wlen);
188  }
189 
190  //assuming ASCII parameters
191  return main(argc, argv);
192 }
193 #endif