SphinxBase  5prealpha
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  { "-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",
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
Miscellaneous useful string functions.
#define E_INFO(...)
Print logging information to standard error stream.
Definition: err.h:114
SPHINXBASE_EXPORT jsgf_rule_t * jsgf_get_rule(jsgf_t *grammar, const char *name)
Get a rule by name from a grammar.
Definition: jsgf.c:469
#define E_ERROR(...)
Print error message to error log.
Definition: err.h:104
#define E_INFOCONT(...)
Continue printing the information to standard error stream.
Definition: err.h:119
SPHINXBASE_EXPORT cmd_ln_t * cmd_ln_parse_r(cmd_ln_t *inout_cmdln, arg_t const *defn, int32 argc, char *argv[], int32 strict)
Parse a list of strings into argumetns.
Definition: cmd_ln.c:556
SPHINXBASE_EXPORT jsgf_t * jsgf_parse_file(const char *filename, jsgf_t *parent)
Parse a JSGF grammar from a file.
Definition: jsgf.c:896
#define ARG_STRING
String argument (optional).
Definition: cmd_ln.h:114
SPHINXBASE_EXPORT char const * cmd_ln_str_r(cmd_ln_t *cmdln, char const *name)
Retrieve a string from a command-line object.
Definition: cmd_ln.c:949
#define REQARG_STRING
Required string argument.
Definition: cmd_ln.h:135
SPHINXBASE_EXPORT char const * jsgf_rule_name(jsgf_rule_t *rule)
Get the rule name from a rule.
Definition: jsgf.c:513
SPHINXBASE_EXPORT logmath_t * logmath_init(float64 base, int shift, int use_table)
Initialize a log math computation table.
Definition: logmath.c:62
char * outfile
Path to output file.
Definition: sphinx_fe.c:77
SPHINXBASE_EXPORT char const * jsgf_grammar_name(jsgf_t *jsgf)
Get the grammar name from the file.
Definition: jsgf.c:216
SPHINXBASE_EXPORT fsg_model_t * jsgf_build_fsg_raw(jsgf_t *grammar, jsgf_rule_t *rule, logmath_t *lmath, float32 lw)
Build a Sphinx FSG object from a JSGF rule.
Definition: jsgf.c:592
Implementation of logging routines.
#define ARG_BOOLEAN
Boolean (true/false) argument (optional).
Definition: cmd_ln.h:118
Argument definition structure.
Opaque structure used to hold the results of command-line parsing.
JSGF grammar compiler.
#define cmd_ln_boolean_r(c, n)
Retrieve a boolean value from a command-line object.
Definition: cmd_ln.h:334
cmd_ln_t * config
Configuration parameters.
Definition: sphinx_fe.c:74
Hash table implementation.
Word level FSG definition.
Definition: fsg_model.h:99
SPHINXBASE_EXPORT void jsgf_grammar_free(jsgf_t *jsgf)
Free a JSGF grammar.
Definition: jsgf.c:108
SPHINXBASE_EXPORT jsgf_rule_t * jsgf_get_public_rule(jsgf_t *grammar)
Returns the first public rule of the grammar.
Definition: jsgf.c:484