| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 1999 */ |
| 6 |
/* All Rights Reserved. */ |
| 7 |
/* */ |
| 8 |
/* Permission is hereby granted, free of charge, to use and distribute */ |
| 9 |
/* this software and its documentation without restriction, including */ |
| 10 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 11 |
/* distribute, sublicense, and/or sell copies of this work, and to */ |
| 12 |
/* permit persons to whom this work is furnished to do so, subject to */ |
| 13 |
/* the following conditions: */ |
| 14 |
/* 1. The code must retain the above copyright notice, this list of */ |
| 15 |
/* conditions and the following disclaimer. */ |
| 16 |
/* 2. Any modifications must be clearly marked as such. */ |
| 17 |
/* 3. Original authors' names are not deleted. */ |
| 18 |
/* 4. The authors' names are not used to endorse or promote products */ |
| 19 |
/* derived from this software without specific prior written */ |
| 20 |
/* permission. */ |
| 21 |
/* */ |
| 22 |
/* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */ |
| 23 |
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ |
| 24 |
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ |
| 25 |
/* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */ |
| 26 |
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ |
| 27 |
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ |
| 28 |
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ |
| 29 |
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
| 30 |
/* THIS SOFTWARE. */ |
| 31 |
/* */ |
| 32 |
/*************************************************************************/ |
| 33 |
/* Author: Alan W Black ([email protected]) */ |
| 34 |
/* Date: December 1999 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Letter to sound rule support */ |
| 38 |
/* */ |
| 39 |
/*************************************************************************/ |
| 40 |
|
| 41 |
#include "cst_features.h" |
| 42 |
#include "cst_lts.h" |
| 43 |
#include "cst_endian.h" |
| 44 |
|
| 45 |
static cst_lts_phone apply_model(cst_lts_letter *vals, |
| 46 |
cst_lts_addr start, |
| 47 |
const cst_lts_model *model); |
| 48 |
|
| 49 |
cst_lts_rules *new_lts_rules() |
| 50 |
{ |
| 51 |
cst_lts_rules *lt = cst_alloc(cst_lts_rules,1); |
| 52 |
lt->name = 0; |
| 53 |
lt->letter_index = 0; |
| 54 |
lt->models = 0; |
| 55 |
lt->phone_table = 0; |
| 56 |
lt->context_window_size = 0; |
| 57 |
lt->context_extra_feats = 0; |
| 58 |
lt->letter_table = 0; |
| 59 |
return lt; |
| 60 |
} |
| 61 |
|
| 62 |
cst_val *lts_apply_val(const cst_val *wlist,const char *feats,const cst_lts_rules *r) |
| 63 |
{ |
| 64 |
/* for symbol to symbol mapping */ |
| 65 |
const cst_val *v; |
| 66 |
cst_val *p; |
| 67 |
char *word; |
| 68 |
int i,j; |
| 69 |
|
| 70 |
word = cst_alloc(char,val_length(wlist)+1); |
| 71 |
|
| 72 |
for (v=wlist,i=0; v; v=val_cdr(v),i++) |
| 73 |
{ |
| 74 |
for (j=0; r->letter_table[j]; j++) |
| 75 |
if (cst_streq(val_string(val_car(v)),r->letter_table[j])) |
| 76 |
{ |
| 77 |
word[i] = j; |
| 78 |
break; |
| 79 |
} |
| 80 |
if (!r->letter_table[j]) |
| 81 |
{ |
| 82 |
#if 0 |
| 83 |
printf("awb_debug unknown letter >%s<\n",val_string(val_car(v))); |
| 84 |
#endif |
| 85 |
i--; /* can't find this letter so skip it */ |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
p = lts_apply(word,feats,r); |
| 90 |
cst_free(word); |
| 91 |
|
| 92 |
return p; |
| 93 |
} |
| 94 |
|
| 95 |
cst_val *lts_apply(const char *word,const char *feats,const cst_lts_rules *r) |
| 96 |
{ |
| 97 |
int pos, index, i; |
| 98 |
cst_val *phones=0; |
| 99 |
cst_lts_letter *fval_buff; |
| 100 |
cst_lts_letter *full_buff; |
| 101 |
cst_lts_phone phone; |
| 102 |
char *left, *right; |
| 103 |
const char *p; |
| 104 |
char hash; |
| 105 |
char zeros[8]; |
| 106 |
|
| 107 |
/* For feature vals for each letter */ |
| 108 |
fval_buff = cst_alloc(cst_lts_letter, |
| 109 |
(r->context_window_size*2)+ |
| 110 |
r->context_extra_feats); |
| 111 |
/* Buffer with added contexts */ |
| 112 |
full_buff = cst_alloc(cst_lts_letter, |
| 113 |
(r->context_window_size*2)+ |
| 114 |
cst_strlen(word)+1); /* TBD assumes single POS feat */ |
| 115 |
if (r->letter_table) |
| 116 |
{ |
| 117 |
for (i=0; i<8; i++) zeros[i] = 2; |
| 118 |
cst_sprintf((char *)full_buff, |
| 119 |
"%.*s%c%s%c%.*s", |
| 120 |
r->context_window_size-1, zeros, |
| 121 |
1, |
| 122 |
word, |
| 123 |
1, |
| 124 |
r->context_window_size-1, zeros); |
| 125 |
hash = 1; |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
/* Assumes l_letter is a char and context < 8 */ |
| 130 |
cst_sprintf((char *)full_buff, |
| 131 |
"%.*s#%s#%.*s", |
| 132 |
r->context_window_size-1, "00000000", |
| 133 |
word, |
| 134 |
r->context_window_size-1, "00000000"); |
| 135 |
hash = '#'; |
| 136 |
} |
| 137 |
|
| 138 |
/* Do the prediction backwards so we don't need to reverse the answer */ |
| 139 |
for (pos = r->context_window_size + cst_strlen(word) - 1; |
| 140 |
full_buff[pos] != hash; |
| 141 |
pos--) |
| 142 |
{ |
| 143 |
/* Fill the features buffer for the predictor */ |
| 144 |
cst_sprintf((char *)fval_buff, |
| 145 |
"%.*s%.*s%s", |
| 146 |
r->context_window_size, |
| 147 |
full_buff+pos-r->context_window_size, |
| 148 |
r->context_window_size, |
| 149 |
full_buff+pos+1, |
| 150 |
feats); |
| 151 |
if ((!r->letter_table |
| 152 |
&& ((full_buff[pos] < 'a') || (full_buff[pos] > 'z')))) |
| 153 |
{ |
| 154 |
#ifdef EXCESSIVELY_CHATTY |
| 155 |
cst_errmsg("lts:skipping unknown char \"%c\"\n", |
| 156 |
full_buff[pos]); |
| 157 |
#endif |
| 158 |
continue; |
| 159 |
} |
| 160 |
if (r->letter_table) |
| 161 |
index = full_buff[pos] - 3; |
| 162 |
else |
| 163 |
index = (full_buff[pos]-'a')%26; |
| 164 |
phone = apply_model(fval_buff, |
| 165 |
r->letter_index[index], |
| 166 |
r->models); |
| 167 |
#if 0 |
| 168 |
/* But these are in reverse order */ |
| 169 |
printf("letter-phone(s) alignment: %c %s\n", |
| 170 |
full_buff[pos], |
| 171 |
(const char *)r->phone_table[phone]); |
| 172 |
#endif |
| 173 |
/* delete epsilons and split dual-phones */ |
| 174 |
if (cst_streq("epsilon",r->phone_table[phone])) |
| 175 |
continue; |
| 176 |
else if ((p=strchr(r->phone_table[phone],'-')) != NULL) |
| 177 |
{ |
| 178 |
left = cst_substr(r->phone_table[phone],0, |
| 179 |
cst_strlen(r->phone_table[phone])-cst_strlen(p)); |
| 180 |
right = cst_substr(r->phone_table[phone], |
| 181 |
(cst_strlen(r->phone_table[phone])-cst_strlen(p))+1, |
| 182 |
(cst_strlen(p)-1)); |
| 183 |
phones = cons_val(string_val(left), |
| 184 |
cons_val(string_val(right),phones)); |
| 185 |
cst_free(left); |
| 186 |
cst_free(right); |
| 187 |
} |
| 188 |
else |
| 189 |
phones = cons_val(string_val(r->phone_table[phone]),phones); |
| 190 |
} |
| 191 |
|
| 192 |
cst_free(full_buff); |
| 193 |
cst_free(fval_buff); |
| 194 |
|
| 195 |
return phones; |
| 196 |
} |
| 197 |
|
| 198 |
static void cst_lts_get_state(cst_lts_rule *state, |
| 199 |
const cst_lts_model *model, |
| 200 |
unsigned short n, |
| 201 |
int rule_size) |
| 202 |
{ /* As some OS's require a more elaborate access than a simple lookup */ |
| 203 |
memmove(state,&model[n*rule_size],rule_size); |
| 204 |
} |
| 205 |
|
| 206 |
static cst_lts_phone apply_model(cst_lts_letter *vals,cst_lts_addr start, |
| 207 |
const cst_lts_model *model) |
| 208 |
{ |
| 209 |
/* because some machines (arm/mips) can't deal with addrs not on */ |
| 210 |
/* word boundaries we use a static and copy the rule values each time */ |
| 211 |
/* so we know its properly aligned */ |
| 212 |
/* Hmm this still might be wrong on some machines that align the */ |
| 213 |
/* structure cst_lts_rules differently */ |
| 214 |
cst_lts_rule state; |
| 215 |
unsigned short nstate; |
| 216 |
static const int sizeof_cst_lts_rule = 6; |
| 217 |
|
| 218 |
cst_lts_get_state(&state,model,start,sizeof_cst_lts_rule); |
| 219 |
for ( ; |
| 220 |
state.feat != CST_LTS_EOR; |
| 221 |
) |
| 222 |
{ |
| 223 |
/* printf("awb_debug %s %c %c %d\n",vals,vals[state.feat],state.val, |
| 224 |
(vals[state.feat] == state.val) ? 1 : 0); */ |
| 225 |
if (vals[state.feat] == state.val) |
| 226 |
nstate = state.qtrue; |
| 227 |
else |
| 228 |
nstate = state.qfalse; |
| 229 |
/* This should really happen at compilation time */ |
| 230 |
if (CST_BIG_ENDIAN) |
| 231 |
nstate = SWAPSHORT(nstate); |
| 232 |
|
| 233 |
cst_lts_get_state(&state,model,nstate,sizeof_cst_lts_rule); |
| 234 |
} |
| 235 |
|
| 236 |
return (cst_lts_phone)state.val; |
| 237 |
} |