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
Miscellaneous useful string functions.
#define jsgf_rule_iter_next(itor)
Advance an iterator to the next rule in the grammar.
Definition: jsgf.h:112
#define E_INFO
Print logging information to standard error stream.
Definition: err.h:147
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:551
SPHINXBASE_EXPORT jsgf_t * jsgf_parse_file(const char *filename, jsgf_t *parent)
Parse a JSGF grammar from a file.
Definition: jsgf.c:734
#define ARG_STRING
String argument (optional).
Definition: cmd_ln.h:114
SPHINXBASE_EXPORT int logmath_free(logmath_t *lmath)
Free a log table.
Definition: logmath.c:342
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:437
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:81
SPHINXBASE_EXPORT logmath_t * logmath_retain(logmath_t *lmath)
Retain ownership of a log table.
Definition: logmath.c:335
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:504
#define E_INFOCONT
Print logging information without header, to standard error stream.
Definition: err.h:153
Implementation of logging routines.
SPHINXBASE_EXPORT int jsgf_rule_public(jsgf_rule_t *rule)
Test if a rule is public or not.
Definition: jsgf.c:443
#define ARG_BOOLEAN
Boolean (true/false) argument (optional).
Definition: cmd_ln.h:118
Argument definition structure.
#define jsgf_rule_iter_rule(itor)
Get the current rule in a rule iterator.
Definition: jsgf.h:117
Opaque structure used to hold the results of command-line parsing.
JSGF grammar compiler.
#define jsgf_rule_iter_free(itor)
Free a rule iterator (if the end hasn't been reached).
Definition: jsgf.h:122
#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:78
Hash table implementation.
Word level FSG definition.
Definition: fsg_model.h:91
SPHINXBASE_EXPORT void jsgf_grammar_free(jsgf_t *jsgf)
Free a JSGF grammar.
Definition: jsgf.c:125
SPHINXBASE_EXPORT jsgf_rule_iter_t * jsgf_rule_iter(jsgf_t *grammar)
Get an iterator over all rules in a grammar.
Definition: jsgf.c:421