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

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