PocketSphinx  5.0.0rc5
A small speech recognizer
model.h
Go to the documentation of this file.
1 /* -*- c-basic-offset:4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1999-2022 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 SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  * ====================================================================
30  */
36 #ifndef __PS_MODEL_H__
37 #define __PS_MODEL_H__
38 
39 #include <stdio.h>
40 
41 #include <pocketsphinx/prim_type.h>
42 #include <pocketsphinx/logmath.h>
43 #include <pocketsphinx/export.h>
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 #if 0
49 }
50 #endif
51 
52 /* Forward declaration to avoid include loops */
53 typedef struct cmd_ln_s ps_config_t;
54 
59 typedef struct jsgf_s jsgf_t;
60 
65 typedef struct jsgf_rule_s jsgf_rule_t;
66 
76 typedef struct fsg_model_s fsg_model_t;
77 
82 typedef struct ngram_model_s ngram_model_t;
83 
91 POCKETSPHINX_EXPORT
92 jsgf_t *jsgf_parse_file(const char *filename, jsgf_t *parent);
93 
101 POCKETSPHINX_EXPORT
102 jsgf_t *jsgf_parse_string(const char *string, jsgf_t *parent);
103 
107 POCKETSPHINX_EXPORT
108 char const *jsgf_grammar_name(jsgf_t *jsgf);
109 
113 POCKETSPHINX_EXPORT
114 void jsgf_grammar_free(jsgf_t *jsgf);
115 
119 POCKETSPHINX_EXPORT
120 jsgf_rule_t *jsgf_get_rule(jsgf_t *grammar, const char *name);
121 
125 POCKETSPHINX_EXPORT
127 
131 POCKETSPHINX_EXPORT
132 char const *jsgf_rule_name(jsgf_rule_t *rule);
133 
137 POCKETSPHINX_EXPORT
138 int jsgf_rule_public(jsgf_rule_t *rule);
139 
143 typedef struct hash_iter_s jsgf_rule_iter_t;
144 
148 POCKETSPHINX_EXPORT
150 
154 POCKETSPHINX_EXPORT
156 
160 POCKETSPHINX_EXPORT
162 
166 POCKETSPHINX_EXPORT
168 
172 POCKETSPHINX_EXPORT
174  logmath_t *lmath, float32 lw);
175 
181 POCKETSPHINX_EXPORT
182 fsg_model_t *jsgf_read_file(const char *file, logmath_t * lmath, float32 lw);
183 
189 POCKETSPHINX_EXPORT
190 fsg_model_t *jsgf_read_string(const char *string, logmath_t * lmath, float32 lw);
191 
198 POCKETSPHINX_EXPORT
199 int jsgf_write_fsg(jsgf_t *grammar, jsgf_rule_t *rule, FILE *outfh);
200 
206 POCKETSPHINX_EXPORT
208 
214 POCKETSPHINX_EXPORT
215 int fsg_model_free(fsg_model_t *fsg);
216 
221 typedef struct ngram_class_s ngram_class_t;
222 
227 typedef enum ngram_file_type_e {
233 
234 #define NGRAM_INVALID_WID -1
255 POCKETSPHINX_EXPORT
257  const char *file_name,
258  ngram_file_type_t file_type,
259  logmath_t *lmath);
260 
266 POCKETSPHINX_EXPORT
267 int ngram_model_write(ngram_model_t *model, const char *file_name,
268  ngram_file_type_t format);
269 
275 POCKETSPHINX_EXPORT
276 ngram_file_type_t ngram_file_name_to_type(const char *file_name);
277 
283 POCKETSPHINX_EXPORT
284 ngram_file_type_t ngram_str_to_type(const char *str_name);
285 
292 POCKETSPHINX_EXPORT
293 char const *ngram_type_to_str(int type);
294 
300 POCKETSPHINX_EXPORT
302 
308 POCKETSPHINX_EXPORT
309 int ngram_model_free(ngram_model_t *model);
310 
314 typedef enum ngram_case_e {
315  NGRAM_UPPER,
316  NGRAM_LOWER
317 } ngram_case_t;
318 
325 POCKETSPHINX_EXPORT
326 int ngram_model_casefold(ngram_model_t *model, int kase);
327 
339 POCKETSPHINX_EXPORT
341  float32 lw, float32 wip);
342 
350 POCKETSPHINX_EXPORT
351 float32 ngram_model_get_weights(ngram_model_t *model, int32 *out_log_wip);
352 
385 POCKETSPHINX_EXPORT
386 int32 ngram_score(ngram_model_t *model, const char *word, ...);
387 
391 POCKETSPHINX_EXPORT
392 int32 ngram_tg_score(ngram_model_t *model,
393  int32 w3, int32 w2, int32 w1,
394  int32 *n_used);
395 
399 POCKETSPHINX_EXPORT
400 int32 ngram_bg_score(ngram_model_t *model,
401  int32 w2, int32 w1,
402  int32 *n_used);
403 
407 POCKETSPHINX_EXPORT
408 int32 ngram_ng_score(ngram_model_t *model, int32 wid, int32 *history,
409  int32 n_hist, int32 *n_used);
410 
421 POCKETSPHINX_EXPORT
422 int32 ngram_probv(ngram_model_t *model, const char *word, ...);
423 
434 POCKETSPHINX_EXPORT
435 int32 ngram_prob(ngram_model_t *model, const char* const *words, int32 n);
436 
443 POCKETSPHINX_EXPORT
444 int32 ngram_ng_prob(ngram_model_t *model, int32 wid, int32 *history,
445  int32 n_hist, int32 *n_used);
446 
458 POCKETSPHINX_EXPORT
459 int32 ngram_score_to_prob(ngram_model_t *model, int32 score);
460 
464 POCKETSPHINX_EXPORT
465 int32 ngram_wid(ngram_model_t *model, const char *word);
466 
470 POCKETSPHINX_EXPORT
471 const char *ngram_word(ngram_model_t *model, int32 wid);
472 
486 POCKETSPHINX_EXPORT
487 int32 ngram_unknown_wid(ngram_model_t *model);
488 
492 POCKETSPHINX_EXPORT
493 int32 ngram_zero(ngram_model_t *model);
494 
498 POCKETSPHINX_EXPORT
499 int32 ngram_model_get_size(ngram_model_t *model);
500 
504 POCKETSPHINX_EXPORT
505 uint32 const *ngram_model_get_counts(ngram_model_t *model);
506 
510 typedef struct ngram_iter_s ngram_iter_t;
511 
520 POCKETSPHINX_EXPORT
522 
526 POCKETSPHINX_EXPORT
527 ngram_iter_t *ngram_iter(ngram_model_t *model, const char *word, ...);
528 
532 POCKETSPHINX_EXPORT
533 ngram_iter_t *ngram_ng_iter(ngram_model_t *model, int32 wid, int32 *history, int32 n_hist);
534 
543 POCKETSPHINX_EXPORT
544 int32 const *ngram_iter_get(ngram_iter_t *itor,
545  int32 *out_score,
546  int32 *out_bowt);
547 
553 POCKETSPHINX_EXPORT
555 
559 POCKETSPHINX_EXPORT
561 
565 POCKETSPHINX_EXPORT
566 void ngram_iter_free(ngram_iter_t *itor);
567 
580 POCKETSPHINX_EXPORT
582  const char *word, float32 weight);
583 
597 POCKETSPHINX_EXPORT
599  const char *file_name);
600 
609 POCKETSPHINX_EXPORT
611  const char *classname,
612  float32 classweight,
613  char **words,
614  const float32 *weights,
615  int32 n_words);
616 
626 POCKETSPHINX_EXPORT
628  const char *classname,
629  const char *word,
630  float32 weight);
631 
656 POCKETSPHINX_EXPORT
658  ngram_model_t **models,
659  char **names,
660  const float32 *weights,
661  int32 n_models);
662 
693 POCKETSPHINX_EXPORT
695  const char *lmctlfile,
696  logmath_t *lmath);
697 
701 POCKETSPHINX_EXPORT
703 
707 typedef struct ngram_model_set_iter_s ngram_model_set_iter_t;
708 
714 POCKETSPHINX_EXPORT
716 
722 POCKETSPHINX_EXPORT
724 
728 POCKETSPHINX_EXPORT
730 
738 POCKETSPHINX_EXPORT
740  char const **lmname);
741 
748 POCKETSPHINX_EXPORT
750  const char *name);
751 
758 POCKETSPHINX_EXPORT
760  const char *name);
761 
765 POCKETSPHINX_EXPORT
766 const char *ngram_model_set_current(ngram_model_t *set);
767 
775 POCKETSPHINX_EXPORT
777  const char **names,
778  const float32 *weights);
779 
792 POCKETSPHINX_EXPORT
794  ngram_model_t *model,
795  const char *name,
796  float32 weight,
797  int reuse_widmap);
798 
807 POCKETSPHINX_EXPORT
809  const char *name,
810  int reuse_widmap);
811 
815 POCKETSPHINX_EXPORT
817  const char **words,
818  int32 n_words);
819 
827 POCKETSPHINX_EXPORT
829  int32 set_wid);
830 
840 POCKETSPHINX_EXPORT
841 int32 ngram_model_set_known_wid(ngram_model_t *set, int32 set_wid);
842 
846 POCKETSPHINX_EXPORT
848 
849 #endif /* __PS_MODEL_H__ */
ngram_model_get_counts
POCKETSPHINX_EXPORT const uint32 * ngram_model_get_counts(ngram_model_t *model)
ngram_model_set_current
const POCKETSPHINX_EXPORT char * ngram_model_set_current(ngram_model_t *set)
ngram_wid
POCKETSPHINX_EXPORT int32 ngram_wid(ngram_model_t *model, const char *word)
logmath_t
struct logmath_s logmath_t
Definition: logmath.h:106
ngram_model_set_map_words
POCKETSPHINX_EXPORT void ngram_model_set_map_words(ngram_model_t *set, const char **words, int32 n_words)
jsgf_build_fsg
POCKETSPHINX_EXPORT fsg_model_t * jsgf_build_fsg(jsgf_t *grammar, jsgf_rule_t *rule, logmath_t *lmath, float32 lw)
NGRAM_BIN
@ NGRAM_BIN
Definition: model.h:231
jsgf_parse_string
POCKETSPHINX_EXPORT jsgf_t * jsgf_parse_string(const char *string, jsgf_t *parent)
ngram_iter_next
POCKETSPHINX_EXPORT ngram_iter_t * ngram_iter_next(ngram_iter_t *itor)
prim_type.h
Basic type definitions used in Sphinx.
jsgf_rule_iter_t
struct hash_iter_s jsgf_rule_iter_t
Definition: model.h:143
ngram_model_set_add
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_add(ngram_model_t *set, ngram_model_t *model, const char *name, float32 weight, int reuse_widmap)
ngram_model_get_size
POCKETSPHINX_EXPORT int32 ngram_model_get_size(ngram_model_t *model)
jsgf_write_fsg
POCKETSPHINX_EXPORT int jsgf_write_fsg(jsgf_t *grammar, jsgf_rule_t *rule, FILE *outfh)
ngram_file_type_t
enum ngram_file_type_e ngram_file_type_t
File types for N-Gram files.
jsgf_rule_t
Rule in a parsed JSGF grammar.
NGRAM_INVALID
@ NGRAM_INVALID
Definition: model.h:228
ngram_case_t
enum ngram_case_e ngram_case_t
ngram_model_flush
POCKETSPHINX_EXPORT void ngram_model_flush(ngram_model_t *lm)
ngram_model_set_init
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_init(ps_config_t *config, ngram_model_t **models, char **names, const float32 *weights, int32 n_models)
ngram_model_set_select
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_select(ngram_model_t *set, const char *name)
ngram_type_to_str
POCKETSPHINX_EXPORT const char * ngram_type_to_str(int type)
jsgf_read_file
POCKETSPHINX_EXPORT fsg_model_t * jsgf_read_file(const char *file, logmath_t *lmath, float32 lw)
ngram_ng_score
POCKETSPHINX_EXPORT int32 ngram_ng_score(ngram_model_t *model, int32 wid, int32 *history, int32 n_hist, int32 *n_used)
logmath.h
Fast integer logarithmic addition operations.
jsgf_rule_iter_next
POCKETSPHINX_EXPORT jsgf_rule_iter_t * jsgf_rule_iter_next(jsgf_rule_iter_t *itor)
ngram_probv
POCKETSPHINX_EXPORT int32 ngram_probv(ngram_model_t *model, const char *word,...)
ngram_score
POCKETSPHINX_EXPORT int32 ngram_score(ngram_model_t *model, const char *word,...)
ngram_model_set_iter
POCKETSPHINX_EXPORT ngram_model_set_iter_t * ngram_model_set_iter(ngram_model_t *set)
ngram_model_set_lookup
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_lookup(ngram_model_t *set, const char *name)
jsgf_rule_iter
POCKETSPHINX_EXPORT jsgf_rule_iter_t * jsgf_rule_iter(jsgf_t *grammar)
ngram_str_to_type
POCKETSPHINX_EXPORT ngram_file_type_t ngram_str_to_type(const char *str_name)
ngram_iter_get
POCKETSPHINX_EXPORT const int32 * ngram_iter_get(ngram_iter_t *itor, int32 *out_score, int32 *out_bowt)
NGRAM_AUTO
@ NGRAM_AUTO
Definition: model.h:229
ngram_model_free
POCKETSPHINX_EXPORT int ngram_model_free(ngram_model_t *model)
ngram_model_set_iter_t
struct ngram_model_set_iter_s ngram_model_set_iter_t
Definition: model.h:707
ngram_file_type_e
ngram_file_type_e
Definition: model.h:227
ngram_tg_score
POCKETSPHINX_EXPORT int32 ngram_tg_score(ngram_model_t *model, int32 w3, int32 w2, int32 w1, int32 *n_used)
ngram_model_set_count
POCKETSPHINX_EXPORT int32 ngram_model_set_count(ngram_model_t *set)
ngram_model_apply_weights
POCKETSPHINX_EXPORT int ngram_model_apply_weights(ngram_model_t *model, float32 lw, float32 wip)
jsgf_read_string
POCKETSPHINX_EXPORT fsg_model_t * jsgf_read_string(const char *string, logmath_t *lmath, float32 lw)
ps_config_t
ngram_model_set_read
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_read(ps_config_t *config, const char *lmctlfile, logmath_t *lmath)
ngram_prob
POCKETSPHINX_EXPORT int32 ngram_prob(ngram_model_t *model, const char *const *words, int32 n)
ngram_model_set_current_wid
POCKETSPHINX_EXPORT int32 ngram_model_set_current_wid(ngram_model_t *set, int32 set_wid)
NGRAM_ARPA
@ NGRAM_ARPA
Definition: model.h:230
fsg_model_free
POCKETSPHINX_EXPORT int fsg_model_free(fsg_model_t *fsg)
ngram_model_read
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_read(ps_config_t *config, const char *file_name, ngram_file_type_t file_type, logmath_t *lmath)
ngram_word
const POCKETSPHINX_EXPORT char * ngram_word(ngram_model_t *model, int32 wid)
ngram_unknown_wid
POCKETSPHINX_EXPORT int32 ngram_unknown_wid(ngram_model_t *model)
ngram_model_write
POCKETSPHINX_EXPORT int ngram_model_write(ngram_model_t *model, const char *file_name, ngram_file_type_t format)
jsgf_grammar_free
POCKETSPHINX_EXPORT void jsgf_grammar_free(jsgf_t *jsgf)
ngram_model_casefold
POCKETSPHINX_EXPORT int ngram_model_casefold(ngram_model_t *model, int kase)
ngram_case_e
ngram_case_e
Definition: model.h:314
ngram_iter_t
struct ngram_iter_s ngram_iter_t
Definition: model.h:510
jsgf_rule_iter_free
POCKETSPHINX_EXPORT void jsgf_rule_iter_free(jsgf_rule_iter_t *itor)
ngram_model_set_iter_free
POCKETSPHINX_EXPORT void ngram_model_set_iter_free(ngram_model_set_iter_t *itor)
ngram_iter_successors
POCKETSPHINX_EXPORT ngram_iter_t * ngram_iter_successors(ngram_iter_t *itor)
jsgf_rule_public
POCKETSPHINX_EXPORT int jsgf_rule_public(jsgf_rule_t *rule)
ngram_model_retain
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_retain(ngram_model_t *model)
ngram_ng_iter
POCKETSPHINX_EXPORT ngram_iter_t * ngram_ng_iter(ngram_model_t *model, int32 wid, int32 *history, int32 n_hist)
ngram_model_mgrams
POCKETSPHINX_EXPORT ngram_iter_t * ngram_model_mgrams(ngram_model_t *model, int m)
jsgf_rule_iter_rule
POCKETSPHINX_EXPORT jsgf_rule_t * jsgf_rule_iter_rule(jsgf_rule_iter_t *itor)
jsgf_grammar_name
POCKETSPHINX_EXPORT const char * jsgf_grammar_name(jsgf_t *jsgf)
ngram_model_t
N-Gram based language model.
jsgf_t
JSGF parser.
ngram_ng_prob
POCKETSPHINX_EXPORT int32 ngram_ng_prob(ngram_model_t *model, int32 wid, int32 *history, int32 n_hist, int32 *n_used)
ngram_zero
POCKETSPHINX_EXPORT int32 ngram_zero(ngram_model_t *model)
jsgf_get_rule
POCKETSPHINX_EXPORT jsgf_rule_t * jsgf_get_rule(jsgf_t *grammar, const char *name)
ngram_model_set_remove
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_remove(ngram_model_t *set, const char *name, int reuse_widmap)
ngram_iter_free
POCKETSPHINX_EXPORT void ngram_iter_free(ngram_iter_t *itor)
fsg_model_t
Finite-state grammar.
ngram_iter
POCKETSPHINX_EXPORT ngram_iter_t * ngram_iter(ngram_model_t *model, const char *word,...)
ngram_model_read_classdef
POCKETSPHINX_EXPORT int32 ngram_model_read_classdef(ngram_model_t *model, const char *file_name)
ngram_model_add_class_word
POCKETSPHINX_EXPORT int32 ngram_model_add_class_word(ngram_model_t *model, const char *classname, const char *word, float32 weight)
ngram_score_to_prob
POCKETSPHINX_EXPORT int32 ngram_score_to_prob(ngram_model_t *model, int32 score)
ngram_model_add_class
POCKETSPHINX_EXPORT int32 ngram_model_add_class(ngram_model_t *model, const char *classname, float32 classweight, char **words, const float32 *weights, int32 n_words)
ngram_file_name_to_type
POCKETSPHINX_EXPORT ngram_file_type_t ngram_file_name_to_type(const char *file_name)
jsgf_get_public_rule
POCKETSPHINX_EXPORT jsgf_rule_t * jsgf_get_public_rule(jsgf_t *grammar)
jsgf_parse_file
POCKETSPHINX_EXPORT jsgf_t * jsgf_parse_file(const char *filename, jsgf_t *parent)
ngram_model_set_interp
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_interp(ngram_model_t *set, const char **names, const float32 *weights)
ngram_model_set_known_wid
POCKETSPHINX_EXPORT int32 ngram_model_set_known_wid(ngram_model_t *set, int32 set_wid)
ngram_model_set_iter_next
POCKETSPHINX_EXPORT ngram_model_set_iter_t * ngram_model_set_iter_next(ngram_model_set_iter_t *itor)
fsg_model_retain
POCKETSPHINX_EXPORT fsg_model_t * fsg_model_retain(fsg_model_t *fsg)
ngram_bg_score
POCKETSPHINX_EXPORT int32 ngram_bg_score(ngram_model_t *model, int32 w2, int32 w1, int32 *n_used)
ngram_model_add_word
POCKETSPHINX_EXPORT int32 ngram_model_add_word(ngram_model_t *model, const char *word, float32 weight)
ngram_model_get_weights
POCKETSPHINX_EXPORT float32 ngram_model_get_weights(ngram_model_t *model, int32 *out_log_wip)
jsgf_rule_name
POCKETSPHINX_EXPORT const char * jsgf_rule_name(jsgf_rule_t *rule)
ngram_class_t
Word class in an N-Gram model.
ngram_model_set_iter_model
POCKETSPHINX_EXPORT ngram_model_t * ngram_model_set_iter_model(ngram_model_set_iter_t *itor, char const **lmname)