int main(int argc, char *argv[]) { bin_mdef_t *mdef; dict_t *dict; cmd_ln_t *config; int i; char buf[100]; TEST_ASSERT(config = cmd_ln_init(NULL, NULL, FALSE, "-dict", MODELDIR "/en-us/cmudict-en-us.dict", "-fdict", MODELDIR "/en-us/en-us/noisedict", NULL)); /* Test dictionary in standard fashion. */ TEST_ASSERT(mdef = bin_mdef_read(NULL, MODELDIR "/en-us/en-us/mdef")); TEST_ASSERT(dict = dict_init(config, mdef, NULL)); printf("Word ID (CARNEGIE) = %d\n", dict_wordid(dict, "CARNEGIE")); printf("Word ID (ASDFASFASSD) = %d\n", dict_wordid(dict, "ASDFASFASSD")); TEST_EQUAL(0, dict_write(dict, "_cmu07a.dic", NULL)); TEST_EQUAL(0, system("diff -uw " MODELDIR "/en-us/cmudict-en-us.dict _cmu07a.dic")); dict_free(dict); bin_mdef_free(mdef); /* Now test an empty dictionary. */ TEST_ASSERT(dict = dict_init(NULL, NULL, NULL)); printf("Word ID(<s>) = %d\n", dict_wordid(dict, "<s>")); TEST_ASSERT(BAD_S3WID != dict_add_word(dict, "FOOBIE", NULL, 0)); TEST_ASSERT(BAD_S3WID != dict_add_word(dict, "BLETCH", NULL, 0)); printf("Word ID(FOOBIE) = %d\n", dict_wordid(dict, "FOOBIE")); printf("Word ID(BLETCH) = %d\n", dict_wordid(dict, "BLETCH")); TEST_ASSERT(dict_real_word(dict, dict_wordid(dict, "FOOBIE"))); TEST_ASSERT(dict_real_word(dict, dict_wordid(dict, "BLETCH"))); TEST_ASSERT(!dict_real_word(dict, dict_wordid(dict, "</s>"))); dict_free(dict); /* Test to add 500k words. */ TEST_ASSERT(dict = dict_init(NULL, NULL, NULL)); for (i = 0; i < 500000; i++) { sprintf(buf, "word_%d", i); TEST_ASSERT(BAD_S3WID != dict_add_word(dict, buf, NULL, 0)); } dict_free(dict); cmd_ln_free_r(config); return 0; }
int dict_write(dict_t *dict, char const *filename, char const *format) { FILE *fh; int i; if ((fh = fopen(filename, "w")) == NULL) { E_ERROR_SYSTEM("Failed to open %s", filename); return -1; } for (i = 0; i < dict->n_word; ++i) { char *phones; int j, phlen; if (!dict_real_word(dict, i)) continue; for (phlen = j = 0; j < dict_pronlen(dict, i); ++j) phlen += strlen(dict_ciphone_str(dict, i, j)) + 1; phones = ckd_calloc(1, phlen); for (j = 0; j < dict_pronlen(dict, i); ++j) { strcat(phones, dict_ciphone_str(dict, i, j)); if (j != dict_pronlen(dict, i) - 1) strcat(phones, " "); } fprintf(fh, "%-30s %s\n", dict_wordstr(dict, i), phones); ckd_free(phones); } fclose(fh); return 0; }
static int write_ctm(FILE *fh, ps_decoder_t *ps, ps_seg_t *itor, char const *uttid, int32 frate) { logmath_t *lmath = ps_get_logmath(ps); char *dupid, *show, *channel, *c; double ustart = 0.0; /* We have semi-standardized on comma-separated uttids which * correspond to the fields of the STM file. So if there's a * comma in the uttid, take the first two fields as show and * channel, and also try to find the start time. */ show = dupid = ckd_salloc(uttid ? uttid : "(null)"); if ((c = strchr(dupid, ',')) != NULL) { *c++ = '\0'; channel = c; if ((c = strchr(c, ',')) != NULL) { *c++ = '\0'; if ((c = strchr(c, ',')) != NULL) { ustart = atof_c(c + 1); } } } else { channel = NULL; } while (itor) { int32 prob, sf, ef, wid; char const *w; /* Skip things that aren't "real words" (FIXME: currently * requires s3kr3t h34d3rz...) */ w = ps_seg_word(itor); wid = dict_wordid(ps->dict, w); if (wid >= 0 && dict_real_word(ps->dict, wid)) { prob = ps_seg_prob(itor, NULL, NULL, NULL); ps_seg_frames(itor, &sf, &ef); fprintf(fh, "%s %s %.2f %.2f %s %.3f\n", show, channel ? channel : "1", ustart + (double)sf / frate, (double)(ef - sf) / frate, /* FIXME: More s3kr3tz */ dict_basestr(ps->dict, wid), logmath_exp(lmath, prob)); } itor = ps_seg_next(itor); } ckd_free(dupid); return 0; }