Debian Codegraph @

src/sphinx_lmtools/sphinx_lm_convert.c

raw
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3 * Copyright (c) 2009 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 * \file sphinx_lm_convert.c
39 * Language model conversion tool.
40 */
41 #include <sphinxbase/logmath.h>
42 #include <sphinxbase/ngram_model.h>
43 #include <sphinxbase/cmd_ln.h>
44 #include <sphinxbase/ckd_alloc.h>
45 #include <sphinxbase/err.h>
46 #include <sphinxbase/pio.h>
47 #include <sphinxbase/strfuncs.h>
48
49 #include <stdio.h>
50 #include <string.h>
51 #include <math.h>
52
53 static const arg_t defn[] = {
54 { "-help",
55 ARG_BOOLEAN,
56 "no",
57 "Shows the usage of the tool"},
58
59 { "-logbase",
60 ARG_FLOAT64,
61 "1.0001",
62 "Base in which all log-likelihoods calculated" },
63
64 { "-i",
65 REQARG_STRING,
66 NULL,
67 "Input language model file (required)"},
68
69 { "-o",
70 REQARG_STRING,
71 NULL,
72 "Output language model file (required)"},
73
74 { "-ifmt",
75 ARG_STRING,
76 NULL,
77 "Input language model format (will guess if not specified)"},
78
79 { "-ofmt",
80 ARG_STRING,
81 NULL,
82 "Output language model file (will guess if not specified)"},
83
84 { "-case",
85 ARG_STRING,
86 NULL,
87 "Ether 'lower' or 'upper' - case fold to lower/upper case (NOT UNICODE AWARE)" },
88
89 { "-mmap",
90 ARG_BOOLEAN,
91 "no",
92 "Use memory-mapped I/O for reading binary LM files"},
93
94 { "-debug",
95 ARG_INT32,
96 NULL,
97 "Verbosity level for debugging messages"
98 },
99
100 { NULL, 0, NULL, NULL }
101 };
102
103 static void
104 usagemsg(char *pgm)
105 {
106 E_INFO("Usage: %s -i <input.lm> \\\n", pgm);
107 E_INFOCONT("\t[-ifmt txt] [-ofmt dmp]\n");
108 E_INFOCONT("\t-o <output.lm.DMP>\n");
109
110 exit(0);
111 }
112
113
114 int
115 main(int argc, char *argv[])
116 {
117 cmd_ln_t *config;
118 ngram_model_t *lm = NULL;
119 logmath_t *lmath;
120 int itype, otype;
121 char const *kase;
122
123 if ((config = cmd_ln_parse_r(NULL, defn, argc, argv, TRUE)) == NULL)
124 return 1;
125
126 if (cmd_ln_boolean_r(config, "-help")) {
127 usagemsg(argv[0]);
128 }
129
130 err_set_debug_level(cmd_ln_int32_r(config, "-debug"));
131
132 /* Create log math object. */
133 if ((lmath = logmath_init
134 (cmd_ln_float64_r(config, "-logbase"), 0, 0)) == NULL) {
135 E_FATAL("Failed to initialize log math\n");
136 }
137
138 if (cmd_ln_str_r(config, "-i") == NULL || cmd_ln_str_r(config, "-i") == NULL) {
139 E_ERROR("Please specify both input and output models\n");
140 goto error_out;
141 }
142
143 /* Load the input language model. */
144 if (cmd_ln_str_r(config, "-ifmt")) {
145 if ((itype = ngram_str_to_type(cmd_ln_str_r(config, "-ifmt")))
146 == NGRAM_INVALID) {
147 E_ERROR("Invalid input type %s\n", cmd_ln_str_r(config, "-ifmt"));
148 goto error_out;
149 }
150 lm = ngram_model_read(config, cmd_ln_str_r(config, "-i"),
151 itype, lmath);
152 }
153 else {
154 lm = ngram_model_read(config, cmd_ln_str_r(config, "-i"),
155 NGRAM_AUTO, lmath);
156 }
157
158 if (lm == NULL) {
159 E_ERROR("Failed to read the model from the file '%s'\n", cmd_ln_str_r(config, "-i"));
160 goto error_out;
161 }
162
163 /* Guess or set the output language model type. */
164 if (cmd_ln_str_r(config, "-ofmt")) {
165 if ((otype = ngram_str_to_type(cmd_ln_str_r(config, "-ofmt")))
166 == NGRAM_INVALID) {
167 E_ERROR("Invalid output type %s\n", cmd_ln_str_r(config, "-ofmt"));
168 goto error_out;
169 }
170 }
171 else {
172 otype = ngram_file_name_to_type(cmd_ln_str_r(config, "-o"));
173 }
174
175 /* Case fold if requested. */
176 if ((kase = cmd_ln_str_r(config, "-case"))) {
177 if (0 == strcmp(kase, "lower")) {
178 ngram_model_casefold(lm, NGRAM_LOWER);
179 }
180 else if (0 == strcmp(kase, "upper")) {
181 ngram_model_casefold(lm, NGRAM_UPPER);
182 }
183 else {
184 E_ERROR("Unknown value for -case: %s\n", kase);
185 goto error_out;
186 }
187 }
188
189 /* Write the output language model. */
190 if (ngram_model_write(lm, cmd_ln_str_r(config, "-o"), otype) != 0) {
191 E_ERROR("Failed to write language model in format %s to %s\n",
192 ngram_type_to_str(otype), cmd_ln_str_r(config, "-o"));
193 goto error_out;
194 }
195
196 /* That's all folks! */
197 ngram_model_free(lm);
198 if (lmath) {
199 logmath_free(lmath);
200 }
201 if (config) {
202 cmd_ln_free_r(config);
203 }
204 return 0;
205
206 error_out:
207 ngram_model_free(lm);
208 if (lmath) {
209 logmath_free(lmath);
210 }
211 if (config) {
212 cmd_ln_free_r(config);
213 }
214 return 1;
215 }