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

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