eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

translate.c 52KB

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