45 #include <sphinxbase/byteorder.h> 47 #include "ngram_model_trie.h" 49 static const char trie_hdr[] =
"Trie Language Model";
50 static const char dmp_hdr[] =
"Darpa Trigram LM";
57 read_counts_arpa(
lineiter_t ** li, uint32 * counts,
int *order)
59 int32 ngram, prev_ngram;
64 if (strcmp((*li)->buf,
"\\data\\") == 0)
69 if (*li == NULL || strcmp((*li)->buf,
"\\data\\") != 0) {
70 E_INFO(
"No \\data\\ mark in LM file\n");
77 if (sscanf((*li)->buf,
"ngram %d=%d", &ngram, &ngram_cnt) != 2)
79 if (ngram != prev_ngram + 1) {
81 (
"Ngram counts in LM file is not in order. %d goes after %d\n",
86 counts[*order] = ngram_cnt;
91 E_ERROR(
"EOF while reading ngram counts\n");
107 while (*li && strcmp((*li)->buf,
"\\1-grams:") != 0) {
116 for (i = 0; i < count; i++) {
122 (
"Unexpected end of ARPA file. Failed to read %dth unigram\n",
126 if ((n =
str2words((*li)->buf, wptr, 3)) < n_parts) {
127 E_ERROR(
"Format error at line %s, Failed to read unigrams\n", (*li)->buf);
131 unigram = &unigrams[i];
134 if (unigram->prob > 0) {
135 E_WARN(
"Unigram '%s' has positive probability\n", wptr[1]);
138 if (n == n_parts + 1) {
152 for (i = 0; i < count; i++) {
155 (
void *) (
long) i)) != (
void *) (
long) i) {
156 E_WARN(
"Duplicate word in dictionary: %s\n",
164 ngram_model_trie_read_arpa(
cmd_ln_t * config,
173 uint32 counts[NGRAM_MAX_ORDER];
177 E_INFO(
"Trying to read LM in arpa format\n");
178 if ((fp =
fopen_comp(path,
"r", &is_pipe)) == NULL) {
179 E_ERROR(
"File %s not found\n", path);
186 if (read_counts_arpa(&li, counts, &order) == -1) {
193 E_INFO(
"LM of order %d\n", order);
194 for (i = 0; i < order; i++) {
195 E_INFO(
"#%d-grams: %d\n", i + 1, counts[i]);
199 ngram_model_init(base, &ngram_model_trie_funcs, lmath, order,
203 model->
trie = lm_trie_create(counts[0], order);
204 if (read_1grams_arpa(&li, counts[0], base, model->
trie->unigrams) < 0) {
213 ngrams_raw_read_arpa(&li, base->
lmath, counts, order,
215 if (raw_ngrams == NULL) {
221 lm_trie_build(model->
trie, raw_ngrams, counts, base->
n_counts, order);
222 ngrams_raw_free(raw_ngrams, counts, order);
232 ngram_model_trie_write_arpa(
ngram_model_t * base,
const char *path)
237 FILE *fp = fopen(path,
"w");
239 E_ERROR(
"Unable to open %s to write arpa LM from trie\n", path);
243 "This is an ARPA-format language model file, generated by CMU Sphinx\n");
245 fprintf(fp,
"\\data\\\n");
246 for (i = 0; i < base->
n; ++i) {
247 fprintf(fp,
"ngram %d=%d\n", i + 1, base->
n_counts[i]);
250 fprintf(fp,
"\n\\1-grams:\n");
251 for (j = 0; j < base->
n_counts[0]; j++) {
253 fprintf(fp,
"%.4f\t%s",
257 fprintf(fp,
"\t%.4f",
264 for (i = 2; i <= base->
n; ++i) {
267 sizeof(*raw_ngrams));
268 uint32 raw_ngram_idx;
270 uint32 hist[NGRAM_MAX_ORDER];
273 range.begin = range.end = 0;
276 lm_trie_fill_raw_ngram(model->
trie, raw_ngrams,
277 &raw_ngram_idx, base->
n_counts, range, hist, 0,
279 assert(raw_ngram_idx == base->
n_counts[i - 1]);
280 qsort(raw_ngrams, (
size_t) base->
n_counts[i - 1],
283 fprintf(fp,
"\n\\%d-grams:\n", i);
284 for (j = 0; j < base->
n_counts[i - 1]; j++) {
287 for (k = 0; k < i; k++) {
289 base->
word_str[raw_ngrams[j].words[k]]);
300 fprintf(fp,
"\n\\end\\\n");
312 fread(&k,
sizeof(k), 1, fp);
313 tmp_word_str = (
char *)
ckd_calloc((
size_t) k, 1);
314 fread(tmp_word_str, 1, (
size_t) k, fp);
317 for (i = 0, j = 0; i < (uint32) k; i++)
318 if (tmp_word_str[i] ==
'\0')
322 (
"Error reading word strings (%d doesn't match n_unigrams %d)\n",
328 for (i = 0; i < base->
n_counts[0]; i++) {
331 (
void *) (
long) i) != (
void *) (
long) i) {
332 E_WARN(
"Duplicate word in dictionary: %s\n",
341 ngram_model_trie_read_bin(
cmd_ln_t * config,
350 uint32 counts[NGRAM_MAX_ORDER];
354 E_INFO(
"Trying to read LM in trie binary format\n");
355 if ((fp =
fopen_comp(path,
"rb", &is_pipe)) == NULL) {
356 E_ERROR(
"File %s not found\n", path);
359 hdr_size = strlen(trie_hdr);
360 hdr = (
char *)
ckd_calloc(hdr_size + 1,
sizeof(*hdr));
361 fread(hdr,
sizeof(*hdr), hdr_size, fp);
362 cmp_res = strcmp(hdr, trie_hdr);
365 E_INFO(
"Header doesn't match\n");
371 fread(&order,
sizeof(order), 1, fp);
372 for (i = 0; i < order; i++) {
373 fread(&counts[i],
sizeof(counts[i]), 1, fp);
375 ngram_model_init(base, &ngram_model_trie_funcs, lmath, order,
377 for (i = 0; i < order; i++) {
381 model->
trie = lm_trie_read_bin(counts, order, fp);
382 read_word_str(base, fp);
395 for (i = 0; i < model->
n_counts[0]; i++)
396 k += strlen(model->
word_str[i]) + 1;
397 fwrite(&k,
sizeof(k), 1, fp);
398 for (i = 0; i < model->
n_counts[0]; i++)
403 ngram_model_trie_write_bin(
ngram_model_t * base,
const char *path)
410 E_ERROR(
"Unable to open %s to write binary trie LM\n", path);
414 fwrite(trie_hdr,
sizeof(*trie_hdr), strlen(trie_hdr), fp);
415 fwrite(&model->
base.
n,
sizeof(model->
base.
n), 1, fp);
416 for (i = 0; i < model->
base.
n; i++) {
421 write_word_str(fp, base);
427 ngram_model_trie_read_dmp(
cmd_ln_t * config,
428 const char *file_name,
logmath_t * lmath)
437 uint32 *unigram_next;
445 E_INFO(
"Trying to read LM in dmp format\n");
446 if ((fp =
fopen_comp(file_name,
"rb", &is_pipe)) == NULL) {
447 E_ERROR(
"Dump file %s not found\n", file_name);
452 fread(&k,
sizeof(k), 1, fp);
453 if (k != strlen(dmp_hdr) + 1) {
455 if (k != strlen(dmp_hdr) + 1) {
457 (
"Wrong magic header size number %x: %s is not a dump file\n",
463 if (fread(str, 1, k, fp) != (
size_t) k) {
464 E_ERROR(
"Cannot read header\n");
467 if (strncmp(str, dmp_hdr, k) != 0) {
468 E_ERROR(
"Wrong header %s: %s is not a dump file\n", dmp_hdr);
472 if (fread(&k,
sizeof(k), 1, fp) != 1)
476 if (fread(str, 1, k, fp) != (
size_t) k) {
477 E_ERROR(
"Cannot read LM filename in header\n");
482 if (fread(&vn,
sizeof(vn), 1, fp) != 1)
488 if (fread(&ts,
sizeof(ts), 1, fp) != 1)
495 if (fread(&k,
sizeof(k), 1, fp) != 1)
501 if (fread(str, 1, k, fp) != (
size_t) k) {
502 E_ERROR(
"Failed to read word\n");
507 if (fread(&count,
sizeof(count), 1, fp) != 1)
517 if (fread(&count,
sizeof(count), 1, fp) != 1)
522 if (fread(&count,
sizeof(count), 1, fp) != 1)
527 E_INFO(
"ngrams 1=%d, 2=%d, 3=%d\n", counts[0], counts[1], counts[2]);
533 else if (counts[1] > 0)
537 ngram_model_init(base, &ngram_model_trie_funcs, lmath, order,
540 model->
trie = lm_trie_create(counts[0], order);
543 (uint32 *)
ckd_calloc((int32) counts[0] + 1,
sizeof(unigram_next));
544 for (j = 0; j <= (int32) counts[0]; j++) {
552 fread(&mapid,
sizeof(int32), 1, fp);
553 fread(&weightp,
sizeof(weightp), 1, fp);
554 fread(&weightb,
sizeof(weightb), 1, fp);
555 fread(&bigrams,
sizeof(int32), 1, fp);
557 SWAP_INT32(&weightp.l);
558 SWAP_INT32(&weightb.l);
559 SWAP_INT32(&bigrams);
563 model->
trie->unigrams[j].next = bigrams;
564 unigram_next[j] = bigrams;
569 ngrams_raw_read_dmp(fp, lmath, counts, order, unigram_next,
571 if (raw_ngrams == NULL) {
577 lm_trie_build(model->
trie, raw_ngrams, counts, base->
n_counts, order);
578 ngrams_raw_free(raw_ngrams, counts, order);
585 read_word_str(base, fp);
595 lm_trie_free(model->
trie);
599 trie_apply_weights(
ngram_model_t * base, float32 lw, float32 wip)
610 return (int32) (score * base->
lw + base->
log_wip);
614 ngram_model_trie_raw_score(
ngram_model_t * base, int32 wid, int32 * hist,
615 int32 n_hist, int32 * n_used)
620 if (n_hist > model->
base.
n - 1)
621 n_hist = model->
base.
n - 1;
622 for (i = 0; i < n_hist; i++) {
629 return (int32) lm_trie_score(model->
trie, model->
base.
n, wid, hist,
634 ngram_model_trie_score(
ngram_model_t * base, int32 wid, int32 * hist,
635 int32 n_hist, int32 * n_used)
637 return weight_score(base,
638 ngram_model_trie_raw_score(base, wid, hist, n_hist,
643 lm_trie_add_ug(
ngram_model_t * base, int32 wid, int32 lweight)
648 assert(!NGRAM_IS_CLASSWID(wid));
651 model->
trie->unigrams =
653 sizeof(*model->
trie->unigrams) *
655 memset(model->
trie->unigrams + (base->
n_counts[0] + 1), 0,
660 model->
trie->unigrams[wid + 1].next = model->
trie->unigrams[wid].next;
661 model->
trie->unigrams[wid].prob = (float) lweight;
664 model->
trie->unigrams[wid].bo = 0;
670 if ((uint32) wid >= base->
n_counts[0])
673 return (int32) weight_score(base, lweight);
681 memset(trie->hist_cache, -1,
sizeof(trie->hist_cache));
682 memset(trie->backoff_cache, 0,
sizeof(trie->backoff_cache));
687 ngram_model_trie_free,
689 ngram_model_trie_score,
690 ngram_model_trie_raw_score,
#define E_ERROR_SYSTEM(...)
Print error text; Call perror("");.
lm_trie_t * trie
Trie structure that stores ngram relations and weights.
Miscellaneous useful string functions.
#define E_INFO(...)
Print logging information to standard error stream.
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
#define E_ERROR(...)
Print error message to error log.
hash_table_t * wid
Mapping of unigram names to word IDs.
char ** word_str
Unigram names.
ngram_model_t base
Base ngram_model_t structure.
Sphinx's memory allocation/deallocation routines.
SPHINXBASE_EXPORT lineiter_t * lineiter_start_clean(FILE *fh)
Start reading lines from a file, skip comments and trim lines.
SPHINXBASE_EXPORT int logmath_log(logmath_t *lmath, float64 p)
Convert linear floating point number to integer log in base B.
uint8 writable
Are word strings writable?
SPHINXBASE_EXPORT int ngram_model_free(ngram_model_t *model)
Release memory associated with an N-Gram model.
#define ckd_salloc(ptr)
Macro for ckd_salloc
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
SPHINXBASE_EXPORT float64 logmath_log_float_to_log10(logmath_t *lmath, float log_p)
Convert float log in base B to base 10 log.
int32 n_1g_alloc
Number of allocated word strings (for new word addition)
SPHINXBASE_EXPORT double atof_c(char const *str)
Locale independent version of atof().
SPHINXBASE_EXPORT void lineiter_free(lineiter_t *li)
Stop reading lines from a file.
uint32 * n_counts
Counts for 1, 2, 3, ...
SPHINXBASE_EXPORT lineiter_t * lineiter_next(lineiter_t *li)
Move to the next line in the file.
uint8 n
This is an n-gram model (1, 2, 3, ...).
Implementation of logging routines.
logmath_t * lmath
Log-math object.
SPHINXBASE_EXPORT void * hash_table_enter(hash_table_t *h, const char *key, void *val)
Try to add a new entry with given key and associated value to hash table h.
SPHINXBASE_EXPORT FILE * fopen_comp(const char *file, const char *mode, int32 *ispipe)
Like fopen, but use popen and zcat if it is determined that "file" is compressed (i.e., has a .z, .Z, .gz, or .GZ extension).
#define E_WARN(...)
Print warning message to error log.
SPHINXBASE_EXPORT float logmath_log10_to_log_float(logmath_t *lmath, float64 log_p)
Convert base 10 log (in floating point) to float log in base B.
SPHINXBASE_EXPORT int32 str2words(char *line, char **wptr, int32 n_wptr)
Convert a line to an array of "words", based on whitespace separators.
Opaque structure used to hold the results of command-line parsing.
Implementation-specific functions for operating on ngram_model_t objects.
float32 lw
Language model scaling factor.
Common implementation of ngram_model_t.
SPHINXBASE_EXPORT void fclose_comp(FILE *fp, int32 ispipe)
Close a file opened using fopen_comp.
#define ckd_realloc(ptr, sz)
Macro for ckd_realloc
file IO related operations.
int32 log_wip
Log of word insertion penalty.