Debian Codegraph @

external/espeak/translateword.cpp

raw symbols SCIP
1
2 /*
3 * Copyright (C) 2005 to 2014 by Jonathan Duddington
4 * email: [email protected]
5 * Copyright (C) 2015-2017 Reece H. Dunn
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see: <http://www.gnu.org/licenses/>.
19 */
20
21 #include "espeak__config.h"
22
23 #include <ctype.h>
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <wchar.h>
30 #include "wctype_portable.h"
31
32 #include "espeak_ng.h"
33 #include "speak_lib.h"
34 #include "encoding.h"
35
36 #include "translate.h"
37 #include "translateword.h"
38 #include "common.h" // for strncpy0
39 #include "dictionary.h" // for TranslateRules, LookupDictList
40 #include "numbers.h" // for SetSpellingStress, ...
41 #include "phoneme.h" // for phonSWITCH, PHONEME_TAB, phonPAUSE_...
42 #include "readclause.h" // for towlower2
43 #include "synthdata.h" // for SelectPhonemeTable, LookupPhonemeTable
44 #include "ucd.h" // for ucd_toupper
45 #include "voice.h" // for voice, voice_t
46
47
48 static void addPluralSuffixes(int flags, Translator *tr, char last_char, char *word_phonemes);
49 static void ApplySpecialAttribute2(Translator *tr, char *phonemes, int dict_flags);
50 static void ChangeWordStress(Translator *tr, char *word, int new_stress);
51 static int CheckDottedAbbrev(char *word1);
52 static int NonAsciiNumber(int letter);
53 static char *SpeakIndividualLetters(Translator *tr, char *word, char *phonemes, int spell_word, const ALPHABET *current_alphabet, char word_phonemes[]);
54 static int TranslateLetter(Translator *tr, char *word, char *phonemes, int control, const ALPHABET *current_alphabet);
55 static int Unpronouncable(Translator *tr, char *word, int posn);
56 static int Unpronouncable2(Translator *tr, char *word);
57
58 int TranslateWord3(Translator *tr, char *word_start, WORD_TAB *wtab, char *word_out, bool *any_stressed_words, ALPHABET *current_alphabet, char word_phonemes[], size_t size_word_phonemes)
59 {
60 // word1 is terminated by space (0x20) character
61
62 char *word1;
63 int word_length;
64 int ix;
65 char *p;
66 int pfix;
67 int n_chars;
68 unsigned int dictionary_flags[2];
69 unsigned int dictionary_flags2[2];
70 int end_type = 0;
71 int end_type1 = 0;
72 int prefix_type = 0;
73 int prefix_stress;
74 char *wordx;
75 char phonemes[N_WORD_PHONEMES];
76 char phonemes2[N_WORD_PHONEMES];
77 char prefix_phonemes[N_WORD_PHONEMES];
78 char unpron_phonemes[N_WORD_PHONEMES];
79 char end_phonemes[N_WORD_PHONEMES];
80 char end_phonemes2[N_WORD_PHONEMES];
81 char word_copy[N_WORD_BYTES];
82 char word_copy2[N_WORD_BYTES];
83 int word_copy_length;
84 char prefix_chars[0x3f + 2];
85 bool found = false;
86 int end_flags;
87 int c_temp; // save a character byte while we temporarily replace it with space
88 int first_char;
89 int last_char = 0;
90 int prefix_flags = 0;
91 bool more_suffixes;
92 bool confirm_prefix;
93 int spell_word;
94 int emphasize_allcaps = 0;
95 int wflags;
96 int was_unpronouncable = 0;
97 int loopcount;
98 int add_suffix_phonemes = 0;
99 WORD_TAB wtab_null[8];
100
101 if (wtab == NULL) {
102 memset(wtab_null, 0, sizeof(wtab_null));
103 wtab = wtab_null;
104 }
105 wflags = wtab->flags;
106
107 dictionary_flags[0] = 0;
108 dictionary_flags[1] = 0;
109 dictionary_flags2[0] = 0;
110 dictionary_flags2[1] = 0;
111 dictionary_skipwords = 0;
112
113 phonemes[0] = 0;
114 unpron_phonemes[0] = 0;
115 prefix_phonemes[0] = 0;
116 end_phonemes[0] = 0;
117
118 if (tr->data_dictlist == NULL) {
119 // dictionary is not loaded
120 word_phonemes[0] = 0;
121 return 0;
122 }
123
124 // count the length of the word
125 word1 = word_start;
126 if (*word1 == ' ') word1++; // possibly a dot was replaced by space: $dot
127 wordx = word1;
128
129 utf8_in(&first_char, wordx);
130 word_length = 0;
131 while ((*wordx != 0) && (*wordx != ' ')) {
132 wordx += utf8_in(&last_char, wordx);
133 word_length++;
134 }
135
136 word_copy_length = wordx - word_start;
137 if (word_copy_length >= N_WORD_BYTES)
138 word_copy_length = N_WORD_BYTES-1;
139 memcpy(word_copy2, word_start, word_copy_length);
140
141 spell_word = 0;
142
143 if ((word_length == 1) && (wflags & FLAG_TRANSLATOR2)) {
144 // retranslating a 1-character word using a different language, say its name
145 utf8_in(&c_temp, wordx+1); // the next character
146 if (!IsAlpha(c_temp) || (AlphabetFromChar(last_char) != AlphabetFromChar(c_temp)))
147 spell_word = 1;
148 }
149
150 if (option_sayas == SAYAS_KEY) {
151 if (word_length == 1)
152 spell_word = 4;
153 else {
154 // is there a translation for this keyname ?
155 word1--;
156 *word1 = '_'; // prefix keyname with '_'
157 found = LookupDictList(tr, &word1, phonemes, dictionary_flags, 0, wtab);
158 }
159 }
160
161 // try an initial lookup in the dictionary list, we may find a pronunciation specified, or
162 // we may just find some flags
163 if (option_sayas & 0x10) {
164 // SAYAS_CHAR, SAYAS_GYLPH, or SAYAS_SINGLE_CHAR
165 spell_word = option_sayas & 0xf; // 2,3,4
166 } else {
167 if (!found)
168 found = LookupDictList(tr, &word1, phonemes, dictionary_flags, FLAG_ALLOW_TEXTMODE, wtab); // the original word
169
170 if ((dictionary_flags[0] & (FLAG_ALLOW_DOT | FLAG_NEEDS_DOT)) && (wordx[1] == '.'))
171 wordx[1] = ' '; // remove a Dot after this word
172
173 if (dictionary_flags[0] & FLAG_TEXTMODE) {
174 if (word_out != NULL)
175 strcpy(word_out, word1);
176
177 return dictionary_flags[0];
178 } else if ((found == false) && (dictionary_flags[0] & FLAG_SKIPWORDS) && !(dictionary_flags[0] & FLAG_ABBREV)) {
179 // grouped words, but no translation. Join the words with hyphens.
180 wordx = word1;
181 ix = 0;
182 while (ix < dictionary_skipwords) {
183 if (*wordx == ' ') {
184 *wordx = '-';
185 ix++;
186 }
187 wordx++;
188 }
189 }
190
191 if ((word_length == 1) && (dictionary_skipwords == 0)) {
192 // is this a series of single letters separated by dots?
193 if (CheckDottedAbbrev(word1)) {
194 dictionary_flags[0] = 0;
195 dictionary_flags[1] = 0;
196 spell_word = 1;
197 if (dictionary_skipwords)
198 dictionary_flags[0] = FLAG_SKIPWORDS;
199 }
200 }
201
202 if (phonemes[0] == phonSWITCH) {
203 // change to another language in order to translate this word
204 strcpy(word_phonemes, phonemes);
205 return 0;
206 }
207
208 if (!found && (dictionary_flags[0] & FLAG_ABBREV)) {
209 // the word has $abbrev flag, but no pronunciation specified. Speak as individual letters
210 spell_word = 1;
211 }
212
213 if (!found && iswdigit_portable(first_char)) {
214 Lookup(tr, "_0lang", word_phonemes);
215 if (word_phonemes[0] == phonSWITCH)
216 return 0;
217
218 if ((tr->langopts.numbers2 & NUM2_ENGLISH_NUMERALS) && !(wtab->flags & FLAG_CHAR_REPLACED)) {
219 // for this language, speak English numerals (0-9) with the English voice
220 sprintf(word_phonemes, "%c", phonSWITCH);
221 return 0;
222 }
223
224 found = TranslateNumber(tr, word1, phonemes, dictionary_flags, wtab, 0);
225 }
226
227 if (!found && ((wflags & FLAG_UPPERS) != FLAG_FIRST_UPPER)) {
228 // either all upper or all lower case
229
230 if ((tr->langopts.numbers & NUM_ROMAN) || ((tr->langopts.numbers & NUM_ROMAN_CAPITALS) && (wflags & FLAG_ALL_UPPER))) {
231 if ((wflags & FLAG_LAST_WORD) || !(wtab[1].flags & FLAG_NOSPACE)) {
232 // don't use Roman number if this word is not separated from the next word (eg. "XLTest")
233 if ((found = TranslateRoman(tr, word1, phonemes, wtab)) != 0)
234 dictionary_flags[0] |= FLAG_ABBREV; // prevent emphasis if capitals
235 }
236 }
237 }
238
239 if ((wflags & FLAG_ALL_UPPER) && (word_length > 1) && iswalpha_portable(first_char)) {
240 if ((option_tone_flags & OPTION_EMPHASIZE_ALLCAPS) && !(dictionary_flags[0] & FLAG_ABBREV)) {
241 // emphasize words which are in capitals
242 emphasize_allcaps = FLAG_EMPHASIZED;
243 } else if (!found && !(dictionary_flags[0] & FLAG_SKIPWORDS) && (word_length < 4) && (tr->clause_lower_count > 3)
244 && (tr->clause_upper_count <= tr->clause_lower_count)) {
245 // An upper case word in a lower case clause. This could be an abbreviation.
246 spell_word = 1;
247 }
248 }
249 }
250
251 if (spell_word > 0) {
252 // Speak as individual letters
253 phonemes[0] = 0;
254
255 if (SpeakIndividualLetters(tr, word1, phonemes, spell_word, current_alphabet, word_phonemes) == NULL) {
256 if (word_length > 1)
257 return FLAG_SPELLWORD; // a mixture of languages, retranslate as individual letters, separated by spaces
258 return 0;
259 }
260 strcpy(word_phonemes, phonemes);
261 if (wflags & FLAG_TRANSLATOR2)
262 return 0;
263
264 addPluralSuffixes(wflags, tr, last_char, word_phonemes);
265 return dictionary_flags[0] & FLAG_SKIPWORDS; // for "b.c.d"
266 } else if (found == false) {
267 // word's pronunciation is not given in the dictionary list, although
268 // dictionary_flags may have ben set there
269
270 int posn;
271 bool non_initial = false;
272 int length;
273
274 posn = 0;
275 length = 999;
276 wordx = word1;
277
278 while (((length < 3) && (length > 0)) || (word_length > 1 && Unpronouncable(tr, wordx, posn))) {
279 // This word looks "unpronouncable", so speak letters individually until we
280 // find a remainder that we can pronounce.
281 was_unpronouncable = FLAG_WAS_UNPRONOUNCABLE;
282 emphasize_allcaps = 0;
283
284 if (wordx[0] == '\'')
285 break;
286
287 if (posn > 0)
288 non_initial = true;
289
290 wordx += TranslateLetter(tr, wordx, unpron_phonemes, non_initial, current_alphabet);
291 posn++;
292 if (unpron_phonemes[0] == phonSWITCH) {
293 // change to another language in order to translate this word
294 strcpy(word_phonemes, unpron_phonemes);
295 if (strcmp(&unpron_phonemes[1], ESPEAKNG_DEFAULT_VOICE) == 0)
296 return FLAG_SPELLWORD; // _^_en must have been set in TranslateLetter(), not *_rules which uses only _^_
297 return 0;
298 }
299
300 length = 0;
301 while (wordx[length] != ' ') length++;
302 }
303 SetSpellingStress(tr, unpron_phonemes, 0, posn);
304
305 // anything left ?
306 if (*wordx != ' ') {
307 if ((unpron_phonemes[0] != 0) && (wordx[0] != '\'')) {
308 // letters which have been spoken individually from affecting the pronunciation of the pronuncable part
309 wordx[-1] = ' ';
310 }
311
312 // Translate the stem
313 end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags, dictionary_flags);
314
315 if (phonemes[0] == phonSWITCH) {
316 // change to another language in order to translate this word
317 strcpy(word_phonemes, phonemes);
318 return 0;
319 }
320
321 if ((phonemes[0] == 0) && (end_phonemes[0] == 0)) {
322 int wc;
323 // characters not recognised, speak them individually
324 // ?? should we say super/sub-script numbers and letters here?
325 utf8_in(&wc, wordx);
326 if ((word_length == 1) && (IsAlpha(wc) || IsSuperscript(wc))) {
327 if ((wordx = SpeakIndividualLetters(tr, wordx, phonemes, spell_word, current_alphabet, word_phonemes)) == NULL)
328 return 0;
329 strcpy(word_phonemes, phonemes);
330 return 0;
331 }
332 }
333
334 c_temp = wordx[-1];
335
336 found = false;
337 confirm_prefix = true;
338 for (loopcount = 0; (loopcount < 50) && (end_type & SUFX_P); loopcount++) {
339 // Found a standard prefix, remove it and retranslate
340 // loopcount guards against an endless loop
341 if (confirm_prefix && !(end_type & SUFX_B)) {
342 int end2;
343 char end_phonemes2l[N_WORD_PHONEMES];
344
345 // remove any standard suffix and confirm that the prefix is still recognised
346 phonemes2[0] = 0;
347 end2 = TranslateRules(tr, wordx, phonemes2, N_WORD_PHONEMES, end_phonemes2l, wflags|FLAG_NO_PREFIX|FLAG_NO_TRACE, dictionary_flags);
348 if (end2) {
349 RemoveEnding(tr, wordx, end2, word_copy);
350 end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags|FLAG_NO_TRACE, dictionary_flags);
351 memcpy(wordx, word_copy, strlen(word_copy));
352 if ((end_type & SUFX_P) == 0) {
353 // after removing the suffix, the prefix is no longer recognised.
354 // Keep the suffix, but don't use the prefix
355 end_type = end2;
356 strcpy(phonemes, phonemes2);
357 strcpy(end_phonemes, end_phonemes2l);
358 if (option_phonemes & espeakPHONEMES_TRACE) {
359 DecodePhonemes(end_phonemes, end_phonemes2l);
360 fprintf(f_trans, " suffix [%s]\n\n", end_phonemes2l);
361 }
362 }
363 confirm_prefix = false;
364 continue;
365 }
366 }
367
368 prefix_type = end_type;
369
370 if (prefix_type & SUFX_V)
371 tr->expect_verb = 1; // use the verb form of the word
372
373 wordx[-1] = c_temp;
374
375 if ((prefix_type & SUFX_B) == 0) {
376 for (ix = (prefix_type & 0xf); ix > 0; ix--) { // num. of characters to remove
377 wordx++;
378 while ((*wordx & 0xc0) == 0x80) wordx++; // for multibyte characters
379 }
380 } else {
381 pfix = 1;
382 prefix_chars[0] = 0;
383 n_chars = prefix_type & 0x3f;
384
385 for (ix = 0; ix < n_chars; ix++) { // num. of bytes to remove
386 prefix_chars[pfix++] = *wordx++;
387
388 if ((prefix_type & SUFX_B) && (ix == (n_chars-1)))
389 prefix_chars[pfix-1] = 0; // discard the last character of the prefix, this is the separator character
390 }
391 prefix_chars[pfix] = 0;
392 }
393 c_temp = wordx[-1];
394 wordx[-1] = ' ';
395 confirm_prefix = true;
396 wflags |= FLAG_PREFIX_REMOVED;
397
398 if (prefix_type & SUFX_B) {
399 // SUFX_B is used for Turkish, tr_rules contains " ' (Pb"
400 // examine the prefix part
401 char *wordpf;
402 char prefix_phonemes2[12];
403
404 strncpy0(prefix_phonemes2, end_phonemes, sizeof(prefix_phonemes2));
405 wordpf = &prefix_chars[1];
406 strcpy(prefix_phonemes, phonemes);
407
408 // look for stress marker or $abbrev
409 found = LookupDictList(tr, &wordpf, phonemes, dictionary_flags, 0, wtab);
410 if (found)
411 strcpy(prefix_phonemes, phonemes);
412 if (dictionary_flags[0] & FLAG_ABBREV) {
413 prefix_phonemes[0] = 0;
414 SpeakIndividualLetters(tr, wordpf, prefix_phonemes, 1, current_alphabet, word_phonemes);
415 }
416 } else
417 strcat(prefix_phonemes, end_phonemes);
418 end_phonemes[0] = 0;
419
420 end_type = 0;
421 found = LookupDictList(tr, &wordx, phonemes, dictionary_flags2, SUFX_P, wtab); // without prefix
422 if (dictionary_flags[0] == 0) {
423 dictionary_flags[0] = dictionary_flags2[0];
424 dictionary_flags[1] = dictionary_flags2[1];
425 } else
426 prefix_flags = 1;
427 if (found == false) {
428 end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags & (FLAG_HYPHEN_AFTER | FLAG_PREFIX_REMOVED), dictionary_flags);
429
430 if (phonemes[0] == phonSWITCH) {
431 // change to another language in order to translate this word
432 wordx[-1] = c_temp;
433 strcpy(word_phonemes, phonemes);
434 return 0;
435 }
436 }
437 }
438
439 if ((end_type != 0) && !(end_type & SUFX_P)) {
440 end_type1 = end_type;
441 strcpy(phonemes2, phonemes);
442
443 // The word has a standard ending, re-translate without this ending
444 end_flags = RemoveEnding(tr, wordx, end_type, word_copy);
445 more_suffixes = true;
446
447 while (more_suffixes) {
448 more_suffixes = false;
449 phonemes[0] = 0;
450
451 if (prefix_phonemes[0] != 0) {
452 // lookup the stem without the prefix removed
453 wordx[-1] = c_temp;
454 found = LookupDictList(tr, &word1, phonemes, dictionary_flags2, end_flags, wtab); // include prefix, but not suffix
455 wordx[-1] = ' ';
456 if (phonemes[0] == phonSWITCH) {
457 // change to another language in order to translate this word
458 memcpy(wordx, word_copy, strlen(word_copy));
459 strcpy(word_phonemes, phonemes);
460 return 0;
461 }
462 if (dictionary_flags[0] == 0) {
463 dictionary_flags[0] = dictionary_flags2[0];
464 dictionary_flags[1] = dictionary_flags2[1];
465 }
466 if (found)
467 prefix_phonemes[0] = 0; // matched whole word, don't need prefix now
468
469 if ((found == false) && (dictionary_flags2[0] != 0))
470 prefix_flags = 1;
471 }
472 if (found == false) {
473 found = LookupDictList(tr, &wordx, phonemes, dictionary_flags2, end_flags, wtab); // without prefix and suffix
474 if (phonemes[0] == phonSWITCH) {
475 // change to another language in order to translate this word
476 memcpy(wordx, word_copy, strlen(word_copy));
477 strcpy(word_phonemes, phonemes);
478 return 0;
479 }
480
481 if (dictionary_flags[0] == 0) {
482 dictionary_flags[0] = dictionary_flags2[0];
483 dictionary_flags[1] = dictionary_flags2[1];
484 }
485 }
486 if (found == false) {
487 if (end_type & SUFX_Q) {
488 // don't retranslate, use the original lookup result
489 strcpy(phonemes, phonemes2);
490 } else {
491 if (end_flags & FLAG_SUFX)
492 wflags |= FLAG_SUFFIX_REMOVED;
493 if (end_type & SUFX_A)
494 wflags |= FLAG_SUFFIX_VOWEL;
495
496 if (end_type & SUFX_M) {
497 // allow more suffixes before this suffix
498 strcpy(end_phonemes2, end_phonemes);
499 end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags, dictionary_flags);
500 strcat(end_phonemes, end_phonemes2); // add the phonemes for the previous suffixes after this one
501
502 if ((end_type != 0) && !(end_type & SUFX_P)) {
503 // there is another suffix
504 end_flags = RemoveEnding(tr, wordx, end_type, NULL);
505 more_suffixes = true;
506 }
507 } else {
508 // don't remove any previous suffix
509 TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, NULL, wflags, dictionary_flags);
510 end_type = 0;
511 }
512
513 if (phonemes[0] == phonSWITCH) {
514 // change to another language in order to translate this word
515 strcpy(word_phonemes, phonemes);
516 memcpy(wordx, word_copy, strlen(word_copy));
517 wordx[-1] = c_temp;
518 return 0;
519 }
520 }
521 }
522 }
523
524
525 if ((end_type1 & SUFX_T) == 0) {
526 // the default is to add the suffix and then determine the word's stress pattern
527 AppendPhonemes(tr, phonemes, N_WORD_PHONEMES, end_phonemes);
528 end_phonemes[0] = 0;
529 }
530 memcpy(wordx, word_copy, strlen(word_copy));
531 }
532
533 wordx[-1] = c_temp;
534 }
535 }
536
537 addPluralSuffixes(wflags, tr, last_char, word_phonemes);
538 wflags |= emphasize_allcaps;
539
540 // determine stress pattern for this word
541
542 add_suffix_phonemes = 0;
543 if (end_phonemes[0] != 0)
544 add_suffix_phonemes = 2;
545
546 prefix_stress = 0;
547 for (p = prefix_phonemes; *p != 0; p++) {
548 if ((*p == phonSTRESS_P) || (*p == phonSTRESS_P2))
549 prefix_stress = *p;
550 }
551 if (prefix_flags || (prefix_stress != 0)) {
552 if ((tr->langopts.param[LOPT_PREFIXES]) || (prefix_type & SUFX_T)) {
553 // German, keep a secondary stress on the stem
554 SetWordStress(tr, phonemes, dictionary_flags, 3, 0);
555
556 // reduce all but the first primary stress
557 ix = 0;
558 for (char *pp = prefix_phonemes; *pp != 0; pp++) {
559 if (*pp == phonSTRESS_P) {
560 if (ix == 0)
561 ix = 1;
562 else
563 *pp = phonSTRESS_3;
564 }
565 }
566 snprintf(word_phonemes, size_word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
567
568 word_phonemes[N_WORD_PHONEMES-1] = 0;
569 SetWordStress(tr, word_phonemes, dictionary_flags, -1, 0);
570 } else {
571 // stress position affects the whole word, including prefix
572 snprintf(word_phonemes, size_word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
573 word_phonemes[N_WORD_PHONEMES-1] = 0;
574 SetWordStress(tr, word_phonemes, dictionary_flags, -1, 0);
575 }
576 } else {
577 SetWordStress(tr, phonemes, dictionary_flags, -1, add_suffix_phonemes);
578 snprintf(word_phonemes, size_word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
579 word_phonemes[N_WORD_PHONEMES-1] = 0;
580 }
581
582 if (end_phonemes[0] != 0) {
583 // a suffix had the SUFX_T option set, add the suffix after the stress pattern has been determined
584 ix = strlen(word_phonemes);
585 end_phonemes[N_WORD_PHONEMES-1-ix] = 0; // ensure no buffer overflow
586 strcpy(&word_phonemes[ix], end_phonemes);
587 }
588
589 if (wflags & FLAG_LAST_WORD) {
590 // don't use $brk pause before the last word of a sentence
591 // (but allow it for emphasis, see below
592 dictionary_flags[0] &= ~FLAG_PAUSE1;
593 }
594
595 if ((wflags & FLAG_HYPHEN) && (tr->langopts.stress_flags & S_HYPEN_UNSTRESS))
596 ChangeWordStress(tr, word_phonemes, 3);
597 else if (wflags & FLAG_EMPHASIZED2) {
598 // A word is indicated in the source text as stressed
599 // Give it stress level 6 (for the intonation module)
600 ChangeWordStress(tr, word_phonemes, 6);
601
602 if (wflags & FLAG_EMPHASIZED)
603 dictionary_flags[0] |= FLAG_PAUSE1; // precede by short pause
604 } else if (wtab[dictionary_skipwords].flags & FLAG_LAST_WORD) {
605 // the word has attribute to stress or unstress when at end of clause
606 if (dictionary_flags[0] & (FLAG_STRESS_END | FLAG_STRESS_END2))
607 ChangeWordStress(tr, word_phonemes, 4);
608 else if ((dictionary_flags[0] & FLAG_UNSTRESS_END) && (any_stressed_words))
609 ChangeWordStress(tr, word_phonemes, 3);
610 }
611
612 // dictionary flags for this word give a clue about which alternative pronunciations of
613 // following words to use.
614 if (end_type1 & SUFX_F) {
615 // expect a verb form, with or without -s suffix
616 tr->expect_verb = 2;
617 tr->expect_verb_s = 2;
618 }
619
620 if (dictionary_flags[1] & FLAG_PASTF) {
621 // expect perfect tense in next two words
622 tr->expect_past = 3;
623 tr->expect_verb = 0;
624 tr->expect_noun = 0;
625 } else if (dictionary_flags[1] & FLAG_VERBF) {
626 // expect a verb in the next word
627 tr->expect_verb = 2;
628 tr->expect_verb_s = 0; // verb won't have -s suffix
629 tr->expect_noun = 0;
630 } else if (dictionary_flags[1] & FLAG_VERBSF) {
631 // expect a verb, must have a -s suffix
632 tr->expect_verb = 0;
633 tr->expect_verb_s = 2;
634 tr->expect_past = 0;
635 tr->expect_noun = 0;
636 } else if (dictionary_flags[1] & FLAG_NOUNF) {
637 // not expecting a verb next
638 tr->expect_noun = 2;
639 tr->expect_verb = 0;
640 tr->expect_verb_s = 0;
641 tr->expect_past = 0;
642 }
643
644 if ((wordx[0] != 0) && (!(dictionary_flags[1] & FLAG_VERB_EXT))) {
645 if (tr->expect_verb > 0)
646 tr->expect_verb--;
647
648 if (tr->expect_verb_s > 0)
649 tr->expect_verb_s--;
650
651 if (tr->expect_noun > 0)
652 tr->expect_noun--;
653
654 if (tr->expect_past > 0)
655 tr->expect_past--;
656 }
657
658 if ((word_length == 1) && (tr->translator_name == L('e', 'n')) && iswalpha_portable(first_char) && (first_char != 'i')) {
659 // English Specific !!!!
660 // any single letter before a dot is an abbreviation, except 'I'
661 dictionary_flags[0] |= FLAG_ALLOW_DOT;
662 }
663
664 if ((tr->langopts.param[LOPT_ALT] & 2) && ((dictionary_flags[0] & (FLAG_ALT_TRANS | FLAG_ALT2_TRANS)) != 0))
665 ApplySpecialAttribute2(tr, word_phonemes, dictionary_flags[0]);
666
667 dictionary_flags[0] |= was_unpronouncable;
668 memcpy(word_start, word_copy2, word_copy_length);
669 return dictionary_flags[0];
670 }
671
672
673 void ApplySpecialAttribute2(Translator *tr, char *phonemes, int dict_flags)
674 {
675 // apply after the translation is complete
676 int len;
677
678
679 len = strlen(phonemes);
680
681 if (tr->langopts.param[LOPT_ALT] & 2) {
682 for (int ix = 0; ix < (len-1); ix++) {
683 if (phonemes[ix] == phonSTRESS_P) {
684 char *p;
685 p = &phonemes[ix+1];
686 if ((dict_flags & FLAG_ALT2_TRANS) != 0) {
687 if (*p == PhonemeCode('E'))
688 *p = PhonemeCode('e');
689 if (*p == PhonemeCode('O'))
690 *p = PhonemeCode('o');
691 } else {
692 if (*p == PhonemeCode('e'))
693 *p = PhonemeCode('E');
694 if (*p == PhonemeCode('o'))
695 *p = PhonemeCode('O');
696 }
697 break;
698 }
699 }
700 }
701 }
702
703
704 static void ChangeWordStress(Translator *tr, char *word, int new_stress)
705 {
706 int ix;
707 unsigned char *p;
708 int max_stress;
709 int vowel_count; // num of vowels + 1
710 int stressed_syllable = 0; // position of stressed syllable
711 unsigned char phonetic[N_WORD_PHONEMES];
712 signed char vowel_stress[N_WORD_PHONEMES/2];
713
714 strcpy((char *)phonetic, word);
715 max_stress = GetVowelStress(tr, phonetic, vowel_stress, &vowel_count, &stressed_syllable, 0);
716
717 if (new_stress >= STRESS_IS_PRIMARY) {
718 // promote to primary stress
719 for (ix = 1; ix < vowel_count; ix++) {
720 if (vowel_stress[ix] >= max_stress) {
721 vowel_stress[ix] = new_stress;
722 break;
723 }
724 }
725 } else {
726 // remove primary stress
727 for (ix = 1; ix < vowel_count; ix++) {
728 if (vowel_stress[ix] > new_stress) // >= allows for diminished stress (=1)
729 vowel_stress[ix] = new_stress;
730 }
731 }
732
733 // write out phonemes
734 ix = 1;
735 p = phonetic;
736 while (*p != 0) {
737 if ((phoneme_tab[*p]->type == phVOWEL) && !(phoneme_tab[*p]->phflags & phNONSYLLABIC)) {
738 if ((vowel_stress[ix] == STRESS_IS_DIMINISHED) || (vowel_stress[ix] > STRESS_IS_UNSTRESSED))
739 *word++ = stress_phonemes[(unsigned char)vowel_stress[ix]];
740
741 ix++;
742 }
743 *word++ = *p++;
744 }
745 *word = 0;
746 }
747
748 static char *SpeakIndividualLetters(Translator *tr, char *word, char *phonemes, int spell_word, const ALPHABET *current_alphabet, char word_phonemes[])
749 {
750 int posn = 0;
751 int capitals = 0;
752 bool non_initial = false;
753
754 if (spell_word > 2)
755 capitals = 2; // speak 'capital'
756 if (spell_word > 1)
757 capitals |= 4; // speak character code for unknown letters
758
759 while ((*word != ' ') && (*word != 0)) {
760 word += TranslateLetter(tr, word, phonemes, capitals | non_initial, current_alphabet);
761 posn++;
762 non_initial = true;
763 if (phonemes[0] == phonSWITCH) {
764 // change to another language in order to translate this word
765 strcpy(word_phonemes, phonemes);
766 return NULL;
767 }
768 }
769 SetSpellingStress(tr, phonemes, spell_word, posn);
770 return word;
771 }
772
773
774 static const char *const hex_letters[] = {"'e:j", "b'i:", "s'i:", "d'i:", "'i:", "'ef"};
775 static const char *const modifiers[] = { NULL, "_sub", "_sup", NULL };
776 // unicode ranges for non-ascii digits 0-9 (these must be in ascending order)
777 static const int number_ranges[] = {
778 0x660, 0x6f0, // arabic
779 0x966, 0x9e6, 0xa66, 0xae6, 0xb66, 0xbe6, 0xc66, 0xce6, 0xd66, // indic
780 0xe50, 0xed0, 0xf20, 0x1040, 0x1090,
781 0
782 };
783
784
785 static int TranslateLetter(Translator *tr, char *word, char *phonemes, int control, const ALPHABET *current_alphabet)
786 {
787 // get pronunciation for an isolated letter
788 // return number of bytes used by the letter
789 // control bit 0: a non-initial letter in a word
790 // bit 1: say 'capital'
791 // bit 2: say character code for unknown letters
792
793 int n_bytes;
794 int letter;
795 int len;
796 const ALPHABET *alphabet;
797 int al_offset;
798 int al_flags;
799 int number;
800 int phontab_1;
801 char capital[30];
802 char ph_buf[80];
803 char ph_buf2[80];
804 char ph_alphabet[80];
805 char hexbuf[12];
806 static const char pause_string[] = { phonPAUSE, 0 };
807
808 ph_buf[0] = 0;
809 ph_alphabet[0] = 0;
810 capital[0] = 0;
811 phontab_1 = translator->phoneme_tab_ix;
812
813 n_bytes = utf8_in(&letter, word);
814
815 if ((letter & 0xfff00) == 0x0e000)
816 letter &= 0xff; // uncode private usage area
817
818 if (control & 2) {
819 // include CAPITAL information
820 if (iswupper_portable(letter))
821 Lookup(tr, "_cap", capital);
822 }
823 letter = towlower2(letter, tr);
824 LookupLetter(tr, letter, word[n_bytes], ph_buf, control & 1);
825
826 if (ph_buf[0] == 0) {
827 // is this a subscript or superscript letter ?
828 int c;
829 if ((c = IsSuperscript(letter)) != 0) {
830 letter = c & 0x3fff;
831
832 const char *modifier;
833 if ((control & 4 ) && ((modifier = modifiers[c >> 14]) != NULL)) {
834 // don't say "superscript" during normal text reading
835 Lookup(tr, modifier, capital);
836 if (capital[0] == 0) {
837 capital[2] = SetTranslator3(ESPEAKNG_DEFAULT_VOICE); // overwrites previous contents of translator3
838 Lookup(translator3, modifier, &capital[3]);
839 if (capital[3] != 0) {
840 capital[0] = phonPAUSE;
841 capital[1] = phonSWITCH;
842 len = strlen(&capital[3]);
843 capital[len+3] = phonSWITCH;
844 capital[len+4] = phontab_1;
845 capital[len+5] = 0;
846 }
847 }
848 }
849 }
850 LookupLetter(tr, letter, word[n_bytes], ph_buf, control & 1);
851 }
852
853 if (ph_buf[0] == phonSWITCH) {
854 strcpy(phonemes, ph_buf);
855 return 0;
856 }
857
858
859 if ((ph_buf[0] == 0) && ((number = NonAsciiNumber(letter)) > 0)) {
860 // convert a non-ascii number to 0-9
861 LookupLetter(tr, number, 0, ph_buf, control & 1);
862 }
863
864 al_offset = 0;
865 al_flags = 0;
866 if ((alphabet = AlphabetFromChar(letter)) != NULL) {
867 al_offset = alphabet->offset;
868 al_flags = alphabet->flags;
869 }
870
871 if (alphabet != current_alphabet) {
872 // speak the name of the alphabet
873 current_alphabet = alphabet;
874 if ((alphabet != NULL) && !(al_flags & AL_DONT_NAME) && (al_offset != translator->letter_bits_offset)) {
875 if ((al_flags & AL_DONT_NAME) || (al_offset == translator->langopts.alt_alphabet) || (al_offset == translator->langopts.our_alphabet)) {
876 // don't say the alphabet name
877 } else {
878 ph_buf2[0] = 0;
879 if (Lookup(translator, alphabet->name, ph_alphabet) == 0) { // the original language for the current voice
880 // Can't find the local name for this alphabet, use the English name
881 ph_alphabet[2] = SetTranslator3(ESPEAKNG_DEFAULT_VOICE); // overwrites previous contents of translator3
882 Lookup(translator3, alphabet->name, ph_buf2);
883 } else if (translator != tr) {
884 phontab_1 = tr->phoneme_tab_ix;
885 strcpy(ph_buf2, ph_alphabet);
886 ph_alphabet[2] = translator->phoneme_tab_ix;
887 }
888
889 if (ph_buf2[0] != 0) {
890 // we used a different language for the alphabet name (now in ph_buf2)
891 ph_alphabet[0] = phonPAUSE;
892 ph_alphabet[1] = phonSWITCH;
893 strcpy(&ph_alphabet[3], ph_buf2);
894 len = strlen(ph_buf2) + 3;
895 ph_alphabet[len] = phonSWITCH;
896 ph_alphabet[len+1] = phontab_1;
897 ph_alphabet[len+2] = 0;
898 }
899 }
900 }
901 }
902
903 // caution: SetWordStress() etc don't expect phonSWITCH + phoneme table number
904
905 if (ph_buf[0] == 0) {
906 int language;
907 if ((al_offset != 0) && (al_offset == translator->langopts.alt_alphabet))
908 language = translator->langopts.alt_alphabet_lang;
909 else if ((alphabet != NULL) && (alphabet->language != 0) && !(al_flags & AL_NOT_LETTERS))
910 language = alphabet->language;
911 else
912 language = L('e', 'n');
913
914 if ((language != tr->translator_name) || (language == L('k', 'o'))) {
915 char *p3;
916 //int initial, code;
917 char hangul_buf[12];
918
919 // speak in the language for this alphabet (or English)
920 char word_buf[5];
921 ph_buf[2] = SetTranslator3(WordToString2(word_buf, language));
922
923 if (translator3 != NULL) {
924 int code;
925 if (((code = letter - 0xac00) >= 0) && (letter <= 0xd7af)) {
926 // Special case for Korean letters.
927 // break a syllable hangul into 2 or 3 individual jamo
928
929 hangul_buf[0] = ' ';
930 p3 = &hangul_buf[1];
931 int initial;
932 if ((initial = (code/28)/21) != 11) {
933 p3 += utf8_out(initial + 0x1100, p3);
934 }
935 utf8_out(((code/28) % 21) + 0x1161, p3); // medial
936 utf8_out((code % 28) + 0x11a7, &p3[3]); // final
937 p3[6] = ' ';
938 p3[7] = 0;
939 ph_buf[3] = 0;
940 TranslateRules(translator3, &hangul_buf[1], &ph_buf[3], sizeof(ph_buf)-3, NULL, 0, NULL);
941 SetWordStress(translator3, &ph_buf[3], NULL, -1, 0);
942 } else
943 LookupLetter(translator3, letter, word[n_bytes], &ph_buf[3], control & 1);
944
945 if (ph_buf[3] == phonSWITCH) {
946 // another level of language change
947 ph_buf[2] = SetTranslator3(&ph_buf[4]);
948 LookupLetter(translator3, letter, word[n_bytes], &ph_buf[3], control & 1);
949 }
950
951 SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
952
953 if (ph_buf[3] != 0) {
954 ph_buf[0] = phonPAUSE;
955 ph_buf[1] = phonSWITCH;
956 len = strlen(&ph_buf[3]) + 3;
957 ph_buf[len] = phonSWITCH; // switch back
958 ph_buf[len+1] = tr->phoneme_tab_ix;
959 ph_buf[len+2] = 0;
960 }
961 }
962 }
963 }
964
965 if (ph_buf[0] == 0) {
966 // character name not found
967 int speak_letter_number = 1;
968 if (!(al_flags & AL_NO_SYMBOL)) {
969 if (iswalpha_portable(letter))
970 Lookup(translator, "_?A", ph_buf);
971
972 if ((ph_buf[0] == 0) && !iswspace_portable(letter))
973 Lookup(translator, "_??", ph_buf);
974
975 if (ph_buf[0] == 0)
976 EncodePhonemes("l'et@", ph_buf, NULL);
977 }
978
979 if (!(control & 4) && (al_flags & AL_NOT_CODE)) {
980 // don't speak the character code number, unless we want full details of this character
981 speak_letter_number = 0;
982 }
983
984 if (speak_letter_number) {
985 char *p2;
986 if (al_offset == 0x2800) {
987 // braille dots symbol, list the numbered dots
988 p2 = hexbuf;
989 for (int ix = 0; ix < 8; ix++) {
990 if (letter & (1 << ix))
991 *p2++ = '1'+ix;
992 }
993 *p2 = 0;
994 } else {
995 // speak the hexadecimal number of the character code
996 sprintf(hexbuf, "%x", letter);
997 }
998
999 char *pbuf;
1000 pbuf = ph_buf;
1001 for (p2 = hexbuf; *p2 != 0; p2++) {
1002 pbuf += strlen(pbuf);
1003 *pbuf++ = phonPAUSE_VSHORT;
1004 LookupLetter(translator, *p2, 0, pbuf, 1);
1005 if (((pbuf[0] == 0) || (pbuf[0] == phonSWITCH)) && (*p2 >= 'a')) {
1006 // This language has no translation for 'a' to 'f', speak English names using base phonemes
1007 EncodePhonemes(hex_letters[*p2 - 'a'], pbuf, NULL);
1008 }
1009 }
1010 strcat(pbuf, pause_string);
1011 }
1012 }
1013
1014 len = strlen(phonemes);
1015
1016 if (tr->langopts.accents & 2) // 'capital' before or after the word ?
1017 sprintf(ph_buf2, "%c%s%s%s", 0xff, ph_alphabet, ph_buf, capital);
1018 else
1019 sprintf(ph_buf2, "%c%s%s%s", 0xff, ph_alphabet, capital, ph_buf); // the 0xff marker will be removed or replaced in SetSpellingStress()
1020 if ((len + strlen(ph_buf2)) < N_WORD_PHONEMES)
1021 strcpy(&phonemes[len], ph_buf2);
1022 return n_bytes;
1023 }
1024
1025 // append plural suffixes depending on preceding letter
1026 static void addPluralSuffixes(int flags, Translator *tr, char last_char, char *word_phonemes)
1027 {
1028 char word_zz[4] = { ' ', 'z', 'z', 0 };
1029 char word_iz[4] = { ' ', 'i', 'z', 0 };
1030 char word_ss[4] = { ' ', 's', 's', 0 };
1031 if (flags & FLAG_HAS_PLURAL) {
1032 // s or 's suffix, append [s], [z] or [Iz] depending on previous letter
1033 if (last_char == 'f')
1034 TranslateRules(tr, &word_ss[1], word_phonemes, N_WORD_PHONEMES,
1035 NULL, 0, NULL);
1036 else if ((last_char == 0) || (strchr_w("hsx", last_char) == NULL))
1037 TranslateRules(tr, &word_zz[1], word_phonemes, N_WORD_PHONEMES,
1038 NULL, 0, NULL);
1039 else
1040 TranslateRules(tr, &word_iz[1], word_phonemes, N_WORD_PHONEMES,
1041 NULL, 0, NULL);
1042 }
1043 }
1044
1045 static int CheckDottedAbbrev(char *word1)
1046 {
1047 int wc;
1048 int count = 0;
1049 int ix;
1050 char *word;
1051 char *wbuf;
1052 char word_buf[80];
1053
1054 word = word1;
1055 wbuf = word_buf;
1056
1057 for (;;) {
1058 int ok = 0;
1059 int nbytes = utf8_in(&wc, word);
1060 if ((word[nbytes] == ' ') && IsAlpha(wc)) {
1061 if (word[nbytes+1] == '.') {
1062 if (word[nbytes+2] == ' ')
1063 ok = 1;
1064 else if (word[nbytes+2] == '\'' && word[nbytes+3] == 's') {
1065 nbytes += 2; // delete the final dot (eg. u.s.a.'s)
1066 ok = 2;
1067 }
1068 } else if ((count > 0))
1069 ok = 2;
1070 }
1071
1072 if (ok == 0)
1073 break;
1074
1075 for (ix = 0; ix < nbytes; ix++)
1076 *wbuf++ = word[ix];
1077
1078 count++;
1079
1080 if (ok == 2) {
1081 word += nbytes;
1082 break;
1083 }
1084
1085 word += (nbytes + 3);
1086 }
1087
1088 if (count > 1) {
1089 ix = wbuf - word_buf;
1090 memcpy(word1, word_buf, ix);
1091 while (&word1[ix] < word)
1092 word1[ix++] = ' ';
1093 dictionary_skipwords = (count - 1)*2;
1094 }
1095 return count;
1096 }
1097
1098 static int NonAsciiNumber(int letter)
1099 {
1100 // Change non-ascii digit into ascii digit '0' to '9', (or -1 if not)
1101 const int *p;
1102 int base;
1103
1104 for (p = number_ranges; (base = *p) != 0; p++) {
1105 if (letter < base)
1106 break; // not found
1107 if (letter < (base+10))
1108 return letter-base+'0';
1109 }
1110 return -1;
1111 }
1112
1113 static int Unpronouncable(Translator *tr, char *word, int posn)
1114 {
1115 /* Determines whether a word in 'unpronouncable', i.e. whether it should
1116 be spoken as individual letters.
1117
1118 This function may be language specific. This is a generic version.
1119 */
1120
1121 int c;
1122 int c1 = 0;
1123 int vowel_posn = 9;
1124 int index;
1125 int count;
1126 const ALPHABET *alphabet;
1127
1128 utf8_in(&c, word);
1129 if ((tr->letter_bits_offset > 0) && (c < 0x241)) {
1130 // Latin characters for a language with a non-latin alphabet
1131 return 0; // so we can re-translate the word as English
1132 }
1133
1134 if (((alphabet = AlphabetFromChar(c)) != NULL) && (alphabet->offset != tr->letter_bits_offset)) {
1135 // Character is not in our alphabet
1136 return 0;
1137 }
1138
1139 if (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 1)
1140 return 0;
1141
1142 if (((c = *word) == ' ') || (c == 0) || (c == '\''))
1143 return 0;
1144
1145 index = 0;
1146 count = 0;
1147 for (;;) {
1148 index += utf8_in(&c, &word[index]);
1149 if ((c == 0) || (c == ' '))
1150 break;
1151
1152 if ((c == '\'') && ((count > 1) || (posn > 0)))
1153 break; // "tv'" but not "l'"
1154
1155 if (count == 0)
1156 c1 = c;
1157
1158 if ((c == '\'') && (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 3)) {
1159 // don't count apostrophe
1160 } else
1161 count++;
1162
1163 if (IsVowel(tr, c)) {
1164 vowel_posn = count; // position of the first vowel
1165 break;
1166 }
1167
1168 if ((c != '\'') && !iswalpha_portable(c))
1169 return 0;
1170 }
1171
1172 if ((vowel_posn > 2) && (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 2)) {
1173 // Lookup unpronounable rules in *_rules
1174 return Unpronouncable2(tr, word);
1175 }
1176
1177 if (c1 == tr->langopts.param[LOPT_UNPRONOUNCABLE])
1178 vowel_posn--; // disregard this as the initial letter when counting
1179
1180 if (vowel_posn > (tr->langopts.max_initial_consonants+1))
1181 return 1; // no vowel, or no vowel in first few letters
1182
1183 return 0;
1184 }
1185
1186 static int Unpronouncable2(Translator *tr, char *word)
1187 {
1188 int c;
1189 int end_flags;
1190 char ph_buf[N_WORD_PHONEMES];
1191
1192 ph_buf[0] = 0;
1193 c = word[-1];
1194 word[-1] = ' '; // ensure there is a space before the "word"
1195 end_flags = TranslateRules(tr, word, ph_buf, sizeof(ph_buf), NULL, FLAG_UNPRON_TEST, NULL);
1196 word[-1] = c;
1197 if ((end_flags == 0) || (end_flags & SUFX_UNPRON))
1198 return 1;
1199 return 0;
1200 }