42 #include <sphinxbase/pio.h>
43 #include <sphinxbase/strfuncs.h>
50 #define DEFAULT_NUM_PHONE (MAX_S3CIPID+1)
53 #define snprintf sprintf_s
56 extern const char *
const cmu6_lts_phone_table[];
59 dict_ciphone_id(
dict_t * d,
const char *str)
62 return bin_mdef_ciphone_id_nocase(d->
mdef, str);
64 return bin_mdef_ciphone_id(d->
mdef, str);
69 dict_ciphone_str(
dict_t * d, s3wid_t wid, int32 pos)
72 assert((wid >= 0) && (wid < d->n_word));
73 assert((pos >= 0) && (pos < d->word[wid].pronlen));
88 E_INFO(
"Reallocating to %d KiB for word entries\n",
99 wordp->
word = (
char *) ckd_salloc(word);
103 ckd_free(wordp->
word);
122 wword = ckd_salloc(word);
123 if ((len = dict_word2basestr(wword)) > 0) {
127 if (hash_table_lookup_int32(d->
ht, wword, &w) < 0) {
128 E_ERROR(
"Missing base word for: %s\n", word);
130 ckd_free(wordp->
word);
149 dict_read(FILE * fp,
dict_t * d)
157 size_t stralloc, phnalloc;
160 p = (
s3cipid_t *) ckd_calloc(maxwd + 4,
sizeof(*p));
161 wptr = (
char **) ckd_calloc(maxwd,
sizeof(
char *));
164 stralloc = phnalloc = 0;
165 for (li = lineiter_start(fp); li; li = lineiter_next(li)) {
167 if (0 == strncmp(li->buf,
"##", 2)
168 || 0 == strncmp(li->buf,
";;", 2))
171 if ((nwd = str2words(li->buf, wptr, maxwd)) < 0) {
173 nwd = str2words(li->buf, NULL, 0);
176 p = (
s3cipid_t *) ckd_realloc(p, (maxwd + 4) *
sizeof(*p));
177 wptr = (
char **) ckd_realloc(wptr, maxwd *
sizeof(*wptr));
184 E_ERROR(
"Line %d: No pronunciation for word '%s'; ignored\n",
191 for (i = 1; i < nwd; i++) {
192 p[i - 1] = dict_ciphone_id(d, wptr[i]);
193 if (NOT_S3CIPID(p[i - 1])) {
194 E_ERROR(
"Line %d: Phone '%s' is mising in the acoustic model; word '%s' ignored\n",
195 lineno, wptr[i], wptr[0]);
201 w = dict_add_word(d, wptr[0], p, nwd - 1);
204 (
"Line %d: Failed to add the word '%s' (duplicate?); ignored\n",
207 stralloc += strlen(d->
word[w].
word);
212 E_INFO(
"Allocated %d KiB for strings, %d KiB for phones\n",
213 (
int)stralloc / 1024, (
int)phnalloc / 1024);
221 dict_write(
dict_t *dict,
char const *filename,
char const *format)
226 if ((fh = fopen(filename,
"w")) == NULL) {
227 E_ERROR_SYSTEM(
"Failed to open '%s'", filename);
230 for (i = 0; i < dict->
n_word; ++i) {
233 if (!dict_real_word(dict, i))
235 for (phlen = j = 0; j < dict_pronlen(dict, i); ++j)
236 phlen += strlen(dict_ciphone_str(dict, i, j)) + 1;
237 phones = ckd_calloc(1, phlen);
238 for (j = 0; j < dict_pronlen(dict, i); ++j) {
239 strcat(phones, dict_ciphone_str(dict, i, j));
240 if (j != dict_pronlen(dict, i) - 1)
243 fprintf(fh,
"%-30s %s\n", dict_wordstr(dict, i), phones);
259 char const *dictfile = NULL, *fillerfile = NULL;
262 dictfile = cmd_ln_str_r(config,
"-dict");
263 fillerfile = cmd_ln_str_r(config,
"-fdict");
274 if ((fp = fopen(dictfile,
"r")) == NULL)
275 E_FATAL_SYSTEM(
"Failed to open dictionary file '%s' for reading", dictfile);
276 for (li = lineiter_start(fp); li; li = lineiter_next(li)) {
277 if (li->buf[0] !=
'#')
285 if ((fp2 = fopen(fillerfile,
"r")) == NULL)
286 E_FATAL_SYSTEM(
"Failed to open filler dictionary file '%s' for reading", fillerfile);
287 for (li = lineiter_start(fp2); li; li = lineiter_next(li)) {
288 if (li->buf[0] !=
'#')
301 (n + S3DICT_INC_SZ < MAX_S3WID) ? n + S3DICT_INC_SZ : MAX_S3WID;
303 E_FATAL(
"Number of words in dictionaries (%d) exceeds limit (%d)\n", n,
306 E_INFO(
"Allocating %d * %d bytes (%d KiB) for word entries\n",
312 d->
mdef = bin_mdef_retain(mdef);
315 if (config && cmd_ln_exists_r(config,
"-dictcase"))
316 d->nocase = cmd_ln_boolean_r(config,
"-dictcase");
321 E_INFO(
"Reading main dictionary: %s\n", dictfile);
324 E_INFO(
"%d words read\n", d->
n_word);
330 E_INFO(
"Reading filler dictionary: %s\n", fillerfile);
336 sil = bin_mdef_silphone(mdef);
339 if (dict_wordid(d, S3_START_WORD) ==
BAD_S3WID) {
340 dict_add_word(d, S3_START_WORD, &sil, 1);
342 if (dict_wordid(d, S3_FINISH_WORD) ==
BAD_S3WID) {
343 dict_add_word(d, S3_FINISH_WORD, &sil, 1);
345 if (dict_wordid(d, S3_SILENCE_WORD) ==
BAD_S3WID) {
346 dict_add_word(d, S3_SILENCE_WORD, &sil, 1);
352 d->
startwid = dict_wordid(d, S3_START_WORD);
353 d->
finishwid = dict_wordid(d, S3_FINISH_WORD);
354 d->
silwid = dict_wordid(d, S3_SILENCE_WORD);
357 || (!dict_filler_word(d, d->
silwid)))
358 E_FATAL(
"Word '%s' must occur (only) in filler dictionary\n",
375 if (hash_table_lookup_int32(d->
ht, word, &w) < 0)
385 assert((w >= 0) && (w < d->n_word));
387 w = dict_basewid(d, w);
399 assert((w >= 0) && (w < d->n_word));
401 w = dict_basewid(d, w);
411 dict_word2basestr(
char *word)
416 if (word[len - 1] ==
')') {
417 for (i = len - 2; (i > 0) && (word[i] !=
'('); --i);
448 for (i = 0; i < d->
n_word; i++) {
451 ckd_free((
void *) word->
word);
453 ckd_free((
void *) word->
ciphone);
457 ckd_free((
void *) d->
word);
459 hash_table_free(d->
ht);
461 bin_mdef_free(d->
mdef);
462 ckd_free((
void *) d);
470 E_INFO_NOFN(
"Initialization of dict_t, report:\n");
471 E_INFO_NOFN(
"Max word: %d\n", d->
max_words);
472 E_INFO_NOFN(
"No of word: %d\n", d->
n_word);