main.c
/* -*- 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 <sphinxbase/hash_table.h>
#include <sphinxbase/fsg_model.h>
#include <sphinxbase/jsgf.h>
#include <sphinxbase/err.h>
#include <sphinxbase/strfuncs.h>
static const arg_t defn[] = {
{ "-help",
ARG_BOOLEAN,
"no",
"Shows the usage of the tool"},
{ "-jsgf",
REQARG_STRING,
NULL,
"Input grammar in jsgf format (required)"},
{ "-rule",
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"},
{ NULL, 0, NULL, NULL }
};
static void
usagemsg(char *pgm)
{
E_INFO("Usage: %s -jsgf <input.jsgf> -rule <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)
{
jsgf_rule_iter_t *itor;
logmath_t *lmath = logmath_init(1.0001, 0, 0);
fsg_model_t *fsg = NULL;
for (itor = jsgf_rule_iter(grammar); itor;
itor = jsgf_rule_iter_next(itor)) {
jsgf_rule_t *rule = jsgf_rule_iter_rule(itor);
char const *rule_name = jsgf_rule_name(rule);
if ((name == NULL && jsgf_rule_public(rule))
|| (name && strlen(rule_name)-2 == strlen(name) &&
0 == strncmp(rule_name + 1, name, strlen(rule_name) - 2))) {
fsg = jsgf_build_fsg_raw(grammar, rule, logmath_retain(lmath), 1.0);
jsgf_rule_iter_free(itor);
break;
}
}
logmath_free(lmath);
return fsg;
}
int
main(int argc, char *argv[])
{
jsgf_t *jsgf;
fsg_model_t *fsg;
cmd_ln_t *config;
if ((config = cmd_ln_parse_r(NULL, defn, argc, argv, TRUE)) == NULL)
return 1;
if (cmd_ln_boolean_r(config, "-help")) {
usagemsg(argv[0]);
}
jsgf = jsgf_parse_file(cmd_ln_str_r(config, "-jsgf"), NULL);
if (jsgf == NULL) {
return 1;
}
fsg = get_fsg(jsgf, cmd_ln_str_r(config, "-rule") ? cmd_ln_str_r(config, "-rule") : NULL);
if (cmd_ln_boolean_r(config, "-compile")) {
fsg_model_null_trans_closure(fsg, NULL);
}
if (cmd_ln_str_r(config, "-fsm")) {
const char* outfile = cmd_ln_str_r(config, "-fsm");
const char* symfile = cmd_ln_str_r(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 = cmd_ln_str_r(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
strfuncs.h
Miscellaneous useful string functions.
jsgf_rule_iter_next
#define jsgf_rule_iter_next(itor)
Advance an iterator to the next rule in the grammar.
Definition: jsgf.h:112
E_INFO
#define E_INFO
Print logging information to standard error stream.
Definition: err.h:147
jsgf_s
Definition: jsgf_internal.h:69
logmath_s
Definition: logmath.c:49
cmd_ln_parse_r
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
jsgf_parse_file
SPHINXBASE_EXPORT jsgf_t * jsgf_parse_file(const char *filename, jsgf_t *parent)
Parse a JSGF grammar from a file.
Definition: jsgf.c:734
ARG_STRING
#define ARG_STRING
String argument (optional).
Definition: cmd_ln.h:114
logmath_free
SPHINXBASE_EXPORT int logmath_free(logmath_t *lmath)
Free a log table.
Definition: logmath.c:342
cmd_ln_str_r
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
REQARG_STRING
#define REQARG_STRING
Required string argument.
Definition: cmd_ln.h:135
jsgf_rule_name
SPHINXBASE_EXPORT char const * jsgf_rule_name(jsgf_rule_t *rule)
Get the rule name from a rule.
Definition: jsgf.c:437
logmath_init
SPHINXBASE_EXPORT logmath_t * logmath_init(float64 base, int shift, int use_table)
Initialize a log math computation table.
Definition: logmath.c:62
sphinx_wave2feat_s::outfile
char * outfile
Path to output file.
Definition: sphinx_fe.c:81
jsgf_rule_s
Definition: jsgf_internal.h:86
logmath_retain
SPHINXBASE_EXPORT logmath_t * logmath_retain(logmath_t *lmath)
Retain ownership of a log table.
Definition: logmath.c:335
jsgf_build_fsg_raw
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
E_INFOCONT
#define E_INFOCONT
Print logging information without header, to standard error stream.
Definition: err.h:153
err.h
Implementation of logging routines.
hash_iter_s
Definition: hash_table.h:168
jsgf_rule_public
SPHINXBASE_EXPORT int jsgf_rule_public(jsgf_rule_t *rule)
Test if a rule is public or not.
Definition: jsgf.c:443
ARG_BOOLEAN
#define ARG_BOOLEAN
Boolean (true/false) argument (optional).
Definition: cmd_ln.h:118
arg_t
Argument definition structure.
jsgf_rule_iter_rule
#define jsgf_rule_iter_rule(itor)
Get the current rule in a rule iterator.
Definition: jsgf.h:117
cmd_ln_t
Opaque structure used to hold the results of command-line parsing.
jsgf.h
JSGF grammar compiler.
jsgf_rule_iter_free
#define jsgf_rule_iter_free(itor)
Free a rule iterator (if the end hasn't been reached).
Definition: jsgf.h:122
cmd_ln_boolean_r
#define cmd_ln_boolean_r(c, n)
Retrieve a boolean value from a command-line object.
Definition: cmd_ln.h:334
sphinx_wave2feat_s::config
cmd_ln_t * config
Configuration parameters.
Definition: sphinx_fe.c:78
hash_table.h
Hash table implementation.
fsg_model_s
Word level FSG definition.
Definition: fsg_model.h:91
jsgf_grammar_free
SPHINXBASE_EXPORT void jsgf_grammar_free(jsgf_t *jsgf)
Free a JSGF grammar.
Definition: jsgf.c:125
jsgf_rule_iter
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