lm3g_model.h
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* ====================================================================
* Copyright (c) 1999-2007 Carnegie Mellon University. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* This work was supported in part by funding from the Defense Advanced
* Research Projects Agency and the National Science Foundation of the
* United States of America, and the CMU Sphinx Speech Consortium.
*
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
*
*/
/*
* \file lm3g_model.h Core Sphinx 3-gram code used in
* DMP/DMP32/ARPA (for now) model code.
*
* Author: A cast of thousands, probably.
*/
#ifndef __NGRAM_MODEL_LM3G_H__
#define __NGRAM_MODEL_LM3G_H__
#include "sphinxbase/listelem_alloc.h"
#include "ngram_model_internal.h"
typedef union {
float32 f;
int32 l;
} lmprob_t;
typedef struct sorted_entry_s {
lmprob_t val;
uint32 lower;
uint32 higher;
} sorted_entry_t;
typedef struct {
sorted_entry_t *list;
int32 free;
int32 size;
} sorted_list_t;
typedef struct unigram_s {
lmprob_t prob1;
lmprob_t bo_wt1;
int32 bigrams;
} unigram_t;
typedef struct bigram_s bigram_t;
typedef struct trigram_s trigram_t;
/*
* To conserve space, bigram info is kept in many tables. Since the number
* of distinct values << #bigrams, these table indices can be 16-bit values.
* prob2 and bo_wt2 are such indices, but keeping trigram index is less easy.
* It is supposed to be the index of the first trigram entry for each bigram.
* But such an index cannot be represented in 16-bits, hence the following
* segmentation scheme: Partition bigrams into segments of BG_SEG_SZ
* consecutive entries, such that #trigrams in each segment <= 2**16 (the
* corresponding trigram segment). The bigram_t.trigrams value is then a
* 16-bit relative index within the trigram segment. A separate table--
* lm_t.tseg_base--has the index of the 1st trigram for each bigram segment.
*/
#define BG_SEG_SZ 512 /* chosen so that #trigram/segment <= 2**16 */
#define LOG_BG_SEG_SZ 9
typedef struct tginfo_s {
int32 w1;
int32 n_tg;
int32 bowt;
int32 used;
trigram_t *tg;
struct tginfo_s *next;
} tginfo_t;
typedef struct lm3g_model_s {
unigram_t *unigrams;
bigram_t *bigrams;
trigram_t *trigrams;
lmprob_t *prob2;
int32 n_prob2;
lmprob_t *bo_wt2;
int32 n_bo_wt2;
lmprob_t *prob3;
int32 n_prob3;
int32 *tseg_base;
tginfo_t **tginfo;
listelem_alloc_t *le;
} lm3g_model_t;
void lm3g_tginfo_free(ngram_model_t *base, lm3g_model_t *lm3g);
void lm3g_tginfo_reset(ngram_model_t *base, lm3g_model_t *lm3g);
void lm3g_apply_weights(ngram_model_t *base,
lm3g_model_t *lm3g,
float32 lw, float32 wip, float32 uw);
int32 lm3g_add_ug(ngram_model_t *base,
lm3g_model_t *lm3g, int32 wid, int32 lweight);
void init_sorted_list(sorted_list_t *l);
void free_sorted_list(sorted_list_t *l);
lmprob_t *vals_in_sorted_list(sorted_list_t *l);
int32 sorted_id(sorted_list_t * l, int32 *val);
#endif /* __NGRAM_MODEL_LM3G_H__ */
unigram_s::bo_wt1
lmprob_t bo_wt1
Unigram backoff weight.
Definition: lm3g_model.h:93
lm3g_model_s::le
listelem_alloc_t * le
List element allocator for tginfo.
Definition: lm3g_model.h:156
tginfo_s::w1
int32 w1
lw1 component of bigram lw1,lw2.
Definition: lm3g_model.h:130
sorted_entry_s::val
lmprob_t val
value being kept in this node
Definition: lm3g_model.h:69
lm3g_model_s::n_prob3
int32 n_prob3
prob3 size
Definition: lm3g_model.h:151
lm3g_model_s::prob2
lmprob_t * prob2
Table of actual bigram probs.
Definition: lm3g_model.h:146
bigram_s
Bigram structure.
Definition: ngram_model_arpa.h:52
tginfo_s::used
int32 used
whether used since last lm_reset
Definition: lm3g_model.h:134
unigram_s
Unigram structure (common among all lm3g implementations)
Definition: lm3g_model.h:91
lm3g_model_s::n_bo_wt2
int32 n_bo_wt2
bo_wt2 size
Definition: lm3g_model.h:149
unigram_s::bigrams
int32 bigrams
Index of 1st entry in lm_t.bigrams[].
Definition: lm3g_model.h:94
lm3g_model_s::prob3
lmprob_t * prob3
Table of actual trigram probs.
Definition: lm3g_model.h:150
tginfo_s::tg
trigram_t * tg
Trigrams for lw1,lw2.
Definition: lm3g_model.h:135
tginfo_s
Trigram information cache.
Definition: lm3g_model.h:129
listelem_alloc_s
Fast linked list allocator.
Definition: listelem_alloc.c:65
trigram_s
Trigram structure.
Definition: ngram_model_arpa.h:66
lm3g_model_s
Common internal structure for Sphinx 3-gram models.
Definition: lm3g_model.h:142
listelem_alloc.h
Fast memory allocator for uniformly sized objects.
tginfo_s::n_tg
int32 n_tg
number tg for parent bigram lw1,lw2
Definition: lm3g_model.h:132
sorted_list_t
The sorted list.
Definition: lm3g_model.h:82
tginfo_s::next
struct tginfo_s * next
Next lw1 with same parent lw2; NULL if none.
Definition: lm3g_model.h:136
sorted_entry_s::lower
uint32 lower
index of another entry.
Definition: lm3g_model.h:70
unigram_s::prob1
lmprob_t prob1
Unigram probability.
Definition: lm3g_model.h:92
lm3g_model_s::bo_wt2
lmprob_t * bo_wt2
Table of actual bigram backoff weights.
Definition: lm3g_model.h:148
sorted_entry_s
Bigram probs and bo-wts, and trigram probs are kept in separate tables rather than within the bigram_...
Definition: lm3g_model.h:68
ngram_model_s
Common implementation of ngram_model_t.
Definition: ngram_model_internal.h:55
lmprob_t
Type used to store language model probabilities.
Definition: lm3g_model.h:54
sorted_list_t::free
int32 free
first free element in list
Definition: lm3g_model.h:84
tginfo_s::bowt
int32 bowt
tg bowt for lw1,lw2
Definition: lm3g_model.h:133
lm3g_model_s::tginfo
tginfo_t ** tginfo
tginfo[lw2] is head of linked list of trigram information for some cached subset of bigrams (*...
Definition: lm3g_model.h:154
lm3g_model_s::n_prob2
int32 n_prob2
prob2 size
Definition: lm3g_model.h:147
lm3g_model_s::tseg_base
int32 * tseg_base
tseg_base[i>>LOG_BG_SEG_SZ] = index of 1st trigram for bigram segment (i>>LOG_BG_SEG_SZ) ...
Definition: lm3g_model.h:152
sorted_entry_s::higher
uint32 higher
index of another entry.
Definition: lm3g_model.h:73