src/libpocketsphinx/lts.c

00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
00002 /*************************************************************************/
00003 /*                                                                       */
00004 /*                  Language Technologies Institute                      */
00005 /*                     Carnegie Mellon University                        */
00006 /*                        Copyright (c) 1999                             */
00007 /*                        All Rights Reserved.                           */
00008 /*                                                                       */
00009 /*  Permission is hereby granted, free of charge, to use and distribute  */
00010 /*  this software and its documentation without restriction, including   */
00011 /*  without limitation the rights to use, copy, modify, merge, publish,  */
00012 /*  distribute, sublicense, and/or sell copies of this work, and to      */
00013 /*  permit persons to whom this work is furnished to do so, subject to   */
00014 /*  the following conditions:                                            */
00015 /*   1. The code must retain the above copyright notice, this list of    */
00016 /*      conditions and the following disclaimer.                         */
00017 /*   2. Any modifications must be clearly marked as such.                */
00018 /*   3. Original authors' names are not deleted.                         */
00019 /*   4. The authors' names are not used to endorse or promote products   */
00020 /*      derived from this software without specific prior written        */
00021 /*      permission.                                                      */
00022 /*                                                                       */
00023 /*  CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK         */
00024 /*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
00025 /*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
00026 /*  SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE      */
00027 /*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
00028 /*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
00029 /*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
00030 /*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
00031 /*  THIS SOFTWARE.                                                       */
00032 /*                                                                       */
00033 /*************************************************************************/
00034 /*             Author:  Alan W Black (awb@cs.cmu.edu)                    */
00035 /*               Date:  December 1999                                    */
00036 /*************************************************************************/
00037 /*                                                                       */
00038 /*  Letter to sound rule support                                         */
00039 /*                                                                       */
00040 /*************************************************************************/
00041 /* Modified for SphinxTrain by David Huggins-Daines <dhuggins@cs.cmu.edu> */
00042 /* Modified for sphinx3 by Arthur Chan <archan@cs.cmu.edu>                */
00043 
00044 /*
00045  * lts.c -- Letter to sound rule support 
00046  * 
00047  * $Log$
00048  * Revision 1.3  2006/03/01  21:44:26  egouvea
00049  * Removed magic number "6", and swap variable if machine is big endian.
00050  * 
00051  * Revision 1.2  2006/02/22 20:44:17  arthchan2003
00052  * Merged from branch SPHINX3_5_2_RCI_IRII_BRANCH: As we have done in SphinxTrain and with the permission of flite developer. check in the routines for using LTS. In dict.c, one will see detail comment on how it was used and how we avoid problematic conditions.
00053  *
00054  * Revision 1.1.2.1  2005/09/25 19:07:52  arthchan2003
00055  * Added LTS rules and the module to use it.
00056  *
00057  *
00058  */
00059 
00060 #include <stdio.h>
00061 #include <string.h>
00062 #include <ctype.h>
00063 #include "lts.h"
00064 #include "ckd_alloc.h"
00065 #include "bio.h"
00066 
00067 
00068 static cst_lts_phone apply_model(cst_lts_letter * vals,
00069                                  cst_lts_addr start,
00070                                  const cst_lts_model * model);
00071 
00072 void
00073 lex_print(lex_entry_t * ent)
00074 {
00075     int i;
00076 
00077     for (i = 0; i < ent->phone_cnt; ++i) {
00078         printf("%s ", ent->phone[i]);
00079     }
00080     printf("\n");
00081     fflush(stdout);
00082 }
00083 
00084 static char *
00085 cst_substr(const char *str, int start, int length)
00086 {
00087     char *nstr = NULL;
00088 
00089     if (str) {
00090         nstr = ckd_malloc(length + 1);
00091         strncpy(nstr, str + start, length);
00092         nstr[length] = '\0';
00093     }
00094     return nstr;
00095 }
00096 
00097 int
00098 lts_apply(const char *in_word, const char *feats,
00099           const cst_lts_rules * r, lex_entry_t * out_phones)
00100 {
00101     int pos, index, i, maxphones;
00102     cst_lts_letter *fval_buff;
00103     cst_lts_letter *full_buff;
00104     cst_lts_phone phone;
00105     char *left, *right, *p;
00106     char hash;
00107     char zeros[8];
00108     char *word;
00109 
00110     /* Downcase the incoming word unless we are a non-roman alphabet. */
00111     word = ckd_salloc((char *) in_word);
00112     if (!r->letter_table)
00113         for (i = 0; i < strlen(word); ++i)
00114             word[i] = tolower(word[i]);
00115 
00116     /* Fill in out_phones structure as best we can. */
00117     maxphones = strlen(word) + 10;
00118     out_phones->phone = ckd_malloc(maxphones * sizeof(char *));
00119     out_phones->ci_acmod_id = ckd_malloc(maxphones * sizeof(acmod_id_t));
00120     out_phones->phone_cnt = 0;
00121 
00122     /* For feature vals for each letter */
00123     fval_buff = ckd_calloc((r->context_window_size * 2) +
00124                            r->context_extra_feats, sizeof(cst_lts_letter));
00125     /* Buffer with added contexts */
00126     full_buff = ckd_calloc((r->context_window_size * 2) +       /* TBD assumes single POS feat */
00127                            strlen(word) + 1, sizeof(cst_lts_letter));
00128     if (r->letter_table) {
00129         for (i = 0; i < 8; i++)
00130             zeros[i] = 2;
00131         sprintf((char *)full_buff, "%.*s%c%s%c%.*s",
00132                 r->context_window_size - 1, zeros,
00133                 1, word, 1, r->context_window_size - 1, zeros);
00134         hash = 1;
00135     }
00136     else {
00137         /* Assumes l_letter is a char and context < 8 */
00138         sprintf((char *)full_buff, "%.*s#%s#%.*s",
00139                 r->context_window_size - 1, "00000000",
00140                 word, r->context_window_size - 1, "00000000");
00141         hash = '#';
00142     }
00143 
00144     /* Do the prediction forwards (begone, foul LISP!) */
00145     for (pos = r->context_window_size; full_buff[pos] != hash; ++pos) {
00146         /* Fill the features buffer for the predictor */
00147         sprintf((char *)fval_buff, "%.*s%.*s%s",
00148                 r->context_window_size,
00149                 full_buff + pos - r->context_window_size,
00150                 r->context_window_size, full_buff + pos + 1, feats);
00151         if ((!r->letter_table
00152              && ((full_buff[pos] < 'a') || (full_buff[pos] > 'z')))) {
00153 #ifdef EXCESSIVELY_CHATTY
00154             E_WARN("lts:skipping unknown char \"%c\"\n", full_buff[pos]);
00155 #endif
00156             continue;
00157         }
00158         if (r->letter_table)
00159             index = full_buff[pos] - 3;
00160         else
00161             index = (full_buff[pos] - 'a') % 26;
00162         phone = apply_model(fval_buff, r->letter_index[index], r->models);
00163         /* delete epsilons and split dual-phones */
00164         if (0 == strcmp("epsilon", r->phone_table[phone]))
00165             continue;
00166         /* dynamically grow out_phones if necessary. */
00167         if (out_phones->phone_cnt + 2 > maxphones) {
00168             maxphones += 10;
00169             out_phones->phone = ckd_realloc(out_phones->phone,
00170                                             maxphones * sizeof(char *));
00171             out_phones->ci_acmod_id = ckd_realloc(out_phones->ci_acmod_id,
00172                                                   maxphones *
00173                                                   sizeof(acmod_id_t));
00174         }
00175         if ((p = strchr(r->phone_table[phone], '-')) != NULL) {
00176             left = cst_substr(r->phone_table[phone], 0,
00177                               strlen(r->phone_table[phone]) - strlen(p));
00178             right = cst_substr(r->phone_table[phone],
00179                                (strlen(r->phone_table[phone]) -
00180                                 strlen(p)) + 1, (strlen(p) - 1));
00181             out_phones->phone[out_phones->phone_cnt++] = left;
00182             out_phones->phone[out_phones->phone_cnt++] = right;
00183         }
00184         else
00185             out_phones->phone[out_phones->phone_cnt++] =
00186                 ckd_salloc((char *) r->phone_table[phone]);
00187     }
00188 
00189 
00190     ckd_free(full_buff);
00191     ckd_free(fval_buff);
00192     ckd_free(word);
00193     return S3_SUCCESS;
00194 }
00195 
00196 static cst_lts_phone
00197 apply_model(cst_lts_letter * vals, cst_lts_addr start,
00198             const cst_lts_model * model)
00199 {
00200     /* because some machines (ipaq/mips) can't deal with addrs not on     */
00201     /* word boundaries we use a static and copy the rule values each time */
00202     /* so we know its properly aligned                                    */
00203     /* Hmm this still might be wrong on some machines that align the      */
00204     /* structure cst_lts_rules differently                                */
00205     cst_lts_rule state;
00206     unsigned short nstate;
00207     static const int sizeof_cst_lts_rule = sizeof(cst_lts_rule);
00208 
00209     memmove(&state, &model[start * sizeof_cst_lts_rule],
00210             sizeof_cst_lts_rule);
00211     for (; state.feat != CST_LTS_EOR;) {
00212         if (vals[state.feat] == state.val)
00213             nstate = state.qtrue;
00214         else
00215             nstate = state.qfalse;
00216 
00217 #if defined(WORDS_BIGENDIAN)
00218         SWAP_INT16(&nstate);
00219 #endif
00220 
00221         memmove(&state, &model[nstate * sizeof_cst_lts_rule],
00222                 sizeof_cst_lts_rule);
00223     }
00224 
00225     return (cst_lts_phone) state.val;
00226 }
00227 
00228 #ifdef UNIT_TEST
00229 /* gcc -DUNIT_TEST -I../../libutil/ -I../libcommon/ cmu6_lts_rules.c lts.c ../libcommon/bio.c ../../libutil/err.c ../../libutil/ckd_alloc.c*/
00230 
00231 int
00232 main(int argc, char *argv[])
00233 {
00234     lex_entry_t out;
00235     int i;
00236 
00237     lts_apply("HELLO", "", &cmu6_lts_rules, &out);
00238     lex_print(&out);
00239     ckd_free(out.phone);
00240     ckd_free(out.ci_acmod_id);
00241 
00242     lts_apply("EXCELLENT", "", &cmu6_lts_rules, &out);
00243     lex_print(&out);
00244     ckd_free(out.phone);
00245     ckd_free(out.ci_acmod_id);
00246 
00247     lts_apply("TWELVE", "", &cmu6_lts_rules, &out);
00248     lex_print(&out);
00249     ckd_free(out.phone);
00250     ckd_free(out.ci_acmod_id);
00251 
00252     return 0;
00253 }
00254 #endif

Generated on Mon Jan 24 21:50:16 2011 for PocketSphinx by  doxygen 1.4.7