| 1 |
/* |
| 2 |
* Copyright (C) 2005 to 2014 by Jonathan Duddington |
| 3 |
* email: [email protected] |
| 4 |
* Copyright (C) 2013-2017 Reece H. Dunn |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, see: <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "espeak__config.h" |
| 21 |
|
| 22 |
#include <ctype.h> |
| 23 |
#include <stdint.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include "wctype_portable.h" |
| 28 |
#include <wchar.h> |
| 29 |
#include <assert.h> |
| 30 |
|
| 31 |
#include "espeak_ng.h" |
| 32 |
#include "speak_lib.h" |
| 33 |
#include "encoding.h" |
| 34 |
|
| 35 |
#include "common.h" // for GetFileLength, strncpy0 |
| 36 |
#include "dictionary.h" |
| 37 |
#include "numbers.h" // for LookupAccentedLetter, Look... |
| 38 |
#include "phoneme.h" // for PHONEME_TAB, phVOWEL, phon... |
| 39 |
#include "readclause.h" // for WordToString2 |
| 40 |
#include "speech.h" // for path_home |
| 41 |
#include "compiledict.h" // for DecodeRule |
| 42 |
#include "synthdata.h" // for PhonemeCode, InterpretPhoneme |
| 43 |
#include "synthesize.h" // for STRESS_IS_PRIMARY, phoneme... |
| 44 |
#include "translate.h" // for Translator, utf8_in, LANGU... |
| 45 |
|
| 46 |
#include "espeak_praat.h" // for theEspeakPraatFileInMemorySet |
| 47 |
|
| 48 |
static int LookupFlags(Translator *tr, const char *word, unsigned int flags_out[2]); |
| 49 |
static void DollarRule(char *word[], char *word_start, int consumed, int group_length, char *word_buf, Translator *tr, int command, int *failed, int *add_points); |
| 50 |
|
| 51 |
typedef struct { |
| 52 |
int points; |
| 53 |
const char *phonemes; |
| 54 |
int end_type; |
| 55 |
char *del_fwd; |
| 56 |
} MatchRecord; |
| 57 |
|
| 58 |
|
| 59 |
int dictionary_skipwords; |
| 60 |
char dictionary_name[40]; |
| 61 |
|
| 62 |
// accented characters which indicate (in some languages) the start of a separate syllable |
| 63 |
static const unsigned short diereses_list[7] = { 0xe4, 0xeb, 0xef, 0xf6, 0xfc, 0xff, 0 }; |
| 64 |
|
| 65 |
// convert characters to an approximate 7 bit ascii equivalent |
| 66 |
// used for checking for vowels (up to 0x259=schwa) |
| 67 |
#define N_REMOVE_ACCENT 0x25e |
| 68 |
static const unsigned char remove_accent[N_REMOVE_ACCENT] = { |
| 69 |
'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', // 0c0 |
| 70 |
'd', 'n', 'o', 'o', 'o', 'o', 'o', 0, 'o', 'u', 'u', 'u', 'u', 'y', 't', 's', // 0d0 |
| 71 |
'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', // 0e0 |
| 72 |
'd', 'n', 'o', 'o', 'o', 'o', 'o', 0, 'o', 'u', 'u', 'u', 'u', 'y', 't', 'y', // 0f0 |
| 73 |
|
| 74 |
'a', 'a', 'a', 'a', 'a', 'a', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'd', 'd', // 100 |
| 75 |
'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'g', 'g', 'g', 'g', // 110 |
| 76 |
'g', 'g', 'g', 'g', 'h', 'h', 'h', 'h', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', // 120 |
| 77 |
'i', 'i', 'i', 'i', 'j', 'j', 'k', 'k', 'k', 'l', 'l', 'l', 'l', 'l', 'l', 'l', // 130 |
| 78 |
'l', 'l', 'l', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', // 140 |
| 79 |
'o', 'o', 'o', 'o', 'r', 'r', 'r', 'r', 'r', 'r', 's', 's', 's', 's', 's', 's', // 150 |
| 80 |
's', 's', 't', 't', 't', 't', 't', 't', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 160 |
| 81 |
'u', 'u', 'u', 'u', 'w', 'w', 'y', 'y', 'y', 'z', 'z', 'z', 'z', 'z', 'z', 's', // 170 |
| 82 |
'b', 'b', 'b', 'b', 0, 0, 'o', 'c', 'c', 'd', 'd', 'd', 'd', 'd', 'e', 'e', // 180 |
| 83 |
'e', 'f', 'f', 'g', 'g', 'h', 'i', 'i', 'k', 'k', 'l', 'l', 'm', 'n', 'n', 'o', // 190 |
| 84 |
'o', 'o', 'o', 'o', 'p', 'p', 'y', 0, 0, 's', 's', 't', 't', 't', 't', 'u', // 1a0 |
| 85 |
'u', 'u', 'v', 'y', 'y', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 0, 0, 0, 'w', // 1b0 |
| 86 |
't', 't', 't', 'k', 'd', 'd', 'd', 'l', 'l', 'l', 'n', 'n', 'n', 'a', 'a', 'i', // 1c0 |
| 87 |
'i', 'o', 'o', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'e', 'a', 'a', // 1d0 |
| 88 |
'a', 'a', 'a', 'a', 'g', 'g', 'g', 'g', 'k', 'k', 'o', 'o', 'o', 'o', 'z', 'z', // 1e0 |
| 89 |
'j', 'd', 'd', 'd', 'g', 'g', 'w', 'w', 'n', 'n', 'a', 'a', 'a', 'a', 'o', 'o', // 1f0 |
| 90 |
|
| 91 |
'a', 'a', 'a', 'a', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'o', 'o', 'o', // 200 |
| 92 |
'r', 'r', 'r', 'r', 'u', 'u', 'u', 'u', 's', 's', 't', 't', 'y', 'y', 'h', 'h', // 210 |
| 93 |
'n', 'd', 'o', 'o', 'z', 'z', 'a', 'a', 'e', 'e', 'o', 'o', 'o', 'o', 'o', 'o', // 220 |
| 94 |
'o', 'o', 'y', 'y', 'l', 'n', 't', 'j', 'd', 'q', 'a', 'c', 'c', 'l', 't', 's', // 230 |
| 95 |
'z', 0, 0, 'b', 'u', 'v', 'e', 'e', 'j', 'j', 'q', 'q', 'r', 'r', 'y', 'y', // 240 |
| 96 |
'a', 'a', 'a', 'b', 'o', 'c', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e' |
| 97 |
}; |
| 98 |
|
| 99 |
static int Reverse4Bytes(int word) |
| 100 |
{ |
| 101 |
// reverse the order of bytes from little-endian to big-endian |
| 102 |
#ifdef ARCH_BIG |
| 103 |
int ix; |
| 104 |
int word2 = 0; |
| 105 |
|
| 106 |
for (ix = 0; ix <= 24; ix += 8) { |
| 107 |
word2 = word2 << 8; |
| 108 |
word2 |= (word >> ix) & 0xff; |
| 109 |
} |
| 110 |
return word2; |
| 111 |
#else |
| 112 |
return word; |
| 113 |
#endif |
| 114 |
} |
| 115 |
|
| 116 |
static void InitGroups(Translator *tr) |
| 117 |
{ |
| 118 |
// Called after dictionary 1 is loaded, to set up table of entry points for translation rule chains |
| 119 |
// for single-letters and two-letter combinations |
| 120 |
|
| 121 |
int ix; |
| 122 |
char *p; |
| 123 |
char *p_name; |
| 124 |
unsigned char c, c2; |
| 125 |
int len; |
| 126 |
|
| 127 |
tr->n_groups2 = 0; |
| 128 |
for (ix = 0; ix < 256; ix++) { |
| 129 |
tr->groups1[ix] = NULL; |
| 130 |
tr->groups2_count[ix] = 0; |
| 131 |
tr->groups2_start[ix] = 255; // indicates "not set" |
| 132 |
} |
| 133 |
memset(tr->letterGroups, 0, sizeof(tr->letterGroups)); |
| 134 |
memset(tr->groups3, 0, sizeof(tr->groups3)); |
| 135 |
|
| 136 |
p = tr->data_dictrules; |
| 137 |
// If there are no rules in the dictionary, compile_dictrules will not |
| 138 |
// write a RULE_GROUP_START (written in the for loop), but will write |
| 139 |
// a RULE_GROUP_END. |
| 140 |
if (*p != RULE_GROUP_END) while (*p != 0) { |
| 141 |
if (*p != RULE_GROUP_START) { |
| 142 |
fprintf(stderr, "Bad rules data in '%s_dict' at 0x%x (%c)\n", dictionary_name, (unsigned int)(p - tr->data_dictrules), *p); |
| 143 |
break; |
| 144 |
} |
| 145 |
p++; |
| 146 |
|
| 147 |
if (p[0] == RULE_REPLACEMENTS) { |
| 148 |
p = (char *)(((intptr_t)p+4) & ~3); // advance to next word boundary |
| 149 |
tr->langopts.replace_chars = (unsigned char *)p; |
| 150 |
|
| 151 |
while ( !is_str_totally_null(p, 4) ) { |
| 152 |
p++; |
| 153 |
} |
| 154 |
|
| 155 |
while (*p != RULE_GROUP_END) p++; |
| 156 |
p++; |
| 157 |
continue; |
| 158 |
} |
| 159 |
|
| 160 |
if (p[0] == RULE_LETTERGP2) { |
| 161 |
ix = p[1] - 'A'; |
| 162 |
if (ix < 0) |
| 163 |
ix += 256; |
| 164 |
p += 2; |
| 165 |
if ((ix >= 0) && (ix < N_LETTER_GROUPS)) |
| 166 |
tr->letterGroups[ix] = p; |
| 167 |
} else { |
| 168 |
len = strlen(p); |
| 169 |
p_name = p; |
| 170 |
c = p_name[0]; |
| 171 |
c2 = p_name[1]; |
| 172 |
|
| 173 |
p += (len+1); |
| 174 |
if (len == 1) |
| 175 |
tr->groups1[c] = p; |
| 176 |
else if (len == 0) |
| 177 |
tr->groups1[0] = p; |
| 178 |
else if (c == 1) { |
| 179 |
// index by offset from letter base |
| 180 |
tr->groups3[c2 - 1] = p; |
| 181 |
} else { |
| 182 |
if (tr->groups2_start[c] == 255) |
| 183 |
tr->groups2_start[c] = tr->n_groups2; |
| 184 |
|
| 185 |
tr->groups2_count[c]++; |
| 186 |
tr->groups2[tr->n_groups2] = p; |
| 187 |
tr->groups2_name[tr->n_groups2++] = (c + (c2 << 8)); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// skip over all the rules in this group |
| 192 |
while (*p != RULE_GROUP_END) |
| 193 |
p += (strlen(p) + 1); |
| 194 |
p++; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
int LoadDictionary(Translator *tr, const char *name, int no_error) |
| 199 |
{ |
| 200 |
int hash; |
| 201 |
char *p; |
| 202 |
int *pw; |
| 203 |
int length; |
| 204 |
FileInMemory f; |
| 205 |
int size; |
| 206 |
char fname[sizeof(path_home)+20]; |
| 207 |
|
| 208 |
Melder_assert (strlen(name) < 40); //ppgb: otherwise no null byte might be appended |
| 209 |
if (dictionary_name != name) |
| 210 |
strncpy(dictionary_name, name, 40); // currently loaded dictionary name |
| 211 |
if (tr->dictionary_name != name) |
| 212 |
strncpy(tr->dictionary_name, name, 40); |
| 213 |
|
| 214 |
// Load a pronunciation data file into memory |
| 215 |
// bytes 0-3: offset to rules data |
| 216 |
// bytes 4-7: number of hash table entries |
| 217 |
sprintf(fname, "%s%c%s_dict", path_home, PATHSEP, name); |
| 218 |
size = GetFileLength(fname); |
| 219 |
|
| 220 |
if (tr->data_dictlist != NULL) { |
| 221 |
free(tr->data_dictlist); |
| 222 |
tr->data_dictlist = NULL; |
| 223 |
} |
| 224 |
|
| 225 |
f = FileInMemorySet_fopen(theEspeakPraatFileInMemorySet(), fname, "rb"); |
| 226 |
if ((f == NULL) || (size <= 0)) { |
| 227 |
if (no_error == 0) |
| 228 |
fprintf(stderr, "Can't read dictionary file: '%s'\n", fname); |
| 229 |
if (f != NULL) |
| 230 |
FileInMemory_fclose(f); |
| 231 |
return 1; |
| 232 |
} |
| 233 |
|
| 234 |
if ((tr->data_dictlist = (char *) malloc(size)) == NULL) { |
| 235 |
FileInMemory_fclose(f); |
| 236 |
return 3; |
| 237 |
} |
| 238 |
size = FileInMemory_fread(tr->data_dictlist, 1, size, f); |
| 239 |
FileInMemory_fclose(f); |
| 240 |
|
| 241 |
pw = (int *)(tr->data_dictlist); |
| 242 |
length = Reverse4Bytes(pw[1]); |
| 243 |
|
| 244 |
if (size <= (N_HASH_DICT + sizeof(int)*2)) { |
| 245 |
fprintf(stderr, "Empty _dict file: '%s\n", fname); |
| 246 |
return 2; |
| 247 |
} |
| 248 |
|
| 249 |
if ((Reverse4Bytes(pw[0]) != N_HASH_DICT) || |
| 250 |
(length <= 0) || (length > 0x8000000)) { |
| 251 |
fprintf(stderr, "Bad data: '%s' (%x length=%x)\n", fname, Reverse4Bytes(pw[0]), length); |
| 252 |
return 2; |
| 253 |
} |
| 254 |
tr->data_dictrules = &(tr->data_dictlist[length]); |
| 255 |
|
| 256 |
// set up indices into data_dictrules |
| 257 |
InitGroups(tr); |
| 258 |
|
| 259 |
// set up hash table for data_dictlist |
| 260 |
p = &(tr->data_dictlist[8]); |
| 261 |
|
| 262 |
for (hash = 0; hash < N_HASH_DICT; hash++) { |
| 263 |
tr->dict_hashtab[hash] = p; |
| 264 |
while ((length = *(uint8_t *)p) != 0) |
| 265 |
p += length; |
| 266 |
p++; // skip over the zero which terminates the list for this hash value |
| 267 |
} |
| 268 |
|
| 269 |
if ((tr->dict_min_size > 0) && (size < (unsigned int)tr->dict_min_size)) |
| 270 |
fprintf(stderr, "Full dictionary is not installed for '%s'\n", name); |
| 271 |
|
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
/* Generate a hash code from the specified string |
| 276 |
This is used to access the dictionary_2 word-lookup dictionary |
| 277 |
*/ |
| 278 |
int HashDictionary(const char *string) |
| 279 |
{ |
| 280 |
int c; |
| 281 |
int chars = 0; |
| 282 |
int hash = 0; |
| 283 |
|
| 284 |
while ((c = (*string++ & 0xff)) != 0) { |
| 285 |
hash = hash * 8 + c; |
| 286 |
hash = (hash & 0x3ff) ^ (hash >> 8); // exclusive or |
| 287 |
chars++; |
| 288 |
} |
| 289 |
|
| 290 |
return (hash+chars) & 0x3ff; // a 10 bit hash code |
| 291 |
} |
| 292 |
|
| 293 |
/* Translate a phoneme string from ascii mnemonics to internal phoneme numbers, |
| 294 |
from 'p' up to next blank . |
| 295 |
Returns advanced 'p' |
| 296 |
outptr contains encoded phonemes, unrecognized phoneme stops the encoding |
| 297 |
bad_phoneme must point to char array of length 2 of more |
| 298 |
*/ |
| 299 |
const char *EncodePhonemes(const char *p, char *outptr, int *bad_phoneme) |
| 300 |
{ |
| 301 |
int ix; |
| 302 |
unsigned char c; |
| 303 |
int count; // num. of matching characters |
| 304 |
int max; // highest num. of matching found so far |
| 305 |
int max_ph; // corresponding phoneme with highest matching |
| 306 |
int consumed; |
| 307 |
unsigned int mnemonic_word; |
| 308 |
|
| 309 |
if (bad_phoneme != NULL) |
| 310 |
*bad_phoneme = 0; |
| 311 |
|
| 312 |
// skip initial blanks |
| 313 |
while ((uint8_t)*p < 0x80 && isspace(*p)) |
| 314 |
p++; |
| 315 |
|
| 316 |
while (((c = *p) != 0) && !isspace(c)) { |
| 317 |
consumed = 0; |
| 318 |
|
| 319 |
switch (c) |
| 320 |
{ |
| 321 |
case '|': |
| 322 |
// used to separate phoneme mnemonics if needed, to prevent characters being treated |
| 323 |
// as a multi-letter mnemonic |
| 324 |
|
| 325 |
if ((c = p[1]) == '|') { |
| 326 |
// treat double || as a word-break symbol, drop through |
| 327 |
// to the default case with c = '|' |
| 328 |
} else { |
| 329 |
p++; |
| 330 |
break; |
| 331 |
} |
| 332 |
default: |
| 333 |
// lookup the phoneme mnemonic, find the phoneme with the highest number of |
| 334 |
// matching characters |
| 335 |
max = -1; |
| 336 |
max_ph = 0; |
| 337 |
|
| 338 |
for (ix = 1; ix < n_phoneme_tab; ix++) { |
| 339 |
if (phoneme_tab[ix] == NULL) |
| 340 |
continue; |
| 341 |
if (phoneme_tab[ix]->type == phINVALID) |
| 342 |
continue; // this phoneme is not defined for this language |
| 343 |
|
| 344 |
count = 0; |
| 345 |
mnemonic_word = phoneme_tab[ix]->mnemonic; |
| 346 |
|
| 347 |
while (((c = p[count]) > ' ') && (count < 4) && |
| 348 |
(c == ((mnemonic_word >> (count*8)) & 0xff))) |
| 349 |
count++; |
| 350 |
|
| 351 |
if ((count > max) && |
| 352 |
((count == 4) || (((mnemonic_word >> (count*8)) & 0xff) == 0))) { |
| 353 |
max = count; |
| 354 |
max_ph = phoneme_tab[ix]->code; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
if (max_ph == 0) { |
| 359 |
// not recognised, report and ignore |
| 360 |
if (bad_phoneme != NULL) |
| 361 |
utf8_in(bad_phoneme, p); |
| 362 |
*outptr++ = 0; |
| 363 |
return p+1; |
| 364 |
} |
| 365 |
|
| 366 |
if (max <= 0) |
| 367 |
max = 1; |
| 368 |
p += (consumed + max); |
| 369 |
*outptr++ = (char)(max_ph); |
| 370 |
|
| 371 |
if (max_ph == phonSWITCH) { |
| 372 |
// Switch Language: this phoneme is followed by a text string |
| 373 |
char *p_lang = outptr; |
| 374 |
while (!isspace(c = *p) && (c != 0)) { |
| 375 |
p++; |
| 376 |
*outptr++ = tolower(c); |
| 377 |
} |
| 378 |
*outptr = 0; |
| 379 |
if (c == 0) { |
| 380 |
if (strcmp(p_lang, ESPEAKNG_DEFAULT_VOICE) == 0) { |
| 381 |
*p_lang = 0; // don't need ESPEAKNG_DEFAULT_VOICE, it's assumed by default |
| 382 |
return p; |
| 383 |
} |
| 384 |
} else |
| 385 |
*outptr++ = '|'; // more phonemes follow, terminate language string with separator |
| 386 |
} |
| 387 |
break; |
| 388 |
} |
| 389 |
} |
| 390 |
// terminate the encoded string |
| 391 |
*outptr = 0; |
| 392 |
return p; |
| 393 |
} |
| 394 |
|
| 395 |
void DecodePhonemes(const char *inptr, char *outptr) |
| 396 |
{ |
| 397 |
// Translate from internal phoneme codes into phoneme mnemonics |
| 398 |
unsigned char phcode; |
| 399 |
unsigned char c; |
| 400 |
unsigned int mnem; |
| 401 |
PHONEME_TAB *ph; |
| 402 |
static const char stress_chars[] = "==,,'* "; |
| 403 |
|
| 404 |
sprintf(outptr, "* "); |
| 405 |
while ((phcode = *inptr++) > 0) { |
| 406 |
if (phcode == 255) |
| 407 |
continue; // indicates unrecognised phoneme |
| 408 |
if ((ph = phoneme_tab[phcode]) == NULL) |
| 409 |
continue; |
| 410 |
|
| 411 |
if ((ph->type == phSTRESS) && (ph->std_length <= 4) && (ph->program == 0)) { |
| 412 |
if (ph->std_length > 1) |
| 413 |
*outptr++ = stress_chars[ph->std_length]; |
| 414 |
} else { |
| 415 |
mnem = ph->mnemonic; |
| 416 |
|
| 417 |
while ((c = (mnem & 0xff)) != 0) { |
| 418 |
*outptr++ = c; |
| 419 |
mnem = mnem >> 8; |
| 420 |
} |
| 421 |
if (phcode == phonSWITCH) { |
| 422 |
while (isalpha(*inptr)) |
| 423 |
*outptr++ = *inptr++; |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
*outptr = 0; // string terminator |
| 428 |
} |
| 429 |
|
| 430 |
// using Kirschenbaum to IPA translation, ascii 0x20 to 0x7f |
| 431 |
static const unsigned short ipa1[96] = { |
| 432 |
0x20, 0x21, 0x22, 0x2b0, 0x24, 0x25, 0x0e6, 0x2c8, 0x28, 0x29, 0x27e, 0x2b, 0x2cc, 0x2d, 0x2e, 0x2f, |
| 433 |
0x252, 0x31, 0x32, 0x25c, 0x34, 0x35, 0x36, 0x37, 0x275, 0x39, 0x2d0, 0x2b2, 0x3c, 0x3d, 0x3e, 0x294, |
| 434 |
0x259, 0x251, 0x3b2, 0xe7, 0xf0, 0x25b, 0x46, 0x262, 0x127, 0x26a, 0x25f, 0x4b, 0x26b, 0x271, 0x14b, 0x254, |
| 435 |
0x3a6, 0x263, 0x280, 0x283, 0x3b8, 0x28a, 0x28c, 0x153, 0x3c7, 0xf8, 0x292, 0x32a, 0x5c, 0x5d, 0x5e, 0x5f, |
| 436 |
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x261, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, |
| 437 |
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x303, 0x7f |
| 438 |
}; |
| 439 |
|
| 440 |
#define N_PHON_OUT 500 // realloc increment |
| 441 |
static char *phon_out_buf = NULL; // passes the result of GetTranslatedPhonemeString() |
| 442 |
static unsigned int phon_out_size = 0; |
| 443 |
|
| 444 |
char *WritePhMnemonic(char *phon_out, PHONEME_TAB *ph, PHONEME_LIST *plist, int use_ipa, int *flags) |
| 445 |
{ |
| 446 |
int c; |
| 447 |
int mnem; |
| 448 |
int len; |
| 449 |
bool first; |
| 450 |
int ix = 0; |
| 451 |
char *p; |
| 452 |
PHONEME_DATA phdata; |
| 453 |
|
| 454 |
if (ph->code == phonEND_WORD) { |
| 455 |
// ignore |
| 456 |
phon_out[0] = 0; |
| 457 |
return phon_out; |
| 458 |
} |
| 459 |
|
| 460 |
if (ph->code == phonSWITCH) { |
| 461 |
// the tone_ph field contains a phoneme table number |
| 462 |
p = phoneme_tab_list[plist->tone_ph].name; |
| 463 |
sprintf(phon_out, "(%s)", p); |
| 464 |
return phon_out + strlen(phon_out); |
| 465 |
} |
| 466 |
|
| 467 |
if (use_ipa) { |
| 468 |
// has an ipa name been defined for this phoneme ? |
| 469 |
phdata.ipa_string[0] = 0; |
| 470 |
|
| 471 |
if (plist == NULL) |
| 472 |
InterpretPhoneme2(ph->code, &phdata); |
| 473 |
else |
| 474 |
InterpretPhoneme(NULL, 0, plist, &phdata, NULL); |
| 475 |
|
| 476 |
p = phdata.ipa_string; |
| 477 |
if (*p == 0x20) { |
| 478 |
// indicates no name for this phoneme |
| 479 |
*phon_out = 0; |
| 480 |
return phon_out; |
| 481 |
} |
| 482 |
if ((*p != 0) && ((*p & 0xff) < 0x20)) { |
| 483 |
// name starts with a flags byte |
| 484 |
if (flags != NULL) |
| 485 |
*flags = *p; |
| 486 |
p++; |
| 487 |
} |
| 488 |
|
| 489 |
len = strlen(p); |
| 490 |
if (len > 0) { |
| 491 |
strcpy(phon_out, p); |
| 492 |
phon_out += len; |
| 493 |
*phon_out = 0; |
| 494 |
return phon_out; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
first = true; |
| 499 |
for (mnem = ph->mnemonic; (c = mnem & 0xff) != 0; mnem = mnem >> 8) { |
| 500 |
if (c == '/') |
| 501 |
break; // discard phoneme variant indicator |
| 502 |
|
| 503 |
if (use_ipa) { |
| 504 |
// convert from ascii to ipa |
| 505 |
if (first && (c == '_')) |
| 506 |
break; // don't show pause phonemes |
| 507 |
|
| 508 |
if ((c == '#') && (ph->type == phVOWEL)) |
| 509 |
break; // # is subscript-h, but only for consonants |
| 510 |
|
| 511 |
// ignore digits after the first character |
| 512 |
if (!first && IsDigit09(c)) |
| 513 |
continue; |
| 514 |
|
| 515 |
if ((c >= 0x20) && (c < 128)) |
| 516 |
c = ipa1[c-0x20]; |
| 517 |
|
| 518 |
ix += utf8_out(c, &phon_out[ix]); |
| 519 |
} else |
| 520 |
phon_out[ix++] = c; |
| 521 |
first = false; |
| 522 |
} |
| 523 |
|
| 524 |
phon_out = &phon_out[ix]; |
| 525 |
*phon_out = 0; |
| 526 |
return phon_out; |
| 527 |
} |
| 528 |
|
| 529 |
const char *GetTranslatedPhonemeString(int phoneme_mode) |
| 530 |
{ |
| 531 |
/* Called after a clause has been translated into phonemes, in order |
| 532 |
to display the clause in phoneme mnemonic form. |
| 533 |
|
| 534 |
phoneme_mode |
| 535 |
bit 1: use IPA phoneme names |
| 536 |
bit 7: use tie between letters in multi-character phoneme names |
| 537 |
bits 8-23 tie or separator character |
| 538 |
|
| 539 |
*/ |
| 540 |
|
| 541 |
int ix; |
| 542 |
unsigned int len; |
| 543 |
int phon_out_ix = 0; |
| 544 |
int stress; |
| 545 |
int c; |
| 546 |
char *p; |
| 547 |
char *buf; |
| 548 |
int count; |
| 549 |
int flags; |
| 550 |
int use_ipa; |
| 551 |
int use_tie; |
| 552 |
int separate_phonemes; |
| 553 |
char phon_buf[30]; |
| 554 |
char phon_buf2[30]; |
| 555 |
PHONEME_LIST *plist; |
| 556 |
|
| 557 |
static const char stress_chars[] = "==,,''"; |
| 558 |
|
| 559 |
if (phon_out_buf == NULL) { |
| 560 |
phon_out_size = N_PHON_OUT; |
| 561 |
if ((phon_out_buf = (char *)malloc(phon_out_size)) == NULL) { |
| 562 |
phon_out_size = 0; |
| 563 |
return ""; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
use_ipa = phoneme_mode & espeakPHONEMES_IPA; |
| 568 |
if (phoneme_mode & espeakPHONEMES_TIE) { |
| 569 |
use_tie = phoneme_mode >> 8; |
| 570 |
separate_phonemes = 0; |
| 571 |
} else { |
| 572 |
separate_phonemes = phoneme_mode >> 8; |
| 573 |
use_tie = 0; |
| 574 |
} |
| 575 |
|
| 576 |
for (ix = 1; ix < (n_phoneme_list-2); ix++) { |
| 577 |
buf = phon_buf; |
| 578 |
|
| 579 |
plist = &phoneme_list[ix]; |
| 580 |
|
| 581 |
WritePhMnemonic(phon_buf2, plist->ph, plist, use_ipa, &flags); |
| 582 |
if (plist->newword & PHLIST_START_OF_WORD && !(plist->newword & (PHLIST_START_OF_SENTENCE | PHLIST_START_OF_CLAUSE))) |
| 583 |
*buf++ = ' '; |
| 584 |
|
| 585 |
if ((!plist->newword) || (separate_phonemes == ' ')) { |
| 586 |
if ((separate_phonemes != 0) && (ix > 1)) { |
| 587 |
utf8_in(&c, phon_buf2); |
| 588 |
if ((c < 0x2b0) || (c > 0x36f)) // not if the phoneme starts with a superscript letter |
| 589 |
buf += utf8_out(separate_phonemes, buf); |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
if (plist->synthflags & SFLAG_SYLLABLE) { |
| 594 |
if ((stress = plist->stresslevel) > 1) { |
| 595 |
c = 0; |
| 596 |
if (stress > STRESS_IS_PRIORITY) stress = STRESS_IS_PRIORITY; |
| 597 |
|
| 598 |
if (use_ipa) { |
| 599 |
c = 0x2cc; // ipa, secondary stress |
| 600 |
if (stress > STRESS_IS_SECONDARY) |
| 601 |
c = 0x02c8; // ipa, primary stress |
| 602 |
} else |
| 603 |
c = stress_chars[stress]; |
| 604 |
|
| 605 |
if (c != 0) |
| 606 |
buf += utf8_out(c, buf); |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
flags = 0; |
| 611 |
count = 0; |
| 612 |
for (p = phon_buf2; *p != 0;) { |
| 613 |
p += utf8_in(&c, p); |
| 614 |
if (use_tie != 0) { |
| 615 |
// look for non-initial alphabetic character, but not diacritic, superscript etc. |
| 616 |
if ((count > 0) && !(flags & (1 << (count-1))) && ((c < 0x2b0) || (c > 0x36f)) && iswalpha_portable(c)) |
| 617 |
buf += utf8_out(use_tie, buf); |
| 618 |
} |
| 619 |
buf += utf8_out(c, buf); |
| 620 |
count++; |
| 621 |
} |
| 622 |
|
| 623 |
if (plist->ph->code != phonSWITCH) { |
| 624 |
if (plist->synthflags & SFLAG_LENGTHEN) |
| 625 |
buf = WritePhMnemonic(buf, phoneme_tab[phonLENGTHEN], plist, use_ipa, NULL); |
| 626 |
if ((plist->synthflags & SFLAG_SYLLABLE) && (plist->type != phVOWEL)) { |
| 627 |
// syllablic consonant |
| 628 |
buf = WritePhMnemonic(buf, phoneme_tab[phonSYLLABIC], plist, use_ipa, NULL); |
| 629 |
} |
| 630 |
if (plist->tone_ph > 0) |
| 631 |
buf = WritePhMnemonic(buf, phoneme_tab[plist->tone_ph], plist, use_ipa, NULL); |
| 632 |
} |
| 633 |
|
| 634 |
len = buf - phon_buf; |
| 635 |
if ((phon_out_ix + len) >= phon_out_size) { |
| 636 |
// enlarge the phoneme buffer |
| 637 |
phon_out_size = phon_out_ix + len + N_PHON_OUT; |
| 638 |
char *new_phon_out_buf = (char *)realloc(phon_out_buf, phon_out_size); |
| 639 |
if (new_phon_out_buf == NULL) { |
| 640 |
phon_out_size = 0; |
| 641 |
return ""; |
| 642 |
} else |
| 643 |
phon_out_buf = new_phon_out_buf; |
| 644 |
} |
| 645 |
|
| 646 |
phon_buf[len] = 0; |
| 647 |
strcpy(&phon_out_buf[phon_out_ix], phon_buf); |
| 648 |
phon_out_ix += len; |
| 649 |
} |
| 650 |
|
| 651 |
if (!phon_out_buf) |
| 652 |
return ""; |
| 653 |
|
| 654 |
phon_out_buf[phon_out_ix] = 0; |
| 655 |
|
| 656 |
return phon_out_buf; |
| 657 |
} |
| 658 |
|
| 659 |
static int LetterGroupNo(char *rule) |
| 660 |
{ |
| 661 |
/* |
| 662 |
* Returns number of letter group |
| 663 |
*/ |
| 664 |
int groupNo = *rule; |
| 665 |
groupNo = groupNo - 'A'; // subtracting 'A' makes letter_group equal to number in .Lxx definition |
| 666 |
if (groupNo < 0) // fix sign if necessary |
| 667 |
groupNo += 256; |
| 668 |
return groupNo; |
| 669 |
} |
| 670 |
|
| 671 |
static int IsLetterGroup(Translator *tr, char *word, int group, int pre) |
| 672 |
{ |
| 673 |
/* Match the word against a list of utf-8 strings. |
| 674 |
* returns length of matching letter group or -1 |
| 675 |
* |
| 676 |
* How this works: |
| 677 |
* |
| 678 |
* +-+ |
| 679 |
* |c|<-(tr->letterGroups[group]) |
| 680 |
* |0| |
| 681 |
* *p->|c|<-len+ +-+ |
| 682 |
* |s|<----+ |a|<-(Actual word to be tested) |
| 683 |
* |0| *word-> |t|<-*w=word-len+1 (for pre-rule) |
| 684 |
* |~| |a|<-*w=word (for post-rule) |
| 685 |
* |7| |s| |
| 686 |
* +-+ +-+ |
| 687 |
* |
| 688 |
* 7=RULE_GROUP_END |
| 689 |
* 0=null terminator |
| 690 |
* pre==1 — pre-rule |
| 691 |
* pre==0 — post-rule |
| 692 |
*/ |
| 693 |
char *p; // group counter |
| 694 |
char *w; // word counter |
| 695 |
int len = 0, i; |
| 696 |
|
| 697 |
p = tr->letterGroups[group]; |
| 698 |
if (p == NULL) |
| 699 |
return -1; |
| 700 |
|
| 701 |
while (*p != RULE_GROUP_END) { |
| 702 |
// If '~' (no character) is allowed in group, return 0. |
| 703 |
if (*p == '~') |
| 704 |
return 0; |
| 705 |
|
| 706 |
if (pre) { |
| 707 |
len = strlen(p); |
| 708 |
w = word; |
| 709 |
for (i = 0; i < len-1; i++) |
| 710 |
{ |
| 711 |
w--; |
| 712 |
if (*w == 0) |
| 713 |
// Not found, skip the rest of this group. |
| 714 |
goto skip; |
| 715 |
} |
| 716 |
} else |
| 717 |
w = word; |
| 718 |
|
| 719 |
// Check current group |
| 720 |
while ((*p == *w) && (*w != 0)) { |
| 721 |
w++; |
| 722 |
p++; |
| 723 |
} |
| 724 |
if (*p == 0) { // Matched the current group. |
| 725 |
if (pre) |
| 726 |
return len; |
| 727 |
return w - word; |
| 728 |
} |
| 729 |
|
| 730 |
// No match, so skip the rest of this group. |
| 731 |
skip: |
| 732 |
while (*p++ != 0) |
| 733 |
; |
| 734 |
} |
| 735 |
// Not found |
| 736 |
return -1; |
| 737 |
} |
| 738 |
|
| 739 |
static int IsLetter(Translator *tr, int letter, int group) |
| 740 |
{ |
| 741 |
int letter2; |
| 742 |
|
| 743 |
if (tr->letter_groups[group] != NULL) { |
| 744 |
if (wcschr(tr->letter_groups[group], letter)) |
| 745 |
return 1; |
| 746 |
return 0; |
| 747 |
} |
| 748 |
|
| 749 |
if (group > 7) |
| 750 |
return 0; |
| 751 |
|
| 752 |
if (tr->letter_bits_offset > 0) { |
| 753 |
if (((letter2 = (letter - tr->letter_bits_offset)) > 0) && (letter2 < 0x100)) |
| 754 |
letter = letter2; |
| 755 |
else |
| 756 |
return 0; |
| 757 |
} else if ((letter >= 0xc0) && (letter < N_REMOVE_ACCENT)) |
| 758 |
return tr->letter_bits[remove_accent[letter-0xc0]] & (1L << group); |
| 759 |
|
| 760 |
if ((letter >= 0) && (letter < 0x100)) |
| 761 |
return tr->letter_bits[letter] & (1L << group); |
| 762 |
|
| 763 |
return 0; |
| 764 |
} |
| 765 |
|
| 766 |
int IsVowel(Translator *tr, int letter) |
| 767 |
{ |
| 768 |
return IsLetter(tr, letter, LETTERGP_VOWEL2); |
| 769 |
} |
| 770 |
|
| 771 |
int GetVowelStress(Translator *tr, unsigned char *phonemes, signed char *vowel_stress, int *vowel_count, int *stressed_syllable, int control) |
| 772 |
{ |
| 773 |
// control = 1, set stress to 1 for forced unstressed vowels |
| 774 |
unsigned char phcode; |
| 775 |
PHONEME_TAB *ph; |
| 776 |
unsigned char *ph_out = phonemes; |
| 777 |
int count = 1; |
| 778 |
int max_stress = -1; |
| 779 |
int ix; |
| 780 |
int j; |
| 781 |
int stress = -1; |
| 782 |
int primary_posn = 0; |
| 783 |
|
| 784 |
vowel_stress[0] = STRESS_IS_UNSTRESSED; |
| 785 |
while (((phcode = *phonemes++) != 0) && (count < (N_WORD_PHONEMES/2)-1)) { |
| 786 |
if ((ph = phoneme_tab[phcode]) == NULL) |
| 787 |
continue; |
| 788 |
|
| 789 |
if ((ph->type == phSTRESS) && (ph->program == 0)) { |
| 790 |
// stress marker, use this for the following vowel |
| 791 |
|
| 792 |
if (phcode == phonSTRESS_PREV) { |
| 793 |
// primary stress on preceeding vowel |
| 794 |
j = count - 1; |
| 795 |
while ((j > 0) && (*stressed_syllable == 0) && (vowel_stress[j] < STRESS_IS_PRIMARY)) { |
| 796 |
if ((vowel_stress[j] != STRESS_IS_DIMINISHED) && (vowel_stress[j] != STRESS_IS_UNSTRESSED)) { |
| 797 |
// don't promote a phoneme which must be unstressed |
| 798 |
vowel_stress[j] = STRESS_IS_PRIMARY; |
| 799 |
|
| 800 |
if (max_stress < STRESS_IS_PRIMARY) { |
| 801 |
max_stress = STRESS_IS_PRIMARY; |
| 802 |
primary_posn = j; |
| 803 |
} |
| 804 |
|
| 805 |
/* reduce any preceding primary stress markers */ |
| 806 |
for (ix = 1; ix < j; ix++) { |
| 807 |
if (vowel_stress[ix] == STRESS_IS_PRIMARY) |
| 808 |
vowel_stress[ix] = STRESS_IS_SECONDARY; |
| 809 |
} |
| 810 |
break; |
| 811 |
} |
| 812 |
j--; |
| 813 |
} |
| 814 |
} else { |
| 815 |
if ((ph->std_length < 4) || (*stressed_syllable == 0)) { |
| 816 |
stress = ph->std_length; |
| 817 |
|
| 818 |
if (stress > max_stress) |
| 819 |
max_stress = stress; |
| 820 |
} |
| 821 |
} |
| 822 |
continue; |
| 823 |
} |
| 824 |
|
| 825 |
if ((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC)) { |
| 826 |
vowel_stress[count] = (char)stress; |
| 827 |
if ((stress >= STRESS_IS_PRIMARY) && (stress >= max_stress)) { |
| 828 |
primary_posn = count; |
| 829 |
max_stress = stress; |
| 830 |
} |
| 831 |
|
| 832 |
if ((stress < 0) && (control & 1) && (ph->phflags & phUNSTRESSED)) |
| 833 |
vowel_stress[count] = STRESS_IS_UNSTRESSED; // weak vowel, must be unstressed |
| 834 |
|
| 835 |
count++; |
| 836 |
stress = -1; |
| 837 |
} else if (phcode == phonSYLLABIC) { |
| 838 |
// previous consonant phoneme is syllablic |
| 839 |
vowel_stress[count] = (char)stress; |
| 840 |
if ((stress < 0) && (control & 1)) |
| 841 |
vowel_stress[count] = STRESS_IS_UNSTRESSED; // syllabic consonant, usually unstressed |
| 842 |
count++; |
| 843 |
} |
| 844 |
|
| 845 |
*ph_out++ = phcode; |
| 846 |
} |
| 847 |
vowel_stress[count] = STRESS_IS_UNSTRESSED; |
| 848 |
*ph_out = 0; |
| 849 |
|
| 850 |
// has the position of the primary stress been specified by $1, $2, etc? |
| 851 |
if (*stressed_syllable > 0) { |
| 852 |
if (*stressed_syllable >= count) |
| 853 |
*stressed_syllable = count-1; // the final syllable |
| 854 |
|
| 855 |
vowel_stress[*stressed_syllable] = STRESS_IS_PRIMARY; |
| 856 |
max_stress = STRESS_IS_PRIMARY; |
| 857 |
primary_posn = *stressed_syllable; |
| 858 |
} |
| 859 |
|
| 860 |
if (max_stress == STRESS_IS_PRIORITY) { |
| 861 |
// priority stress, replaces any other primary stress marker |
| 862 |
for (ix = 1; ix < count; ix++) { |
| 863 |
if (vowel_stress[ix] == STRESS_IS_PRIMARY) { |
| 864 |
if (tr->langopts.stress_flags & S_PRIORITY_STRESS) |
| 865 |
vowel_stress[ix] = STRESS_IS_UNSTRESSED; |
| 866 |
else |
| 867 |
vowel_stress[ix] = STRESS_IS_SECONDARY; |
| 868 |
} |
| 869 |
|
| 870 |
if (vowel_stress[ix] == STRESS_IS_PRIORITY) { |
| 871 |
vowel_stress[ix] = STRESS_IS_PRIMARY; |
| 872 |
primary_posn = ix; |
| 873 |
} |
| 874 |
} |
| 875 |
max_stress = STRESS_IS_PRIMARY; |
| 876 |
} |
| 877 |
|
| 878 |
*stressed_syllable = primary_posn; |
| 879 |
*vowel_count = count; |
| 880 |
return max_stress; |
| 881 |
} |
| 882 |
|
| 883 |
const char stress_phonemes[] = { |
| 884 |
phonSTRESS_D, phonSTRESS_U, phonSTRESS_2, phonSTRESS_3, |
| 885 |
phonSTRESS_P, phonSTRESS_P2, phonSTRESS_TONIC |
| 886 |
}; |
| 887 |
|
| 888 |
void SetWordStress(Translator *tr, char *output, unsigned int *dictionary_flags, int tonic, int control) |
| 889 |
{ |
| 890 |
/* Guess stress pattern of word. This is language specific |
| 891 |
|
| 892 |
'output' is used for input and output |
| 893 |
|
| 894 |
'dictionary_flags' has bits 0-3 position of stressed vowel (if > 0) |
| 895 |
or unstressed (if == 7) or syllables 1 and 2 (if == 6) |
| 896 |
bits 8... dictionary flags |
| 897 |
|
| 898 |
If 'tonic' is set (>= 0), replace highest stress by this value. |
| 899 |
|
| 900 |
control: bit 0 This is an individual symbol, not a word |
| 901 |
bit 1 Suffix phonemes are still to be added |
| 902 |
*/ |
| 903 |
|
| 904 |
unsigned char phcode; |
| 905 |
unsigned char *p; |
| 906 |
PHONEME_TAB *ph; |
| 907 |
int stress; |
| 908 |
int max_stress; |
| 909 |
int max_stress_input; // any stress specified in the input? |
| 910 |
int vowel_count; // num of vowels + 1 |
| 911 |
int ix; |
| 912 |
int v; |
| 913 |
int v_stress; |
| 914 |
int stressed_syllable; // position of stressed syllable |
| 915 |
int max_stress_posn; |
| 916 |
char *max_output; |
| 917 |
int final_ph; |
| 918 |
int final_ph2; |
| 919 |
int mnem; |
| 920 |
int opt_length; |
| 921 |
int stressflags; |
| 922 |
int dflags = 0; |
| 923 |
int first_primary; |
| 924 |
int long_vowel; |
| 925 |
|
| 926 |
signed char vowel_stress[N_WORD_PHONEMES/2]; |
| 927 |
char syllable_weight[N_WORD_PHONEMES/2]; |
| 928 |
char vowel_length[N_WORD_PHONEMES/2]; |
| 929 |
unsigned char phonetic[N_WORD_PHONEMES]; |
| 930 |
|
| 931 |
static const char consonant_types[16] = { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 }; |
| 932 |
|
| 933 |
memset(syllable_weight, 0, sizeof(syllable_weight)); |
| 934 |
memset(vowel_length, 0, sizeof(vowel_length)); |
| 935 |
|
| 936 |
stressflags = tr->langopts.stress_flags; |
| 937 |
|
| 938 |
if (dictionary_flags != NULL) |
| 939 |
dflags = dictionary_flags[0]; |
| 940 |
|
| 941 |
// copy input string into internal buffer |
| 942 |
for (ix = 0; ix < N_WORD_PHONEMES; ix++) { |
| 943 |
phonetic[ix] = output[ix]; |
| 944 |
// check for unknown phoneme codes |
| 945 |
if (phonetic[ix] >= n_phoneme_tab) |
| 946 |
phonetic[ix] = phonSCHWA; |
| 947 |
if (phonetic[ix] == 0) |
| 948 |
break; |
| 949 |
} |
| 950 |
if (ix == 0) return; |
| 951 |
final_ph = phonetic[ix-1]; |
| 952 |
final_ph2 = phonetic[(ix > 1) ? ix-2 : ix-1]; |
| 953 |
|
| 954 |
max_output = output + (N_WORD_PHONEMES-3); // check for overrun |
| 955 |
|
| 956 |
|
| 957 |
// any stress position marked in the xx_list dictionary ? |
| 958 |
bool unstressed_word = false; |
| 959 |
stressed_syllable = dflags & 0x7; |
| 960 |
if (dflags & 0x8) { |
| 961 |
// this indicates a word without a primary stress |
| 962 |
stressed_syllable = dflags & 0x3; |
| 963 |
unstressed_word = true; |
| 964 |
} |
| 965 |
|
| 966 |
max_stress = max_stress_input = GetVowelStress(tr, phonetic, vowel_stress, &vowel_count, &stressed_syllable, 1); |
| 967 |
if ((max_stress < 0) && dictionary_flags) |
| 968 |
max_stress = STRESS_IS_DIMINISHED; |
| 969 |
|
| 970 |
// heavy or light syllables |
| 971 |
ix = 1; |
| 972 |
for (p = phonetic; *p != 0; p++) { |
| 973 |
if ((phoneme_tab[p[0]]->type == phVOWEL) && !(phoneme_tab[p[0]]->phflags & phNONSYLLABIC)) { |
| 974 |
int weight = 0; |
| 975 |
bool lengthened = false; |
| 976 |
|
| 977 |
if (phoneme_tab[p[1]]->code == phonLENGTHEN) |
| 978 |
lengthened = true; |
| 979 |
|
| 980 |
if (lengthened || (phoneme_tab[p[0]]->phflags & phLONG)) { |
| 981 |
// long vowel, increase syllable weight |
| 982 |
weight++; |
| 983 |
} |
| 984 |
vowel_length[ix] = weight; |
| 985 |
|
| 986 |
if (lengthened) p++; // advance over phonLENGTHEN |
| 987 |
|
| 988 |
if (consonant_types[phoneme_tab[p[1]]->type] && ((phoneme_tab[p[2]]->type != phVOWEL) || (phoneme_tab[p[1]]->phflags & phLONG))) { |
| 989 |
// followed by two consonants, a long consonant, or consonant and end-of-word |
| 990 |
weight++; |
| 991 |
} |
| 992 |
syllable_weight[ix] = weight; |
| 993 |
ix++; |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
switch (tr->langopts.stress_rule) |
| 998 |
{ |
| 999 |
case STRESSPOSN_2LLH: |
| 1000 |
// stress on first syllable, unless it is a light syllable followed by a heavy syllable |
| 1001 |
if ((syllable_weight[1] > 0) || (syllable_weight[2] == 0)) |
| 1002 |
break; |
| 1003 |
// fallthrough: |
| 1004 |
case STRESSPOSN_2L: |
| 1005 |
// stress on second syllable |
| 1006 |
if ((stressed_syllable == 0) && (vowel_count > 2)) { |
| 1007 |
stressed_syllable = 2; |
| 1008 |
if (max_stress == STRESS_IS_DIMINISHED) |
| 1009 |
vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY; |
| 1010 |
max_stress = STRESS_IS_PRIMARY; |
| 1011 |
} |
| 1012 |
break; |
| 1013 |
|
| 1014 |
case STRESSPOSN_2R: |
| 1015 |
// a language with stress on penultimate vowel |
| 1016 |
|
| 1017 |
if (stressed_syllable == 0) { |
| 1018 |
// no explicit stress - stress the penultimate vowel |
| 1019 |
max_stress = STRESS_IS_PRIMARY; |
| 1020 |
|
| 1021 |
if (vowel_count > 2) { |
| 1022 |
stressed_syllable = vowel_count - 2; |
| 1023 |
|
| 1024 |
if (stressflags & S_FINAL_SPANISH) { |
| 1025 |
// LANG=Spanish, stress on last vowel if the word ends in a consonant other than 'n' or 's' |
| 1026 |
if (phoneme_tab[final_ph]->type != phVOWEL) { |
| 1027 |
mnem = phoneme_tab[final_ph]->mnemonic; |
| 1028 |
|
| 1029 |
if (tr->translator_name == L('a', 'n')) { |
| 1030 |
if (((mnem != 's') && (mnem != 'n')) || phoneme_tab[final_ph2]->type != phVOWEL) |
| 1031 |
stressed_syllable = vowel_count - 1; // stress on last syllable |
| 1032 |
} else if (tr->translator_name == L('i', 'a')) { |
| 1033 |
if ((mnem != 's') || phoneme_tab[final_ph2]->type != phVOWEL) |
| 1034 |
stressed_syllable = vowel_count - 1; // stress on last syllable |
| 1035 |
} else { |
| 1036 |
if ((mnem == 's') && (phoneme_tab[final_ph2]->type == phNASAL)) { |
| 1037 |
// -ns stress remains on penultimate syllable |
| 1038 |
} else if (((phoneme_tab[final_ph]->type != phNASAL) && (mnem != 's')) || (phoneme_tab[final_ph2]->type != phVOWEL)) |
| 1039 |
stressed_syllable = vowel_count - 1; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
if (stressflags & S_FINAL_LONG) { |
| 1045 |
// stress on last syllable if it has a long vowel, but previous syllable has a short vowel |
| 1046 |
if (vowel_length[vowel_count - 1] > vowel_length[vowel_count - 2]) |
| 1047 |
stressed_syllable = vowel_count - 1; |
| 1048 |
} |
| 1049 |
|
| 1050 |
if ((vowel_stress[stressed_syllable] == STRESS_IS_DIMINISHED) || (vowel_stress[stressed_syllable] == STRESS_IS_UNSTRESSED)) { |
| 1051 |
// but this vowel is explicitly marked as unstressed |
| 1052 |
if (stressed_syllable > 1) |
| 1053 |
stressed_syllable--; |
| 1054 |
else |
| 1055 |
stressed_syllable++; |
| 1056 |
} |
| 1057 |
} else |
| 1058 |
stressed_syllable = 1; |
| 1059 |
|
| 1060 |
// only set the stress if it's not already marked explicitly |
| 1061 |
if (vowel_stress[stressed_syllable] < 0) { |
| 1062 |
// don't stress if next and prev syllables are stressed |
| 1063 |
if ((vowel_stress[stressed_syllable-1] < STRESS_IS_PRIMARY) || (vowel_stress[stressed_syllable+1] < STRESS_IS_PRIMARY)) |
| 1064 |
vowel_stress[stressed_syllable] = max_stress; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
break; |
| 1068 |
case STRESSPOSN_1R: |
| 1069 |
// stress on last vowel |
| 1070 |
if (stressed_syllable == 0) { |
| 1071 |
// no explicit stress - stress the final vowel |
| 1072 |
stressed_syllable = vowel_count - 1; |
| 1073 |
|
| 1074 |
while (stressed_syllable > 0) { |
| 1075 |
// find the last vowel which is not unstressed |
| 1076 |
if (vowel_stress[stressed_syllable] < STRESS_IS_DIMINISHED) { |
| 1077 |
vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY; |
| 1078 |
break; |
| 1079 |
} else |
| 1080 |
stressed_syllable--; |
| 1081 |
} |
| 1082 |
max_stress = STRESS_IS_PRIMARY; |
| 1083 |
} |
| 1084 |
break; |
| 1085 |
case STRESSPOSN_3R: // stress on antipenultimate vowel |
| 1086 |
if (stressed_syllable == 0) { |
| 1087 |
stressed_syllable = vowel_count - 3; |
| 1088 |
if (stressed_syllable < 1) |
| 1089 |
stressed_syllable = 1; |
| 1090 |
|
| 1091 |
if (max_stress == STRESS_IS_DIMINISHED) |
| 1092 |
vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY; |
| 1093 |
max_stress = STRESS_IS_PRIMARY; |
| 1094 |
} |
| 1095 |
break; |
| 1096 |
case STRESSPOSN_SYLCOUNT: |
| 1097 |
// LANG=Russian |
| 1098 |
if (stressed_syllable == 0) { |
| 1099 |
// no explicit stress - guess the stress from the number of syllables |
| 1100 |
static const char guess_ru[16] = { 0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 10, 11 }; |
| 1101 |
static const char guess_ru_v[16] = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 10 }; // for final phoneme is a vowel |
| 1102 |
static const char guess_ru_t[16] = { 0, 0, 1, 2, 3, 3, 3, 4, 5, 6, 7, 7, 7, 8, 9, 10 }; // for final phoneme is an unvoiced stop |
| 1103 |
|
| 1104 |
stressed_syllable = vowel_count - 3; |
| 1105 |
if (vowel_count < 16) { |
| 1106 |
if (phoneme_tab[final_ph]->type == phVOWEL) |
| 1107 |
stressed_syllable = guess_ru_v[vowel_count]; |
| 1108 |
else if (phoneme_tab[final_ph]->type == phSTOP) |
| 1109 |
stressed_syllable = guess_ru_t[vowel_count]; |
| 1110 |
else |
| 1111 |
stressed_syllable = guess_ru[vowel_count]; |
| 1112 |
} |
| 1113 |
vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY; |
| 1114 |
max_stress = STRESS_IS_PRIMARY; |
| 1115 |
} |
| 1116 |
break; |
| 1117 |
case STRESSPOSN_1RH: // LANG=hi stress on the last heaviest syllable |
| 1118 |
if (stressed_syllable == 0) { |
| 1119 |
int wt; |
| 1120 |
int max_weight = -1; |
| 1121 |
|
| 1122 |
// find the heaviest syllable, excluding the final syllable |
| 1123 |
for (ix = 1; ix < (vowel_count-1); ix++) { |
| 1124 |
if (vowel_stress[ix] < STRESS_IS_DIMINISHED) { |
| 1125 |
if ((wt = syllable_weight[ix]) >= max_weight) { |
| 1126 |
max_weight = wt; |
| 1127 |
stressed_syllable = ix; |
| 1128 |
} |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
if ((syllable_weight[vowel_count-1] == 2) && (max_weight < 2)) { |
| 1133 |
// the only double=heavy syllable is the final syllable, so stress this |
| 1134 |
stressed_syllable = vowel_count-1; |
| 1135 |
} else if (max_weight <= 0) { |
| 1136 |
// all syllables, exclusing the last, are light. Stress the first syllable |
| 1137 |
stressed_syllable = 1; |
| 1138 |
} |
| 1139 |
|
| 1140 |
vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY; |
| 1141 |
max_stress = STRESS_IS_PRIMARY; |
| 1142 |
} |
| 1143 |
break; |
| 1144 |
case STRESSPOSN_1RU : // LANG=tr, the last syllable for any vowel marked explicitly as unstressed |
| 1145 |
if (stressed_syllable == 0) { |
| 1146 |
stressed_syllable = vowel_count - 1; |
| 1147 |
for (ix = 1; ix < vowel_count; ix++) { |
| 1148 |
if (vowel_stress[ix] == STRESS_IS_UNSTRESSED) { |
| 1149 |
stressed_syllable = ix-1; |
| 1150 |
break; |
| 1151 |
} |
| 1152 |
} |
| 1153 |
vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY; |
| 1154 |
max_stress = STRESS_IS_PRIMARY; |
| 1155 |
} |
| 1156 |
break; |
| 1157 |
case STRESSPOSN_ALL: // mark all as stressed |
| 1158 |
for (ix = 1; ix < vowel_count; ix++) { |
| 1159 |
if (vowel_stress[ix] < STRESS_IS_DIMINISHED) |
| 1160 |
vowel_stress[ix] = STRESS_IS_PRIMARY; |
| 1161 |
} |
| 1162 |
break; |
| 1163 |
case STRESSPOSN_GREENLANDIC: // LANG=kl (Greenlandic) |
| 1164 |
long_vowel = 0; |
| 1165 |
for (ix = 1; ix < vowel_count; ix++) { |
| 1166 |
if (vowel_stress[ix] == STRESS_IS_PRIMARY) |
| 1167 |
vowel_stress[ix] = STRESS_IS_SECONDARY; // change marked stress (consonant clusters) to secondary (except the last) |
| 1168 |
|
| 1169 |
if (vowel_length[ix] > 0) { |
| 1170 |
long_vowel = ix; |
| 1171 |
vowel_stress[ix] = STRESS_IS_SECONDARY; // give secondary stress to all long vowels |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
// 'stressed_syllable' gives the last marked stress |
| 1176 |
if (stressed_syllable == 0) { |
| 1177 |
// no marked stress, choose the last long vowel |
| 1178 |
if (long_vowel > 0) |
| 1179 |
stressed_syllable = long_vowel; |
| 1180 |
else { |
| 1181 |
// no long vowels or consonant clusters |
| 1182 |
if (vowel_count > 5) |
| 1183 |
stressed_syllable = vowel_count - 3; // more than 4 syllables |
| 1184 |
else |
| 1185 |
stressed_syllable = vowel_count - 1; |
| 1186 |
} |
| 1187 |
} |
| 1188 |
vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY; |
| 1189 |
max_stress = STRESS_IS_PRIMARY; |
| 1190 |
break; |
| 1191 |
case STRESSPOSN_1SL: // LANG=ml, 1st unless 1st vowel is short and 2nd is long |
| 1192 |
if (stressed_syllable == 0) { |
| 1193 |
stressed_syllable = 1; |
| 1194 |
if ((vowel_length[1] == 0) && (vowel_count > 2) && (vowel_length[2] > 0)) |
| 1195 |
stressed_syllable = 2; |
| 1196 |
vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY; |
| 1197 |
max_stress = STRESS_IS_PRIMARY; |
| 1198 |
} |
| 1199 |
break; |
| 1200 |
|
| 1201 |
case STRESSPOSN_EU: // LANG=eu. If more than 2 syllables: primary stress in second syllable and secondary on last. |
| 1202 |
if ((stressed_syllable == 0) && (vowel_count > 2)) { |
| 1203 |
for (ix = 1; ix < vowel_count; ix++) { |
| 1204 |
vowel_stress[ix] = STRESS_IS_DIMINISHED; |
| 1205 |
} |
| 1206 |
stressed_syllable = 2; |
| 1207 |
if (max_stress == STRESS_IS_DIMINISHED) |
| 1208 |
vowel_stress[stressed_syllable] = STRESS_IS_PRIMARY; |
| 1209 |
max_stress = STRESS_IS_PRIMARY; |
| 1210 |
if (vowel_count > 3) { |
| 1211 |
vowel_stress[vowel_count - 1] = STRESS_IS_SECONDARY; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
break; |
| 1215 |
} |
| 1216 |
|
| 1217 |
if ((stressflags & S_FINAL_VOWEL_UNSTRESSED) && ((control & 2) == 0) && (vowel_count > 2) && (max_stress_input < STRESS_IS_SECONDARY) && (vowel_stress[vowel_count - 1] == STRESS_IS_PRIMARY)) { |
| 1218 |
// Don't allow stress on a word-final vowel |
| 1219 |
// Only do this if there is no suffix phonemes to be added, and if a stress position was not given explicitly |
| 1220 |
if (phoneme_tab[final_ph]->type == phVOWEL) { |
| 1221 |
vowel_stress[vowel_count - 1] = STRESS_IS_UNSTRESSED; |
| 1222 |
vowel_stress[vowel_count - 2] = STRESS_IS_PRIMARY; |
| 1223 |
} |
| 1224 |
} |
| 1225 |
|
| 1226 |
// now guess the complete stress pattern |
| 1227 |
if (max_stress < STRESS_IS_PRIMARY) |
| 1228 |
stress = STRESS_IS_PRIMARY; // no primary stress marked, use for 1st syllable |
| 1229 |
else |
| 1230 |
stress = STRESS_IS_SECONDARY; |
| 1231 |
|
| 1232 |
if (unstressed_word == false) { |
| 1233 |
if ((stressflags & S_2_SYL_2) && (vowel_count == 3)) { |
| 1234 |
// Two syllable word, if one syllable has primary stress, then give the other secondary stress |
| 1235 |
if (vowel_stress[1] == STRESS_IS_PRIMARY) |
| 1236 |
vowel_stress[2] = STRESS_IS_SECONDARY; |
| 1237 |
if (vowel_stress[2] == STRESS_IS_PRIMARY) |
| 1238 |
vowel_stress[1] = STRESS_IS_SECONDARY; |
| 1239 |
} |
| 1240 |
|
| 1241 |
if ((stressflags & S_INITIAL_2) && (vowel_stress[1] < STRESS_IS_DIMINISHED)) { |
| 1242 |
// If there is only one syllable before the primary stress, give it a secondary stress |
| 1243 |
if ((vowel_count > 3) && (vowel_stress[2] >= STRESS_IS_PRIMARY)) |
| 1244 |
vowel_stress[1] = STRESS_IS_SECONDARY; |
| 1245 |
} |
| 1246 |
} |
| 1247 |
|
| 1248 |
bool done = false; |
| 1249 |
first_primary = 0; |
| 1250 |
for (v = 1; v < vowel_count; v++) { |
| 1251 |
if (vowel_stress[v] < STRESS_IS_DIMINISHED) { |
| 1252 |
if ((stressflags & S_FINAL_NO_2) && (stress < STRESS_IS_PRIMARY) && (v == vowel_count-1)) { |
| 1253 |
// flag: don't give secondary stress to final vowel |
| 1254 |
} else if ((stressflags & 0x8000) && (done == false)) { |
| 1255 |
vowel_stress[v] = (char)stress; |
| 1256 |
done = true; |
| 1257 |
stress = STRESS_IS_SECONDARY; // use secondary stress for remaining syllables |
| 1258 |
} else if ((vowel_stress[v-1] <= STRESS_IS_UNSTRESSED) && ((vowel_stress[v+1] <= STRESS_IS_UNSTRESSED) || ((stress == STRESS_IS_PRIMARY) && (vowel_stress[v+1] <= STRESS_IS_NOT_STRESSED)))) { |
| 1259 |
// trochaic: give stress to vowel surrounded by unstressed vowels |
| 1260 |
|
| 1261 |
if ((stress == STRESS_IS_SECONDARY) && (stressflags & S_NO_AUTO_2)) |
| 1262 |
continue; // don't use secondary stress |
| 1263 |
|
| 1264 |
// don't put secondary stress on a light syllable if the rest of the word (excluding last syllable) contains a heavy syllable |
| 1265 |
if ((v > 1) && (stressflags & S_2_TO_HEAVY) && (syllable_weight[v] == 0)) { |
| 1266 |
bool skip = false; |
| 1267 |
for (int i = v; i < vowel_count - 1; i++) { |
| 1268 |
if (syllable_weight[i] > 0) { |
| 1269 |
skip = true; |
| 1270 |
break; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
if (skip == true) |
| 1274 |
continue; |
| 1275 |
} |
| 1276 |
|
| 1277 |
if ((v > 1) && (stressflags & S_2_TO_HEAVY) && (syllable_weight[v] == 0) && (syllable_weight[v+1] > 0)) { |
| 1278 |
// don't put secondary stress on a light syllable which is followed by a heavy syllable |
| 1279 |
continue; |
| 1280 |
} |
| 1281 |
|
| 1282 |
// should start with secondary stress on the first syllable, or should it count back from |
| 1283 |
// the primary stress and put secondary stress on alternate syllables? |
| 1284 |
vowel_stress[v] = (char)stress; |
| 1285 |
done = true; |
| 1286 |
stress = STRESS_IS_SECONDARY; // use secondary stress for remaining syllables |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
if (vowel_stress[v] >= STRESS_IS_PRIMARY) { |
| 1291 |
if (first_primary == 0) |
| 1292 |
first_primary = v; |
| 1293 |
else if (stressflags & S_FIRST_PRIMARY) { |
| 1294 |
// reduce primary stresses after the first to secondary |
| 1295 |
vowel_stress[v] = STRESS_IS_SECONDARY; |
| 1296 |
} |
| 1297 |
} |
| 1298 |
} |
| 1299 |
|
| 1300 |
if ((unstressed_word) && (tonic < 0)) { |
| 1301 |
if (vowel_count <= 2) |
| 1302 |
tonic = tr->langopts.unstressed_wd1; // monosyllable - unstressed |
| 1303 |
else |
| 1304 |
tonic = tr->langopts.unstressed_wd2; // more than one syllable, used secondary stress as the main stress |
| 1305 |
} |
| 1306 |
|
| 1307 |
max_stress = STRESS_IS_DIMINISHED; |
| 1308 |
max_stress_posn = 0; |
| 1309 |
for (v = 1; v < vowel_count; v++) { |
| 1310 |
if (vowel_stress[v] >= max_stress) { |
| 1311 |
max_stress = vowel_stress[v]; |
| 1312 |
max_stress_posn = v; |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
if (tonic >= 0) { |
| 1317 |
// find position of highest stress, and replace it by 'tonic' |
| 1318 |
|
| 1319 |
// don't disturb an explicitly set stress by 'unstress-at-end' flag |
| 1320 |
if ((tonic > max_stress) || (max_stress <= STRESS_IS_PRIMARY)) |
| 1321 |
vowel_stress[max_stress_posn] = (char)tonic; |
| 1322 |
max_stress = tonic; |
| 1323 |
} |
| 1324 |
|
| 1325 |
// produce output phoneme string |
| 1326 |
p = phonetic; |
| 1327 |
v = 1; |
| 1328 |
|
| 1329 |
if (!(control & 1) && ((ph = phoneme_tab[*p]) != NULL)) { |
| 1330 |
while ((ph->type == phSTRESS) || (*p == phonEND_WORD)) { |
| 1331 |
p++; |
| 1332 |
ph = phoneme_tab[p[0]]; |
| 1333 |
} |
| 1334 |
|
| 1335 |
if ((tr->langopts.vowel_pause & 0x30) && (ph->type == phVOWEL)) { |
| 1336 |
// word starts with a vowel |
| 1337 |
|
| 1338 |
if ((tr->langopts.vowel_pause & 0x20) && (vowel_stress[1] >= STRESS_IS_PRIMARY)) |
| 1339 |
*output++ = phonPAUSE_NOLINK; // not to be replaced by link |
| 1340 |
else |
| 1341 |
*output++ = phonPAUSE_VSHORT; // break, but no pause |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
p = phonetic; |
| 1346 |
/* Note: v progression has to strictly follow the vowel_stress production in GetVowelStress */ |
| 1347 |
while (((phcode = *p++) != 0) && (output < max_output)) { |
| 1348 |
if ((ph = phoneme_tab[phcode]) == NULL) |
| 1349 |
continue; |
| 1350 |
|
| 1351 |
if (ph->type == phPAUSE) |
| 1352 |
tr->prev_last_stress = 0; |
| 1353 |
else if (((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC)) || (*p == phonSYLLABIC)) { |
| 1354 |
// a vowel, or a consonant followed by a syllabic consonant marker |
| 1355 |
|
| 1356 |
assert(v <= vowel_count); |
| 1357 |
|
| 1358 |
v_stress = vowel_stress[v]; |
| 1359 |
tr->prev_last_stress = v_stress; |
| 1360 |
|
| 1361 |
if (v_stress <= STRESS_IS_UNSTRESSED) { |
| 1362 |
if ((v > 1) && (max_stress >= 2) && (stressflags & S_FINAL_DIM) && (v == (vowel_count-1))) { |
| 1363 |
// option: mark unstressed final syllable as diminished |
| 1364 |
v_stress = STRESS_IS_DIMINISHED; |
| 1365 |
} else if ((stressflags & S_NO_DIM) || (v == 1) || (v == (vowel_count-1))) { |
| 1366 |
// first or last syllable, or option 'don't set diminished stress' |
| 1367 |
v_stress = STRESS_IS_UNSTRESSED; |
| 1368 |
} else if ((v == (vowel_count-2)) && (vowel_stress[vowel_count-1] <= STRESS_IS_UNSTRESSED)) { |
| 1369 |
// penultimate syllable, followed by an unstressed final syllable |
| 1370 |
v_stress = STRESS_IS_UNSTRESSED; |
| 1371 |
} else { |
| 1372 |
// unstressed syllable within a word |
| 1373 |
if ((vowel_stress[v-1] < STRESS_IS_DIMINISHED) || ((stressflags & S_MID_DIM) == 0)) { |
| 1374 |
v_stress = STRESS_IS_DIMINISHED; |
| 1375 |
vowel_stress[v] = v_stress; |
| 1376 |
} |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
if ((v_stress == STRESS_IS_DIMINISHED) || (v_stress > STRESS_IS_UNSTRESSED)) |
| 1381 |
*output++ = stress_phonemes[v_stress]; // mark stress of all vowels except 1 (unstressed) |
| 1382 |
|
| 1383 |
if (vowel_stress[v] > max_stress) |
| 1384 |
max_stress = vowel_stress[v]; |
| 1385 |
|
| 1386 |
if ((*p == phonLENGTHEN) && ((opt_length = tr->langopts.param[LOPT_IT_LENGTHEN]) & 1)) { |
| 1387 |
// remove lengthen indicator from non-stressed syllables |
| 1388 |
bool shorten = false; |
| 1389 |
|
| 1390 |
if (opt_length & 0x10) { |
| 1391 |
// only allow lengthen indicator on the highest stress syllable in the word |
| 1392 |
if (v != max_stress_posn) |
| 1393 |
shorten = true; |
| 1394 |
} else if (v_stress < STRESS_IS_PRIMARY) { |
| 1395 |
// only allow lengthen indicator if stress >= STRESS_IS_PRIMARY. |
| 1396 |
shorten = true; |
| 1397 |
} |
| 1398 |
|
| 1399 |
if (shorten) |
| 1400 |
p++; |
| 1401 |
} |
| 1402 |
v++; |
| 1403 |
} |
| 1404 |
|
| 1405 |
if (phcode != 1) |
| 1406 |
*output++ = phcode; |
| 1407 |
} |
| 1408 |
*output++ = 0; |
| 1409 |
|
| 1410 |
return; |
| 1411 |
} |
| 1412 |
|
| 1413 |
void AppendPhonemes(Translator *tr, char *string, int size, const char *ph) |
| 1414 |
{ |
| 1415 |
/* Add new phoneme string "ph" to "string" |
| 1416 |
Keeps count of the number of vowel phonemes in the word, and whether these |
| 1417 |
can be stressed syllables. These values can be used in translation rules |
| 1418 |
*/ |
| 1419 |
|
| 1420 |
const char *p; |
| 1421 |
unsigned char c; |
| 1422 |
int length; |
| 1423 |
|
| 1424 |
length = strlen(ph) + strlen(string); |
| 1425 |
if (length >= size) |
| 1426 |
return; |
| 1427 |
|
| 1428 |
// any stressable vowel ? |
| 1429 |
bool unstress_mark = false; |
| 1430 |
p = ph; |
| 1431 |
while ((c = *p++) != 0) { |
| 1432 |
if (c >= n_phoneme_tab) continue; |
| 1433 |
|
| 1434 |
if (phoneme_tab[c]->type == phSTRESS) { |
| 1435 |
if (phoneme_tab[c]->std_length < 4) |
| 1436 |
unstress_mark = true; |
| 1437 |
} else { |
| 1438 |
if (phoneme_tab[c]->type == phVOWEL) { |
| 1439 |
if (((phoneme_tab[c]->phflags & phUNSTRESSED) == 0) && |
| 1440 |
(unstress_mark == false)) { |
| 1441 |
tr->word_stressed_count++; |
| 1442 |
} |
| 1443 |
unstress_mark = false; |
| 1444 |
tr->word_vowel_count++; |
| 1445 |
} |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| 1449 |
if (string != NULL) |
| 1450 |
strcat(string, ph); |
| 1451 |
} |
| 1452 |
|
| 1453 |
static void MatchRule(Translator *tr, char *word[], char *word_start, int group_length, char *rule, MatchRecord *match_out, int word_flags, int dict_flags) |
| 1454 |
{ |
| 1455 |
/* Checks a specified word against dictionary rules. |
| 1456 |
Returns with phoneme code string, or NULL if no match found. |
| 1457 |
|
| 1458 |
word (indirect) points to current character group within the input word |
| 1459 |
This is advanced by this procedure as characters are consumed |
| 1460 |
|
| 1461 |
group: the initial characters used to choose the rules group |
| 1462 |
|
| 1463 |
rule: address of dictionary rule data for this character group |
| 1464 |
|
| 1465 |
match_out: returns best points score |
| 1466 |
|
| 1467 |
word_flags: indicates whether this is a retranslation after a suffix has been removed |
| 1468 |
*/ |
| 1469 |
|
| 1470 |
unsigned char rb; // current instuction from rule |
| 1471 |
unsigned char letter; // current letter from input word, single byte |
| 1472 |
int letter_w; // current letter, wide character |
| 1473 |
int last_letter_w; // last letter, wide character |
| 1474 |
int letter_xbytes; // number of extra bytes of multibyte character (num bytes - 1) |
| 1475 |
|
| 1476 |
char *pre_ptr; |
| 1477 |
char *post_ptr; // pointer to first character after group |
| 1478 |
|
| 1479 |
char *rule_start; // start of current match template |
| 1480 |
// char *p; |
| 1481 |
int match_type; // left, right, or consume |
| 1482 |
int syllable_count; |
| 1483 |
int vowel; |
| 1484 |
int letter_group; |
| 1485 |
int lg_pts; |
| 1486 |
int n_bytes; |
| 1487 |
int add_points; |
| 1488 |
int command; |
| 1489 |
|
| 1490 |
MatchRecord match; |
| 1491 |
MatchRecord best; |
| 1492 |
|
| 1493 |
int total_consumed; // letters consumed for best match |
| 1494 |
|
| 1495 |
unsigned char condition_num; |
| 1496 |
char *common_phonemes; // common to a group of entries |
| 1497 |
char *group_chars; |
| 1498 |
char word_buf[N_WORD_BYTES]; |
| 1499 |
|
| 1500 |
group_chars = *word; |
| 1501 |
|
| 1502 |
if (rule == NULL) { |
| 1503 |
match_out->points = 0; |
| 1504 |
(*word)++; |
| 1505 |
return; |
| 1506 |
} |
| 1507 |
|
| 1508 |
total_consumed = 0; |
| 1509 |
common_phonemes = NULL; |
| 1510 |
|
| 1511 |
best.points = 0; |
| 1512 |
best.phonemes = ""; |
| 1513 |
best.end_type = 0; |
| 1514 |
best.del_fwd = NULL; |
| 1515 |
|
| 1516 |
// search through dictionary rules |
| 1517 |
while (rule[0] != RULE_GROUP_END) { |
| 1518 |
bool check_atstart = false; |
| 1519 |
int consumed = 0; // number of letters consumed from input |
| 1520 |
int distance_left = -2; |
| 1521 |
int distance_right = -6; // used to reduce points for matches further away the current letter |
| 1522 |
int failed = 0; |
| 1523 |
int unpron_ignore = word_flags & FLAG_UNPRON_TEST; |
| 1524 |
|
| 1525 |
match_type = 0; |
| 1526 |
letter_w = 0; |
| 1527 |
|
| 1528 |
match.points = 1; |
| 1529 |
match.end_type = 0; |
| 1530 |
match.del_fwd = NULL; |
| 1531 |
|
| 1532 |
pre_ptr = *word; |
| 1533 |
post_ptr = *word + group_length; |
| 1534 |
|
| 1535 |
// work through next rule until end, or until no-match proved |
| 1536 |
rule_start = rule; |
| 1537 |
|
| 1538 |
while (!failed) { |
| 1539 |
rb = *rule++; |
| 1540 |
add_points = 0; |
| 1541 |
|
| 1542 |
if (rb <= RULE_LINENUM) { |
| 1543 |
switch (rb) |
| 1544 |
{ |
| 1545 |
case 0: // no phoneme string for this rule, use previous common rule |
| 1546 |
if (common_phonemes != NULL) { |
| 1547 |
match.phonemes = common_phonemes; |
| 1548 |
while (((rb = *match.phonemes++) != 0) && (rb != RULE_PHONEMES)) { |
| 1549 |
if (rb == RULE_CONDITION) |
| 1550 |
match.phonemes++; // skip over condition number |
| 1551 |
if (rb == RULE_LINENUM) |
| 1552 |
match.phonemes += 2; // skip over line number |
| 1553 |
} |
| 1554 |
} else |
| 1555 |
match.phonemes = ""; |
| 1556 |
rule--; // so we are still pointing at the 0 |
| 1557 |
failed = 2; // matched OK |
| 1558 |
break; |
| 1559 |
case RULE_PRE_ATSTART: // pre rule with implied 'start of word' |
| 1560 |
check_atstart = true; |
| 1561 |
unpron_ignore = 0; |
| 1562 |
match_type = RULE_PRE; |
| 1563 |
break; |
| 1564 |
case RULE_PRE: |
| 1565 |
match_type = RULE_PRE; |
| 1566 |
if (word_flags & FLAG_UNPRON_TEST) { |
| 1567 |
// checking the start of the word for unpronouncable character sequences, only |
| 1568 |
// consider rules which explicitly match the start of a word |
| 1569 |
// Note: Those rules now use RULE_PRE_ATSTART |
| 1570 |
failed = 1; |
| 1571 |
} |
| 1572 |
break; |
| 1573 |
case RULE_POST: |
| 1574 |
match_type = RULE_POST; |
| 1575 |
break; |
| 1576 |
case RULE_PHONEMES: |
| 1577 |
match.phonemes = rule; |
| 1578 |
failed = 2; // matched OK |
| 1579 |
break; |
| 1580 |
case RULE_PH_COMMON: |
| 1581 |
common_phonemes = rule; |
| 1582 |
break; |
| 1583 |
case RULE_CONDITION: |
| 1584 |
// conditional rule, next byte gives condition number |
| 1585 |
condition_num = *rule++; |
| 1586 |
|
| 1587 |
if (condition_num >= 32) { |
| 1588 |
// allow the rule only if the condition number is NOT set |
| 1589 |
if ((tr->dict_condition & (1L << (condition_num-32))) != 0) |
| 1590 |
failed = 1; |
| 1591 |
} else { |
| 1592 |
// allow the rule only if the condition number is set |
| 1593 |
if ((tr->dict_condition & (1L << condition_num)) == 0) |
| 1594 |
failed = 1; |
| 1595 |
} |
| 1596 |
|
| 1597 |
if (!failed) |
| 1598 |
match.points++; // add one point for a matched conditional rule |
| 1599 |
break; |
| 1600 |
case RULE_LINENUM: |
| 1601 |
rule += 2; |
| 1602 |
break; |
| 1603 |
} |
| 1604 |
continue; |
| 1605 |
} |
| 1606 |
|
| 1607 |
switch (match_type) |
| 1608 |
{ |
| 1609 |
case 0: |
| 1610 |
// match and consume this letter |
| 1611 |
letter = *post_ptr++; |
| 1612 |
|
| 1613 |
if ((letter == rb) || ((letter == (unsigned char)REPLACED_E) && (rb == 'e'))) { |
| 1614 |
if ((letter & 0xc0) != 0x80) |
| 1615 |
add_points = 21; // don't add point for non-initial UTF-8 bytes |
| 1616 |
consumed++; |
| 1617 |
} else |
| 1618 |
failed = 1; |
| 1619 |
break; |
| 1620 |
case RULE_POST: |
| 1621 |
// continue moving forwards |
| 1622 |
distance_right += 6; |
| 1623 |
if (distance_right > 18) |
| 1624 |
distance_right = 19; |
| 1625 |
last_letter_w = letter_w; |
| 1626 |
if (!post_ptr[-1]) { |
| 1627 |
// we had already reached the end of text! |
| 1628 |
// reading after that does not make sense, that cannot match |
| 1629 |
failed = 1; |
| 1630 |
break; |
| 1631 |
} |
| 1632 |
letter_xbytes = utf8_in(&letter_w, post_ptr)-1; |
| 1633 |
letter = *post_ptr++; |
| 1634 |
|
| 1635 |
switch (rb) |
| 1636 |
{ |
| 1637 |
case RULE_LETTERGP: |
| 1638 |
letter_group = LetterGroupNo(rule++); |
| 1639 |
if (IsLetter(tr, letter_w, letter_group)) { |
| 1640 |
lg_pts = 20; |
| 1641 |
if (letter_group == 2) |
| 1642 |
lg_pts = 19; // fewer points for C, general consonant |
| 1643 |
add_points = (lg_pts-distance_right); |
| 1644 |
post_ptr += letter_xbytes; |
| 1645 |
} else |
| 1646 |
failed = 1; |
| 1647 |
break; |
| 1648 |
case RULE_LETTERGP2: // match against a list of utf-8 strings |
| 1649 |
letter_group = LetterGroupNo(rule++); |
| 1650 |
if ((n_bytes = IsLetterGroup(tr, post_ptr-1, letter_group, 0)) >= 0) { |
| 1651 |
add_points = (20-distance_right); |
| 1652 |
// move pointer, if group was found |
| 1653 |
post_ptr += (n_bytes-1); |
| 1654 |
} else |
| 1655 |
failed = 1; |
| 1656 |
break; |
| 1657 |
case RULE_NOTVOWEL: |
| 1658 |
if (IsLetter(tr, letter_w, 0) || ((letter_w == ' ') && (word_flags & FLAG_SUFFIX_VOWEL))) |
| 1659 |
failed = 1; |
| 1660 |
else { |
| 1661 |
add_points = (20-distance_right); |
| 1662 |
post_ptr += letter_xbytes; |
| 1663 |
} |
| 1664 |
break; |
| 1665 |
case RULE_DIGIT: |
| 1666 |
if (IsDigit(letter_w)) { |
| 1667 |
add_points = (20-distance_right); |
| 1668 |
post_ptr += letter_xbytes; |
| 1669 |
} else if (tr->langopts.tone_numbers) { |
| 1670 |
// also match if there is no digit |
| 1671 |
add_points = (20-distance_right); |
| 1672 |
post_ptr--; |
| 1673 |
} else |
| 1674 |
failed = 1; |
| 1675 |
break; |
| 1676 |
case RULE_NONALPHA: |
| 1677 |
if (!iswalpha_portable(letter_w)) { |
| 1678 |
add_points = (21-distance_right); |
| 1679 |
post_ptr += letter_xbytes; |
| 1680 |
} else |
| 1681 |
failed = 1; |
| 1682 |
break; |
| 1683 |
case RULE_DOUBLE: |
| 1684 |
if (letter_w == last_letter_w) { |
| 1685 |
add_points = (21-distance_right); |
| 1686 |
post_ptr += letter_xbytes; |
| 1687 |
} else |
| 1688 |
failed = 1; |
| 1689 |
break; |
| 1690 |
case RULE_DOLLAR: |
| 1691 |
post_ptr--; |
| 1692 |
command = *rule++; |
| 1693 |
if (command == DOLLAR_UNPR) |
| 1694 |
match.end_type = SUFX_UNPRON; // $unpron |
| 1695 |
else if (command == DOLLAR_NOPREFIX) { // $noprefix |
| 1696 |
if (word_flags & FLAG_PREFIX_REMOVED) |
| 1697 |
failed = 1; // a prefix has been removed |
| 1698 |
else |
| 1699 |
add_points = 1; |
| 1700 |
} else if ((command & 0xf0) == 0x10) { |
| 1701 |
// $w_alt |
| 1702 |
if (dict_flags & (1 << (BITNUM_FLAG_ALT + (command & 0xf)))) |
| 1703 |
add_points = 23; |
| 1704 |
else |
| 1705 |
failed = 1; |
| 1706 |
} else if (((command & 0xf0) == 0x20) || (command == DOLLAR_LIST)) { |
| 1707 |
DollarRule(word, word_start, consumed, group_length, word_buf, tr, command, &failed, &add_points); |
| 1708 |
} |
| 1709 |
|
| 1710 |
break; |
| 1711 |
case '-': |
| 1712 |
if ((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN_AFTER))) |
| 1713 |
add_points = (22-distance_right); // one point more than match against space |
| 1714 |
else |
| 1715 |
failed = 1; |
| 1716 |
break; |
| 1717 |
case RULE_SYLLABLE: |
| 1718 |
{ |
| 1719 |
// more than specified number of vowel letters to the right |
| 1720 |
char *p1 = post_ptr + letter_xbytes; |
| 1721 |
int vowel_count = 0; |
| 1722 |
|
| 1723 |
syllable_count = 1; |
| 1724 |
while (*rule == RULE_SYLLABLE) { |
| 1725 |
rule++; |
| 1726 |
syllable_count += 1; // number of syllables to match |
| 1727 |
} |
| 1728 |
vowel = 0; |
| 1729 |
while (letter_w != RULE_SPACE) { |
| 1730 |
if ((vowel == 0) && IsLetter(tr, letter_w, LETTERGP_VOWEL2)) { |
| 1731 |
// this is counting vowels which are separated by non-vowel letters |
| 1732 |
vowel_count++; |
| 1733 |
} |
| 1734 |
vowel = IsLetter(tr, letter_w, LETTERGP_VOWEL2); |
| 1735 |
p1 += utf8_in(&letter_w, p1); |
| 1736 |
} |
| 1737 |
if (syllable_count <= vowel_count) |
| 1738 |
add_points = (18+syllable_count-distance_right); |
| 1739 |
else |
| 1740 |
failed = 1; |
| 1741 |
} |
| 1742 |
break; |
| 1743 |
case RULE_NOVOWELS: |
| 1744 |
{ |
| 1745 |
char *p1 = post_ptr + letter_xbytes; |
| 1746 |
while (letter_w != RULE_SPACE) { |
| 1747 |
if (IsLetter(tr, letter_w, LETTERGP_VOWEL2)) { |
| 1748 |
failed = 1; |
| 1749 |
break; |
| 1750 |
} |
| 1751 |
p1 += utf8_in(&letter_w, p1); |
| 1752 |
} |
| 1753 |
if (!failed) |
| 1754 |
add_points = (19-distance_right); |
| 1755 |
} |
| 1756 |
break; |
| 1757 |
case RULE_SKIPCHARS: |
| 1758 |
{ |
| 1759 |
// '(Jxy' means 'skip characters until xy' |
| 1760 |
char *p = post_ptr - 1; // to allow empty jump (without letter between), go one back |
| 1761 |
char *p2 = p; // pointer to the previous character in the word |
| 1762 |
int rule_w; // first wide character of skip rule |
| 1763 |
utf8_in(&rule_w, rule); |
| 1764 |
int g_bytes = -1; // bytes of successfully found character group |
| 1765 |
while ((letter_w != rule_w) && (letter_w != RULE_SPACE) && (letter_w != 0) && (g_bytes == -1)) { |
| 1766 |
if (rule_w == RULE_LETTERGP2) |
| 1767 |
g_bytes = IsLetterGroup(tr, p, LetterGroupNo(rule + 1), 0); |
| 1768 |
p2 = p; |
| 1769 |
p += utf8_in(&letter_w, p); |
| 1770 |
} |
| 1771 |
if ((letter_w == rule_w) || (g_bytes >= 0)) |
| 1772 |
post_ptr = p2; |
| 1773 |
} |
| 1774 |
break; |
| 1775 |
case RULE_INC_SCORE: |
| 1776 |
post_ptr--; |
| 1777 |
add_points = 20; // force an increase in points |
| 1778 |
break; |
| 1779 |
case RULE_DEC_SCORE: |
| 1780 |
post_ptr--; |
| 1781 |
add_points = -20; // force an decrease in points |
| 1782 |
break; |
| 1783 |
case RULE_DEL_FWD: |
| 1784 |
// find the next 'e' in the word and replace by 'E' |
| 1785 |
for (char *p = *word + group_length; p < post_ptr; p++) { |
| 1786 |
if (*p == 'e') { |
| 1787 |
match.del_fwd = p; |
| 1788 |
break; |
| 1789 |
} |
| 1790 |
} |
| 1791 |
break; |
| 1792 |
case RULE_ENDING: |
| 1793 |
{ |
| 1794 |
int end_type; |
| 1795 |
// next 3 bytes are a (non-zero) ending type. 2 bytes of flags + suffix length |
| 1796 |
end_type = (rule[0] << 16) + ((rule[1] & 0x7f) << 8) + (rule[2] & 0x7f); |
| 1797 |
|
| 1798 |
if ((tr->word_vowel_count == 0) && !(end_type & SUFX_P) && (tr->langopts.param[LOPT_SUFFIX] & 1)) |
| 1799 |
failed = 1; // don't match a suffix rule if there are no previous syllables (needed for lang=tr). |
| 1800 |
else { |
| 1801 |
match.end_type = end_type; |
| 1802 |
rule += 3; |
| 1803 |
} |
| 1804 |
} |
| 1805 |
break; |
| 1806 |
case RULE_NO_SUFFIX: |
| 1807 |
if (word_flags & FLAG_SUFFIX_REMOVED) |
| 1808 |
failed = 1; // a suffix has been removed |
| 1809 |
else { |
| 1810 |
post_ptr--; |
| 1811 |
add_points = 1; |
| 1812 |
} |
| 1813 |
break; |
| 1814 |
default: |
| 1815 |
if (letter == rb) { |
| 1816 |
if ((letter & 0xc0) != 0x80) { |
| 1817 |
// not for non-initial UTF-8 bytes |
| 1818 |
add_points = (21-distance_right); |
| 1819 |
} |
| 1820 |
} else |
| 1821 |
failed = 1; |
| 1822 |
break; |
| 1823 |
} |
| 1824 |
break; |
| 1825 |
case RULE_PRE: |
| 1826 |
// match backwards from start of current group |
| 1827 |
distance_left += 2; |
| 1828 |
if (distance_left > 18) |
| 1829 |
distance_left = 19; |
| 1830 |
|
| 1831 |
if (!*pre_ptr) { |
| 1832 |
// we had already reached the beginning of text! |
| 1833 |
// reading before this does not make sense, that cannot match |
| 1834 |
failed = 1; |
| 1835 |
break; |
| 1836 |
} |
| 1837 |
utf8_in(&last_letter_w, pre_ptr); |
| 1838 |
pre_ptr--; |
| 1839 |
letter_xbytes = utf8_in2(&letter_w, pre_ptr, 1)-1; |
| 1840 |
letter = *pre_ptr; |
| 1841 |
|
| 1842 |
switch (rb) |
| 1843 |
{ |
| 1844 |
case RULE_LETTERGP: |
| 1845 |
letter_group = LetterGroupNo(rule++); |
| 1846 |
if (IsLetter(tr, letter_w, letter_group)) { |
| 1847 |
lg_pts = 20; |
| 1848 |
if (letter_group == 2) |
| 1849 |
lg_pts = 19; // fewer points for C, general consonant |
| 1850 |
add_points = (lg_pts-distance_left); |
| 1851 |
pre_ptr -= letter_xbytes; |
| 1852 |
} else |
| 1853 |
failed = 1; |
| 1854 |
break; |
| 1855 |
case RULE_LETTERGP2: // match against a list of utf-8 strings |
| 1856 |
letter_group = LetterGroupNo(rule++); |
| 1857 |
if ((n_bytes = IsLetterGroup(tr, pre_ptr, letter_group, 1)) >= 0) { |
| 1858 |
add_points = (20-distance_right); |
| 1859 |
// move pointer, if group was found |
| 1860 |
pre_ptr -= (n_bytes-1); |
| 1861 |
} else |
| 1862 |
failed = 1; |
| 1863 |
break; |
| 1864 |
case RULE_NOTVOWEL: |
| 1865 |
if (!IsLetter(tr, letter_w, 0)) { |
| 1866 |
add_points = (20-distance_left); |
| 1867 |
pre_ptr -= letter_xbytes; |
| 1868 |
} else |
| 1869 |
failed = 1; |
| 1870 |
break; |
| 1871 |
case RULE_DOUBLE: |
| 1872 |
if (letter_w == last_letter_w) { |
| 1873 |
add_points = (21-distance_left); |
| 1874 |
pre_ptr -= letter_xbytes; |
| 1875 |
} else |
| 1876 |
failed = 1; |
| 1877 |
break; |
| 1878 |
case RULE_DIGIT: |
| 1879 |
if (IsDigit(letter_w)) { |
| 1880 |
add_points = (21-distance_left); |
| 1881 |
pre_ptr -= letter_xbytes; |
| 1882 |
} else |
| 1883 |
failed = 1; |
| 1884 |
break; |
| 1885 |
case RULE_NONALPHA: |
| 1886 |
if (!iswalpha_portable(letter_w)) { |
| 1887 |
add_points = (21-distance_right); |
| 1888 |
pre_ptr -= letter_xbytes; |
| 1889 |
} else |
| 1890 |
failed = 1; |
| 1891 |
break; |
| 1892 |
case RULE_DOLLAR: |
| 1893 |
pre_ptr++; |
| 1894 |
command = *rule++; |
| 1895 |
if ((command == DOLLAR_LIST) || ((command & 0xf0) == 0x20)) { |
| 1896 |
DollarRule(word, word_start, consumed, group_length, word_buf, tr, command, &failed, &add_points); |
| 1897 |
} |
| 1898 |
break; |
| 1899 |
case RULE_SYLLABLE: |
| 1900 |
// more than specified number of vowels to the left |
| 1901 |
syllable_count = 1; |
| 1902 |
while (*rule == RULE_SYLLABLE) { |
| 1903 |
rule++; |
| 1904 |
syllable_count++; // number of syllables to match |
| 1905 |
} |
| 1906 |
if (syllable_count <= tr->word_vowel_count) |
| 1907 |
add_points = (18+syllable_count-distance_left); |
| 1908 |
else |
| 1909 |
failed = 1; |
| 1910 |
break; |
| 1911 |
case RULE_STRESSED: |
| 1912 |
pre_ptr++; |
| 1913 |
if (tr->word_stressed_count > 0) |
| 1914 |
add_points = 19; |
| 1915 |
else |
| 1916 |
failed = 1; |
| 1917 |
break; |
| 1918 |
case RULE_NOVOWELS: |
| 1919 |
{ |
| 1920 |
char *p = pre_ptr - letter_xbytes; |
| 1921 |
while (letter_w != RULE_SPACE) { |
| 1922 |
if (IsLetter(tr, letter_w, LETTERGP_VOWEL2)) { |
| 1923 |
failed = 1; |
| 1924 |
break; |
| 1925 |
} |
| 1926 |
p -= utf8_in2(&letter_w, p-1, 1); |
| 1927 |
} |
| 1928 |
if (!failed) |
| 1929 |
add_points = 3; |
| 1930 |
} |
| 1931 |
break; |
| 1932 |
case RULE_IFVERB: |
| 1933 |
pre_ptr++; |
| 1934 |
if (tr->expect_verb) |
| 1935 |
add_points = 1; |
| 1936 |
else |
| 1937 |
failed = 1; |
| 1938 |
break; |
| 1939 |
case RULE_CAPITAL: |
| 1940 |
pre_ptr++; |
| 1941 |
if (word_flags & FLAG_FIRST_UPPER) |
| 1942 |
add_points = 1; |
| 1943 |
else |
| 1944 |
failed = 1; |
| 1945 |
break; |
| 1946 |
case '.': |
| 1947 |
{ |
| 1948 |
// dot in pre- section, match on any dot before this point in the word |
| 1949 |
char *p; |
| 1950 |
for (p = pre_ptr; *p != ' '; p--) { |
| 1951 |
if (*p == '.') { |
| 1952 |
add_points = 50; |
| 1953 |
break; |
| 1954 |
} |
| 1955 |
} |
| 1956 |
if (*p == ' ') |
| 1957 |
failed = 1; |
| 1958 |
} |
| 1959 |
break; |
| 1960 |
case '-': |
| 1961 |
if ((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN))) |
| 1962 |
add_points = (22-distance_right); // one point more than match against space |
| 1963 |
else |
| 1964 |
failed = 1; |
| 1965 |
break; |
| 1966 |
|
| 1967 |
case RULE_SKIPCHARS: { |
| 1968 |
// 'xyJ)' means 'skip characters backwards until xy' |
| 1969 |
char *p = pre_ptr + 1; // to allow empty jump (without letter between), go one forward |
| 1970 |
char *p2 = p; // pointer to previous character in word |
| 1971 |
int g_bytes = -1; // bytes of successfully found character group |
| 1972 |
|
| 1973 |
while ((*p != *rule) && (*p != RULE_SPACE) && (*p != 0) && (g_bytes == -1)) { |
| 1974 |
p2 = p; |
| 1975 |
p--; |
| 1976 |
if (*rule == RULE_LETTERGP2) |
| 1977 |
g_bytes = IsLetterGroup(tr, p2, LetterGroupNo(rule + 1), 1); |
| 1978 |
} |
| 1979 |
|
| 1980 |
// if succeed, set pre_ptr to next character after 'xy' and remaining |
| 1981 |
// 'xy' part is checked as usual in following cycles of PRE rule characters |
| 1982 |
if (*p == *rule) |
| 1983 |
pre_ptr = p2; |
| 1984 |
if (g_bytes >= 0) |
| 1985 |
pre_ptr = p2 + 1; |
| 1986 |
|
| 1987 |
} |
| 1988 |
break; |
| 1989 |
|
| 1990 |
default: |
| 1991 |
if (letter == rb) { |
| 1992 |
if (letter == RULE_SPACE) |
| 1993 |
add_points = 4; |
| 1994 |
else if ((letter & 0xc0) != 0x80) { |
| 1995 |
// not for non-initial UTF-8 bytes |
| 1996 |
add_points = (21-distance_left); |
| 1997 |
} |
| 1998 |
} else |
| 1999 |
failed = 1; |
| 2000 |
break; |
| 2001 |
} |
| 2002 |
break; |
| 2003 |
} |
| 2004 |
|
| 2005 |
if (failed == 0) |
| 2006 |
match.points += add_points; |
| 2007 |
} |
| 2008 |
|
| 2009 |
if ((failed == 2) && (unpron_ignore == 0)) { |
| 2010 |
// do we also need to check for 'start of word' ? |
| 2011 |
if ((check_atstart == false) || (pre_ptr[-1] == ' ')) { |
| 2012 |
if (check_atstart) |
| 2013 |
match.points += 4; |
| 2014 |
|
| 2015 |
// matched OK, is this better than the last best match ? |
| 2016 |
if (match.points >= best.points) { |
| 2017 |
memcpy(&best, &match, sizeof(match)); |
| 2018 |
total_consumed = consumed; |
| 2019 |
} |
| 2020 |
|
| 2021 |
if ((option_phonemes & espeakPHONEMES_TRACE) && (match.points > 0) && ((word_flags & FLAG_NO_TRACE) == 0)) { |
| 2022 |
// show each rule that matches, and it's points score |
| 2023 |
int pts; |
| 2024 |
char decoded_phonemes[80]; |
| 2025 |
char output[80]; |
| 2026 |
|
| 2027 |
pts = match.points; |
| 2028 |
if (group_length > 1) |
| 2029 |
pts += 35; // to account for an extra letter matching |
| 2030 |
DecodePhonemes(match.phonemes, decoded_phonemes); |
| 2031 |
fprintf(f_trans, "%3d\t%s [%s]\n", pts, DecodeRule(group_chars, group_length, rule_start, word_flags, output), decoded_phonemes); |
| 2032 |
} |
| 2033 |
} |
| 2034 |
} |
| 2035 |
|
| 2036 |
// skip phoneme string to reach start of next template |
| 2037 |
while (*rule++ != 0) ; |
| 2038 |
} |
| 2039 |
|
| 2040 |
// advance input data pointer |
| 2041 |
total_consumed += group_length; |
| 2042 |
if (total_consumed == 0) |
| 2043 |
total_consumed = 1; // always advance over 1st letter |
| 2044 |
|
| 2045 |
*word += total_consumed; |
| 2046 |
|
| 2047 |
if (best.points == 0) |
| 2048 |
best.phonemes = ""; |
| 2049 |
memcpy(match_out, &best, sizeof(MatchRecord)); |
| 2050 |
} |
| 2051 |
|
| 2052 |
int TranslateRules(Translator *tr, char *p_start, char *phonemes, int ph_size, char *end_phonemes, int word_flags, unsigned int *dict_flags) |
| 2053 |
{ |
| 2054 |
/* Translate a word bounded by space characters |
| 2055 |
Append the result to 'phonemes' and any standard prefix/suffix in 'end_phonemes' */ |
| 2056 |
|
| 2057 |
unsigned char c, c2; |
| 2058 |
unsigned int c12; |
| 2059 |
int wc = 0; |
| 2060 |
char *p2; // copy of p for use in double letter chain match |
| 2061 |
int found; |
| 2062 |
int g; // group chain number |
| 2063 |
int g1; // first group for this letter |
| 2064 |
int letter; |
| 2065 |
int any_alpha = 0; |
| 2066 |
int ix; |
| 2067 |
unsigned int digit_count = 0; |
| 2068 |
char *p; |
| 2069 |
char word_buf[5]; |
| 2070 |
const ALPHABET *alphabet; |
| 2071 |
int dict_flags0 = 0; |
| 2072 |
MatchRecord match1 = { 0 }; |
| 2073 |
MatchRecord match2 = { 0 }; |
| 2074 |
char ph_buf[N_PHONEME_BYTES]; |
| 2075 |
char word_copy[N_WORD_BYTES]; |
| 2076 |
static const char str_pause[2] = { phonPAUSE_NOLINK, 0 }; |
| 2077 |
|
| 2078 |
if (tr->data_dictrules == NULL) |
| 2079 |
return 0; |
| 2080 |
|
| 2081 |
if (dict_flags != NULL) |
| 2082 |
dict_flags0 = dict_flags[0]; |
| 2083 |
|
| 2084 |
for (ix = 0; ix < (N_WORD_BYTES-1);) { |
| 2085 |
c = p_start[ix]; |
| 2086 |
word_copy[ix++] = c; |
| 2087 |
if (c == 0) |
| 2088 |
break; |
| 2089 |
} |
| 2090 |
word_copy[ix] = 0; |
| 2091 |
|
| 2092 |
if ((option_phonemes & espeakPHONEMES_TRACE) && ((word_flags & FLAG_NO_TRACE) == 0)) { |
| 2093 |
char wordbuf[120]; |
| 2094 |
unsigned int ix1; |
| 2095 |
|
| 2096 |
for (ix1 = 0; ((c = p_start[ix1]) != ' ') && (c != 0) && (ix1 < (sizeof(wordbuf)-1)); ix1++) |
| 2097 |
wordbuf[ix1] = c; |
| 2098 |
wordbuf[ix1] = 0; |
| 2099 |
if (word_flags & FLAG_UNPRON_TEST) |
| 2100 |
fprintf(f_trans, "Unpronouncable? '%s'\n", wordbuf); |
| 2101 |
else |
| 2102 |
fprintf(f_trans, "Translate '%s'\n", wordbuf); |
| 2103 |
} |
| 2104 |
|
| 2105 |
p = p_start; |
| 2106 |
tr->word_vowel_count = 0; |
| 2107 |
tr->word_stressed_count = 0; |
| 2108 |
|
| 2109 |
if (end_phonemes != NULL) |
| 2110 |
end_phonemes[0] = 0; |
| 2111 |
|
| 2112 |
while (((c = *p) != ' ') && (c != 0)) { |
| 2113 |
int wc_bytes = utf8_in(&wc, p); |
| 2114 |
if (IsAlpha(wc)) |
| 2115 |
any_alpha++; |
| 2116 |
|
| 2117 |
int n = tr->groups2_count[c]; |
| 2118 |
if (IsDigit(wc) && ((tr->langopts.tone_numbers == 0) || !any_alpha)) { |
| 2119 |
// lookup the number in *_list not *_rules |
| 2120 |
char string[8]; |
| 2121 |
char buf[40]; |
| 2122 |
string[0] = '_'; |
| 2123 |
memcpy(&string[1], p, wc_bytes); |
| 2124 |
string[1+wc_bytes] = 0; |
| 2125 |
Lookup(tr, string, buf); |
| 2126 |
if (++digit_count >= 2) { |
| 2127 |
strcat(buf, str_pause); |
| 2128 |
digit_count = 0; |
| 2129 |
} |
| 2130 |
AppendPhonemes(tr, phonemes, ph_size, buf); |
| 2131 |
p += wc_bytes; |
| 2132 |
continue; |
| 2133 |
} else { |
| 2134 |
digit_count = 0; |
| 2135 |
found = 0; |
| 2136 |
|
| 2137 |
if (((ix = wc - tr->letter_bits_offset) >= 0) && (ix < 128)) { |
| 2138 |
if (tr->groups3[ix] != NULL) { |
| 2139 |
MatchRule(tr, &p, p_start, wc_bytes, tr->groups3[ix], &match1, word_flags, dict_flags0); |
| 2140 |
found = 1; |
| 2141 |
} |
| 2142 |
} |
| 2143 |
|
| 2144 |
if (!found && (n > 0)) { |
| 2145 |
// there are some 2 byte chains for this initial letter |
| 2146 |
c2 = p[1]; |
| 2147 |
c12 = c + (c2 << 8); // 2 characters |
| 2148 |
|
| 2149 |
g1 = tr->groups2_start[c]; |
| 2150 |
for (g = g1; g < (g1+n); g++) { |
| 2151 |
if (tr->groups2_name[g] == c12) { |
| 2152 |
found = 1; |
| 2153 |
|
| 2154 |
p2 = p; |
| 2155 |
MatchRule(tr, &p2, p_start, 2, tr->groups2[g], &match2, word_flags, dict_flags0); |
| 2156 |
if (match2.points > 0) |
| 2157 |
match2.points += 35; // to acount for 2 letters matching |
| 2158 |
|
| 2159 |
// now see whether single letter chain gives a better match ? |
| 2160 |
MatchRule(tr, &p, p_start, 1, tr->groups1[c], &match1, word_flags, dict_flags0); |
| 2161 |
|
| 2162 |
if (match2.points >= match1.points) { |
| 2163 |
// use match from the 2-letter group |
| 2164 |
memcpy(&match1, &match2, sizeof(MatchRecord)); |
| 2165 |
p = p2; |
| 2166 |
} |
| 2167 |
} |
| 2168 |
} |
| 2169 |
} |
| 2170 |
|
| 2171 |
if (!found) { |
| 2172 |
// alphabetic, single letter chain |
| 2173 |
if (tr->groups1[c] != NULL) |
| 2174 |
MatchRule(tr, &p, p_start, 1, tr->groups1[c], &match1, word_flags, dict_flags0); |
| 2175 |
else { |
| 2176 |
// no group for this letter, use default group |
| 2177 |
MatchRule(tr, &p, p_start, 0, tr->groups1[0], &match1, word_flags, dict_flags0); |
| 2178 |
|
| 2179 |
if ((match1.points == 0) && ((option_sayas & 0x10) == 0)) { |
| 2180 |
n = utf8_in(&letter, p-1)-1; |
| 2181 |
|
| 2182 |
if (tr->letter_bits_offset > 0) { |
| 2183 |
// not a Latin alphabet, switch to the default Latin alphabet language |
| 2184 |
if ((letter <= 0x241) && iswalpha_portable(letter)) { |
| 2185 |
sprintf(phonemes, "%cen", phonSWITCH); |
| 2186 |
return 0; |
| 2187 |
} |
| 2188 |
} |
| 2189 |
|
| 2190 |
// is it a bracket ? |
| 2191 |
if (letter == 0xe000+'(') { |
| 2192 |
if (pre_pause < tr->langopts.param[LOPT_BRACKET_PAUSE_ANNOUNCED]) |
| 2193 |
pre_pause = tr->langopts.param[LOPT_BRACKET_PAUSE_ANNOUNCED]; // a bracket, already spoken by AnnouncePunctuation() |
| 2194 |
} |
| 2195 |
if (IsBracket(letter)) { |
| 2196 |
if (pre_pause < tr->langopts.param[LOPT_BRACKET_PAUSE]) |
| 2197 |
pre_pause = tr->langopts.param[LOPT_BRACKET_PAUSE]; |
| 2198 |
} |
| 2199 |
|
| 2200 |
// no match, try removing the accent and re-translating the word |
| 2201 |
if ((letter >= 0xc0) && (letter < N_REMOVE_ACCENT) && ((ix = remove_accent[letter-0xc0]) != 0)) { |
| 2202 |
// within range of the remove_accent table |
| 2203 |
if ((p[-2] != ' ') || (p[n] != ' ')) { |
| 2204 |
// not the only letter in the word |
| 2205 |
p2 = p-1; |
| 2206 |
p[-1] = ix; |
| 2207 |
while ((p[0] = p[n]) != ' ') p++; |
| 2208 |
while (n-- > 0) *p++ = ' '; // replacement character must be no longer than original |
| 2209 |
|
| 2210 |
if (tr->langopts.param[LOPT_DIERESES] && (lookupwchar(diereses_list, letter) > 0)) { |
| 2211 |
// vowel with dieresis, replace and continue from this point |
| 2212 |
p = p2; |
| 2213 |
continue; |
| 2214 |
} |
| 2215 |
|
| 2216 |
phonemes[0] = 0; // delete any phonemes which have been produced so far |
| 2217 |
p = p_start; |
| 2218 |
tr->word_vowel_count = 0; |
| 2219 |
tr->word_stressed_count = 0; |
| 2220 |
continue; // start again at the beginning of the word |
| 2221 |
} |
| 2222 |
} |
| 2223 |
|
| 2224 |
if (((alphabet = AlphabetFromChar(letter)) != NULL) && (alphabet->offset != tr->letter_bits_offset)) { |
| 2225 |
if (tr->langopts.alt_alphabet == alphabet->offset) { |
| 2226 |
sprintf(phonemes, "%c%s", phonSWITCH, WordToString2(word_buf, tr->langopts.alt_alphabet_lang)); |
| 2227 |
return 0; |
| 2228 |
} |
| 2229 |
if (alphabet->flags & AL_WORDS) { |
| 2230 |
// switch to the nominated language for this alphabet |
| 2231 |
sprintf(phonemes, "%c%s", phonSWITCH, WordToString2(word_buf, alphabet->language)); |
| 2232 |
return 0; |
| 2233 |
} |
| 2234 |
} |
| 2235 |
} |
| 2236 |
} |
| 2237 |
|
| 2238 |
if (match1.points == 0) { |
| 2239 |
if ((wc >= 0x300) && (wc <= 0x36f)) { |
| 2240 |
// combining accent inside a word, ignore |
| 2241 |
} else if (IsAlpha(wc)) { |
| 2242 |
if ((any_alpha > 1) || (p[wc_bytes-1] > ' ')) { |
| 2243 |
// an unrecognised character in a word, abort and then spell the word |
| 2244 |
phonemes[0] = 0; |
| 2245 |
if (dict_flags != NULL) |
| 2246 |
dict_flags[0] |= FLAG_SPELLWORD; |
| 2247 |
break; |
| 2248 |
} |
| 2249 |
} else { |
| 2250 |
LookupLetter(tr, wc, -1, ph_buf, 0); |
| 2251 |
if (ph_buf[0]) { |
| 2252 |
match1.phonemes = ph_buf; |
| 2253 |
match1.points = 1; |
| 2254 |
} |
| 2255 |
} |
| 2256 |
p += (wc_bytes-1); |
| 2257 |
} else |
| 2258 |
tr->phonemes_repeat_count = 0; |
| 2259 |
} |
| 2260 |
} |
| 2261 |
|
| 2262 |
if (match1.phonemes == NULL) |
| 2263 |
match1.phonemes = ""; |
| 2264 |
|
| 2265 |
if (match1.points > 0) { |
| 2266 |
if (word_flags & FLAG_UNPRON_TEST) |
| 2267 |
return match1.end_type | 1; |
| 2268 |
|
| 2269 |
if ((match1.phonemes[0] == phonSWITCH) && ((word_flags & FLAG_DONT_SWITCH_TRANSLATOR) == 0)) { |
| 2270 |
// an instruction to switch language, return immediately so we can re-translate |
| 2271 |
strcpy(phonemes, match1.phonemes); |
| 2272 |
return 0; |
| 2273 |
} |
| 2274 |
|
| 2275 |
if ((option_phonemes & espeakPHONEMES_TRACE) && ((word_flags & FLAG_NO_TRACE) == 0)) |
| 2276 |
fprintf(f_trans, "\n"); |
| 2277 |
|
| 2278 |
match1.end_type &= ~SUFX_UNPRON; |
| 2279 |
|
| 2280 |
if ((match1.end_type != 0) && (end_phonemes != NULL)) { |
| 2281 |
// a standard ending has been found, re-translate the word without it |
| 2282 |
if ((match1.end_type & SUFX_P) && (word_flags & FLAG_NO_PREFIX)) { |
| 2283 |
// ignore the match on a prefix |
| 2284 |
} else { |
| 2285 |
if ((match1.end_type & SUFX_P) && ((match1.end_type & 0x7f) == 0)) { |
| 2286 |
// no prefix length specified |
| 2287 |
match1.end_type |= p - p_start; |
| 2288 |
} |
| 2289 |
strcpy(end_phonemes, match1.phonemes); |
| 2290 |
memcpy(p_start, word_copy, strlen(word_copy)); |
| 2291 |
return match1.end_type; |
| 2292 |
} |
| 2293 |
} |
| 2294 |
if (match1.del_fwd != NULL) |
| 2295 |
*match1.del_fwd = REPLACED_E; |
| 2296 |
AppendPhonemes(tr, phonemes, ph_size, match1.phonemes); |
| 2297 |
} |
| 2298 |
} |
| 2299 |
|
| 2300 |
memcpy(p_start, word_copy, strlen(word_copy)); |
| 2301 |
|
| 2302 |
return 0; |
| 2303 |
} |
| 2304 |
|
| 2305 |
int TransposeAlphabet(Translator *tr, char *text) |
| 2306 |
{ |
| 2307 |
// transpose cyrillic alphabet (for example) into ascii (single byte) character codes |
| 2308 |
// return: number of bytes, bit 6: 1=used compression |
| 2309 |
|
| 2310 |
int c; |
| 2311 |
int offset; |
| 2312 |
int min; |
| 2313 |
int max; |
| 2314 |
const char *map; |
| 2315 |
char *p = text; |
| 2316 |
char *p2; |
| 2317 |
bool all_alpha = true; |
| 2318 |
int pairs_start; |
| 2319 |
int bufix; |
| 2320 |
char buf[N_WORD_BYTES+1]; |
| 2321 |
|
| 2322 |
offset = tr->transpose_min - 1; |
| 2323 |
min = tr->transpose_min; |
| 2324 |
max = tr->transpose_max; |
| 2325 |
map = tr->transpose_map; |
| 2326 |
|
| 2327 |
pairs_start = max - min + 2; |
| 2328 |
|
| 2329 |
bufix = 0; |
| 2330 |
do { |
| 2331 |
p += utf8_in(&c, p); |
| 2332 |
if (c != 0) { |
| 2333 |
if ((c >= min) && (c <= max)) { |
| 2334 |
if (map == NULL) |
| 2335 |
buf[bufix++] = c - offset; |
| 2336 |
else { |
| 2337 |
// get the code from the transpose map |
| 2338 |
if (map[c - min] > 0) |
| 2339 |
buf[bufix++] = map[c - min]; |
| 2340 |
else { |
| 2341 |
all_alpha = false; |
| 2342 |
break; |
| 2343 |
} |
| 2344 |
} |
| 2345 |
} else { |
| 2346 |
all_alpha = false; |
| 2347 |
break; |
| 2348 |
} |
| 2349 |
} |
| 2350 |
} while ((c != 0) && (bufix < N_WORD_BYTES)); |
| 2351 |
buf[bufix] = 0; |
| 2352 |
|
| 2353 |
if (all_alpha) { |
| 2354 |
// compress to 6 bits per character |
| 2355 |
int ix; |
| 2356 |
int acc = 0; |
| 2357 |
int bits = 0; |
| 2358 |
|
| 2359 |
p = buf; |
| 2360 |
p2 = buf; |
| 2361 |
while ((c = *p++) != 0) { |
| 2362 |
const short *pairs_list; |
| 2363 |
if ((pairs_list = tr->frequent_pairs) != NULL) { |
| 2364 |
int c2 = c + (*p << 8); |
| 2365 |
for (ix = 0; c2 >= pairs_list[ix]; ix++) { |
| 2366 |
if (c2 == pairs_list[ix]) { |
| 2367 |
// found an encoding for a 2-character pair |
| 2368 |
c = ix + pairs_start; // 2-character codes start after the single letter codes |
| 2369 |
p++; |
| 2370 |
break; |
| 2371 |
} |
| 2372 |
} |
| 2373 |
} |
| 2374 |
acc = (acc << 6) + (c & 0x3f); |
| 2375 |
bits += 6; |
| 2376 |
|
| 2377 |
if (bits >= 8) { |
| 2378 |
bits -= 8; |
| 2379 |
*p2++ = (acc >> bits); |
| 2380 |
} |
| 2381 |
} |
| 2382 |
if (bits > 0) |
| 2383 |
*p2++ = (acc << (8-bits)); |
| 2384 |
*p2 = 0; |
| 2385 |
ix = p2 - buf; |
| 2386 |
memcpy(text, buf, ix); |
| 2387 |
return ix | 0x40; // bit 6 indicates compressed characters |
| 2388 |
} |
| 2389 |
return strlen(text); |
| 2390 |
} |
| 2391 |
|
| 2392 |
/* Find an entry in the word_dict file for a specified word. |
| 2393 |
Returns NULL if no match, else returns 'word_end' |
| 2394 |
|
| 2395 |
word zero terminated word to match |
| 2396 |
word2 pointer to next word(s) in the input text (terminated by space) |
| 2397 |
|
| 2398 |
flags: returns dictionary flags which are associated with a matched word |
| 2399 |
|
| 2400 |
end_flags: indicates whether this is a retranslation after removing a suffix |
| 2401 |
*/ |
| 2402 |
static const char *LookupDict2(Translator *tr, const char *word, const char *word2, |
| 2403 |
char *phonetic, unsigned int *flags, int end_flags, WORD_TAB *wtab) |
| 2404 |
{ |
| 2405 |
char *p; |
| 2406 |
char *next; |
| 2407 |
int hash; |
| 2408 |
int phoneme_len; |
| 2409 |
int wlen; |
| 2410 |
unsigned char flag; |
| 2411 |
unsigned int dictionary_flags; |
| 2412 |
unsigned int dictionary_flags2; |
| 2413 |
bool condition_failed = false; |
| 2414 |
int n_chars; |
| 2415 |
int no_phonemes; |
| 2416 |
int skipwords; |
| 2417 |
int ix; |
| 2418 |
int c; |
| 2419 |
const char *word_end; |
| 2420 |
const char *word1; |
| 2421 |
int wflags = 0; |
| 2422 |
int lookup_symbol; |
| 2423 |
char word_buf[N_WORD_BYTES+1]; |
| 2424 |
char dict_flags_buf[80]; |
| 2425 |
|
| 2426 |
if (wtab != NULL) |
| 2427 |
wflags = wtab->flags; |
| 2428 |
|
| 2429 |
lookup_symbol = flags[1] & FLAG_LOOKUP_SYMBOL; |
| 2430 |
word1 = word; |
| 2431 |
if (tr->transpose_min > 0) { |
| 2432 |
strncpy0(word_buf, word, N_WORD_BYTES); |
| 2433 |
wlen = TransposeAlphabet(tr, word_buf); // bit 6 indicates compressed characters |
| 2434 |
word = word_buf; |
| 2435 |
} else |
| 2436 |
wlen = strlen(word); |
| 2437 |
|
| 2438 |
hash = HashDictionary(word); |
| 2439 |
p = tr->dict_hashtab[hash]; |
| 2440 |
|
| 2441 |
if (p == NULL) { |
| 2442 |
if (flags != NULL) |
| 2443 |
*flags = 0; |
| 2444 |
return 0; |
| 2445 |
} |
| 2446 |
|
| 2447 |
// Find the first entry in the list for this hash value which matches. |
| 2448 |
// This corresponds to the last matching entry in the *_list file. |
| 2449 |
|
| 2450 |
while (*p != 0) { |
| 2451 |
next = p + (p[0] & 0xff); |
| 2452 |
|
| 2453 |
if (((p[1] & 0x7f) != wlen) || (memcmp(word, &p[2], wlen & 0x3f) != 0)) { |
| 2454 |
// bit 6 of wlen indicates whether the word has been compressed; so we need to match on this also. |
| 2455 |
p = next; |
| 2456 |
continue; |
| 2457 |
} |
| 2458 |
|
| 2459 |
// found matching entry. Decode the phonetic string |
| 2460 |
word_end = word2; |
| 2461 |
|
| 2462 |
dictionary_flags = 0; |
| 2463 |
dictionary_flags2 = 0; |
| 2464 |
no_phonemes = p[1] & 0x80; |
| 2465 |
|
| 2466 |
p += ((p[1] & 0x3f) + 2); |
| 2467 |
|
| 2468 |
if (no_phonemes) { |
| 2469 |
phonetic[0] = 0; |
| 2470 |
phoneme_len = 0; |
| 2471 |
} else { |
| 2472 |
phoneme_len = strlen(p); |
| 2473 |
assert(phoneme_len < N_PHONEME_BYTES); |
| 2474 |
strcpy(phonetic, p); |
| 2475 |
p += (phoneme_len + 1); |
| 2476 |
} |
| 2477 |
|
| 2478 |
while (p < next) { |
| 2479 |
// examine the flags which follow the phoneme string |
| 2480 |
|
| 2481 |
flag = *p++; |
| 2482 |
if (flag >= 100) { |
| 2483 |
// conditional rule |
| 2484 |
if (flag >= 132) { |
| 2485 |
// fail if this condition is set |
| 2486 |
if ((tr->dict_condition & (1 << (flag-132))) != 0) |
| 2487 |
condition_failed = true; |
| 2488 |
} else { |
| 2489 |
// allow only if this condition is set |
| 2490 |
if ((tr->dict_condition & (1 << (flag-100))) == 0) |
| 2491 |
condition_failed = true; |
| 2492 |
} |
| 2493 |
} else if (flag > 80) { |
| 2494 |
// flags 81 to 90 match more than one word |
| 2495 |
// This comes after the other flags |
| 2496 |
n_chars = next - p; |
| 2497 |
skipwords = flag - 80; |
| 2498 |
|
| 2499 |
// don't use the contraction if any of the words are emphasized |
| 2500 |
// or has an embedded command, such as MARK |
| 2501 |
if (wtab != NULL) { |
| 2502 |
for (ix = 0; ix <= skipwords && wtab[ix].length; ix++) { |
| 2503 |
if (wtab[ix].flags & FLAG_EMPHASIZED2) |
| 2504 |
condition_failed = true; |
| 2505 |
|
| 2506 |
} |
| 2507 |
} |
| 2508 |
|
| 2509 |
if (strncmp(word2, p, n_chars) != 0) |
| 2510 |
condition_failed = true; |
| 2511 |
|
| 2512 |
if (condition_failed) { |
| 2513 |
p = next; |
| 2514 |
break; |
| 2515 |
} |
| 2516 |
|
| 2517 |
dictionary_flags |= FLAG_SKIPWORDS; |
| 2518 |
dictionary_skipwords = skipwords; |
| 2519 |
p = next; |
| 2520 |
word_end = word2 + n_chars; |
| 2521 |
} else if (flag > 64) { |
| 2522 |
// stressed syllable information, put in bits 0-3 |
| 2523 |
dictionary_flags = (dictionary_flags & ~0xf) | (flag & 0xf); |
| 2524 |
if ((flag & 0xc) == 0xc) |
| 2525 |
dictionary_flags |= FLAG_STRESS_END; |
| 2526 |
} else if (flag >= 32) |
| 2527 |
dictionary_flags2 |= (1L << (flag-32)); |
| 2528 |
else |
| 2529 |
dictionary_flags |= (1L << flag); |
| 2530 |
} |
| 2531 |
|
| 2532 |
if (condition_failed) { |
| 2533 |
condition_failed = false; |
| 2534 |
continue; |
| 2535 |
} |
| 2536 |
|
| 2537 |
if ((end_flags & FLAG_SUFX) == 0) { |
| 2538 |
// no suffix has been removed |
| 2539 |
if (dictionary_flags2 & FLAG_STEM) |
| 2540 |
continue; // this word must have a suffix |
| 2541 |
} |
| 2542 |
|
| 2543 |
if ((end_flags & SUFX_P) && (dictionary_flags2 & (FLAG_ONLY | FLAG_ONLY_S))) |
| 2544 |
continue; // $only or $onlys, don't match if a prefix has been removed |
| 2545 |
|
| 2546 |
if (end_flags & FLAG_SUFX) { |
| 2547 |
// a suffix was removed from the word |
| 2548 |
if (dictionary_flags2 & FLAG_ONLY) |
| 2549 |
continue; // no match if any suffix |
| 2550 |
|
| 2551 |
if ((dictionary_flags2 & FLAG_ONLY_S) && ((end_flags & FLAG_SUFX_S) == 0)) { |
| 2552 |
// only a 's' suffix allowed, but the suffix wasn't 's' |
| 2553 |
continue; |
| 2554 |
} |
| 2555 |
} |
| 2556 |
|
| 2557 |
if (dictionary_flags2 & FLAG_CAPITAL) { |
| 2558 |
if (!(wflags & FLAG_FIRST_UPPER)) |
| 2559 |
continue; |
| 2560 |
} |
| 2561 |
if (dictionary_flags2 & FLAG_ALLCAPS) { |
| 2562 |
if (!(wflags & FLAG_ALL_UPPER)) |
| 2563 |
continue; |
| 2564 |
} |
| 2565 |
if (dictionary_flags & FLAG_NEEDS_DOT) { |
| 2566 |
if (!(wflags & FLAG_HAS_DOT)) |
| 2567 |
continue; |
| 2568 |
} |
| 2569 |
|
| 2570 |
if ((dictionary_flags2 & FLAG_ATEND) && (word_end < translator->clause_end) && (lookup_symbol == 0)) { |
| 2571 |
// only use this pronunciation if it's the last word of the clause, or called from Lookup() |
| 2572 |
continue; |
| 2573 |
} |
| 2574 |
|
| 2575 |
if ((dictionary_flags2 & FLAG_ATSTART) && !(wflags & FLAG_FIRST_WORD)) { |
| 2576 |
// only use this pronunciation if it's the first word of a clause |
| 2577 |
continue; |
| 2578 |
} |
| 2579 |
|
| 2580 |
if ((dictionary_flags2 & FLAG_SENTENCE) && !(translator->clause_terminator & CLAUSE_TYPE_SENTENCE)) { |
| 2581 |
// only if this clause is a sentence , i.e. terminator is {. ? !} not {, : :} |
| 2582 |
continue; |
| 2583 |
} |
| 2584 |
|
| 2585 |
if (dictionary_flags2 & FLAG_VERB) { |
| 2586 |
// this is a verb-form pronunciation |
| 2587 |
|
| 2588 |
if (tr->expect_verb || (tr->expect_verb_s && (end_flags & FLAG_SUFX_S))) { |
| 2589 |
// OK, we are expecting a verb |
| 2590 |
if ((tr->translator_name == L('e', 'n')) && (tr->prev_dict_flags[0] & FLAG_ALT7_TRANS) && (end_flags & FLAG_SUFX_S)) { |
| 2591 |
// lang=en, don't use verb form after 'to' if the word has 's' suffix |
| 2592 |
continue; |
| 2593 |
} |
| 2594 |
} else { |
| 2595 |
// don't use the 'verb' pronunciation unless we are expecting a verb |
| 2596 |
continue; |
| 2597 |
} |
| 2598 |
} |
| 2599 |
if (dictionary_flags2 & FLAG_PAST) { |
| 2600 |
if (!tr->expect_past) { |
| 2601 |
// don't use the 'past' pronunciation unless we are expecting past tense |
| 2602 |
continue; |
| 2603 |
} |
| 2604 |
} |
| 2605 |
if (dictionary_flags2 & FLAG_NOUN) { |
| 2606 |
if ((!tr->expect_noun) || (end_flags & SUFX_V)) { |
| 2607 |
// don't use the 'noun' pronunciation unless we are expecting a noun |
| 2608 |
continue; |
| 2609 |
} |
| 2610 |
} |
| 2611 |
if (dictionary_flags2 & FLAG_NATIVE) { |
| 2612 |
if (tr != translator) |
| 2613 |
continue; // don't use if we've switched translators |
| 2614 |
} |
| 2615 |
if (dictionary_flags & FLAG_ALT2_TRANS) { |
| 2616 |
// language specific |
| 2617 |
if ((tr->translator_name == L('h', 'u')) && !(tr->prev_dict_flags[0] & FLAG_ALT_TRANS)) |
| 2618 |
continue; |
| 2619 |
} |
| 2620 |
|
| 2621 |
if (flags != NULL) { |
| 2622 |
flags[0] = dictionary_flags | FLAG_FOUND_ATTRIBUTES; |
| 2623 |
flags[1] = dictionary_flags2; |
| 2624 |
} |
| 2625 |
|
| 2626 |
if (phoneme_len == 0) { |
| 2627 |
if (option_phonemes & espeakPHONEMES_TRACE) { |
| 2628 |
print_dictionary_flags(flags, dict_flags_buf, sizeof(dict_flags_buf)); |
| 2629 |
fprintf(f_trans, "Flags: %s %s\n", word1, dict_flags_buf); |
| 2630 |
} |
| 2631 |
return 0; // no phoneme translation found here, only flags. So use rules |
| 2632 |
} |
| 2633 |
|
| 2634 |
if (flags != NULL) |
| 2635 |
flags[0] |= FLAG_FOUND; // this flag indicates word was found in dictionary |
| 2636 |
|
| 2637 |
if (option_phonemes & espeakPHONEMES_TRACE) { |
| 2638 |
char ph_decoded[N_WORD_PHONEMES]; |
| 2639 |
bool textmode; |
| 2640 |
|
| 2641 |
DecodePhonemes(phonetic, ph_decoded); |
| 2642 |
|
| 2643 |
if ((dictionary_flags & FLAG_TEXTMODE) == 0) |
| 2644 |
textmode = false; |
| 2645 |
else |
| 2646 |
textmode = true; |
| 2647 |
|
| 2648 |
if (textmode == translator->langopts.textmode) { |
| 2649 |
// only show this line if the word translates to phonemes, not replacement text |
| 2650 |
if ((dictionary_flags & FLAG_SKIPWORDS) && (wtab != NULL)) { |
| 2651 |
// matched more than one word |
| 2652 |
// (check for wtab prevents showing RULE_SPELLING byte when speaking individual letters) |
| 2653 |
memcpy(word_buf, word2, word_end-word2); |
| 2654 |
word_buf[word_end-word2-1] = 0; |
| 2655 |
fprintf(f_trans, "Found: '%s %s\n", word1, word_buf); |
| 2656 |
} else |
| 2657 |
fprintf(f_trans, "Found: '%s", word1); |
| 2658 |
print_dictionary_flags(flags, dict_flags_buf, sizeof(dict_flags_buf)); |
| 2659 |
fprintf(f_trans, "' [%s] %s\n", ph_decoded, dict_flags_buf); |
| 2660 |
} |
| 2661 |
} |
| 2662 |
|
| 2663 |
ix = utf8_in(&c, word); |
| 2664 |
if (flags != NULL && (word[ix] == 0) && !IsAlpha(c)) |
| 2665 |
flags[0] |= FLAG_MAX3; |
| 2666 |
return word_end; |
| 2667 |
|
| 2668 |
} |
| 2669 |
return 0; |
| 2670 |
} |
| 2671 |
|
| 2672 |
|
| 2673 |
static int utf8_nbytes(const char *buf) |
| 2674 |
{ |
| 2675 |
// Returns the number of bytes for the first UTF-8 character in buf |
| 2676 |
|
| 2677 |
unsigned char c = (unsigned char)buf[0]; |
| 2678 |
if (c < 0x80) |
| 2679 |
return 1; |
| 2680 |
if (c < 0xe0) |
| 2681 |
return 2; |
| 2682 |
if (c < 0xf0) |
| 2683 |
return 3; |
| 2684 |
return 4; |
| 2685 |
} |
| 2686 |
|
| 2687 |
/* Lookup a specified word in the word dictionary. |
| 2688 |
Returns phonetic data in 'phonetic' and bits in 'flags' |
| 2689 |
|
| 2690 |
end_flags: indicates if a suffix has been removed |
| 2691 |
*/ |
| 2692 |
int LookupDictList(Translator *tr, char **wordptr, char *ph_out, unsigned int *flags, int end_flags, WORD_TAB *wtab) |
| 2693 |
{ |
| 2694 |
int length; |
| 2695 |
const char *found; |
| 2696 |
const char *word1; |
| 2697 |
const char *word2; |
| 2698 |
unsigned char c; |
| 2699 |
int nbytes; |
| 2700 |
int len; |
| 2701 |
char word[N_WORD_BYTES]; |
| 2702 |
static char word_replacement[N_WORD_BYTES]; |
| 2703 |
|
| 2704 |
MAKE_MEM_UNDEFINED(&word_replacement, sizeof(word_replacement)); |
| 2705 |
|
| 2706 |
length = 0; |
| 2707 |
word2 = word1 = *wordptr; |
| 2708 |
|
| 2709 |
while ((word2[nbytes = utf8_nbytes(word2)] == ' ') && (word2[nbytes+1] == '.')) { |
| 2710 |
// look for an abbreviation of the form a.b.c |
| 2711 |
// try removing the spaces between the dots and looking for a match |
| 2712 |
if (length + 1 > sizeof(word)) { |
| 2713 |
/* Too long abbreviation, leave as it is */ |
| 2714 |
length = 0; |
| 2715 |
break; |
| 2716 |
} |
| 2717 |
memcpy(&word[length], word2, nbytes); |
| 2718 |
length += nbytes; |
| 2719 |
word[length++] = '.'; |
| 2720 |
word2 += nbytes+3; |
| 2721 |
} |
| 2722 |
if (length > 0) { |
| 2723 |
// found an abbreviation containing dots |
| 2724 |
nbytes = 0; |
| 2725 |
while (((c = word2[nbytes]) != 0) && (c != ' ')) |
| 2726 |
nbytes++; |
| 2727 |
if (length + nbytes + 1 <= sizeof(word)) { |
| 2728 |
memcpy(&word[length], word2, nbytes); |
| 2729 |
word[length+nbytes] = 0; |
| 2730 |
found = LookupDict2(tr, word, word2, ph_out, flags, end_flags, wtab); |
| 2731 |
if (found) { |
| 2732 |
// set the skip words flag |
| 2733 |
flags[0] |= FLAG_SKIPWORDS; |
| 2734 |
dictionary_skipwords = length; |
| 2735 |
return 1; |
| 2736 |
} |
| 2737 |
} |
| 2738 |
} |
| 2739 |
|
| 2740 |
for (length = 0; length < (N_WORD_BYTES-1); length++) { |
| 2741 |
if (((c = *word1++) == 0) || (c == ' ')) |
| 2742 |
break; |
| 2743 |
|
| 2744 |
if ((c == '.') && (length > 0) && (IsDigit09(word[length-1]))) |
| 2745 |
break; // needed for lang=hu, eg. "december 2.-ig" |
| 2746 |
|
| 2747 |
word[length] = c; |
| 2748 |
} |
| 2749 |
word[length] = 0; |
| 2750 |
|
| 2751 |
found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab); |
| 2752 |
|
| 2753 |
if (flags[0] & FLAG_MAX3) { |
| 2754 |
if (strcmp(ph_out, tr->phonemes_repeat) == 0) { |
| 2755 |
tr->phonemes_repeat_count++; |
| 2756 |
if (tr->phonemes_repeat_count > 3) |
| 2757 |
ph_out[0] = 0; |
| 2758 |
} else { |
| 2759 |
strncpy0(tr->phonemes_repeat, ph_out, sizeof(tr->phonemes_repeat)); |
| 2760 |
tr->phonemes_repeat_count = 1; |
| 2761 |
} |
| 2762 |
} else |
| 2763 |
tr->phonemes_repeat_count = 0; |
| 2764 |
|
| 2765 |
if ((found == 0) && (flags[1] & FLAG_ACCENT)) { |
| 2766 |
int letter; |
| 2767 |
word2 = word; |
| 2768 |
if (*word2 == '_') word2++; |
| 2769 |
len = utf8_in(&letter, word2); |
| 2770 |
LookupAccentedLetter(tr, letter, ph_out); |
| 2771 |
found = word2 + len; |
| 2772 |
} |
| 2773 |
|
| 2774 |
if (found == 0 && length >= 2) { |
| 2775 |
ph_out[0] = 0; |
| 2776 |
|
| 2777 |
// try modifications to find a recognised word |
| 2778 |
|
| 2779 |
if ((end_flags & FLAG_SUFX_E_ADDED) && (word[length-1] == 'e')) { |
| 2780 |
// try removing an 'e' which has been added by RemoveEnding |
| 2781 |
word[length-1] = 0; |
| 2782 |
found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab); |
| 2783 |
} else if ((end_flags & SUFX_D) && (word[length-1] == word[length-2])) { |
| 2784 |
// try removing a double letter |
| 2785 |
word[length-1] = 0; |
| 2786 |
found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab); |
| 2787 |
} |
| 2788 |
} |
| 2789 |
|
| 2790 |
if (found) { |
| 2791 |
// if textmode is the default, then words which have phonemes are marked. |
| 2792 |
if (tr->langopts.textmode) |
| 2793 |
*flags ^= FLAG_TEXTMODE; |
| 2794 |
|
| 2795 |
if (*flags & FLAG_TEXTMODE) { |
| 2796 |
// the word translates to replacement text, not to phonemes |
| 2797 |
|
| 2798 |
if (end_flags & FLAG_ALLOW_TEXTMODE) { |
| 2799 |
// only use replacement text if this is the original word, not if a prefix or suffix has been removed |
| 2800 |
word_replacement[0] = 0; |
| 2801 |
word_replacement[1] = ' '; |
| 2802 |
sprintf(&word_replacement[2], "%s ", ph_out); // replacement word, preceded by zerochar and space |
| 2803 |
|
| 2804 |
word1 = *wordptr; |
| 2805 |
*wordptr = &word_replacement[2]; |
| 2806 |
|
| 2807 |
if (option_phonemes & espeakPHONEMES_TRACE) { |
| 2808 |
len = found - word1; |
| 2809 |
memcpy(word, word1, len); // include multiple matching words |
| 2810 |
word[len] = 0; |
| 2811 |
fprintf(f_trans, "Replace: %s %s\n", word, *wordptr); |
| 2812 |
} |
| 2813 |
} |
| 2814 |
|
| 2815 |
ph_out[0] = 0; |
| 2816 |
return 0; |
| 2817 |
} |
| 2818 |
|
| 2819 |
return 1; |
| 2820 |
} |
| 2821 |
|
| 2822 |
ph_out[0] = 0; |
| 2823 |
return 0; |
| 2824 |
} |
| 2825 |
|
| 2826 |
extern char word_phonemes[N_WORD_PHONEMES]; // a word translated into phoneme codes |
| 2827 |
|
| 2828 |
int Lookup(Translator *tr, const char *word, char *ph_out) |
| 2829 |
{ |
| 2830 |
// Look up in *_list, returns dictionary flags[0] and phonemes |
| 2831 |
|
| 2832 |
int flags0; |
| 2833 |
unsigned int flags[2]; |
| 2834 |
char *word1 = (char *)word; |
| 2835 |
|
| 2836 |
flags[0] = 0; |
| 2837 |
flags[1] = FLAG_LOOKUP_SYMBOL; |
| 2838 |
if ((flags0 = LookupDictList(tr, &word1, ph_out, flags, FLAG_ALLOW_TEXTMODE, NULL)) != 0) |
| 2839 |
flags0 = flags[0]; |
| 2840 |
|
| 2841 |
if (flags[0] & FLAG_TEXTMODE) { |
| 2842 |
int say_as = option_sayas; |
| 2843 |
option_sayas = 0; // don't speak replacement word as letter names |
| 2844 |
// NOTE: TranslateRoman checks text[-2] and IsLetterGroup looks |
| 2845 |
// for a heading \0, so pad the start of text to prevent |
| 2846 |
// it reading data on the stack. |
| 2847 |
char text[80]; |
| 2848 |
|
| 2849 |
text[0] = 0; |
| 2850 |
text[1] = ' '; |
| 2851 |
text[2] = ' '; |
| 2852 |
strncpy0(text+3, word1, sizeof(text)-3); |
| 2853 |
flags0 = TranslateWord(tr, text+3, NULL, NULL); |
| 2854 |
strcpy(ph_out, word_phonemes); |
| 2855 |
option_sayas = say_as; |
| 2856 |
} |
| 2857 |
return flags0; |
| 2858 |
} |
| 2859 |
|
| 2860 |
static int LookupFlags(Translator *tr, const char *word, unsigned int flags_out[2]) |
| 2861 |
{ |
| 2862 |
char buf[100]; |
| 2863 |
static unsigned int flags[2]; |
| 2864 |
char *word1 = (char *)word; |
| 2865 |
|
| 2866 |
flags[0] = flags[1] = 0; |
| 2867 |
LookupDictList(tr, &word1, buf, flags, 0, NULL); |
| 2868 |
flags_out[0] = flags[0]; |
| 2869 |
flags_out[1] = flags[1]; |
| 2870 |
return flags[0]; |
| 2871 |
} |
| 2872 |
|
| 2873 |
int RemoveEnding(Translator *tr, char *word, int end_type, char *word_copy) |
| 2874 |
{ |
| 2875 |
/* Removes a standard suffix from a word, once it has been indicated by the dictionary rules. |
| 2876 |
end_type: bits 0-6 number of letters |
| 2877 |
bits 8-14 suffix flags |
| 2878 |
|
| 2879 |
word_copy: make a copy of the original word |
| 2880 |
This routine is language specific. In English it deals with reversing y->i and e-dropping |
| 2881 |
that were done when the suffix was added to the original word. |
| 2882 |
*/ |
| 2883 |
|
| 2884 |
int i; |
| 2885 |
char *word_end; |
| 2886 |
int len_ending; |
| 2887 |
int end_flags; |
| 2888 |
char ending[50] = {0}; |
| 2889 |
|
| 2890 |
// these lists are language specific, but are only relevant if the 'e' suffix flag is used |
| 2891 |
static const char * const add_e_exceptions[] = { |
| 2892 |
"ion", NULL |
| 2893 |
}; |
| 2894 |
|
| 2895 |
static const char * const add_e_additions[] = { |
| 2896 |
"c", "rs", "ir", "ur", "ath", "ns", "u", |
| 2897 |
"spong", // sponge |
| 2898 |
"rang", // strange |
| 2899 |
"larg", // large |
| 2900 |
NULL |
| 2901 |
}; |
| 2902 |
|
| 2903 |
for (word_end = word; *word_end != ' '; word_end++) { |
| 2904 |
// replace discarded 'e's |
| 2905 |
if (*word_end == REPLACED_E) |
| 2906 |
*word_end = 'e'; |
| 2907 |
} |
| 2908 |
i = word_end - word; |
| 2909 |
if (i >= N_WORD_BYTES) i = N_WORD_BYTES-1; |
| 2910 |
|
| 2911 |
if (word_copy != NULL) { |
| 2912 |
memcpy(word_copy, word, i); |
| 2913 |
word_copy[i] = 0; |
| 2914 |
} |
| 2915 |
|
| 2916 |
// look for multibyte characters to increase the number of bytes to remove |
| 2917 |
for (len_ending = i = (end_type & 0x3f); i > 0; i--) { // num.of characters of the suffix |
| 2918 |
word_end--; |
| 2919 |
while (word_end >= word && (*word_end & 0xc0) == 0x80) { |
| 2920 |
word_end--; // for multibyte characters |
| 2921 |
len_ending++; |
| 2922 |
} |
| 2923 |
} |
| 2924 |
|
| 2925 |
// remove bytes from the end of the word and replace them by spaces |
| 2926 |
for (i = 0; (i < len_ending) && (i < (int)sizeof(ending)-1); i++) { |
| 2927 |
ending[i] = word_end[i]; |
| 2928 |
word_end[i] = ' '; |
| 2929 |
} |
| 2930 |
ending[i] = 0; |
| 2931 |
word_end--; // now pointing at last character of stem |
| 2932 |
|
| 2933 |
end_flags = (end_type & 0xfff0) | FLAG_SUFX; |
| 2934 |
|
| 2935 |
/* add an 'e' to the stem if appropriate, |
| 2936 |
if stem ends in vowel+consonant |
| 2937 |
or stem ends in 'c' (add 'e' to soften it) */ |
| 2938 |
|
| 2939 |
if (end_type & SUFX_I) { |
| 2940 |
if (word_end[0] == 'i') |
| 2941 |
word_end[0] = 'y'; |
| 2942 |
} |
| 2943 |
|
| 2944 |
if (end_type & SUFX_E) { |
| 2945 |
if (tr->translator_name == L('n', 'l')) { |
| 2946 |
if (((word_end[0] & 0x80) == 0) && ((word_end[-1] & 0x80) == 0) && IsVowel(tr, word_end[-1]) && IsLetter(tr, word_end[0], LETTERGP_C) && !IsVowel(tr, word_end[-2])) { |
| 2947 |
// double the vowel before the (ascii) final consonant |
| 2948 |
word_end[1] = word_end[0]; |
| 2949 |
word_end[0] = word_end[-1]; |
| 2950 |
word_end[2] = ' '; |
| 2951 |
} |
| 2952 |
} else if (tr->translator_name == L('e', 'n')) { |
| 2953 |
// add 'e' to end of stem |
| 2954 |
if (IsLetter(tr, word_end[-1], LETTERGP_VOWEL2) && IsLetter(tr, word_end[0], 1)) { |
| 2955 |
// vowel(incl.'y') + hard.consonant |
| 2956 |
|
| 2957 |
const char *p; |
| 2958 |
for (i = 0; (p = add_e_exceptions[i]) != NULL; i++) { |
| 2959 |
int len = strlen(p); |
| 2960 |
if (memcmp(p, &word_end[1-len], len) == 0) |
| 2961 |
break; |
| 2962 |
} |
| 2963 |
if (p == NULL) |
| 2964 |
end_flags |= FLAG_SUFX_E_ADDED; // no exception found |
| 2965 |
} else { |
| 2966 |
const char *p; |
| 2967 |
for (i = 0; (p = add_e_additions[i]) != NULL; i++) { |
| 2968 |
int len = strlen(p); |
| 2969 |
if (memcmp(p, &word_end[1-len], len) == 0) { |
| 2970 |
end_flags |= FLAG_SUFX_E_ADDED; |
| 2971 |
break; |
| 2972 |
} |
| 2973 |
} |
| 2974 |
} |
| 2975 |
} else if (tr->langopts.suffix_add_e != 0) |
| 2976 |
end_flags |= FLAG_SUFX_E_ADDED; |
| 2977 |
|
| 2978 |
if (end_flags & FLAG_SUFX_E_ADDED) { |
| 2979 |
utf8_out(tr->langopts.suffix_add_e, &word_end[1]); |
| 2980 |
|
| 2981 |
if (option_phonemes & espeakPHONEMES_TRACE) |
| 2982 |
fprintf(f_trans, "add e\n"); |
| 2983 |
} |
| 2984 |
} |
| 2985 |
|
| 2986 |
if ((end_type & SUFX_V) && (tr->expect_verb == 0)) |
| 2987 |
tr->expect_verb = 1; // this suffix indicates the verb pronunciation |
| 2988 |
|
| 2989 |
|
| 2990 |
if ((strcmp(ending, "s") == 0) || (strcmp(ending, "es") == 0)) |
| 2991 |
end_flags |= FLAG_SUFX_S; |
| 2992 |
|
| 2993 |
if (ending[0] == '\'') |
| 2994 |
end_flags &= ~FLAG_SUFX; // don't consider 's as an added suffix |
| 2995 |
|
| 2996 |
return end_flags; |
| 2997 |
} |
| 2998 |
|
| 2999 |
static void DollarRule(char *word[], char *word_start, int consumed, int group_length, char *word_buf, Translator *tr, int command, int *failed, int *add_points) { |
| 3000 |
// $list or $p_alt |
| 3001 |
// make a copy of the word up to the post-match characters |
| 3002 |
int ix = *word - word_start + consumed + group_length + 1; |
| 3003 |
memcpy(word_buf, word_start-1, ix); |
| 3004 |
word_buf[ix] = ' '; |
| 3005 |
word_buf[ix+1] = 0; |
| 3006 |
unsigned int flags[2]; |
| 3007 |
LookupFlags(tr, &word_buf[1], flags); |
| 3008 |
|
| 3009 |
if ((command == DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY)) |
| 3010 |
*add_points = 23; |
| 3011 |
else if (flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf)))) |
| 3012 |
*add_points = 23; |
| 3013 |
else |
| 3014 |
*failed = 1; |
| 3015 |
} |