Debian Codegraph @

src/sphinx_jsgf2fsg/main.c

raw
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",
48 ARG_BOOLEAN,
49 "no",
50 "Shows the usage of the tool"},
51
52 { "-jsgf",
53 REQARG_STRING,
54 NULL,
55 "Input grammar in jsgf format (required)"},
56
57 { "-toprule",
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",
78 ARG_BOOLEAN,
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> -toprule <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 logmath_t *lmath;
100 fsg_model_t *fsg;
101 jsgf_rule_t *rule;
102
103 /* Take the -toprule if specified. */
104 if (name) {
105 rule = jsgf_get_rule(grammar, name);
106 if (rule == NULL) {
107 E_ERROR("Start rule %s not found\n", name);
108 return NULL;
109 }
110 } else {
111 rule = jsgf_get_public_rule(grammar);
112 if (rule == NULL) {
113 E_ERROR("No public rules found in grammar %s\n", jsgf_grammar_name(grammar));
114 return NULL;
115 } else {
116 E_INFO("No -toprule was given; grabbing the first public rule: "
117 "'%s' of the grammar '%s'.\n",
118 jsgf_rule_name(rule), jsgf_grammar_name(grammar));
119 }
120 }
121
122 lmath = logmath_init(1.0001, 0, 0);
123 fsg = jsgf_build_fsg_raw(grammar, rule, lmath, 1.0);
124 return fsg;
125 }
126
127 int
128 main(int argc, char *argv[])
129 {
130 jsgf_t *jsgf;
131 fsg_model_t *fsg;
132 cmd_ln_t *config;
133 const char *rule;
134
135 if ((config = cmd_ln_parse_r(NULL, defn, argc, argv, TRUE)) == NULL)
136 return 1;
137
138 if (cmd_ln_boolean_r(config, "-help")) {
139 usagemsg(argv[0]);
140 }
141
142 jsgf = jsgf_parse_file(cmd_ln_str_r(config, "-jsgf"), NULL);
143 if (jsgf == NULL) {
144 return 1;
145 }
146
147 rule = cmd_ln_str_r(config, "-toprule") ? cmd_ln_str_r(config, "-toprule") : NULL;
148 if (!(fsg = get_fsg(jsgf, rule))) {
149 E_ERROR("No fsg was built for the given rule '%s'.\n"
150 "Check rule name; it should be qualified (with grammar name)\n"
151 "and not enclosed in angle brackets (e.g. 'grammar.rulename').",
152 rule);
153 return 1;
154 }
155
156
157 if (cmd_ln_boolean_r(config, "-compile")) {
158 fsg_model_null_trans_closure(fsg, NULL);
159 }
160
161
162 if (cmd_ln_str_r(config, "-fsm")) {
163 const char* outfile = cmd_ln_str_r(config, "-fsm");
164 const char* symfile = cmd_ln_str_r(config, "-symtab");
165 if (outfile)
166 fsg_model_writefile_fsm(fsg, outfile);
167 else
168 fsg_model_write_fsm(fsg, stdout);
169 if (symfile)
170 fsg_model_writefile_symtab(fsg, symfile);
171 }
172 else {
173 const char *outfile = cmd_ln_str_r(config, "-fsg");
174 if (outfile)
175 fsg_model_writefile(fsg, outfile);
176 else
177 fsg_model_write(fsg, stdout);
178 }
179 fsg_model_free(fsg);
180 jsgf_grammar_free(jsgf);
181
182 return 0;
183 }
184
185
186 #if defined(_WIN32_WCE)
187 #pragma comment(linker,"/entry:mainWCRTStartup")
188 #include <windows.h>
189
190 /* Windows Mobile has the Unicode main only */
191 int wmain(int32 argc, wchar_t *wargv[]) {
192 char** argv;
193 size_t wlen;
194 size_t len;
195 int i;
196
197 argv = malloc(argc*sizeof(char*));
198 for (i = 0; i < argc; i++){
199 wlen = lstrlenW(wargv[i]);
200 len = wcstombs(NULL, wargv[i], wlen);
201 argv[i] = malloc(len+1);
202 wcstombs(argv[i], wargv[i], wlen);
203 }
204
205 /* assuming ASCII parameters */
206 return main(argc, argv);
207 }
208 #endif