SphinxBase 0.6
|
00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 00002 /* ==================================================================== 00003 * Copyright (c) 2007 Carnegie Mellon University. All rights 00004 * reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in 00015 * the documentation and/or other materials provided with the 00016 * distribution. 00017 * 00018 * This work was supported in part by funding from the Defense Advanced 00019 * Research Projects Agency and the National Science Foundation of the 00020 * United States of America, and the CMU Sphinx Speech Consortium. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 00023 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 00024 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00025 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 00026 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00028 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00029 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00030 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00032 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 * ==================================================================== 00035 * 00036 */ 00037 00038 #include <string.h> 00039 00040 #include <sphinxbase/hash_table.h> 00041 #include <sphinxbase/fsg_model.h> 00042 #include <sphinxbase/jsgf.h> 00043 #include <sphinxbase/err.h> 00044 #include <sphinxbase/strfuncs.h> 00045 00046 static const arg_t defn[] = { 00047 { "-help", 00048 ARG_BOOLEAN, 00049 "no", 00050 "Shows the usage of the tool"}, 00051 00052 { "-jsgf", 00053 REQARG_STRING, 00054 NULL, 00055 "Input grammar in jsgf format (required)"}, 00056 00057 { "-rule", 00058 ARG_STRING, 00059 NULL, 00060 "Root rule name (optional)"}, 00061 00062 { "-fsg", 00063 ARG_STRING, 00064 NULL, 00065 "Output grammar in fsg format"}, 00066 00067 { "-fsm", 00068 ARG_STRING, 00069 NULL, 00070 "Output grammar in FSM format"}, 00071 00072 { "-symtab", 00073 ARG_STRING, 00074 NULL, 00075 "Output symtab for grammar in FSM format"}, 00076 00077 { "-compile", 00078 ARG_BOOLEAN, 00079 "no", 00080 "Compute grammar closure to speedup loading"}, 00081 00082 { NULL, 0, NULL, NULL } 00083 }; 00084 00085 00086 static void 00087 usagemsg(char *pgm) 00088 { 00089 E_INFO("Usage: %s -jsgf <input.jsgf> -rule <rule name>\\\n", pgm); 00090 E_INFOCONT("\t[-fsm yes/no] [-compile yes/no]\n"); 00091 E_INFOCONT("\t-fsg <output.fsg>\n"); 00092 00093 exit(0); 00094 } 00095 00096 static fsg_model_t * 00097 get_fsg(jsgf_t *grammar, const char *name) 00098 { 00099 jsgf_rule_iter_t *itor; 00100 logmath_t *lmath = logmath_init(1.0001, 0, 0); 00101 fsg_model_t *fsg = NULL; 00102 00103 for (itor = jsgf_rule_iter(grammar); itor; 00104 itor = jsgf_rule_iter_next(itor)) { 00105 jsgf_rule_t *rule = jsgf_rule_iter_rule(itor); 00106 char const *rule_name = jsgf_rule_name(rule); 00107 00108 if ((name == NULL && jsgf_rule_public(rule)) 00109 || (name && strlen(rule_name)-2 == strlen(name) && 00110 0 == strncmp(rule_name + 1, name, strlen(rule_name) - 2))) { 00111 fsg = jsgf_build_fsg_raw(grammar, rule, logmath_retain(lmath), 1.0); 00112 jsgf_rule_iter_free(itor); 00113 break; 00114 } 00115 } 00116 00117 logmath_free(lmath); 00118 return fsg; 00119 } 00120 00121 int 00122 main(int argc, char *argv[]) 00123 { 00124 jsgf_t *jsgf; 00125 fsg_model_t *fsg; 00126 cmd_ln_t *config; 00127 00128 if ((config = cmd_ln_parse_r(NULL, defn, argc, argv, TRUE)) == NULL) 00129 return 1; 00130 00131 if (cmd_ln_boolean_r(config, "-help")) { 00132 usagemsg(argv[0]); 00133 } 00134 00135 jsgf = jsgf_parse_file(cmd_ln_str_r(config, "-jsgf"), NULL); 00136 if (jsgf == NULL) { 00137 return 1; 00138 } 00139 00140 fsg = get_fsg(jsgf, cmd_ln_str_r(config, "-rule") ? cmd_ln_str_r(config, "-rule") : NULL); 00141 00142 if (cmd_ln_boolean_r(config, "-compile")) { 00143 fsg_model_null_trans_closure(fsg, NULL); 00144 } 00145 00146 00147 if (cmd_ln_str_r(config, "-fsm")) { 00148 const char* outfile = cmd_ln_str_r(config, "-fsm"); 00149 const char* symfile = cmd_ln_str_r(config, "-symtab"); 00150 if (outfile) 00151 fsg_model_writefile_fsm(fsg, outfile); 00152 else 00153 fsg_model_write_fsm(fsg, stdout); 00154 if (symfile) 00155 fsg_model_writefile_symtab(fsg, symfile); 00156 } 00157 else { 00158 const char *outfile = cmd_ln_str_r(config, "-fsg"); 00159 if (outfile) 00160 fsg_model_writefile(fsg, outfile); 00161 else 00162 fsg_model_write(fsg, stdout); 00163 } 00164 fsg_model_free(fsg); 00165 jsgf_grammar_free(jsgf); 00166 00167 return 0; 00168 } 00169 00170 00171 #if defined(_WIN32_WCE) 00172 #pragma comment(linker,"/entry:mainWCRTStartup") 00173 #include <windows.h> 00174 00175 //Windows Mobile has the Unicode main only 00176 int wmain(int32 argc, wchar_t *wargv[]) { 00177 char** argv; 00178 size_t wlen; 00179 size_t len; 00180 int i; 00181 00182 argv = malloc(argc*sizeof(char*)); 00183 for (i=0; i<argc; i++){ 00184 wlen = lstrlenW(wargv[i]); 00185 len = wcstombs(NULL, wargv[i], wlen); 00186 argv[i] = malloc(len+1); 00187 wcstombs(argv[i], wargv[i], wlen); 00188 } 00189 00190 //assuming ASCII parameters 00191 return main(argc, argv); 00192 } 00193 #endif