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