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

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