Debian Codegraph @

external/espeak/translate.cpp

raw
1 /*
2 * Copyright (C) 2005 to 2014 by Jonathan Duddington
3 * email: [email protected]
4 * Copyright (C) 2015-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 <stdbool.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <wchar.h>
29 #include "wctype_portable.h"
30
31 #include "espeak_ng.h"
32 #include "speak_lib.h"
33 #include "encoding.h"
34
35 #include "translate.h"
36 #include "common.h"
37 #include "dictionary.h" // for TranslateRules, LookupDictList, Cha...
38 #include "phoneme.h" // for phonSWITCH, PHONEME_TAB, phonPAUSE_...
39 #include "phonemelist.h" // for MakePhonemeList
40 #include "readclause.h" // for towlower2, Eof, ReadClause, is_str_...
41 #include "synthdata.h" // for SelectPhonemeTable, LookupPhonemeTable
42 #include "synthesize.h" // for PHONEME_LIST2, N_PHONEME_LIST, PHON...
43 #include "ucd.h" // for ucd_toupper
44 #include "voice.h" // for voice, voice_t
45 #include "speech.h" // for MAKE_MEM_UNDEFINED
46 #include "translateword.h"
47
48 static int CalcWordLength(int source_index, int charix_top, short int *charix, WORD_TAB *words, int word_count);
49 static void CombineFlag(Translator *tr, WORD_TAB *wtab, char *word, int *flags, unsigned char *p, char *wordPhonemes);
50 static void SwitchLanguage(char *word, char *wordPhonemes);
51
52 Translator *translator = NULL; // the main translator
53 Translator *translator2 = NULL; // secondary translator for certain words
54 static char translator2_language[20] = { 0 };
55 Translator *translator3 = NULL; // tertiary translator for certain words
56 static char translator3_language[20] = { 0 };
57
58 FILE *f_trans = NULL; // phoneme output text
59 int option_tone_flags = 0; // bit 8=emphasize allcaps, bit 9=emphasize penultimate stress
60 int option_phonemes = 0;
61 int option_phoneme_events = 0;
62 int option_endpause = 0; // suppress pause after end of text
63 int option_capitals = 0;
64 int option_punctuation = 0;
65 int option_sayas = 0;
66 static int option_sayas2 = 0; // used in translate_clause()
67 static int option_emphasis = 0; // 0=normal, 1=normal, 2=weak, 3=moderate, 4=strong
68 int option_ssml = 0;
69 int option_phoneme_input = 0; // allow [[phonemes]] in input
70 int option_wordgap = 0;
71
72 static int count_sayas_digits;
73 int skip_sentences;
74 int skip_words;
75 int skip_characters;
76 char skip_marker[N_MARKER_LENGTH];
77 bool skipping_text; // waiting until word count, sentence count, or named marker is reached
78 int end_character_position;
79 int count_sentences;
80 static int count_words;
81 int clause_start_char;
82 int clause_start_word;
83 static bool new_sentence;
84 static int word_emphasis = 0; // set if emphasis level 3 or 4
85 static int embedded_flag = 0; // there are embedded commands to be applied to the next phoneme, used in TranslateWord2()
86
87 static int max_clause_pause = 0;
88 static bool any_stressed_words;
89 int pre_pause;
90 static ALPHABET *current_alphabet;
91
92 char word_phonemes[N_WORD_PHONEMES]; // a word translated into phoneme codes
93 int n_ph_list2;
94 PHONEME_LIST2 ph_list2[N_PHONEME_LIST]; // first stage of text->phonemes
95
96 wchar_t option_punctlist[N_PUNCTLIST] = { 0 };
97
98 // these are overridden by defaults set in the "speak" file
99 int option_linelength = 0;
100
101 #define N_EMBEDDED_LIST 250
102 static int embedded_ix;
103 static int embedded_read;
104 unsigned int embedded_list[N_EMBEDDED_LIST];
105
106 // the source text of a single clause (UTF8 bytes)
107 static char source[N_TR_SOURCE+40]; // extra space for embedded command & voice change info at end
108
109 int n_replace_phonemes;
110 REPLACE_PHONEMES replace_phonemes[N_REPLACE_PHONEMES];
111
112 // other characters which break a word, but don't produce a pause
113 static const unsigned short breaks[] = { '_', 0 };
114
115 void DeleteTranslator(Translator *tr)
116 {
117 if (!tr) return;
118
119 if (tr->data_dictlist != NULL)
120 free(tr->data_dictlist);
121 free(tr);
122 }
123
124 int lookupwchar(const unsigned short *list, int c)
125 {
126 // Is the character c in the list ?
127 int ix;
128
129 for (ix = 0; list[ix] != 0; ix++) {
130 if (list[ix] == c)
131 return ix+1;
132 }
133 return 0;
134 }
135
136 char *strchr_w(const char *s, int c)
137 {
138 // return NULL for any non-ascii character
139 if (c >= 0x80)
140 return NULL;
141 return strchr((char *)s, c); // (char *) is needed for Borland compiler
142 }
143
144 int TranslateWord(Translator *tr, char *word_start, WORD_TAB *wtab, char *word_out)
145 {
146 char words_phonemes[N_WORD_PHONEMES]; // a word translated into phoneme codes
147 char *phonemes = words_phonemes;
148
149
150 int flags = TranslateWord3(tr, word_start, wtab, word_out, &any_stressed_words, current_alphabet, word_phonemes, sizeof(word_phonemes));
151 if (flags & FLAG_TEXTMODE && word_out) {
152 // Ensure that start of word rules match with the replaced text,
153 // so that emoji and other characters are pronounced correctly.
154 char word[N_WORD_BYTES+1];
155 word[0] = 0;
156 word[1] = ' ';
157 strcpy(word+2, word_out);
158 word_out = word+2;
159
160 bool first_word = true;
161 int available = N_WORD_PHONEMES;
162 while (*word_out && available > 1) {
163 int c;
164 utf8_in(&c, word_out);
165 if (iswupper_portable(c)) {
166 wtab->flags |= FLAG_FIRST_UPPER;
167 utf8_out(tolower(c), word_out);
168 } else {
169 wtab->flags &= ~FLAG_FIRST_UPPER;
170 }
171
172 // dictionary_skipwords is a global variable and TranslateWord3 will reset it to 0 at the beginning.
173 // However, dictionary_skipwords value is still needed outside this scope.
174 // So we backup and restore it at the end of this scope.
175 int skipwords = dictionary_skipwords;
176 TranslateWord3(tr, word_out, wtab, NULL, &any_stressed_words, current_alphabet, word_phonemes, sizeof(word_phonemes));
177
178 int n;
179 if (first_word) {
180 n = snprintf(phonemes, available, "%s", word_phonemes);
181 first_word = false;
182 } else {
183 n = snprintf(phonemes, available, "%c%s", phonEND_WORD, word_phonemes);
184 }
185
186 available -= n;
187 phonemes += n;
188
189 // skip to the next word in a multi-word replacement. Always skip at least one word.
190 for (dictionary_skipwords++; dictionary_skipwords > 0; dictionary_skipwords--) {
191 while (!isspace(*word_out)) ++word_out;
192 while (isspace(*word_out)) ++word_out;
193 }
194 dictionary_skipwords = skipwords;
195 }
196
197 // If the list file contains a text replacement to another
198 // entry in the list file, e.g.:
199 // ripost riposte $text
200 // riposte rI#p0st
201 // calling it from a prefix or suffix rule such as 'riposted'
202 // causes word_out[0] to be NULL, as TranslateWord3 has the
203 // information needed to perform the mapping. In this case,
204 // no phonemes have been written in this loop and the phonemes
205 // have been calculated, so don't override them.
206 if (phonemes != words_phonemes) {
207 snprintf(word_phonemes, sizeof(word_phonemes), "%s", words_phonemes);
208 }
209 }
210 return flags;
211 }
212
213 static void SetPlist2(PHONEME_LIST2 *p, unsigned char phcode)
214 {
215 p->phcode = phcode;
216 p->stresslevel = 0;
217 p->tone_ph = 0;
218 p->synthflags = embedded_flag;
219 p->sourceix = 0;
220 embedded_flag = 0;
221 }
222
223 static int CountSyllables(unsigned char *phonemes)
224 {
225 int count = 0;
226 int phon;
227 while ((phon = *phonemes++) != 0) {
228 if (phoneme_tab[phon]->type == phVOWEL)
229 count++;
230 }
231 return count;
232 }
233
234 static void Word_EmbeddedCmd(void)
235 {
236 // Process embedded commands for emphasis, sayas, and break
237 int embedded_cmd;
238 do {
239 embedded_cmd = embedded_list[embedded_read++];
240 int value = embedded_cmd >> 8;
241
242 switch (embedded_cmd & 0x1f)
243 {
244 case EMBED_Y:
245 option_sayas = value;
246 break;
247
248 case EMBED_F:
249 option_emphasis = value;
250 break;
251
252 case EMBED_B:
253 // break command
254 if (value == 0)
255 pre_pause = 0; // break=none
256 else
257 pre_pause += value;
258 break;
259 }
260 } while (((embedded_cmd & 0x80) == 0) && (embedded_read < embedded_ix));
261 }
262
263 static int SetAlternateTranslator(const char *new_language, Translator **trans, char translator_language[20])
264 {
265 // Set alternate translator to a second language
266 int new_phoneme_tab;
267
268 if ((new_phoneme_tab = SelectPhonemeTableName(new_language)) >= 0) {
269 if ((*trans != NULL) && (strcmp(new_language, translator_language) != 0)) {
270 // we already have an alternative translator, but not for the required language, delete it
271 DeleteTranslator(*trans);
272 *trans = NULL;
273 }
274
275 if (*trans == NULL) {
276 *trans = SelectTranslator(new_language);
277 strcpy(translator_language, new_language);
278
279 if (LoadDictionary(*trans, (*trans)->dictionary_name, 0) != 0) {
280 SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
281 new_phoneme_tab = -1;
282 translator_language[0] = 0;
283 }
284 (*trans)->phoneme_tab_ix = new_phoneme_tab;
285 }
286 }
287 if (*trans != NULL)
288 (*trans)->phonemes_repeat[0] = 0;
289 return new_phoneme_tab;
290 }
291
292 int SetTranslator2(const char *new_language)
293 {
294 return SetAlternateTranslator(new_language, &translator2, translator2_language);
295 }
296
297 int SetTranslator3(const char *new_language)
298 {
299 return SetAlternateTranslator(new_language, &translator3, translator3_language);
300 }
301
302 static int TranslateWord2(Translator *tr, char *word, WORD_TAB *wtab, int prePause)
303 {
304 int flags = 0;
305 int stress;
306 int next_stress;
307 int next_tone = 0;
308 unsigned char *p;
309 int srcix;
310 int found_dict_flag;
311 unsigned char ph_code;
312 PHONEME_LIST2 *plist2;
313 PHONEME_TAB *ph;
314 int max_stress;
315 int max_stress_ix = 0;
316 int prev_vowel = -1;
317 int pitch_raised = 0;
318 int switch_phonemes = -1;
319 bool first_phoneme = true;
320 int source_ix;
321 int len;
322 int bad_phoneme;
323 int word_flags;
324 char word_copy[N_WORD_BYTES+1];
325 char word_replaced[N_WORD_BYTES+1];
326 char old_dictionary_name[40];
327
328 len = wtab->length;
329 if (len > 31) len = 31;
330 source_ix = (wtab->sourceix & 0x7ff) | (len << 11); // bits 0-10 sourceix, bits 11-15 word length
331
332 word_flags = wtab[0].flags;
333 if (word_flags & FLAG_EMBEDDED) {
334 wtab[0].flags &= ~FLAG_EMBEDDED; // clear it in case we call TranslateWord2() again for the same word
335 embedded_flag = SFLAG_EMBEDDED;
336
337 Word_EmbeddedCmd();
338 }
339
340 if (n_ph_list2 >= N_PHONEME_LIST-2) {
341 // No room, can't translate anything
342 return 0;
343 }
344
345 if ((word[0] == 0) || (word_flags & FLAG_DELETE_WORD)) {
346 // nothing to translate. Add a dummy phoneme to carry any embedded commands
347 if (embedded_flag) {
348 SetPlist2(&ph_list2[n_ph_list2], phonEND_WORD);
349 ph_list2[n_ph_list2].wordstress = 0;
350 n_ph_list2++;
351 embedded_flag = 0;
352 }
353 word_phonemes[0] = 0;
354 return 0;
355 }
356
357 if (n_ph_list2 >= N_PHONEME_LIST-7-2) {
358 // We may require up to 7 phonemes, plus the 2 phonemes from the caller, can't translate safely
359 return 0;
360 }
361
362 // after a $pause word attribute, ignore a $pause attribute on the next two words
363 if (tr->prepause_timeout > 0)
364 tr->prepause_timeout--;
365
366 if ((option_sayas & 0xf0) == 0x10) {
367 if (!(word_flags & FLAG_FIRST_WORD)) {
368 // SAYAS_CHARS, SAYAS_GLYPHS, or SAYAS_SINGLECHARS. Pause between each word.
369 prePause += 4;
370 }
371 }
372
373 if (word_flags & FLAG_FIRST_UPPER) {
374 if ((option_capitals > 2) && (embedded_ix < N_EMBEDDED_LIST-6)) {
375 // indicate capital letter by raising pitch
376 if (embedded_flag)
377 embedded_list[embedded_ix-1] &= ~0x80; // already embedded command before this word, remove terminator
378 if ((pitch_raised = option_capitals) == 3)
379 pitch_raised = 20; // default pitch raise for capitals
380 embedded_list[embedded_ix++] = EMBED_P+0x40+0x80 + (pitch_raised << 8); // raise pitch
381 embedded_flag = SFLAG_EMBEDDED;
382 }
383 }
384
385 p = (unsigned char *)word_phonemes;
386 if (word_flags & FLAG_PHONEMES) {
387 // The input is in phoneme mnemonics, not language text
388
389 if (memcmp(word, "_^_", 3) == 0) {
390 SwitchLanguage(word, word_phonemes);
391 } else {
392 EncodePhonemes(word, word_phonemes, &bad_phoneme);
393 }
394
395 flags = FLAG_FOUND;
396 } else {
397 int c2;
398 int ix = 0;
399 int word_copy_len;
400 while (((c2 = word_copy[ix] = word[ix]) != ' ') && (c2 != 0) && (ix < N_WORD_BYTES)) ix++;
401 word_copy_len = ix;
402
403 word_replaced[2] = 0;
404 flags = TranslateWord(translator, word, wtab, &word_replaced[2]);
405
406 if (flags & FLAG_SPELLWORD) {
407 // re-translate the word as individual letters, separated by spaces
408 memcpy(word, word_copy, word_copy_len);
409 return flags;
410 }
411
412 if ((flags & FLAG_COMBINE) && !(wtab[1].flags & FLAG_PHONEMES)) {
413 CombineFlag(tr, wtab, word, &flags, p, word_phonemes);
414 }
415
416 if (p[0] == phonSWITCH) {
417 int switch_attempt;
418 strcpy(old_dictionary_name, dictionary_name);
419 for (switch_attempt = 0; switch_attempt < 2; switch_attempt++) {
420 // this word uses a different language
421 memcpy(word, word_copy, word_copy_len);
422
423 const char *new_language;
424 new_language = (char *)(&p[1]);
425 if (new_language[0] == 0)
426 new_language = ESPEAKNG_DEFAULT_VOICE;
427
428 switch_phonemes = SetTranslator2(new_language);
429
430 if (switch_phonemes >= 0) {
431 // re-translate the word using the new translator
432 wtab[0].flags |= FLAG_TRANSLATOR2;
433 if (word_replaced[2] != 0) {
434 word_replaced[0] = 0; // byte before the start of the word
435 word_replaced[1] = ' ';
436 flags = TranslateWord(translator2, &word_replaced[1], wtab, NULL);
437 } else
438 flags = TranslateWord(translator2, word, wtab, &word_replaced[2]);
439 }
440
441 if (p[0] != phonSWITCH)
442 break;
443 }
444
445 if (p[0] == phonSWITCH)
446 return FLAG_SPELLWORD;
447
448 if (switch_phonemes < 0) {
449 // language code is not recognised or 2nd translator won't translate it
450 p[0] = phonSCHWA; // just say something
451 p[1] = phonSCHWA;
452 p[2] = 0;
453 }
454
455 if (switch_phonemes == -1) {
456 strcpy(dictionary_name, old_dictionary_name);
457 SelectPhonemeTable(voice->phoneme_tab_ix);
458
459 // leave switch_phonemes set, but use the original phoneme table number.
460 // This will suppress LOPT_REGRESSIVE_VOICING
461 switch_phonemes = voice->phoneme_tab_ix; // original phoneme table
462 }
463 }
464
465 if (!(word_flags & FLAG_HYPHEN)) {
466 if (flags & FLAG_PAUSE1) {
467 if (prePause < 1)
468 prePause = 1;
469 }
470 if ((flags & FLAG_PREPAUSE) && !(word_flags & (FLAG_LAST_WORD | FLAG_FIRST_WORD)) && !(wtab[-1].flags & FLAG_FIRST_WORD) && (tr->prepause_timeout == 0)) {
471 // the word is marked in the dictionary list with $pause
472 if (prePause < 4) prePause = 4;
473 tr->prepause_timeout = 3;
474 }
475 }
476
477 if ((option_emphasis >= 3) && (prePause < 1))
478 prePause = 1;
479 }
480
481 stress = 0;
482 next_stress = 1;
483 srcix = 0;
484 max_stress = -1;
485
486 found_dict_flag = 0;
487 if ((flags & FLAG_FOUND) && !(flags & FLAG_TEXTMODE))
488 found_dict_flag = SFLAG_DICTIONARY;
489
490 // Each iteration may require up to 1 phoneme
491 // and after this loop we may require up to 7 phonemes
492 // and our caller requires 2 phonemes
493 while ((prePause > 0) && (n_ph_list2 < N_PHONEME_LIST-7-2)) {
494 // add pause phonemes here. Either because of punctuation (brackets or quotes) in the
495 // text, or because the word is marked in the dictionary lookup as a conjunction
496 if (prePause > 1) {
497 SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE);
498 prePause -= 2;
499 } else {
500 SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_NOLINK);
501 prePause--;
502 }
503 tr->end_stressed_vowel = 0; // forget about the previous word
504 tr->prev_dict_flags[0] = 0;
505 tr->prev_dict_flags[1] = 0;
506 }
507 plist2 = &ph_list2[n_ph_list2];
508 // From here we may require up to 4+1+3 phonemes
509
510 // This may require up to 4 phonemes
511 if ((option_capitals == 1) && (word_flags & FLAG_FIRST_UPPER)) {
512 SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_SHORT);
513 SetPlist2(&ph_list2[n_ph_list2++], phonCAPITAL);
514 if ((word_flags & FLAG_ALL_UPPER) && IsAlpha(word[1])) {
515 // word > 1 letter and all capitals
516 SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_SHORT);
517 SetPlist2(&ph_list2[n_ph_list2++], phonCAPITAL);
518 }
519 }
520
521 // This may require up to 1 phoneme
522 if (switch_phonemes >= 0) {
523 if ((p[0] == phonPAUSE) && (p[1] == phonSWITCH)) {
524 // the new word starts with a phoneme table switch, so there's no need to switch before it.
525 if (ph_list2[n_ph_list2-1].phcode == phonSWITCH) {
526 // previous phoneme is also a phonSWITCH, delete it
527 n_ph_list2--;
528 }
529 } else {
530 // this word uses a different phoneme table
531 if (ph_list2[n_ph_list2-1].phcode == phonSWITCH) {
532 // previous phoneme is also a phonSWITCH, just change its phoneme table number
533 n_ph_list2--;
534 } else
535 SetPlist2(&ph_list2[n_ph_list2], phonSWITCH);
536 ph_list2[n_ph_list2++].tone_ph = switch_phonemes; // temporary phoneme table number
537 }
538 }
539
540 // remove initial pause from a word if it follows a hyphen
541 if ((word_flags & FLAG_HYPHEN) && (phoneme_tab[*p]->type == phPAUSE))
542 p++;
543
544 if ((p[0] == 0) && (embedded_flag)) {
545 // no phonemes. Insert a very short pause to carry an embedded command
546 p[0] = phonPAUSE_VSHORT;
547 p[1] = 0;
548 }
549
550 // Each iteration may require up to 1 phoneme
551 // and after this loop we may require up to 3 phonemes
552 // and our caller requires 2 phonemes
553 while (((ph_code = *p++) != 0) && (n_ph_list2 < N_PHONEME_LIST-3-2)) {
554 if (ph_code == 255)
555 continue; // unknown phoneme
556
557 // Add the phonemes to the first stage phoneme list (ph_list2)
558 ph = phoneme_tab[ph_code];
559 if (ph == NULL) {
560 printf("Invalid phoneme code %d\n", ph_code);
561 continue;
562 }
563
564 if (ph_code == phonSWITCH) {
565 ph_list2[n_ph_list2].phcode = ph_code;
566 ph_list2[n_ph_list2].stresslevel = 0;
567 ph_list2[n_ph_list2].sourceix = 0;
568 ph_list2[n_ph_list2].synthflags = 0;
569 ph_list2[n_ph_list2++].tone_ph = *p;
570 SelectPhonemeTable(*p);
571 p++;
572 } else if (ph->type == phSTRESS) {
573 // don't add stress phonemes codes to the list, but give their stress
574 // value to the next vowel phoneme
575 // std_length is used to hold stress number or (if >10) a tone number for a tone language
576 if (ph->program == 0)
577 next_stress = ph->std_length;
578 else {
579 // for tone languages, the tone number for a syllable follows the vowel
580 if (prev_vowel >= 0)
581 ph_list2[prev_vowel].tone_ph = ph_code;
582 else
583 next_tone = ph_code; // no previous vowel, apply to the next vowel
584 }
585 } else if (ph_code == phonSYLLABIC) {
586 // mark the previous phoneme as a syllabic consonant
587 prev_vowel = n_ph_list2-1;
588 ph_list2[prev_vowel].synthflags |= SFLAG_SYLLABLE;
589 ph_list2[prev_vowel].stresslevel = next_stress;
590 } else if (ph_code == phonLENGTHEN)
591 ph_list2[n_ph_list2-1].synthflags |= SFLAG_LENGTHEN;
592 else if (ph_code == phonEND_WORD) {
593 // a || symbol in a phoneme string was used to indicate a word boundary
594 // Don't add this phoneme to the list, but make sure the next phoneme has
595 // a newword indication
596 srcix = source_ix+1;
597 } else if (ph_code == phonX1) {
598 // a language specific action
599 flags |= FLAG_DOUBLING;
600 } else {
601 ph_list2[n_ph_list2].phcode = ph_code;
602 ph_list2[n_ph_list2].tone_ph = 0;
603 ph_list2[n_ph_list2].synthflags = embedded_flag | found_dict_flag;
604 embedded_flag = 0;
605 ph_list2[n_ph_list2].sourceix = srcix;
606 srcix = 0;
607
608 if (ph->type == phVOWEL) {
609 stress = next_stress;
610 next_stress = 1; // default is 'unstressed'
611
612 if (stress >= 4)
613 any_stressed_words = true;
614
615 if ((prev_vowel >= 0) && (n_ph_list2-1) != prev_vowel)
616 ph_list2[n_ph_list2-1].stresslevel = stress; // set stress for previous consonant
617
618 ph_list2[n_ph_list2].synthflags |= SFLAG_SYLLABLE;
619 prev_vowel = n_ph_list2;
620
621 if (stress > max_stress) {
622 max_stress = stress;
623 max_stress_ix = n_ph_list2;
624 }
625 if (next_tone != 0) {
626 ph_list2[n_ph_list2].tone_ph = next_tone;
627 next_tone = 0;
628 }
629 } else {
630 if (first_phoneme && tr->prev_dict_flags[0] & FLAG_DOUBLING) {
631 // double the initial consonant if the previous word is marked with a flag
632 ph_list2[n_ph_list2].synthflags |= SFLAG_LENGTHEN;
633 }
634 }
635
636 ph_list2[n_ph_list2].stresslevel = stress;
637 n_ph_list2++;
638 first_phoneme = false;
639 }
640 }
641 // From here, we may require up to 3 phonemes
642
643 // This may require up to 1 phoneme
644 if (word_flags & FLAG_COMMA_AFTER)
645 SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_CLAUSE);
646
647 // don't set new-word if there is a hyphen before it
648 if ((word_flags & FLAG_HYPHEN) == 0)
649 plist2->sourceix = source_ix;
650
651 tr->end_stressed_vowel = 0;
652 if ((stress >= 4) && (phoneme_tab[ph_list2[n_ph_list2-1].phcode]->type == phVOWEL))
653 tr->end_stressed_vowel = 1; // word ends with a stressed vowel
654
655 // This may require up to 1 phoneme
656 if (switch_phonemes >= 0) {
657 // this word uses a different phoneme table, now switch back
658 strcpy(dictionary_name, old_dictionary_name);
659 SelectPhonemeTable(voice->phoneme_tab_ix);
660 SetPlist2(&ph_list2[n_ph_list2], phonSWITCH);
661 ph_list2[n_ph_list2++].tone_ph = voice->phoneme_tab_ix; // original phoneme table number
662 }
663
664
665 // This may require up to 1 phoneme
666 if (pitch_raised > 0) {
667 embedded_list[embedded_ix++] = EMBED_P+0x60+0x80 + (pitch_raised << 8); // lower pitch
668 SetPlist2(&ph_list2[n_ph_list2], phonPAUSE_SHORT);
669 ph_list2[n_ph_list2++].synthflags = SFLAG_EMBEDDED;
670 }
671
672 if (flags & FLAG_STRESS_END2) {
673 // this's word's stress could be increased later
674 ph_list2[max_stress_ix].synthflags |= SFLAG_PROMOTE_STRESS;
675 }
676
677 tr->prev_dict_flags[0] = flags;
678 return flags;
679 }
680
681 static int EmbeddedCommand(unsigned int *source_index_out)
682 {
683 // An embedded command to change the pitch, volume, etc.
684 // returns number of commands added to embedded_list
685
686 // pitch,speed,amplitude,expression,reverb,tone,voice,sayas
687 const char *commands = "PSARHTIVYMUBF";
688 int value = -1;
689 int sign = 0;
690 unsigned char c;
691 char *p;
692 int cmd;
693 int source_index = *source_index_out;
694
695 c = source[source_index];
696 if (c == '+') {
697 sign = 0x40;
698 source_index++;
699 } else if (c == '-') {
700 sign = 0x60;
701 source_index++;
702 }
703
704 if (IsDigit09(source[source_index])) {
705 value = atoi(&source[source_index]);
706 while (IsDigit09(source[source_index]))
707 source_index++;
708 }
709
710 c = source[source_index++];
711 if (embedded_ix >= (N_EMBEDDED_LIST - 2))
712 return 0; // list is full
713
714 if ((p = strchr_w(commands, c)) == NULL)
715 return 0;
716 cmd = (p - commands)+1;
717 if (value == -1) {
718 value = embedded_default[cmd];
719 sign = 0;
720 }
721
722 if (cmd == EMBED_Y) {
723 option_sayas2 = value;
724 count_sayas_digits = 0;
725 }
726 if (cmd == EMBED_F) {
727 if (value >= 3)
728 word_emphasis = FLAG_EMPHASIZED;
729 else
730 word_emphasis = 0;
731 }
732
733 embedded_list[embedded_ix++] = cmd + sign + (value << 8);
734 *source_index_out = source_index;
735 return 1;
736 }
737
738 static const char *FindReplacementChars(Translator *tr, const char **pfrom, unsigned int c, const char *next, int *ignore_next_n)
739 {
740 const char *from = *pfrom;
741 while ( !is_str_totally_null(from, 4) ) {
742 unsigned int fc = 0; // from character
743 unsigned int nc = c; // next character
744 const char *match_next = next;
745
746 *pfrom = from;
747
748 from += utf8_in((int *)&fc, from);
749 if (nc == fc) {
750 if (*from == 0) return from + 1;
751
752 bool matched = true;
753 int nmatched = 0;
754 while (*from != 0) {
755 from += utf8_in((int *)&fc, from);
756
757 match_next += utf8_in((int *)&nc, match_next);
758 nc = towlower2(nc, tr);
759
760 if (nc != fc)
761 matched = false;
762 else
763 nmatched++;
764 }
765
766 if (matched) {
767 *ignore_next_n = nmatched;
768 return from + 1;
769 }
770 }
771
772 // replacement 'from' string (skip the remaining part, if any)
773 while (*from != '\0') from++;
774 from++;
775
776 // replacement 'to' string
777 while (*from != '\0') from++;
778 from++;
779 }
780 return NULL;
781 }
782
783 // handle .replace rule in xx_rules file
784 static int SubstituteChar(Translator *tr, unsigned int c, unsigned int next_in, const char *next, int *insert, int *wordflags)
785 {
786 unsigned int new_c, c2 = ' ', c_lower;
787 int upper_case = 0;
788
789 static int ignore_next_n = 0;
790 if (ignore_next_n > 0) {
791 ignore_next_n--;
792 return 8;
793 }
794
795 if (c == 0) return 0;
796
797 const char *from = (const char *)tr->langopts.replace_chars;
798 if (from == NULL)
799 return c;
800
801 // there is a list of character codes to be substituted with alternative codes
802
803 if (iswupper_portable(c_lower = c)) {
804 c_lower = towlower2(c, tr);
805 upper_case = 1;
806 }
807
808 const char *to = FindReplacementChars(tr, &from, c_lower, next, &ignore_next_n);
809 if (to == NULL)
810 return c; // no substitution
811
812 if (option_phonemes & espeakPHONEMES_TRACE)
813 fprintf(f_trans, "Replace: %s > %s\n", from, to);
814
815 to += utf8_in((int *)&new_c, to);
816 if (*to != 0) {
817 // there is a second character to be inserted
818 // don't convert the case of the second character unless the next letter is also upper case
819 to += utf8_in((int *)&c2, to);
820 if (upper_case && iswupper_portable(next_in))
821 c2 = ucd_toupper(c2);
822 *insert = c2;
823 }
824
825 if (upper_case)
826 new_c = ucd_toupper(new_c);
827
828 *wordflags |= FLAG_CHAR_REPLACED;
829 return new_c;
830 }
831
832 static int TranslateChar(Translator *tr, char *ptr, int prev_in, unsigned int c, unsigned int next_in, int *insert, int *wordflags)
833 {
834 // To allow language specific examination and replacement of characters
835
836 int code;
837 int next2;
838
839 static const unsigned char hangul_compatibility[0x34] = {
840 0, 0x00, 0x01, 0xaa, 0x02, 0xac, 0xad, 0x03,
841 0x04, 0x05, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb4,
842 0xb6, 0x06, 0x07, 0x08, 0xb9, 0x09, 0x0a, 0xbc,
843 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x61,
844 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
845 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71,
846 0x72, 0x73, 0x74, 0x75
847 };
848
849 // check for Korean Hangul letters
850 if (((code = c - 0xac00) >= 0) && (c <= 0xd7af)) {
851 // break a syllable hangul into 2 or 3 individual jamo
852 int initial = (code/28)/21;
853 int medial = (code/28) % 21;
854 int final = code % 28;
855
856 if (initial == 11) {
857 // null initial
858 c = medial + 0x1161;
859 if (final > 0)
860 *insert = final + 0x11a7;
861 } else {
862 // extract the initial and insert the remainder with a null initial
863 c = initial + 0x1100;
864 *insert = (11*28*21) + (medial*28) + final + 0xac00;
865 }
866 return c;
867 } else if (((code = c - 0x3130) >= 0) && (code < 0x34)) {
868 // Hangul compatibility jamo
869 return hangul_compatibility[code] + 0x1100;
870 }
871
872 switch (tr->translator_name)
873 {
874 case L('a', 'f'):
875 case L('n', 'l'):
876 // look for 'n and replace by a special character (unicode: schwa)
877
878 if ((c == '\'') && !iswalpha_portable(prev_in)) {
879 utf8_in(&next2, &ptr[1]);
880
881 if (IsSpace(next2)) {
882 if ((next_in == 'n') && (tr->translator_name == L('a', 'f'))) {
883 // n preceded by either apostrophe or U2019 "right single quotation mark"
884 ptr[0] = ' '; // delete the n
885 return 0x0259; // replace ' by unicode schwa character
886 }
887 if ((next_in == 'n') || (next_in == 't')) {
888 // Dutch, [@n] and [@t]
889 return 0x0259; // replace ' by unicode schwa character
890 }
891 }
892 }
893 break;
894 }
895 // handle .replace rule in xx_rules file
896 return SubstituteChar(tr, c, next_in, ptr, insert, wordflags);
897 }
898
899 static const char *const UCase_ga[] = { "bp", "bhf", "dt", "gc", "hA", "mb", "nd", "ng", "ts", "tA", "nA", NULL };
900
901 static int UpperCaseInWord(Translator *tr, char *word, int c)
902 {
903 if (tr->translator_name == L('g', 'a')) {
904 int ix;
905 const char *p;
906
907 for (ix = 0;; ix++) {
908 int len;
909 if ((p = UCase_ga[ix]) == NULL)
910 break;
911
912 len = strlen(p);
913 if ((word[-len] == ' ') && (memcmp(&word[-len+1], p, len-1) == 0)) {
914 if ((c == p[len-1]) || ((p[len-1] == 'A') && IsVowel(tr, c)))
915 return 1;
916 }
917 }
918 }
919 return 0;
920 }
921
922 void TranslateClause(Translator *tr, int *tone_out, char **voice_change)
923 {
924 int ix;
925 int c;
926 int cc = 0;
927 unsigned int source_index = 0;
928 int source_index_word = 0;
929 int prev_in;
930 int prev_out = ' ';
931 int prev_in_save = 0;
932 int next_in;
933 int next_in_nbytes;
934 int char_inserted = 0;
935 int clause_pause;
936 int pre_pause_add = 0;
937 int all_upper_case = FLAG_ALL_UPPER;
938 int alpha_count = 0;
939 bool finished = false;
940 bool single_quoted = false;
941 bool phoneme_mode = false;
942 int dict_flags = 0; // returned from dictionary lookup
943 int word_flags; // set here
944 int next_word_flags;
945 bool new_sentence2;
946 int embedded_count = 0;
947 int letter_count = 0;
948 bool space_inserted = false;
949 bool syllable_marked = false;
950 bool decimal_sep_count = false;
951 char *word;
952 char *p;
953 int j, k;
954 int n_digits;
955 int charix_top = 0;
956
957 short charix[N_TR_SOURCE+4];
958 WORD_TAB words[N_CLAUSE_WORDS];
959 static char voice_change_name[40];
960 int word_count = 0; // index into words
961
962 char sbuf[N_TR_SOURCE];
963
964 int terminator;
965 int tone;
966
967 if (tr == NULL)
968 return;
969
970 MAKE_MEM_UNDEFINED(&voice_change_name, sizeof(voice_change_name));
971
972 embedded_ix = 0;
973 embedded_read = 0;
974 pre_pause = 0;
975 any_stressed_words = false;
976
977 if ((clause_start_char = count_characters) < 0)
978 clause_start_char = 0;
979 clause_start_word = count_words + 1;
980
981 for (ix = 0; ix < N_TR_SOURCE; ix++)
982 charix[ix] = 0;
983 MAKE_MEM_UNDEFINED(&source, sizeof(source));
984 terminator = ReadClause(tr, source, charix, &charix_top, N_TR_SOURCE, &tone, voice_change_name);
985
986 if (tone_out != NULL) {
987 if (tone == 0)
988 *tone_out = (terminator & CLAUSE_INTONATION_TYPE) >> 12; // tone type not overridden in ReadClause, use default
989 else
990 *tone_out = tone; // override tone type
991 }
992
993 charix[charix_top+1] = 0;
994 charix[charix_top+2] = 0x7fff;
995 charix[charix_top+3] = 0;
996
997 clause_pause = (terminator & CLAUSE_PAUSE) * 10; // mS
998 if (terminator & CLAUSE_PAUSE_LONG)
999 clause_pause = clause_pause * 32; // pause value is *320mS not *10mS
1000
1001 for (p = source; *p != 0; p++) {
1002 if (!isspace2(*p))
1003 break;
1004 }
1005 if (*p == 0) {
1006 // No characters except spaces. This is not a sentence.
1007 // Don't add this pause, just make up the previous pause to this value;
1008 clause_pause -= max_clause_pause;
1009 if (clause_pause < 0)
1010 clause_pause = 0;
1011
1012 if (new_sentence)
1013 terminator |= CLAUSE_TYPE_SENTENCE; // carry forward an end-of-sentence indicator
1014 max_clause_pause += clause_pause;
1015 new_sentence2 = false;
1016 } else {
1017 max_clause_pause = clause_pause;
1018 new_sentence2 = new_sentence;
1019 }
1020 tr->clause_terminator = terminator;
1021
1022 if (new_sentence2) {
1023 count_sentences++;
1024 if (skip_sentences > 0) {
1025 skip_sentences--;
1026 if (skip_sentences == 0)
1027 skipping_text = false;
1028 }
1029 }
1030
1031 MAKE_MEM_UNDEFINED(&ph_list2, sizeof(ph_list2));
1032 memset(&ph_list2[0], 0, sizeof(ph_list2[0]));
1033 ph_list2[0].phcode = phonPAUSE_SHORT;
1034
1035 n_ph_list2 = 1;
1036 tr->prev_last_stress = 0;
1037 tr->prepause_timeout = 0;
1038 tr->expect_verb = 0;
1039 tr->expect_noun = 0;
1040 tr->expect_past = 0;
1041 tr->expect_verb_s = 0;
1042 tr->phonemes_repeat_count = 0;
1043 tr->end_stressed_vowel = 0;
1044 tr->prev_dict_flags[0] = 0;
1045 tr->prev_dict_flags[1] = 0;
1046
1047 word_count = 0;
1048 word_flags = 0;
1049 next_word_flags = 0;
1050
1051 sbuf[0] = 0;
1052 sbuf[1] = ' ';
1053 sbuf[2] = ' ';
1054 ix = 3;
1055 prev_in = ' ';
1056
1057 words[0].start = ix;
1058 words[0].flags = 0;
1059
1060 words[0].length = CalcWordLength(source_index, charix_top, charix, words, 0);
1061
1062 int prev_out2;
1063 while (!finished && (ix < (int)sizeof(sbuf) - 1)) {
1064 prev_out2 = prev_out;
1065 utf8_in2(&prev_out, &sbuf[ix-1], 1);
1066
1067 if (tr->langopts.tone_numbers && IsDigit09(prev_out) && IsAlpha(prev_out2)) {
1068 // tone numbers can be part of a word, consider them as alphabetic
1069 prev_out = 'a';
1070 }
1071
1072 if (prev_in_save != 0) {
1073 prev_in = prev_in_save;
1074 prev_in_save = 0;
1075 } else if (source_index > 0)
1076 utf8_in2(&prev_in, &source[source_index-1], 1);
1077
1078 unsigned int prev_source_index = source_index;
1079
1080 if (char_inserted) {
1081 c = char_inserted;
1082 char_inserted = 0;
1083 } else {
1084 source_index += utf8_in(&cc, &source[source_index]);
1085 c = cc;
1086 }
1087
1088 if (c == 0) {
1089 finished = true;
1090 c = ' ';
1091 next_in = ' ';
1092 next_in_nbytes = 0;
1093 }
1094 else
1095 next_in_nbytes = utf8_in(&next_in, &source[source_index]);
1096
1097 if (c == CTRL_EMBEDDED) {
1098 // start of embedded command in the text
1099 int srcix = source_index-1;
1100
1101 if (prev_in != ' ') {
1102 c = ' ';
1103 prev_in_save = c;
1104 source_index--;
1105 } else {
1106 embedded_count += EmbeddedCommand(&source_index);
1107 prev_in_save = prev_in;
1108 // replace the embedded command by spaces
1109 memset(&source[srcix], ' ', source_index-srcix);
1110 source_index = srcix;
1111 continue;
1112 }
1113 }
1114
1115 if ((option_sayas2 == SAYAS_KEY) && (c != ' ')) {
1116 if ((prev_in == ' ') && (next_in == ' '))
1117 option_sayas2 = SAYAS_SINGLE_CHARS; // single character, speak its name
1118 c = towlower2(c, tr);
1119 }
1120
1121
1122 if (phoneme_mode) {
1123 all_upper_case = FLAG_PHONEMES;
1124
1125 if ((c == ']') && (next_in == ']')) {
1126 phoneme_mode = false;
1127 source_index++;
1128 c = ' ';
1129 }
1130 } else if ((option_sayas2 & 0xf0) == SAYAS_DIGITS) {
1131 if (iswdigit_portable(c)) {
1132 count_sayas_digits++;
1133 if (count_sayas_digits > (option_sayas2 & 0xf)) {
1134 // break after the specified number of digits
1135 c = ' ';
1136 space_inserted = true;
1137 count_sayas_digits = 0;
1138 }
1139 } else {
1140 count_sayas_digits = 0;
1141 if (iswdigit_portable(prev_out)) {
1142 c = ' ';
1143 space_inserted = true;
1144 }
1145 }
1146 } else if ((option_sayas2 & 0x10) == 0) {
1147 // speak as words
1148
1149 if ((c == 0x92) || (c == 0xb4) || (c == 0x2019) || (c == 0x2032))
1150 c = '\''; // 'microsoft' quote or sexed closing single quote, or prime - possibly used as apostrophe
1151
1152 if (((c == 0x2018) || (c == '?')) && IsAlpha(prev_out) && IsAlpha(next_in)) {
1153 // ? between two letters may be a smart-quote replaced by ?
1154 c = '\'';
1155 }
1156
1157 if (c == CHAR_EMPHASIS) {
1158 // this character is a marker that the previous word is the focus of the clause
1159 c = ' ';
1160 word_flags |= FLAG_FOCUS;
1161 }
1162
1163 if (c == CHAR_COMMA_BREAK) {
1164 c = ' ';
1165 word_flags |= FLAG_COMMA_AFTER;
1166 }
1167 // language specific character translations
1168 c = TranslateChar(tr, &source[source_index], prev_in, c, next_in, &char_inserted, &word_flags);
1169 if (c == 8)
1170 continue; // ignore this character
1171
1172 if (char_inserted)
1173 next_in = char_inserted;
1174
1175 // allow certain punctuation within a word (usually only apostrophe)
1176 if (!IsAlpha(c) && !IsSpace(c) && (wcschr(tr->punct_within_word, c) == 0)) {
1177 if (IsAlpha(prev_out)) {
1178 if (tr->langopts.tone_numbers && IsDigit09(c) && !IsDigit09(next_in)) {
1179 // allow a tone number as part of the word
1180 } else {
1181 c = ' '; // ensure we have an end-of-word terminator
1182 space_inserted = true;
1183 }
1184 }
1185 }
1186
1187 if (iswdigit_portable(prev_out)) {
1188 if (!iswdigit_portable(c) && (c != '.') && (c != ',') && (c != ' ')) {
1189 c = ' '; // terminate digit string with a space
1190 space_inserted = true;
1191 }
1192 } else { // Prev output is not digit
1193 if (prev_in == ',') {
1194 // Workaround for several consecutive commas —
1195 // replace current character with space
1196 if (c == ',')
1197 c = ' ';
1198 } else {
1199 decimal_sep_count = false;
1200 }
1201 }
1202
1203 if (c == '[') {
1204 if ((next_in == '\002') || ((next_in == '[') && option_phoneme_input)) {
1205 // "[\002" is used internally to start phoneme mode
1206 phoneme_mode = true;
1207 source_index++;
1208 continue;
1209 }
1210 }
1211
1212 if (IsAlpha(c)) {
1213 alpha_count++;
1214 if (!IsAlpha(prev_out) || (tr->langopts.ideographs && ((c > 0x3040) || (prev_out > 0x3040)))) {
1215 if (wcschr(tr->punct_within_word, prev_out) == 0)
1216 letter_count = 0; // don't reset count for an apostrophy within a word
1217
1218 if ((prev_out != ' ') && (wcschr(tr->punct_within_word, prev_out) == 0)) {
1219 // start of word, insert space if not one there already
1220 c = ' ';
1221 space_inserted = true;
1222
1223 if (!IsBracket(prev_out)) // ?? perhaps only set FLAG_NOSPACE for . - / (hyphenated words, URLs, etc)
1224 next_word_flags |= FLAG_NOSPACE;
1225 } else {
1226 if (iswupper_portable(c))
1227 word_flags |= FLAG_FIRST_UPPER;
1228
1229 if ((prev_out == ' ') && iswdigit_portable(sbuf[ix-2]) && !iswdigit_portable(prev_in)) {
1230 // word, following a number, but with a space between
1231 // Add an extra space, to distinguish "2 a" from "2a"
1232 sbuf[ix++] = ' ';
1233 words[word_count].start++;
1234 }
1235 }
1236 }
1237
1238 if (c != ' ') {
1239 letter_count++;
1240
1241 if (tr->letter_bits_offset > 0) {
1242 if (((c < 0x250) && (prev_out >= tr->letter_bits_offset)) ||
1243 ((c >= tr->letter_bits_offset) && (letter_count > 1) && (prev_out < 0x250))) {
1244 // Don't mix native and Latin characters in the same word
1245 // Break into separate words
1246 if (IsAlpha(prev_out)) {
1247 c = ' ';
1248 space_inserted = true;
1249 word_flags |= FLAG_HYPHEN_AFTER;
1250 next_word_flags |= FLAG_HYPHEN;
1251 }
1252 }
1253 }
1254 }
1255
1256 if (iswupper_portable(c)) {
1257 c = towlower2(c, tr);
1258
1259 if (tr->langopts.param[LOPT_CAPS_IN_WORD]) {
1260 if (syllable_marked == false) {
1261 char_inserted = c;
1262 c = 0x2c8; // stress marker
1263 syllable_marked = true;
1264 }
1265 } else {
1266 if (iswlower_portable(prev_in)) {
1267 // lower case followed by upper case, possibly CamelCase
1268 if ((prev_out != ' ') && UpperCaseInWord(tr, &sbuf[ix], c) == 0) { // start a new word
1269 c = ' ';
1270 space_inserted = true;
1271 prev_in_save = c;
1272 }
1273 } else if ((c != ' ') && iswupper_portable(prev_in) && iswlower_portable(next_in)) {
1274 int next2_in;
1275 utf8_in(&next2_in, &source[source_index + next_in_nbytes]);
1276
1277 if ((tr->translator_name == L('n', 'l')) && (letter_count == 2) && (c == 'j') && (prev_in == 'I')) {
1278 // Dutch words may capitalise initial IJ, don't split
1279 } else if ((prev_out != ' ') && IsAlpha(next2_in)) {
1280 // changing from upper to lower case, start new word at the last uppercase, if 3 or more letters
1281 c = ' ';
1282 space_inserted = true;
1283 prev_in_save = c;
1284 next_word_flags |= FLAG_NOSPACE;
1285 }
1286 }
1287 }
1288 } else {
1289 if ((all_upper_case) && (letter_count > 2)) {
1290 // Flag as plural only English
1291 if (tr->translator_name == L('e', 'n') && (c == 's') && (next_in == ' ')) {
1292 c = ' ';
1293 all_upper_case |= FLAG_HAS_PLURAL;
1294
1295 if (sbuf[ix-1] == '\'')
1296 sbuf[ix-1] = ' ';
1297 } else
1298 all_upper_case = 0; // current word contains lower case letters, not "'s"
1299 } else
1300 all_upper_case = 0;
1301 }
1302 } else if (c == '-') {
1303 if (!IsSpace(prev_in) && IsAlpha(next_in)) {
1304 if (prev_out != ' ') {
1305 // previous 'word' not yet ended (not alpha or numeric), start new word now.
1306 c = ' ';
1307 space_inserted = true;
1308 } else {
1309 // '-' between two letters is a hyphen, treat as a space
1310 word_flags |= FLAG_HYPHEN;
1311 if (word_count > 0)
1312 words[word_count-1].flags |= FLAG_HYPHEN_AFTER;
1313 c = ' ';
1314 }
1315 } else if ((prev_in == ' ') && (next_in == ' ')) {
1316 // ' - ' dash between two spaces, treat as pause
1317 c = ' ';
1318 pre_pause_add = 4;
1319 } else if (next_in == '-') {
1320 // double hyphen, treat as pause
1321 source_index++;
1322 c = ' ';
1323 pre_pause_add = 4;
1324 } else if ((prev_out == ' ') && IsAlpha(prev_out2) && !IsAlpha(prev_in)) {
1325 // insert extra space between a word + space + hyphen, to distinguish 'a -2' from 'a-2'
1326 sbuf[ix++] = ' ';
1327 words[word_count].start++;
1328 }
1329 } else if (c == '.') {
1330 if (prev_out == '.') {
1331 // multiple dots, separate by spaces. Note >3 dots has been replaced by elipsis
1332 c = ' ';
1333 space_inserted = true;
1334 } else if ((word_count > 0) && !(words[word_count-1].flags & FLAG_NOSPACE) && IsAlpha(prev_in)) {
1335 // dot after a word, with space following, probably an abbreviation
1336 words[word_count-1].flags |= FLAG_HAS_DOT;
1337
1338 if (IsSpace(next_in) || (next_in == '-'))
1339 c = ' '; // remove the dot if it's followed by a space or hyphen, so that it's not pronounced
1340 }
1341 } else if (c == '\'') {
1342 if (((prev_in == '.' && next_in == 's') || iswalnum_portable(prev_in)) && IsAlpha(next_in)) {
1343 // between two letters, or in an abbreviation (eg. u.s.a.'s). Consider the apostrophe as part of the word
1344 single_quoted = false;
1345 } else if ((tr->langopts.param[LOPT_APOSTROPHE] & 1) && IsAlpha(next_in))
1346 single_quoted = false; // apostrophe at start of word is part of the word
1347 else if ((tr->langopts.param[LOPT_APOSTROPHE] & 2) && IsAlpha(prev_in))
1348 single_quoted = false; // apostrophe at end of word is part of the word
1349 else if ((wcschr(tr->char_plus_apostrophe, prev_in) != 0) && (prev_out2 == ' ')) {
1350 // consider single character plus apostrophe as a word
1351 single_quoted = false;
1352 if (next_in == ' ')
1353 source_index++; // skip following space
1354 } else {
1355 if ((prev_out == 's') && (single_quoted == false)) {
1356 // looks like apostrophe after an 's'
1357 c = ' ';
1358 } else {
1359 if (IsSpace(prev_out))
1360 single_quoted = true;
1361 else
1362 single_quoted = false;
1363
1364 pre_pause_add = 4; // single quote
1365 c = ' ';
1366 }
1367 }
1368 } else if (lookupwchar(breaks, c) != 0)
1369 c = ' '; // various characters to treat as space
1370 else if (iswdigit_portable(c)) {
1371 if (tr->langopts.tone_numbers && IsAlpha(prev_out) && !IsDigit(next_in)) {
1372 } else if ((prev_out != ' ') && !iswdigit_portable(prev_out)) {
1373 if ((prev_out != tr->langopts.decimal_sep) || ((decimal_sep_count == true) && (tr->langopts.decimal_sep == ','))) {
1374 c = ' ';
1375 space_inserted = true;
1376 } else
1377 decimal_sep_count = true;
1378 } else if ((prev_out == ' ') && IsAlpha(prev_out2) && !IsAlpha(prev_in)) {
1379 // insert extra space between a word and a number, to distinguish 'a 2' from 'a2'
1380 sbuf[ix++] = ' ';
1381 words[word_count].start++;
1382 }
1383 }
1384 }
1385
1386 if (IsSpace(c)) {
1387 if (prev_out == ' ') {
1388 word_flags |= FLAG_MULTIPLE_SPACES;
1389 continue; // multiple spaces
1390 }
1391
1392 if ((cc == 0x09) || (cc == 0x0a))
1393 next_word_flags |= FLAG_MULTIPLE_SPACES; // tab or newline, not a simple space
1394
1395 if (space_inserted) {
1396 // count the number of characters since the start of the word
1397 j = 0;
1398 k = source_index - 1;
1399 while ((k >= source_index_word) && (charix[k] != 0)) {
1400 if (charix[k] > 0) // don't count initial bytes of multi-byte character
1401 j++;
1402 k--;
1403 }
1404 words[word_count].length = j;
1405 }
1406
1407 source_index_word = source_index;
1408
1409 // end of 'word'
1410 sbuf[ix++] = ' ';
1411
1412 if ((word_count < N_CLAUSE_WORDS-1) && (ix > words[word_count].start)) {
1413 if (embedded_count > 0) {
1414 // there are embedded commands before this word
1415 embedded_list[embedded_ix-1] |= 0x80; // terminate list of commands for this word
1416 words[word_count].flags |= FLAG_EMBEDDED;
1417 embedded_count = 0;
1418 }
1419 if (alpha_count == 0) {
1420 all_upper_case &= ~FLAG_ALL_UPPER;
1421 }
1422 words[word_count].pre_pause = pre_pause;
1423 words[word_count].flags |= (all_upper_case | word_flags | word_emphasis);
1424
1425 if (pre_pause > 0) {
1426 // insert an extra space before the word, to prevent influence from previous word across the pause
1427 for (j = ix; j > words[word_count].start; j--)
1428 sbuf[j] = sbuf[j-1];
1429 sbuf[j] = ' ';
1430 words[word_count].start++;
1431 ix++;
1432 }
1433
1434 word_count++;
1435 words[word_count].start = ix;
1436 words[word_count].flags = 0;
1437
1438 words[word_count].length = CalcWordLength(source_index, charix_top, charix, words, word_count);
1439
1440 word_flags = next_word_flags;
1441 next_word_flags = 0;
1442 pre_pause = 0;
1443 all_upper_case = FLAG_ALL_UPPER;
1444 alpha_count = 0;
1445 syllable_marked = false;
1446 }
1447
1448 if (space_inserted) {
1449 source_index = prev_source_index; // rewind to the previous character
1450 char_inserted = 0;
1451 space_inserted = false;
1452 }
1453 } else {
1454 if ((ix < (N_TR_SOURCE - 4)))
1455 ix += utf8_out(c, &sbuf[ix]);
1456 }
1457 if (pre_pause_add > pre_pause)
1458 pre_pause = pre_pause_add;
1459 pre_pause_add = 0;
1460 }
1461
1462 if ((word_count == 0) && (embedded_count > 0)) {
1463 // add a null 'word' to carry the embedded command flag
1464 embedded_list[embedded_ix-1] |= 0x80;
1465 words[word_count].flags |= FLAG_EMBEDDED;
1466 word_count = 1;
1467 }
1468
1469 tr->clause_end = &sbuf[ix-1];
1470 sbuf[ix] = 0;
1471 words[0].pre_pause = 0; // don't add extra pause at beginning of clause
1472 words[word_count].pre_pause = 8;
1473 if (word_count > 0) {
1474 ix = word_count-1;
1475 while ((ix > 0) && (IsBracket(sbuf[words[ix].start])))
1476 ix--; // the last word is a bracket, mark the previous word as last
1477 words[ix].flags |= FLAG_LAST_WORD;
1478
1479 // FLAG_NOSPACE check to avoid recognizing .mr -mr
1480 if ((terminator & CLAUSE_DOT_AFTER_LAST_WORD) && !(words[word_count-1].flags & FLAG_NOSPACE))
1481 words[word_count-1].flags |= FLAG_HAS_DOT;
1482 }
1483 words[0].flags |= FLAG_FIRST_WORD;
1484
1485 // Each TranslateWord2 may require up to 7 phonemes
1486 // and after this loop we require 2 phonemes
1487 for (ix = 0; ix < word_count && (n_ph_list2 < N_PHONEME_LIST-7-2); ix++) {
1488 int nx;
1489 int c_temp;
1490 char *pn;
1491 char *pw;
1492 char number_buf[150];
1493 WORD_TAB num_wtab[50]; // copy of 'words', when splitting numbers into parts
1494
1495 // start speaking at a specified word position in the text?
1496 count_words++;
1497 if (skip_words > 0) {
1498 skip_words--;
1499 if (skip_words == 0)
1500 skipping_text = false;
1501 }
1502 if (skipping_text)
1503 continue;
1504
1505 current_alphabet = NULL;
1506
1507 // digits should have been converted to Latin alphabet ('0' to '9')
1508 word = pw = &sbuf[words[ix].start];
1509
1510 if (iswdigit_portable(word[0]) && (tr->langopts.break_numbers != BREAK_THOUSANDS)) {
1511 // Languages with 100000 numbers. Remove thousands separators so that we can insert them again later
1512 pn = number_buf;
1513 while (pn < &number_buf[sizeof(number_buf)-20]) {
1514 if (iswdigit_portable(*pw))
1515 *pn++ = *pw++;
1516 else if ((*pw == tr->langopts.thousands_sep) && (pw[1] == ' ')
1517 && iswdigit_portable(pw[2]) && (pw[3] != ' ') && (pw[4] != ' ')) { // don't allow only 1 or 2 digits in the final part
1518 pw += 2;
1519 ix++; // skip "word"
1520 } else {
1521 nx = pw - word;
1522 memset(word, ' ', nx);
1523 nx = pn - number_buf;
1524 memcpy(word, number_buf, nx);
1525 break;
1526 }
1527 }
1528 pw = word;
1529 }
1530
1531 for (n_digits = 0; iswdigit_portable(word[n_digits]); n_digits++) // count consecutive digits
1532 ;
1533
1534 if (n_digits > 4 && n_digits <= 32) {
1535 // word is entirely digits, insert commas and break into 3 digit "words"
1536 int nw = 0;
1537
1538 number_buf[0] = ' ';
1539 number_buf[1] = ' ';
1540 number_buf[2] = ' ';
1541 pn = &number_buf[3];
1542 nx = n_digits;
1543
1544 if ((n_digits > tr->langopts.max_digits) || (word[0] == '0'))
1545 words[ix].flags |= FLAG_INDIVIDUAL_DIGITS;
1546
1547 while (pn < &number_buf[sizeof(number_buf)-20]) {
1548 if (!IsDigit09(c = *pw++) && (c != tr->langopts.decimal_sep))
1549 break;
1550
1551 *pn++ = c;
1552 nx--;
1553 if ((nx > 0) && (tr->langopts.break_numbers & (1U << nx))) {
1554 memcpy(&num_wtab[nw++], &words[ix], sizeof(WORD_TAB)); // copy the 'words' entry for each word of numbers
1555
1556 if (tr->langopts.thousands_sep != ' ')
1557 *pn++ = tr->langopts.thousands_sep;
1558 *pn++ = ' ';
1559
1560 if ((words[ix].flags & FLAG_INDIVIDUAL_DIGITS) == 0) {
1561 if (tr->langopts.break_numbers & (1 << (nx-1))) {
1562 // the next group only has 1 digits, make it three
1563 *pn++ = '0';
1564 *pn++ = '0';
1565 }
1566 if (tr->langopts.break_numbers & (1 << (nx-2))) {
1567 // the next group only has 2 digits (eg. Indian languages), make it three
1568 *pn++ = '0';
1569 }
1570 }
1571 }
1572 }
1573 pw--;
1574 memcpy(&num_wtab[nw], &words[ix], sizeof(WORD_TAB)*2); // the original number word, and the word after it
1575
1576 for (j = 1; j <= nw; j++)
1577 num_wtab[j].flags &= ~(FLAG_MULTIPLE_SPACES | FLAG_EMBEDDED); // don't use these flags for subsequent parts when splitting a number
1578
1579 // include the next few characters, in case there are an ordinal indicator or other suffix
1580 memcpy(pn, pw, 16);
1581 pn[16] = 0;
1582 nw = 0;
1583
1584 for (pw = &number_buf[3]; pw < pn;) {
1585 // keep wflags for each part, for FLAG_HYPHEN_AFTER
1586 dict_flags = TranslateWord2(tr, pw, &num_wtab[nw++], words[ix].pre_pause);
1587 while (*pw++ != ' ')
1588 ;
1589 words[ix].pre_pause = 0;
1590 }
1591 } else {
1592 pre_pause = 0;
1593
1594 dict_flags = TranslateWord2(tr, word, &words[ix], words[ix].pre_pause);
1595
1596 if (pre_pause > words[ix+1].pre_pause) {
1597 words[ix+1].pre_pause = pre_pause;
1598 pre_pause = 0;
1599 }
1600
1601 if (dict_flags & FLAG_SPELLWORD) {
1602 // redo the word, speaking single letters
1603 for (pw = word; *pw != ' ';) {
1604 memset(number_buf, 0, sizeof(number_buf));
1605 memset(number_buf, ' ', 9);
1606 nx = utf8_in(&c_temp, pw);
1607 memcpy(&number_buf[2], pw, nx);
1608 TranslateWord2(tr, &number_buf[2], &words[ix], 0);
1609 pw += nx;
1610 }
1611 }
1612
1613 if ((dict_flags & (FLAG_ALLOW_DOT | FLAG_NEEDS_DOT)) && (ix == word_count - 1 - dictionary_skipwords) && (terminator & CLAUSE_DOT_AFTER_LAST_WORD)) {
1614 // probably an abbreviation such as Mr. or B. rather than end of sentence
1615 clause_pause = 10;
1616 if (tone_out != NULL)
1617 *tone_out = 4;
1618 }
1619 }
1620
1621 if (dict_flags & FLAG_SKIPWORDS) {
1622 // dictionary indicates skip next word(s)
1623 while (dictionary_skipwords > 0) {
1624 words[ix+dictionary_skipwords].flags |= FLAG_DELETE_WORD;
1625 dictionary_skipwords--;
1626 }
1627 }
1628 }
1629
1630 if (embedded_read < embedded_ix) {
1631 // any embedded commands not yet processed?
1632 Word_EmbeddedCmd();
1633 }
1634
1635 for (ix = 0; ix < 2; ix++) {
1636 // terminate the clause with 2 PAUSE phonemes
1637 PHONEME_LIST2 *p2;
1638 p2 = &ph_list2[n_ph_list2 + ix];
1639 p2->phcode = phonPAUSE;
1640 p2->stresslevel = 0;
1641 p2->sourceix = source_index;
1642 p2->synthflags = 0;
1643 }
1644 n_ph_list2 += 2;
1645
1646 if (Eof() && ((word_count == 0) || (option_endpause == 0)))
1647 clause_pause = 10;
1648
1649 MakePhonemeList(tr, clause_pause, new_sentence2);
1650 phoneme_list[N_PHONEME_LIST].ph = NULL; // recognize end of phoneme_list array, in Generate()
1651 phoneme_list[N_PHONEME_LIST].sourceix = 1;
1652
1653 if (embedded_count) { // ???? is this needed
1654 phoneme_list[n_phoneme_list-2].synthflags = SFLAG_EMBEDDED;
1655 embedded_list[embedded_ix-1] |= 0x80;
1656 embedded_list[embedded_ix] = 0x80;
1657 }
1658
1659 new_sentence = false;
1660 if (terminator & CLAUSE_TYPE_SENTENCE)
1661 new_sentence = true; // next clause is a new sentence
1662
1663 if (voice_change != NULL) {
1664 // return new voice name if an embedded voice change command terminated the clause
1665 if (terminator & CLAUSE_TYPE_VOICE_CHANGE)
1666 *voice_change = voice_change_name;
1667 else
1668 *voice_change = NULL;
1669 }
1670 }
1671
1672 static int CalcWordLength(int source_index, int charix_top, short int *charix, WORD_TAB *words, int word_count) {
1673 int j;
1674 int k;
1675
1676 for (j = source_index; j < charix_top && charix[j] <= 0; j++); // skip blanks
1677 words[word_count].sourceix = charix[j];
1678 k = 0;
1679 while (charix[j] != 0) {
1680 // count the number of characters (excluding multibyte continuation bytes)
1681 if (charix[j++] != -1)
1682 k++;
1683 }
1684 return k;
1685 }
1686
1687 static void CombineFlag(Translator *tr, WORD_TAB *wtab, char *word, int *flags, unsigned char *p, char *wordPhonemes) {
1688 // combine a preposition with the following word
1689
1690
1691 int sylimit; // max. number of syllables in a word to be combined with a preceding preposition
1692 sylimit = tr->langopts.param[LOPT_COMBINE_WORDS];
1693
1694
1695 char *p2;
1696 p2 = word;
1697 while (*p2 != ' ') p2++;
1698
1699 bool ok = true;
1700 int c_word2;
1701
1702 utf8_in(&c_word2, p2+1); // first character of the next word;
1703
1704 if (!iswalpha_portable(c_word2))
1705 ok = false;
1706
1707 int flags2[2];
1708 flags2[0] = 0;
1709
1710
1711 if (ok) {
1712 char ph_buf[N_WORD_PHONEMES];
1713 strcpy(ph_buf, wordPhonemes);
1714
1715 flags2[0] = TranslateWord(tr, p2+1, wtab+1, NULL);
1716 if ((flags2[0] & FLAG_WAS_UNPRONOUNCABLE) || (wordPhonemes[0] == phonSWITCH))
1717 ok = false;
1718
1719 if ((sylimit & 0x100) && ((flags2[0] & FLAG_ALT_TRANS) == 0)) {
1720 // only if the second word has $alt attribute
1721 ok = false;
1722 }
1723
1724 if ((sylimit & 0x200) && ((wtab+1)->flags & FLAG_LAST_WORD)) {
1725 // not if the next word is end-of-sentence
1726 ok = false;
1727 }
1728
1729 if (ok == false)
1730 strcpy(wordPhonemes, ph_buf);
1731 }
1732
1733 if (ok) {
1734 *p2 = '-'; // replace next space by hyphen
1735 wtab[0].flags &= ~FLAG_ALL_UPPER; // prevent it being considered an abbreviation
1736 *flags = TranslateWord(translator, word, wtab, NULL); // translate the combined word
1737 if ((sylimit > 0) && (CountSyllables(p) > (sylimit & 0x1f))) {
1738 // revert to separate words
1739 *p2 = ' ';
1740 *flags = TranslateWord(translator, word, wtab, NULL);
1741 } else {
1742 if (*flags == 0)
1743 *flags = flags2[0]; // no flags for the combined word, so use flags from the second word eg. lang-hu "nem december 7-e"
1744 *flags |= FLAG_SKIPWORDS;
1745 dictionary_skipwords = 1;
1746 }
1747 }
1748 }
1749
1750 static void SwitchLanguage(char *word, char *wordPhonemes) {
1751 char lang_name[12];
1752 int ix;
1753
1754 word += 3;
1755
1756 for (ix = 0;;) {
1757 int c1;
1758 c1 = *word++;
1759 if ((c1 == ' ') || (c1 == 0))
1760 break;
1761 lang_name[ix++] = tolower(c1);
1762 }
1763 lang_name[ix] = 0;
1764
1765 if ((ix = LookupPhonemeTable(lang_name)) > 0) {
1766 SelectPhonemeTable(ix);
1767 wordPhonemes[0] = phonSWITCH;
1768 wordPhonemes[1] = ix;
1769 wordPhonemes[2] = 0;
1770 }
1771 }
1772
1773 void InitText(int control)
1774 {
1775 count_sentences = 0;
1776 count_words = 0;
1777 end_character_position = 0;
1778 skip_sentences = 0;
1779 skip_marker[0] = 0;
1780 skip_words = 0;
1781 skip_characters = 0;
1782 skipping_text = false;
1783 new_sentence = true;
1784
1785 option_sayas = 0;
1786 option_sayas2 = 0;
1787 option_emphasis = 0;
1788 word_emphasis = 0;
1789 embedded_flag = 0;
1790
1791 InitText2();
1792
1793 if ((control & espeakKEEP_NAMEDATA) == 0)
1794 InitNamedata();
1795 }