SphinxBase 0.6

src/sphinx_jsgf2fsg/main.c

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 fsg_model_t *
00047 get_fsg(jsgf_t *grammar, const char *name)
00048 {
00049     jsgf_rule_iter_t *itor;
00050     logmath_t *lmath = logmath_init(1.0001, 0, 0);
00051     fsg_model_t *fsg = NULL;
00052 
00053     for (itor = jsgf_rule_iter(grammar); itor;
00054          itor = jsgf_rule_iter_next(itor)) {
00055         jsgf_rule_t *rule = jsgf_rule_iter_rule(itor);
00056         char const *rule_name = jsgf_rule_name(rule);
00057 
00058         if ((name == NULL && jsgf_rule_public(rule))
00059             || (name && strlen(rule_name)-2 == strlen(name) &&
00060                 0 == strncmp(rule_name + 1, name, strlen(rule_name) - 2))) {
00061             fsg = jsgf_build_fsg_raw(grammar, rule, logmath_retain(lmath), 1.0);
00062             jsgf_rule_iter_free(itor);
00063             break;
00064         }
00065     }
00066 
00067     logmath_free(lmath);
00068     return fsg;
00069 }
00070 
00071 int
00072 main(int argc, char *argv[])
00073 {
00074     jsgf_t *jsgf;
00075     fsg_model_t *fsg;
00076     int fsm = 0;
00077     char *outfile = NULL;
00078     char *symfile = NULL;
00079 
00080     if (argc > 1 && 0 == strcmp(argv[1], "-fsm")) {
00081         fsm = 1;
00082         ++argv;
00083     }
00084 
00085     jsgf = jsgf_parse_file(argc > 1 ? argv[1] : NULL, NULL);
00086     if (jsgf == NULL) {
00087         return 1;
00088     }
00089     fsg = get_fsg(jsgf, argc > 2 ? argv[2] : NULL);
00090 
00091     if (argc > 3)
00092         outfile = argv[3];
00093     if (argc > 4)
00094         symfile = argv[4];
00095 
00096     if (fsm) {
00097         if (outfile)
00098             fsg_model_writefile_fsm(fsg, outfile);
00099         else
00100             fsg_model_write_fsm(fsg, stdout);
00101         if (symfile)
00102             fsg_model_writefile_symtab(fsg, symfile);
00103     }
00104     else {
00105         if (outfile)
00106             fsg_model_writefile(fsg, outfile);
00107         else
00108             fsg_model_write(fsg, stdout);
00109     }
00110     fsg_model_free(fsg);
00111     jsgf_grammar_free(jsgf);
00112 
00113     return 0;
00114 }
00115 
00116 
00118 #if defined(_WIN32_WCE)
00119 #pragma comment(linker,"/entry:mainWCRTStartup")
00120 #include <windows.h>
00121 
00122 //Windows Mobile has the Unicode main only
00123 int wmain(int32 argc, wchar_t *wargv[]) {
00124     char** argv;
00125     size_t wlen;
00126     size_t len;
00127     int i;
00128 
00129     argv = malloc(argc*sizeof(char*));
00130     for (i=0; i<argc; i++){
00131         wlen = lstrlenW(wargv[i]);
00132         len = wcstombs(NULL, wargv[i], wlen);
00133         argv[i] = malloc(len+1);
00134         wcstombs(argv[i], wargv[i], wlen);
00135     }
00136 
00137     //assuming ASCII parameters
00138     return main(argc, argv);
00139 }
00140 #endif