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 78KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749
  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 "dictionary.h" // for TranslateRules, LookupDictList, Cha...
  33. #include "numbers.h" // for SetSpellingStress, TranslateLetter
  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. Translator *translator = NULL; // the main translator
  43. Translator *translator2 = NULL; // secondary translator for certain words
  44. static char translator2_language[20] = { 0 };
  45. Translator *translator3 = NULL; // tertiary translator for certain words
  46. static char translator3_language[20] = { 0 };
  47. FILE *f_trans = NULL; // phoneme output text
  48. int option_tone_flags = 0; // bit 8=emphasize allcaps, bit 9=emphasize penultimate stress
  49. int option_phonemes = 0;
  50. int option_phoneme_events = 0;
  51. int option_endpause = 0; // suppress pause after end of text
  52. int option_capitals = 0;
  53. int option_punctuation = 0;
  54. int option_sayas = 0;
  55. static int option_sayas2 = 0; // used in translate_clause()
  56. static int option_emphasis = 0; // 0=normal, 1=normal, 2=weak, 3=moderate, 4=strong
  57. int option_ssml = 0;
  58. int option_phoneme_input = 0; // allow [[phonemes]] in input
  59. int option_wordgap = 0;
  60. static int count_sayas_digits;
  61. int skip_sentences;
  62. int skip_words;
  63. int skip_characters;
  64. char skip_marker[N_MARKER_LENGTH];
  65. bool skipping_text; // waiting until word count, sentence count, or named marker is reached
  66. int end_character_position;
  67. int count_sentences;
  68. int count_words;
  69. int clause_start_char;
  70. int clause_start_word;
  71. bool new_sentence;
  72. static int word_emphasis = 0; // set if emphasis level 3 or 4
  73. static int embedded_flag = 0; // there are embedded commands to be applied to the next phoneme, used in TranslateWord2()
  74. static int prev_clause_pause = 0;
  75. static int max_clause_pause = 0;
  76. static bool any_stressed_words;
  77. int pre_pause;
  78. ALPHABET *current_alphabet;
  79. // these were previously in translator class
  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. // brackets, also 0x2014 to 0x021f which don't need to be in this list
  95. static const unsigned short brackets[] = {
  96. '(', ')', '[', ']', '{', '}', '<', '>', '"', '\'', '`',
  97. 0xab, 0xbb, // double angle brackets
  98. 0x300a, 0x300b, // double angle brackets (ideograph)
  99. 0xe000+'<', // private usage area
  100. 0
  101. };
  102. // other characters which break a word, but don't produce a pause
  103. static const unsigned short breaks[] = { '_', 0 };
  104. int IsAlpha(unsigned int c)
  105. {
  106. // Replacement for iswalph() which also checks for some in-word symbols
  107. static const unsigned short extra_indic_alphas[] = {
  108. 0xa70, 0xa71, // Gurmukhi: tippi, addak
  109. 0
  110. };
  111. if (iswalpha(c))
  112. return 1;
  113. if (c < 0x300)
  114. return 0;
  115. if ((c >= 0x901) && (c <= 0xdf7)) {
  116. // Indic scripts: Devanagari, Tamil, etc
  117. if ((c & 0x7f) < 0x64)
  118. return 1;
  119. if (lookupwchar(extra_indic_alphas, c) != 0)
  120. return 1;
  121. if ((c >= 0xd7a) && (c <= 0xd7f))
  122. return 1; // malaytalam chillu characters
  123. return 0;
  124. }
  125. if ((c >= 0x5b0) && (c <= 0x5c2))
  126. return 1; // Hebrew vowel marks
  127. if (c == 0x0605)
  128. return 1;
  129. if ((c == 0x670) || ((c >= 0x64b) && (c <= 0x65e)))
  130. return 1; // arabic vowel marks
  131. if ((c >= 0x300) && (c <= 0x36f))
  132. return 1; // combining accents
  133. if ((c >= 0xf40) && (c <= 0xfbc))
  134. return 1; // tibetan
  135. if ((c >= 0x1100) && (c <= 0x11ff))
  136. return 1; // Korean jamo
  137. if ((c >= 0x2800) && (c <= 0x28ff))
  138. return 1; // braille
  139. if ((c > 0x3040) && (c <= 0xa700))
  140. return 1; // Chinese/Japanese. Should never get here, but Mac OS 10.4's iswalpha seems to be broken, so just make sure
  141. return 0;
  142. }
  143. int IsDigit09(unsigned int c)
  144. {
  145. if ((c >= '0') && (c <= '9'))
  146. return 1;
  147. return 0;
  148. }
  149. int IsDigit(unsigned int c)
  150. {
  151. if (iswdigit(c))
  152. return 1;
  153. if ((c >= 0x966) && (c <= 0x96f))
  154. return 1;
  155. return 0;
  156. }
  157. static int IsSpace(unsigned int c)
  158. {
  159. if (c == 0)
  160. return 0;
  161. if ((c >= 0x2500) && (c < 0x25a0))
  162. return 1; // box drawing characters
  163. if ((c >= 0xfff9) && (c <= 0xffff))
  164. return 1; // unicode specials
  165. return iswspace(c);
  166. }
  167. int isspace2(unsigned int c)
  168. {
  169. // can't use isspace() because on Windows, isspace(0xe1) gives TRUE !
  170. int c2;
  171. if (((c2 = (c & 0xff)) == 0) || (c > ' '))
  172. return 0;
  173. return 1;
  174. }
  175. void DeleteTranslator(Translator *tr)
  176. {
  177. if (!tr) return;
  178. if (tr->data_dictlist != NULL)
  179. free(tr->data_dictlist);
  180. free(tr);
  181. }
  182. int lookupwchar(const unsigned short *list, int c)
  183. {
  184. // Is the character c in the list ?
  185. int ix;
  186. for (ix = 0; list[ix] != 0; ix++) {
  187. if (list[ix] == c)
  188. return ix+1;
  189. }
  190. return 0;
  191. }
  192. int lookupwchar2(const unsigned short *list, int c)
  193. {
  194. // Replace character c by another character.
  195. // Returns 0 = not found, 1 = delete character
  196. int ix;
  197. for (ix = 0; list[ix] != 0; ix += 2) {
  198. if (list[ix] == c)
  199. return list[ix+1];
  200. }
  201. return 0;
  202. }
  203. int IsBracket(int c)
  204. {
  205. if ((c >= 0x2014) && (c <= 0x201f))
  206. return 1;
  207. return lookupwchar(brackets, c);
  208. }
  209. int utf8_nbytes(const char *buf)
  210. {
  211. // Returns the number of bytes for the first UTF-8 character in buf
  212. unsigned char c = (unsigned char)buf[0];
  213. if (c < 0x80)
  214. return 1;
  215. if (c < 0xe0)
  216. return 2;
  217. if (c < 0xf0)
  218. return 3;
  219. return 4;
  220. }
  221. int utf8_in2(int *c, const char *buf, int backwards)
  222. {
  223. // Reads a unicode characater from a UTF8 string
  224. // Returns the number of UTF8 bytes used.
  225. // c: holds integer representation of multibyte character
  226. // buf: position of buffer is moved, if character is read
  227. // backwards: set if we are moving backwards through the UTF8 string
  228. int c1;
  229. int n_bytes;
  230. int ix;
  231. static const unsigned char mask[4] = { 0xff, 0x1f, 0x0f, 0x07 };
  232. // find the start of the next/previous character
  233. while ((*buf & 0xc0) == 0x80) {
  234. // skip over non-initial bytes of a multi-byte utf8 character
  235. if (backwards)
  236. buf--;
  237. else
  238. buf++;
  239. }
  240. n_bytes = 0;
  241. if ((c1 = *buf++) & 0x80) {
  242. if ((c1 & 0xe0) == 0xc0)
  243. n_bytes = 1;
  244. else if ((c1 & 0xf0) == 0xe0)
  245. n_bytes = 2;
  246. else if ((c1 & 0xf8) == 0xf0)
  247. n_bytes = 3;
  248. c1 &= mask[n_bytes];
  249. for (ix = 0; ix < n_bytes; ix++)
  250. c1 = (c1 << 6) + (*buf++ & 0x3f);
  251. }
  252. *c = c1;
  253. return n_bytes+1;
  254. }
  255. #pragma GCC visibility push(default)
  256. int utf8_in(int *c, const char *buf)
  257. {
  258. /* Read a unicode characater from a UTF8 string
  259. * Returns the number of UTF8 bytes used.
  260. * buf: position of buffer is moved, if character is read
  261. * c: holds UTF-16 representation of multibyte character by
  262. * skipping UTF-8 header bits of bytes in following way:
  263. * 2-byte character "ā":
  264. * hex binary
  265. * c481 1100010010000001
  266. * | 11000100 000001
  267. * V \ \ | |
  268. * 0101 0000000100000001
  269. * 3-byte character "ꙅ":
  270. * ea9985 111010101001100110000101
  271. * 1010 011001 000101
  272. * | + +--.\ \ | |
  273. * V `--. \`. `.| |
  274. * A645 1010011001000101
  275. * 4-byte character "𠜎":
  276. * f0a09c8e 11110000101000001001110010001110
  277. * V 000 100000 011100 001110
  278. * 02070e 000000100000011100001110
  279. */
  280. return utf8_in2(c, buf, 0);
  281. }
  282. #pragma GCC visibility pop
  283. int utf8_out(unsigned int c, char *buf)
  284. {
  285. // write a UTF-16 character into a buffer as UTF-8
  286. // returns the number of bytes written
  287. int n_bytes;
  288. int j;
  289. int shift;
  290. static const char unsigned code[4] = { 0, 0xc0, 0xe0, 0xf0 };
  291. if (c < 0x80) {
  292. buf[0] = c;
  293. return 1;
  294. }
  295. if (c >= 0x110000) {
  296. buf[0] = ' '; // out of range character code
  297. return 1;
  298. }
  299. if (c < 0x0800)
  300. n_bytes = 1;
  301. else if (c < 0x10000)
  302. n_bytes = 2;
  303. else
  304. n_bytes = 3;
  305. shift = 6*n_bytes;
  306. buf[0] = code[n_bytes] | (c >> shift);
  307. for (j = 0; j < n_bytes; j++) {
  308. shift -= 6;
  309. buf[j+1] = 0x80 + ((c >> shift) & 0x3f);
  310. }
  311. return n_bytes+1;
  312. }
  313. char *strchr_w(const char *s, int c)
  314. {
  315. // return NULL for any non-ascii character
  316. if (c >= 0x80)
  317. return NULL;
  318. return strchr((char *)s, c); // (char *) is needed for Borland compiler
  319. }
  320. // append plural suffixes depending on preceding letter
  321. static void addPluralSuffixes(int flags, Translator *tr, char last_char, char *word_phonemes)
  322. {
  323. char word_zz[4] = { ' ', 'z', 'z', 0 };
  324. char word_iz[4] = { ' ', 'i', 'z', 0 };
  325. char word_ss[4] = { ' ', 's', 's', 0 };
  326. if (flags & FLAG_HAS_PLURAL) {
  327. // s or 's suffix, append [s], [z] or [Iz] depending on previous letter
  328. if (last_char == 'f')
  329. TranslateRules(tr, &word_ss[1], word_phonemes, N_WORD_PHONEMES,
  330. NULL, 0, NULL);
  331. else if ((last_char == 0) || (strchr_w("hsx", last_char) == NULL))
  332. TranslateRules(tr, &word_zz[1], word_phonemes, N_WORD_PHONEMES,
  333. NULL, 0, NULL);
  334. else
  335. TranslateRules(tr, &word_iz[1], word_phonemes, N_WORD_PHONEMES,
  336. NULL, 0, NULL);
  337. }
  338. }
  339. static char *SpeakIndividualLetters(Translator *tr, char *word, char *phonemes, int spell_word)
  340. {
  341. int posn = 0;
  342. int capitals = 0;
  343. bool non_initial = false;
  344. if (spell_word > 2)
  345. capitals = 2; // speak 'capital'
  346. if (spell_word > 1)
  347. capitals |= 4; // speak character code for unknown letters
  348. while ((*word != ' ') && (*word != 0)) {
  349. word += TranslateLetter(tr, word, phonemes, capitals | non_initial, current_alphabet);
  350. posn++;
  351. non_initial = true;
  352. if (phonemes[0] == phonSWITCH) {
  353. // change to another language in order to translate this word
  354. strcpy(word_phonemes, phonemes);
  355. return NULL;
  356. }
  357. }
  358. SetSpellingStress(tr, phonemes, spell_word, posn);
  359. return word;
  360. }
  361. static int CheckDottedAbbrev(char *word1)
  362. {
  363. int wc;
  364. int count = 0;
  365. int nbytes;
  366. int ok;
  367. int ix;
  368. char *word;
  369. char *wbuf;
  370. char word_buf[80];
  371. word = word1;
  372. wbuf = word_buf;
  373. for (;;) {
  374. ok = 0;
  375. nbytes = utf8_in(&wc, word);
  376. if ((word[nbytes] == ' ') && IsAlpha(wc)) {
  377. if (word[nbytes+1] == '.') {
  378. if (word[nbytes+2] == ' ')
  379. ok = 1;
  380. else if (word[nbytes+2] == '\'' && word[nbytes+3] == 's') {
  381. nbytes += 2; // delete the final dot (eg. u.s.a.'s)
  382. ok = 2;
  383. }
  384. } else if ((count > 0) && (word[nbytes] == ' '))
  385. ok = 2;
  386. }
  387. if (ok == 0)
  388. break;
  389. for (ix = 0; ix < nbytes; ix++)
  390. *wbuf++ = word[ix];
  391. count++;
  392. if (ok == 2) {
  393. word += nbytes;
  394. break;
  395. }
  396. word += (nbytes + 3);
  397. }
  398. if (count > 1) {
  399. ix = wbuf - word_buf;
  400. memcpy(word1, word_buf, ix);
  401. while (&word1[ix] < word)
  402. word1[ix++] = ' ';
  403. dictionary_skipwords = (count - 1)*2;
  404. }
  405. return count;
  406. }
  407. static int TranslateWord3(Translator *tr, char *word_start, WORD_TAB *wtab, char *word_out)
  408. {
  409. // word1 is terminated by space (0x20) character
  410. char *word1;
  411. int word_length;
  412. int ix;
  413. char *p;
  414. int pfix;
  415. int n_chars;
  416. unsigned int dictionary_flags[2];
  417. unsigned int dictionary_flags2[2];
  418. int end_type = 0;
  419. int end_type1 = 0;
  420. int prefix_type = 0;
  421. int prefix_stress;
  422. char *wordx;
  423. char phonemes[N_WORD_PHONEMES];
  424. char phonemes2[N_WORD_PHONEMES];
  425. char prefix_phonemes[N_WORD_PHONEMES];
  426. char unpron_phonemes[N_WORD_PHONEMES];
  427. char end_phonemes[N_WORD_PHONEMES];
  428. char end_phonemes2[N_WORD_PHONEMES];
  429. char word_copy[N_WORD_BYTES];
  430. char word_copy2[N_WORD_BYTES];
  431. int word_copy_length;
  432. char prefix_chars[0x3f + 2];
  433. bool found = false;
  434. int end_flags;
  435. int c_temp; // save a character byte while we temporarily replace it with space
  436. int first_char;
  437. int last_char = 0;
  438. int prefix_flags = 0;
  439. bool more_suffixes;
  440. bool confirm_prefix;
  441. int spell_word;
  442. int emphasize_allcaps = 0;
  443. int wflags;
  444. int was_unpronouncable = 0;
  445. int loopcount;
  446. int add_suffix_phonemes = 0;
  447. WORD_TAB wtab_null[8];
  448. if (wtab == NULL) {
  449. memset(wtab_null, 0, sizeof(wtab_null));
  450. wtab = wtab_null;
  451. }
  452. wflags = wtab->flags;
  453. dictionary_flags[0] = 0;
  454. dictionary_flags[1] = 0;
  455. dictionary_flags2[0] = 0;
  456. dictionary_flags2[1] = 0;
  457. dictionary_skipwords = 0;
  458. phonemes[0] = 0;
  459. unpron_phonemes[0] = 0;
  460. prefix_phonemes[0] = 0;
  461. end_phonemes[0] = 0;
  462. if (tr->data_dictlist == NULL) {
  463. // dictionary is not loaded
  464. word_phonemes[0] = 0;
  465. return 0;
  466. }
  467. // count the length of the word
  468. word1 = word_start;
  469. if (*word1 == ' ') word1++; // possibly a dot was replaced by space: $dot
  470. wordx = word1;
  471. utf8_in(&first_char, wordx);
  472. word_length = 0;
  473. while ((*wordx != 0) && (*wordx != ' ')) {
  474. wordx += utf8_in(&last_char, wordx);
  475. word_length++;
  476. }
  477. word_copy_length = wordx - word_start;
  478. if (word_copy_length >= N_WORD_BYTES)
  479. word_copy_length = N_WORD_BYTES-1;
  480. memcpy(word_copy2, word_start, word_copy_length);
  481. spell_word = 0;
  482. if ((word_length == 1) && (wflags & FLAG_TRANSLATOR2)) {
  483. // retranslating a 1-character word using a different language, say its name
  484. utf8_in(&c_temp, wordx+1); // the next character
  485. if (!IsAlpha(c_temp) || (AlphabetFromChar(last_char) != AlphabetFromChar(c_temp)))
  486. spell_word = 1;
  487. }
  488. if (option_sayas == SAYAS_KEY) {
  489. if (word_length == 1)
  490. spell_word = 4;
  491. else {
  492. // is there a translation for this keyname ?
  493. word1--;
  494. *word1 = '_'; // prefix keyname with '_'
  495. found = LookupDictList(tr, &word1, phonemes, dictionary_flags, 0, wtab);
  496. }
  497. }
  498. // try an initial lookup in the dictionary list, we may find a pronunciation specified, or
  499. // we may just find some flags
  500. if (option_sayas & 0x10) {
  501. // SAYAS_CHAR, SAYAS_GYLPH, or SAYAS_SINGLE_CHAR
  502. spell_word = option_sayas & 0xf; // 2,3,4
  503. } else {
  504. if (!found)
  505. found = LookupDictList(tr, &word1, phonemes, dictionary_flags, FLAG_ALLOW_TEXTMODE, wtab); // the original word
  506. if ((dictionary_flags[0] & (FLAG_ALLOW_DOT | FLAG_NEEDS_DOT)) && (wordx[1] == '.'))
  507. wordx[1] = ' '; // remove a Dot after this word
  508. if (dictionary_flags[0] & FLAG_TEXTMODE) {
  509. if (word_out != NULL)
  510. strcpy(word_out, word1);
  511. return dictionary_flags[0];
  512. } else if ((found == false) && (dictionary_flags[0] & FLAG_SKIPWORDS) && !(dictionary_flags[0] & FLAG_ABBREV)) {
  513. // grouped words, but no translation. Join the words with hyphens.
  514. wordx = word1;
  515. ix = 0;
  516. while (ix < dictionary_skipwords) {
  517. if (*wordx == ' ') {
  518. *wordx = '-';
  519. ix++;
  520. }
  521. wordx++;
  522. }
  523. }
  524. if ((word_length == 1) && (dictionary_skipwords == 0)) {
  525. // is this a series of single letters separated by dots?
  526. if (CheckDottedAbbrev(word1)) {
  527. dictionary_flags[0] = 0;
  528. dictionary_flags[1] = 0;
  529. spell_word = 1;
  530. if (dictionary_skipwords)
  531. dictionary_flags[0] = FLAG_SKIPWORDS;
  532. }
  533. }
  534. if (phonemes[0] == phonSWITCH) {
  535. // change to another language in order to translate this word
  536. strcpy(word_phonemes, phonemes);
  537. return 0;
  538. }
  539. if (!found && (dictionary_flags[0] & FLAG_ABBREV)) {
  540. // the word has $abbrev flag, but no pronunciation specified. Speak as individual letters
  541. spell_word = 1;
  542. }
  543. if (!found && iswdigit(first_char)) {
  544. Lookup(tr, "_0lang", word_phonemes);
  545. if (word_phonemes[0] == phonSWITCH)
  546. return 0;
  547. if ((tr->langopts.numbers2 & NUM2_ENGLISH_NUMERALS) && !(wtab->flags & FLAG_CHAR_REPLACED)) {
  548. // for this language, speak English numerals (0-9) with the English voice
  549. sprintf(word_phonemes, "%c", phonSWITCH);
  550. return 0;
  551. }
  552. found = TranslateNumber(tr, word1, phonemes, dictionary_flags, wtab, 0);
  553. }
  554. if (!found && ((wflags & FLAG_UPPERS) != FLAG_FIRST_UPPER)) {
  555. // either all upper or all lower case
  556. if ((tr->langopts.numbers & NUM_ROMAN) || ((tr->langopts.numbers & NUM_ROMAN_CAPITALS) && (wflags & FLAG_ALL_UPPER))) {
  557. if ((wflags & FLAG_LAST_WORD) || !(wtab[1].flags & FLAG_NOSPACE)) {
  558. // don't use Roman number if this word is not separated from the next word (eg. "XLTest")
  559. if ((found = TranslateRoman(tr, word1, phonemes, wtab)) != 0)
  560. dictionary_flags[0] |= FLAG_ABBREV; // prevent emphasis if capitals
  561. }
  562. }
  563. }
  564. if ((wflags & FLAG_ALL_UPPER) && (word_length > 1) && iswalpha(first_char)) {
  565. if ((option_tone_flags & OPTION_EMPHASIZE_ALLCAPS) && !(dictionary_flags[0] & FLAG_ABBREV)) {
  566. // emphasize words which are in capitals
  567. emphasize_allcaps = FLAG_EMPHASIZED;
  568. } else if (!found && !(dictionary_flags[0] & FLAG_SKIPWORDS) && (word_length < 4) && (tr->clause_lower_count > 3)
  569. && (tr->clause_upper_count <= tr->clause_lower_count)) {
  570. // An upper case word in a lower case clause. This could be an abbreviation.
  571. spell_word = 1;
  572. }
  573. }
  574. }
  575. if (spell_word > 0) {
  576. // Speak as individual letters
  577. phonemes[0] = 0;
  578. if (SpeakIndividualLetters(tr, word1, phonemes, spell_word) == NULL) {
  579. if (word_length > 1)
  580. return FLAG_SPELLWORD; // a mixture of languages, retranslate as individual letters, separated by spaces
  581. return 0;
  582. }
  583. strcpy(word_phonemes, phonemes);
  584. if (wflags & FLAG_TRANSLATOR2)
  585. return 0;
  586. addPluralSuffixes(wflags, tr, last_char, word_phonemes);
  587. return dictionary_flags[0] & FLAG_SKIPWORDS; // for "b.c.d"
  588. } else if (found == false) {
  589. // word's pronunciation is not given in the dictionary list, although
  590. // dictionary_flags may have ben set there
  591. int posn;
  592. bool non_initial = false;
  593. int length;
  594. posn = 0;
  595. length = 999;
  596. wordx = word1;
  597. while (((length < 3) && (length > 0)) || (word_length > 1 && Unpronouncable(tr, wordx, posn))) {
  598. // This word looks "unpronouncable", so speak letters individually until we
  599. // find a remainder that we can pronounce.
  600. was_unpronouncable = FLAG_WAS_UNPRONOUNCABLE;
  601. emphasize_allcaps = 0;
  602. if (wordx[0] == '\'')
  603. break;
  604. if (posn > 0)
  605. non_initial = true;
  606. wordx += TranslateLetter(tr, wordx, unpron_phonemes, non_initial, current_alphabet);
  607. posn++;
  608. if (unpron_phonemes[0] == phonSWITCH) {
  609. // change to another language in order to translate this word
  610. strcpy(word_phonemes, unpron_phonemes);
  611. if (strcmp(&unpron_phonemes[1], ESPEAKNG_DEFAULT_VOICE) == 0)
  612. return FLAG_SPELLWORD; // _^_en must have been set in TranslateLetter(), not *_rules which uses only _^_
  613. return 0;
  614. }
  615. length = 0;
  616. while (wordx[length] != ' ') length++;
  617. }
  618. SetSpellingStress(tr, unpron_phonemes, 0, posn);
  619. // anything left ?
  620. if (*wordx != ' ') {
  621. if ((unpron_phonemes[0] != 0) && (wordx[0] != '\'')) {
  622. // letters which have been spoken individually from affecting the pronunciation of the pronuncable part
  623. wordx[-1] = ' ';
  624. }
  625. // Translate the stem
  626. end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags, dictionary_flags);
  627. if (phonemes[0] == phonSWITCH) {
  628. // change to another language in order to translate this word
  629. strcpy(word_phonemes, phonemes);
  630. return 0;
  631. }
  632. if ((phonemes[0] == 0) && (end_phonemes[0] == 0)) {
  633. int wc;
  634. // characters not recognised, speak them individually
  635. // ?? should we say super/sub-script numbers and letters here?
  636. utf8_in(&wc, wordx);
  637. if ((word_length == 1) && (IsAlpha(wc) || IsSuperscript(wc))) {
  638. if ((wordx = SpeakIndividualLetters(tr, wordx, phonemes, spell_word)) == NULL)
  639. return 0;
  640. strcpy(word_phonemes, phonemes);
  641. return 0;
  642. }
  643. }
  644. c_temp = wordx[-1];
  645. found = false;
  646. confirm_prefix = true;
  647. for (loopcount = 0; (loopcount < 50) && (end_type & SUFX_P); loopcount++) {
  648. // Found a standard prefix, remove it and retranslate
  649. // loopcount guards against an endless loop
  650. if (confirm_prefix && !(end_type & SUFX_B)) {
  651. int end2;
  652. char end_phonemes2[N_WORD_PHONEMES];
  653. // remove any standard suffix and confirm that the prefix is still recognised
  654. phonemes2[0] = 0;
  655. end2 = TranslateRules(tr, wordx, phonemes2, N_WORD_PHONEMES, end_phonemes2, wflags|FLAG_NO_PREFIX|FLAG_NO_TRACE, dictionary_flags);
  656. if (end2) {
  657. RemoveEnding(tr, wordx, end2, word_copy);
  658. end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags|FLAG_NO_TRACE, dictionary_flags);
  659. memcpy(wordx, word_copy, strlen(word_copy));
  660. if ((end_type & SUFX_P) == 0) {
  661. // after removing the suffix, the prefix is no longer recognised.
  662. // Keep the suffix, but don't use the prefix
  663. end_type = end2;
  664. strcpy(phonemes, phonemes2);
  665. strcpy(end_phonemes, end_phonemes2);
  666. if (option_phonemes & espeakPHONEMES_TRACE) {
  667. DecodePhonemes(end_phonemes, end_phonemes2);
  668. fprintf(f_trans, " suffix [%s]\n\n", end_phonemes2);
  669. }
  670. }
  671. confirm_prefix = false;
  672. continue;
  673. }
  674. }
  675. prefix_type = end_type;
  676. if (prefix_type & SUFX_V)
  677. tr->expect_verb = 1; // use the verb form of the word
  678. wordx[-1] = c_temp;
  679. if ((prefix_type & SUFX_B) == 0) {
  680. for (ix = (prefix_type & 0xf); ix > 0; ix--) { // num. of characters to remove
  681. wordx++;
  682. while ((*wordx & 0xc0) == 0x80) wordx++; // for multibyte characters
  683. }
  684. } else {
  685. pfix = 1;
  686. prefix_chars[0] = 0;
  687. n_chars = prefix_type & 0x3f;
  688. for (ix = 0; ix < n_chars; ix++) { // num. of bytes to remove
  689. prefix_chars[pfix++] = *wordx++;
  690. if ((prefix_type & SUFX_B) && (ix == (n_chars-1)))
  691. prefix_chars[pfix-1] = 0; // discard the last character of the prefix, this is the separator character
  692. }
  693. prefix_chars[pfix] = 0;
  694. }
  695. c_temp = wordx[-1];
  696. wordx[-1] = ' ';
  697. confirm_prefix = true;
  698. wflags |= FLAG_PREFIX_REMOVED;
  699. if (prefix_type & SUFX_B) {
  700. // SUFX_B is used for Turkish, tr_rules contains " ' (Pb"
  701. // examine the prefix part
  702. char *wordpf;
  703. char prefix_phonemes2[12];
  704. strncpy0(prefix_phonemes2, end_phonemes, sizeof(prefix_phonemes2));
  705. wordpf = &prefix_chars[1];
  706. strcpy(prefix_phonemes, phonemes);
  707. // look for stress marker or $abbrev
  708. found = LookupDictList(tr, &wordpf, phonemes, dictionary_flags, 0, wtab);
  709. if (found)
  710. strcpy(prefix_phonemes, phonemes);
  711. if (dictionary_flags[0] & FLAG_ABBREV) {
  712. prefix_phonemes[0] = 0;
  713. SpeakIndividualLetters(tr, wordpf, prefix_phonemes, 1);
  714. }
  715. } else
  716. strcat(prefix_phonemes, end_phonemes);
  717. end_phonemes[0] = 0;
  718. end_type = 0;
  719. found = LookupDictList(tr, &wordx, phonemes, dictionary_flags2, SUFX_P, wtab); // without prefix
  720. if (dictionary_flags[0] == 0) {
  721. dictionary_flags[0] = dictionary_flags2[0];
  722. dictionary_flags[1] = dictionary_flags2[1];
  723. } else
  724. prefix_flags = 1;
  725. if (found == false) {
  726. end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags & (FLAG_HYPHEN_AFTER | FLAG_PREFIX_REMOVED), dictionary_flags);
  727. if (phonemes[0] == phonSWITCH) {
  728. // change to another language in order to translate this word
  729. wordx[-1] = c_temp;
  730. strcpy(word_phonemes, phonemes);
  731. return 0;
  732. }
  733. }
  734. }
  735. if ((end_type != 0) && !(end_type & SUFX_P)) {
  736. end_type1 = end_type;
  737. strcpy(phonemes2, phonemes);
  738. // The word has a standard ending, re-translate without this ending
  739. end_flags = RemoveEnding(tr, wordx, end_type, word_copy);
  740. more_suffixes = true;
  741. while (more_suffixes) {
  742. more_suffixes = false;
  743. phonemes[0] = 0;
  744. if (prefix_phonemes[0] != 0) {
  745. // lookup the stem without the prefix removed
  746. wordx[-1] = c_temp;
  747. found = LookupDictList(tr, &word1, phonemes, dictionary_flags2, end_flags, wtab); // include prefix, but not suffix
  748. wordx[-1] = ' ';
  749. if (phonemes[0] == phonSWITCH) {
  750. // change to another language in order to translate this word
  751. memcpy(wordx, word_copy, strlen(word_copy));
  752. strcpy(word_phonemes, phonemes);
  753. return 0;
  754. }
  755. if (dictionary_flags[0] == 0) {
  756. dictionary_flags[0] = dictionary_flags2[0];
  757. dictionary_flags[1] = dictionary_flags2[1];
  758. }
  759. if (found)
  760. prefix_phonemes[0] = 0; // matched whole word, don't need prefix now
  761. if ((found == false) && (dictionary_flags2[0] != 0))
  762. prefix_flags = 1;
  763. }
  764. if (found == false) {
  765. found = LookupDictList(tr, &wordx, phonemes, dictionary_flags2, end_flags, wtab); // without prefix and suffix
  766. if (phonemes[0] == phonSWITCH) {
  767. // change to another language in order to translate this word
  768. memcpy(wordx, word_copy, strlen(word_copy));
  769. strcpy(word_phonemes, phonemes);
  770. return 0;
  771. }
  772. if (dictionary_flags[0] == 0) {
  773. dictionary_flags[0] = dictionary_flags2[0];
  774. dictionary_flags[1] = dictionary_flags2[1];
  775. }
  776. }
  777. if (found == false) {
  778. if (end_type & SUFX_Q) {
  779. // don't retranslate, use the original lookup result
  780. strcpy(phonemes, phonemes2);
  781. } else {
  782. if (end_flags & FLAG_SUFX)
  783. wflags |= FLAG_SUFFIX_REMOVED;
  784. if (end_type & SUFX_A)
  785. wflags |= FLAG_SUFFIX_VOWEL;
  786. if (end_type & SUFX_M) {
  787. // allow more suffixes before this suffix
  788. strcpy(end_phonemes2, end_phonemes);
  789. end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags, dictionary_flags);
  790. strcat(end_phonemes, end_phonemes2); // add the phonemes for the previous suffixes after this one
  791. if ((end_type != 0) && !(end_type & SUFX_P)) {
  792. // there is another suffix
  793. end_flags = RemoveEnding(tr, wordx, end_type, NULL);
  794. more_suffixes = true;
  795. }
  796. } else {
  797. // don't remove any previous suffix
  798. TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, NULL, wflags, dictionary_flags);
  799. end_type = 0;
  800. }
  801. if (phonemes[0] == phonSWITCH) {
  802. // change to another language in order to translate this word
  803. strcpy(word_phonemes, phonemes);
  804. memcpy(wordx, word_copy, strlen(word_copy));
  805. wordx[-1] = c_temp;
  806. return 0;
  807. }
  808. }
  809. }
  810. }
  811. if ((end_type1 & SUFX_T) == 0) {
  812. // the default is to add the suffix and then determine the word's stress pattern
  813. AppendPhonemes(tr, phonemes, N_WORD_PHONEMES, end_phonemes);
  814. end_phonemes[0] = 0;
  815. }
  816. memcpy(wordx, word_copy, strlen(word_copy));
  817. }
  818. wordx[-1] = c_temp;
  819. }
  820. }
  821. addPluralSuffixes(wflags, tr, last_char, word_phonemes);
  822. wflags |= emphasize_allcaps;
  823. // determine stress pattern for this word
  824. add_suffix_phonemes = 0;
  825. if (end_phonemes[0] != 0)
  826. add_suffix_phonemes = 2;
  827. prefix_stress = 0;
  828. for (p = prefix_phonemes; *p != 0; p++) {
  829. if ((*p == phonSTRESS_P) || (*p == phonSTRESS_P2))
  830. prefix_stress = *p;
  831. }
  832. if (prefix_flags || (prefix_stress != 0)) {
  833. if ((tr->langopts.param[LOPT_PREFIXES]) || (prefix_type & SUFX_T)) {
  834. char *p;
  835. // German, keep a secondary stress on the stem
  836. SetWordStress(tr, phonemes, dictionary_flags, 3, 0);
  837. // reduce all but the first primary stress
  838. ix = 0;
  839. for (p = prefix_phonemes; *p != 0; p++) {
  840. if (*p == phonSTRESS_P) {
  841. if (ix == 0)
  842. ix = 1;
  843. else
  844. *p = phonSTRESS_3;
  845. }
  846. }
  847. snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  848. word_phonemes[N_WORD_PHONEMES-1] = 0;
  849. SetWordStress(tr, word_phonemes, dictionary_flags, -1, 0);
  850. } else {
  851. // stress position affects the whole word, including prefix
  852. snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  853. word_phonemes[N_WORD_PHONEMES-1] = 0;
  854. SetWordStress(tr, word_phonemes, dictionary_flags, -1, 0);
  855. }
  856. } else {
  857. SetWordStress(tr, phonemes, dictionary_flags, -1, add_suffix_phonemes);
  858. snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  859. word_phonemes[N_WORD_PHONEMES-1] = 0;
  860. }
  861. if (end_phonemes[0] != 0) {
  862. // a suffix had the SUFX_T option set, add the suffix after the stress pattern has been determined
  863. ix = strlen(word_phonemes);
  864. end_phonemes[N_WORD_PHONEMES-1-ix] = 0; // ensure no buffer overflow
  865. strcpy(&word_phonemes[ix], end_phonemes);
  866. }
  867. if (wflags & FLAG_LAST_WORD) {
  868. // don't use $brk pause before the last word of a sentence
  869. // (but allow it for emphasis, see below
  870. dictionary_flags[0] &= ~FLAG_PAUSE1;
  871. }
  872. if ((wflags & FLAG_HYPHEN) && (tr->langopts.stress_flags & S_HYPEN_UNSTRESS))
  873. ChangeWordStress(tr, word_phonemes, 3);
  874. else if (wflags & FLAG_EMPHASIZED2) {
  875. // A word is indicated in the source text as stressed
  876. // Give it stress level 6 (for the intonation module)
  877. ChangeWordStress(tr, word_phonemes, 6);
  878. if (wflags & FLAG_EMPHASIZED)
  879. dictionary_flags[0] |= FLAG_PAUSE1; // precede by short pause
  880. } else if (wtab[dictionary_skipwords].flags & FLAG_LAST_WORD) {
  881. // the word has attribute to stress or unstress when at end of clause
  882. if (dictionary_flags[0] & (FLAG_STRESS_END | FLAG_STRESS_END2))
  883. ChangeWordStress(tr, word_phonemes, 4);
  884. else if ((dictionary_flags[0] & FLAG_UNSTRESS_END) && (any_stressed_words))
  885. ChangeWordStress(tr, word_phonemes, 3);
  886. }
  887. // dictionary flags for this word give a clue about which alternative pronunciations of
  888. // following words to use.
  889. if (end_type1 & SUFX_F) {
  890. // expect a verb form, with or without -s suffix
  891. tr->expect_verb = 2;
  892. tr->expect_verb_s = 2;
  893. }
  894. if (dictionary_flags[1] & FLAG_PASTF) {
  895. // expect perfect tense in next two words
  896. tr->expect_past = 3;
  897. tr->expect_verb = 0;
  898. tr->expect_noun = 0;
  899. } else if (dictionary_flags[1] & FLAG_VERBF) {
  900. // expect a verb in the next word
  901. tr->expect_verb = 2;
  902. tr->expect_verb_s = 0; // verb won't have -s suffix
  903. tr->expect_noun = 0;
  904. } else if (dictionary_flags[1] & FLAG_VERBSF) {
  905. // expect a verb, must have a -s suffix
  906. tr->expect_verb = 0;
  907. tr->expect_verb_s = 2;
  908. tr->expect_past = 0;
  909. tr->expect_noun = 0;
  910. } else if (dictionary_flags[1] & FLAG_NOUNF) {
  911. // not expecting a verb next
  912. tr->expect_noun = 2;
  913. tr->expect_verb = 0;
  914. tr->expect_verb_s = 0;
  915. tr->expect_past = 0;
  916. }
  917. if ((wordx[0] != 0) && (!(dictionary_flags[1] & FLAG_VERB_EXT))) {
  918. if (tr->expect_verb > 0)
  919. tr->expect_verb--;
  920. if (tr->expect_verb_s > 0)
  921. tr->expect_verb_s--;
  922. if (tr->expect_noun > 0)
  923. tr->expect_noun--;
  924. if (tr->expect_past > 0)
  925. tr->expect_past--;
  926. }
  927. if ((word_length == 1) && (tr->translator_name == L('e', 'n')) && iswalpha(first_char) && (first_char != 'i')) {
  928. // English Specific !!!!
  929. // any single letter before a dot is an abbreviation, except 'I'
  930. dictionary_flags[0] |= FLAG_ALLOW_DOT;
  931. }
  932. if ((tr->langopts.param[LOPT_ALT] & 2) && ((dictionary_flags[0] & (FLAG_ALT_TRANS | FLAG_ALT2_TRANS)) != 0))
  933. ApplySpecialAttribute2(tr, word_phonemes, dictionary_flags[0]);
  934. dictionary_flags[0] |= was_unpronouncable;
  935. memcpy(word_start, word_copy2, word_copy_length);
  936. return dictionary_flags[0];
  937. }
  938. int TranslateWord(Translator *tr, char *word_start, WORD_TAB *wtab, char *word_out)
  939. {
  940. char words_phonemes[N_WORD_PHONEMES]; // a word translated into phoneme codes
  941. char *phonemes = words_phonemes;
  942. int available = N_WORD_PHONEMES;
  943. bool first_word = true;
  944. int flags = TranslateWord3(tr, word_start, wtab, word_out);
  945. if (flags & FLAG_TEXTMODE && word_out) {
  946. // Ensure that start of word rules match with the replaced text,
  947. // so that emoji and other characters are pronounced correctly.
  948. char word[N_WORD_BYTES+1];
  949. word[0] = 0;
  950. word[1] = ' ';
  951. strcpy(word+2, word_out);
  952. word_out = word+2;
  953. while (*word_out && available > 1) {
  954. int c;
  955. utf8_in(&c, word_out);
  956. if (iswupper(c)) {
  957. wtab->flags |= FLAG_FIRST_UPPER;
  958. utf8_out(tolower(c), word_out);
  959. } else {
  960. wtab->flags &= ~FLAG_FIRST_UPPER;
  961. }
  962. TranslateWord3(tr, word_out, wtab, NULL);
  963. int n;
  964. if (first_word) {
  965. n = snprintf(phonemes, available, "%s", word_phonemes);
  966. first_word = false;
  967. } else {
  968. n = snprintf(phonemes, available, "%c%s", phonEND_WORD, word_phonemes);
  969. }
  970. available -= n;
  971. phonemes += n;
  972. // skip to the next word in a multi-word replacement. Always skip at least one word.
  973. for (dictionary_skipwords++; dictionary_skipwords > 0; dictionary_skipwords--) {
  974. while (!isspace(*word_out)) ++word_out;
  975. while (isspace(*word_out)) ++word_out;
  976. }
  977. }
  978. // If the list file contains a text replacement to another
  979. // entry in the list file, e.g.:
  980. // ripost riposte $text
  981. // riposte rI#p0st
  982. // calling it from a prefix or suffix rule such as 'riposted'
  983. // causes word_out[0] to be NULL, as TranslateWord3 has the
  984. // information needed to perform the mapping. In this case,
  985. // no phonemes have been written in this loop and the phonemes
  986. // have been calculated, so don't override them.
  987. if (phonemes != words_phonemes) {
  988. snprintf(word_phonemes, sizeof(word_phonemes), "%s", words_phonemes);
  989. }
  990. }
  991. return flags;
  992. }
  993. static void SetPlist2(PHONEME_LIST2 *p, unsigned char phcode)
  994. {
  995. p->phcode = phcode;
  996. p->stresslevel = 0;
  997. p->tone_ph = 0;
  998. p->synthflags = embedded_flag;
  999. p->sourceix = 0;
  1000. embedded_flag = 0;
  1001. }
  1002. static int CountSyllables(unsigned char *phonemes)
  1003. {
  1004. int count = 0;
  1005. int phon;
  1006. while ((phon = *phonemes++) != 0) {
  1007. if (phoneme_tab[phon]->type == phVOWEL)
  1008. count++;
  1009. }
  1010. return count;
  1011. }
  1012. static void Word_EmbeddedCmd()
  1013. {
  1014. // Process embedded commands for emphasis, sayas, and break
  1015. int embedded_cmd;
  1016. int value;
  1017. do {
  1018. embedded_cmd = embedded_list[embedded_read++];
  1019. value = embedded_cmd >> 8;
  1020. switch (embedded_cmd & 0x1f)
  1021. {
  1022. case EMBED_Y:
  1023. option_sayas = value;
  1024. break;
  1025. case EMBED_F:
  1026. option_emphasis = value;
  1027. break;
  1028. case EMBED_B:
  1029. // break command
  1030. if (value == 0)
  1031. pre_pause = 0; // break=none
  1032. else
  1033. pre_pause += value;
  1034. break;
  1035. }
  1036. } while (((embedded_cmd & 0x80) == 0) && (embedded_read < embedded_ix));
  1037. }
  1038. static int SetAlternateTranslator(const char *new_language, Translator **translator, char translator_language[20])
  1039. {
  1040. // Set alternate translator to a second language
  1041. int new_phoneme_tab;
  1042. if ((new_phoneme_tab = SelectPhonemeTableName(new_language)) >= 0) {
  1043. if ((*translator != NULL) && (strcmp(new_language, translator_language) != 0)) {
  1044. // we already have an alternative translator, but not for the required language, delete it
  1045. DeleteTranslator(*translator);
  1046. *translator = NULL;
  1047. }
  1048. if (*translator == NULL) {
  1049. *translator = SelectTranslator(new_language);
  1050. strcpy(translator_language, new_language);
  1051. if (LoadDictionary(*translator, (*translator)->dictionary_name, 0) != 0) {
  1052. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  1053. new_phoneme_tab = -1;
  1054. translator_language[0] = 0;
  1055. }
  1056. (*translator)->phoneme_tab_ix = new_phoneme_tab;
  1057. }
  1058. }
  1059. if (*translator != NULL)
  1060. (*translator)->phonemes_repeat[0] = 0;
  1061. return new_phoneme_tab;
  1062. }
  1063. int SetTranslator2(const char *new_language)
  1064. {
  1065. return SetAlternateTranslator(new_language, &translator2, translator2_language);
  1066. }
  1067. int SetTranslator3(const char *new_language)
  1068. {
  1069. return SetAlternateTranslator(new_language, &translator3, translator3_language);
  1070. }
  1071. static int TranslateWord2(Translator *tr, char *word, WORD_TAB *wtab, int pre_pause)
  1072. {
  1073. int flags = 0;
  1074. int stress;
  1075. int next_stress;
  1076. int next_tone = 0;
  1077. unsigned char *p;
  1078. int srcix;
  1079. int found_dict_flag;
  1080. unsigned char ph_code;
  1081. PHONEME_LIST2 *plist2;
  1082. PHONEME_TAB *ph;
  1083. int max_stress;
  1084. int max_stress_ix = 0;
  1085. int prev_vowel = -1;
  1086. int pitch_raised = 0;
  1087. int switch_phonemes = -1;
  1088. bool first_phoneme = true;
  1089. int source_ix;
  1090. int len;
  1091. int ix;
  1092. int sylimit; // max. number of syllables in a word to be combined with a preceding preposition
  1093. const char *new_language;
  1094. int bad_phoneme;
  1095. int word_flags;
  1096. int word_copy_len;
  1097. char word_copy[N_WORD_BYTES+1];
  1098. char word_replaced[N_WORD_BYTES+1];
  1099. char old_dictionary_name[40];
  1100. len = wtab->length;
  1101. if (len > 31) len = 31;
  1102. source_ix = (wtab->sourceix & 0x7ff) | (len << 11); // bits 0-10 sourceix, bits 11-15 word length
  1103. word_flags = wtab[0].flags;
  1104. if (word_flags & FLAG_EMBEDDED) {
  1105. wtab[0].flags &= ~FLAG_EMBEDDED; // clear it in case we call TranslateWord2() again for the same word
  1106. embedded_flag = SFLAG_EMBEDDED;
  1107. Word_EmbeddedCmd();
  1108. }
  1109. if (n_ph_list2 >= N_PHONEME_LIST-2) {
  1110. // No room, can't translate anything
  1111. return 0;
  1112. }
  1113. if ((word[0] == 0) || (word_flags & FLAG_DELETE_WORD)) {
  1114. // nothing to translate. Add a dummy phoneme to carry any embedded commands
  1115. if (embedded_flag) {
  1116. ph_list2[n_ph_list2].phcode = phonEND_WORD;
  1117. ph_list2[n_ph_list2].stresslevel = 0;
  1118. ph_list2[n_ph_list2].wordstress = 0;
  1119. ph_list2[n_ph_list2].tone_ph = 0;
  1120. ph_list2[n_ph_list2].synthflags = embedded_flag;
  1121. ph_list2[n_ph_list2].sourceix = 0;
  1122. n_ph_list2++;
  1123. embedded_flag = 0;
  1124. }
  1125. word_phonemes[0] = 0;
  1126. return 0;
  1127. }
  1128. if (n_ph_list2 >= N_PHONEME_LIST-7-2) {
  1129. // We may require up to 7 phonemes, plus the 2 phonemes from the caller, can't translate safely
  1130. return 0;
  1131. }
  1132. // after a $pause word attribute, ignore a $pause attribute on the next two words
  1133. if (tr->prepause_timeout > 0)
  1134. tr->prepause_timeout--;
  1135. if ((option_sayas & 0xf0) == 0x10) {
  1136. if (!(word_flags & FLAG_FIRST_WORD)) {
  1137. // SAYAS_CHARS, SAYAS_GLYPHS, or SAYAS_SINGLECHARS. Pause between each word.
  1138. pre_pause += 4;
  1139. }
  1140. }
  1141. if (word_flags & FLAG_FIRST_UPPER) {
  1142. if ((option_capitals > 2) && (embedded_ix < N_EMBEDDED_LIST-6)) {
  1143. // indicate capital letter by raising pitch
  1144. if (embedded_flag)
  1145. embedded_list[embedded_ix-1] &= ~0x80; // already embedded command before this word, remove terminator
  1146. if ((pitch_raised = option_capitals) == 3)
  1147. pitch_raised = 20; // default pitch raise for capitals
  1148. embedded_list[embedded_ix++] = EMBED_P+0x40+0x80 + (pitch_raised << 8); // raise pitch
  1149. embedded_flag = SFLAG_EMBEDDED;
  1150. }
  1151. }
  1152. p = (unsigned char *)word_phonemes;
  1153. if (word_flags & FLAG_PHONEMES) {
  1154. // The input is in phoneme mnemonics, not language text
  1155. int c1;
  1156. char lang_name[12];
  1157. if (memcmp(word, "_^_", 3) == 0) {
  1158. // switch languages
  1159. word += 3;
  1160. for (ix = 0;;) {
  1161. c1 = *word++;
  1162. if ((c1 == ' ') || (c1 == 0))
  1163. break;
  1164. lang_name[ix++] = tolower(c1);
  1165. }
  1166. lang_name[ix] = 0;
  1167. if ((ix = LookupPhonemeTable(lang_name)) > 0) {
  1168. SelectPhonemeTable(ix);
  1169. word_phonemes[0] = phonSWITCH;
  1170. word_phonemes[1] = ix;
  1171. word_phonemes[2] = 0;
  1172. }
  1173. } else
  1174. EncodePhonemes(word, word_phonemes, &bad_phoneme);
  1175. flags = FLAG_FOUND;
  1176. } else {
  1177. int c2;
  1178. ix = 0;
  1179. while (((c2 = word_copy[ix] = word[ix]) != ' ') && (c2 != 0) && (ix < N_WORD_BYTES)) ix++;
  1180. word_copy_len = ix;
  1181. word_replaced[2] = 0;
  1182. flags = TranslateWord(translator, word, wtab, &word_replaced[2]);
  1183. if (flags & FLAG_SPELLWORD) {
  1184. // re-translate the word as individual letters, separated by spaces
  1185. memcpy(word, word_copy, word_copy_len);
  1186. return flags;
  1187. }
  1188. if ((flags & FLAG_COMBINE) && !(wtab[1].flags & FLAG_PHONEMES)) {
  1189. char *p2;
  1190. bool ok = true;
  1191. unsigned int flags2[2];
  1192. int c_word2;
  1193. char ph_buf[N_WORD_PHONEMES];
  1194. flags2[0] = 0;
  1195. sylimit = tr->langopts.param[LOPT_COMBINE_WORDS];
  1196. // LANG=cs,sk
  1197. // combine a preposition with the following word
  1198. p2 = word;
  1199. while (*p2 != ' ') p2++;
  1200. utf8_in(&c_word2, p2+1); // first character of the next word;
  1201. if (!iswalpha(c_word2))
  1202. ok = false;
  1203. if (ok == true) {
  1204. strcpy(ph_buf, word_phonemes);
  1205. flags2[0] = TranslateWord(translator, p2+1, wtab+1, NULL);
  1206. if ((flags2[0] & FLAG_WAS_UNPRONOUNCABLE) || (word_phonemes[0] == phonSWITCH))
  1207. ok = false;
  1208. if (sylimit & 0x100) {
  1209. // only if the second word has $alt attribute
  1210. if ((flags2[0] & FLAG_ALT_TRANS) == 0)
  1211. ok = false;
  1212. }
  1213. if ((sylimit & 0x200) && ((wtab+1)->flags & FLAG_LAST_WORD)) {
  1214. // not if the next word is end-of-sentence
  1215. ok = false;
  1216. }
  1217. if (ok == false)
  1218. strcpy(word_phonemes, ph_buf);
  1219. }
  1220. if (ok) {
  1221. *p2 = '-'; // replace next space by hyphen
  1222. wtab[0].flags &= ~FLAG_ALL_UPPER; // prevent it being considered an abbreviation
  1223. flags = TranslateWord(translator, word, wtab, NULL); // translate the combined word
  1224. if ((sylimit > 0) && (CountSyllables(p) > (sylimit & 0x1f))) {
  1225. // revert to separate words
  1226. *p2 = ' ';
  1227. flags = TranslateWord(translator, word, wtab, NULL);
  1228. } else {
  1229. if (flags == 0)
  1230. flags = flags2[0]; // no flags for the combined word, so use flags from the second word eg. lang-hu "nem december 7-e"
  1231. flags |= FLAG_SKIPWORDS;
  1232. dictionary_skipwords = 1;
  1233. }
  1234. }
  1235. }
  1236. if (p[0] == phonSWITCH) {
  1237. int switch_attempt;
  1238. strcpy(old_dictionary_name, dictionary_name);
  1239. for (switch_attempt = 0; switch_attempt < 2; switch_attempt++) {
  1240. // this word uses a different language
  1241. memcpy(word, word_copy, word_copy_len);
  1242. new_language = (char *)(&p[1]);
  1243. if (new_language[0] == 0)
  1244. new_language = ESPEAKNG_DEFAULT_VOICE;
  1245. switch_phonemes = SetTranslator2(new_language);
  1246. if (switch_phonemes >= 0) {
  1247. // re-translate the word using the new translator
  1248. wtab[0].flags |= FLAG_TRANSLATOR2;
  1249. if (word_replaced[2] != 0) {
  1250. word_replaced[0] = 0; // byte before the start of the word
  1251. word_replaced[1] = ' ';
  1252. flags = TranslateWord(translator2, &word_replaced[1], wtab, NULL);
  1253. } else
  1254. flags = TranslateWord(translator2, word, wtab, &word_replaced[2]);
  1255. }
  1256. if (p[0] != phonSWITCH)
  1257. break;
  1258. }
  1259. if (p[0] == phonSWITCH)
  1260. return FLAG_SPELLWORD;
  1261. if (switch_phonemes < 0) {
  1262. // language code is not recognised or 2nd translator won't translate it
  1263. p[0] = phonSCHWA; // just say something
  1264. p[1] = phonSCHWA;
  1265. p[2] = 0;
  1266. }
  1267. if (switch_phonemes == -1) {
  1268. strcpy(dictionary_name, old_dictionary_name);
  1269. SelectPhonemeTable(voice->phoneme_tab_ix);
  1270. // leave switch_phonemes set, but use the original phoneme table number.
  1271. // This will suppress LOPT_REGRESSIVE_VOICING
  1272. switch_phonemes = voice->phoneme_tab_ix; // original phoneme table
  1273. }
  1274. }
  1275. if (!(word_flags & FLAG_HYPHEN)) {
  1276. if (flags & FLAG_PAUSE1) {
  1277. if (pre_pause < 1)
  1278. pre_pause = 1;
  1279. }
  1280. if ((flags & FLAG_PREPAUSE) && !(word_flags & (FLAG_LAST_WORD | FLAG_FIRST_WORD)) && !(wtab[-1].flags & FLAG_FIRST_WORD) && (tr->prepause_timeout == 0)) {
  1281. // the word is marked in the dictionary list with $pause
  1282. if (pre_pause < 4) pre_pause = 4;
  1283. tr->prepause_timeout = 3;
  1284. }
  1285. }
  1286. if ((option_emphasis >= 3) && (pre_pause < 1))
  1287. pre_pause = 1;
  1288. }
  1289. stress = 0;
  1290. next_stress = 1;
  1291. srcix = 0;
  1292. max_stress = -1;
  1293. found_dict_flag = 0;
  1294. if ((flags & FLAG_FOUND) && !(flags & FLAG_TEXTMODE))
  1295. found_dict_flag = SFLAG_DICTIONARY;
  1296. // Each iteration may require up to 1 phoneme
  1297. // and after this loop we may require up to 7 phonemes
  1298. // and our caller requires 2 phonemes
  1299. while ((pre_pause > 0) && (n_ph_list2 < N_PHONEME_LIST-7-2)) {
  1300. // add pause phonemes here. Either because of punctuation (brackets or quotes) in the
  1301. // text, or because the word is marked in the dictionary lookup as a conjunction
  1302. if (pre_pause > 1) {
  1303. SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE);
  1304. pre_pause -= 2;
  1305. } else {
  1306. SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_NOLINK);
  1307. pre_pause--;
  1308. }
  1309. tr->end_stressed_vowel = 0; // forget about the previous word
  1310. tr->prev_dict_flags[0] = 0;
  1311. tr->prev_dict_flags[1] = 0;
  1312. }
  1313. plist2 = &ph_list2[n_ph_list2];
  1314. // From here we may require up to 4+1+3 phonemes
  1315. // This may require up to 4 phonemes
  1316. if ((option_capitals == 1) && (word_flags & FLAG_FIRST_UPPER)) {
  1317. SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_SHORT);
  1318. SetPlist2(&ph_list2[n_ph_list2++], phonCAPITAL);
  1319. if ((word_flags & FLAG_ALL_UPPER) && IsAlpha(word[1])) {
  1320. // word > 1 letter and all capitals
  1321. SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_SHORT);
  1322. SetPlist2(&ph_list2[n_ph_list2++], phonCAPITAL);
  1323. }
  1324. }
  1325. // This may require up to 1 phoneme
  1326. if (switch_phonemes >= 0) {
  1327. if ((p[0] == phonPAUSE) && (p[1] == phonSWITCH)) {
  1328. // the new word starts with a phoneme table switch, so there's no need to switch before it.
  1329. if (ph_list2[n_ph_list2-1].phcode == phonSWITCH) {
  1330. // previous phoneme is also a phonSWITCH, delete it
  1331. n_ph_list2--;
  1332. }
  1333. } else {
  1334. // this word uses a different phoneme table
  1335. if (ph_list2[n_ph_list2-1].phcode == phonSWITCH) {
  1336. // previous phoneme is also a phonSWITCH, just change its phoneme table number
  1337. n_ph_list2--;
  1338. } else
  1339. SetPlist2(&ph_list2[n_ph_list2], phonSWITCH);
  1340. ph_list2[n_ph_list2++].tone_ph = switch_phonemes; // temporary phoneme table number
  1341. }
  1342. }
  1343. // remove initial pause from a word if it follows a hyphen
  1344. if ((word_flags & FLAG_HYPHEN) && (phoneme_tab[*p]->type == phPAUSE))
  1345. p++;
  1346. if ((p[0] == 0) && (embedded_flag)) {
  1347. // no phonemes. Insert a very short pause to carry an embedded command
  1348. p[0] = phonPAUSE_VSHORT;
  1349. p[1] = 0;
  1350. }
  1351. // Each iteration may require up to 1 phoneme
  1352. // and after this loop we may require up to 3 phonemes
  1353. // and our caller requires 2 phonemes
  1354. while (((ph_code = *p++) != 0) && (n_ph_list2 < N_PHONEME_LIST-3-2)) {
  1355. if (ph_code == 255)
  1356. continue; // unknown phoneme
  1357. // Add the phonemes to the first stage phoneme list (ph_list2)
  1358. ph = phoneme_tab[ph_code];
  1359. if (ph == NULL) {
  1360. printf("Invalid phoneme code %d\n", ph_code);
  1361. continue;
  1362. }
  1363. if (ph_code == phonSWITCH) {
  1364. ph_list2[n_ph_list2].phcode = ph_code;
  1365. ph_list2[n_ph_list2].stresslevel = 0;
  1366. ph_list2[n_ph_list2].sourceix = 0;
  1367. ph_list2[n_ph_list2].synthflags = 0;
  1368. ph_list2[n_ph_list2++].tone_ph = *p;
  1369. SelectPhonemeTable(*p);
  1370. p++;
  1371. } else if (ph->type == phSTRESS) {
  1372. // don't add stress phonemes codes to the list, but give their stress
  1373. // value to the next vowel phoneme
  1374. // std_length is used to hold stress number or (if >10) a tone number for a tone language
  1375. if (ph->program == 0)
  1376. next_stress = ph->std_length;
  1377. else {
  1378. // for tone languages, the tone number for a syllable follows the vowel
  1379. if (prev_vowel >= 0)
  1380. ph_list2[prev_vowel].tone_ph = ph_code;
  1381. else
  1382. next_tone = ph_code; // no previous vowel, apply to the next vowel
  1383. }
  1384. } else if (ph_code == phonSYLLABIC) {
  1385. // mark the previous phoneme as a syllabic consonant
  1386. prev_vowel = n_ph_list2-1;
  1387. ph_list2[prev_vowel].synthflags |= SFLAG_SYLLABLE;
  1388. ph_list2[prev_vowel].stresslevel = next_stress;
  1389. } else if (ph_code == phonLENGTHEN)
  1390. ph_list2[n_ph_list2-1].synthflags |= SFLAG_LENGTHEN;
  1391. else if (ph_code == phonEND_WORD) {
  1392. // a || symbol in a phoneme string was used to indicate a word boundary
  1393. // Don't add this phoneme to the list, but make sure the next phoneme has
  1394. // a newword indication
  1395. srcix = source_ix+1;
  1396. } else if (ph_code == phonX1) {
  1397. // a language specific action
  1398. if (tr->langopts.param[LOPT_IT_DOUBLING])
  1399. flags |= FLAG_DOUBLING;
  1400. } else {
  1401. ph_list2[n_ph_list2].phcode = ph_code;
  1402. ph_list2[n_ph_list2].tone_ph = 0;
  1403. ph_list2[n_ph_list2].synthflags = embedded_flag | found_dict_flag;
  1404. embedded_flag = 0;
  1405. ph_list2[n_ph_list2].sourceix = srcix;
  1406. srcix = 0;
  1407. if (ph->type == phVOWEL) {
  1408. stress = next_stress;
  1409. next_stress = 1; // default is 'unstressed'
  1410. if (stress >= 4)
  1411. any_stressed_words = true;
  1412. if ((prev_vowel >= 0) && (n_ph_list2-1) != prev_vowel)
  1413. ph_list2[n_ph_list2-1].stresslevel = stress; // set stress for previous consonant
  1414. ph_list2[n_ph_list2].synthflags |= SFLAG_SYLLABLE;
  1415. prev_vowel = n_ph_list2;
  1416. if (stress > max_stress) {
  1417. max_stress = stress;
  1418. max_stress_ix = n_ph_list2;
  1419. }
  1420. if (next_tone != 0) {
  1421. ph_list2[n_ph_list2].tone_ph = next_tone;
  1422. next_tone = 0;
  1423. }
  1424. } else {
  1425. if (first_phoneme && tr->langopts.param[LOPT_IT_DOUBLING]) {
  1426. if (((tr->prev_dict_flags[0] & FLAG_DOUBLING) && (tr->langopts.param[LOPT_IT_DOUBLING] & 1)) ||
  1427. (tr->end_stressed_vowel && (tr->langopts.param[LOPT_IT_DOUBLING] & 2))) {
  1428. // italian, double the initial consonant if the previous word ends with a
  1429. // stressed vowel, or is marked with a flag
  1430. ph_list2[n_ph_list2].synthflags |= SFLAG_LENGTHEN;
  1431. }
  1432. }
  1433. }
  1434. ph_list2[n_ph_list2].stresslevel = stress;
  1435. n_ph_list2++;
  1436. first_phoneme = false;
  1437. }
  1438. }
  1439. // From here, we may require up to 3 phonemes
  1440. // This may require up to 1 phoneme
  1441. if (word_flags & FLAG_COMMA_AFTER)
  1442. SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_CLAUSE);
  1443. // don't set new-word if there is a hyphen before it
  1444. if ((word_flags & FLAG_HYPHEN) == 0)
  1445. plist2->sourceix = source_ix;
  1446. tr->end_stressed_vowel = 0;
  1447. if ((stress >= 4) && (phoneme_tab[ph_list2[n_ph_list2-1].phcode]->type == phVOWEL))
  1448. tr->end_stressed_vowel = 1; // word ends with a stressed vowel
  1449. // This may require up to 1 phoneme
  1450. if (switch_phonemes >= 0) {
  1451. // this word uses a different phoneme table, now switch back
  1452. strcpy(dictionary_name, old_dictionary_name);
  1453. SelectPhonemeTable(voice->phoneme_tab_ix);
  1454. SetPlist2(&ph_list2[n_ph_list2], phonSWITCH);
  1455. ph_list2[n_ph_list2++].tone_ph = voice->phoneme_tab_ix; // original phoneme table number
  1456. }
  1457. // This may require up to 1 phoneme
  1458. if (pitch_raised > 0) {
  1459. embedded_list[embedded_ix++] = EMBED_P+0x60+0x80 + (pitch_raised << 8); // lower pitch
  1460. SetPlist2(&ph_list2[n_ph_list2], phonPAUSE_SHORT);
  1461. ph_list2[n_ph_list2++].synthflags = SFLAG_EMBEDDED;
  1462. }
  1463. if (flags & FLAG_STRESS_END2) {
  1464. // this's word's stress could be increased later
  1465. ph_list2[max_stress_ix].synthflags |= SFLAG_PROMOTE_STRESS;
  1466. }
  1467. tr->prev_dict_flags[0] = flags;
  1468. return flags;
  1469. }
  1470. static int EmbeddedCommand(unsigned int *source_index_out)
  1471. {
  1472. // An embedded command to change the pitch, volume, etc.
  1473. // returns number of commands added to embedded_list
  1474. // pitch,speed,amplitude,expression,reverb,tone,voice,sayas
  1475. const char *commands = "PSARHTIVYMUBF";
  1476. int value = -1;
  1477. int sign = 0;
  1478. unsigned char c;
  1479. char *p;
  1480. int cmd;
  1481. int source_index = *source_index_out;
  1482. c = source[source_index];
  1483. if (c == '+') {
  1484. sign = 0x40;
  1485. source_index++;
  1486. } else if (c == '-') {
  1487. sign = 0x60;
  1488. source_index++;
  1489. }
  1490. if (IsDigit09(source[source_index])) {
  1491. value = atoi(&source[source_index]);
  1492. while (IsDigit09(source[source_index]))
  1493. source_index++;
  1494. }
  1495. c = source[source_index++];
  1496. if (embedded_ix >= (N_EMBEDDED_LIST - 2))
  1497. return 0; // list is full
  1498. if ((p = strchr_w(commands, c)) == NULL)
  1499. return 0;
  1500. cmd = (p - commands)+1;
  1501. if (value == -1) {
  1502. value = embedded_default[cmd];
  1503. sign = 0;
  1504. }
  1505. if (cmd == EMBED_Y) {
  1506. option_sayas2 = value;
  1507. count_sayas_digits = 0;
  1508. }
  1509. if (cmd == EMBED_F) {
  1510. if (value >= 3)
  1511. word_emphasis = FLAG_EMPHASIZED;
  1512. else
  1513. word_emphasis = 0;
  1514. }
  1515. embedded_list[embedded_ix++] = cmd + sign + (value << 8);
  1516. *source_index_out = source_index;
  1517. return 1;
  1518. }
  1519. static const char *FindReplacementChars(Translator *tr, const char **pfrom, unsigned int c, const char *next, int *ignore_next_n)
  1520. {
  1521. const char *from = *pfrom;
  1522. while ( !is_str_totally_null(from, 4) ) {
  1523. unsigned int fc = 0; // from character
  1524. unsigned int nc = c; // next character
  1525. const char *match_next = next;
  1526. *pfrom = from;
  1527. from += utf8_in((int *)&fc, from);
  1528. if (nc == fc) {
  1529. if (*from == 0) return from + 1;
  1530. bool matched = true;
  1531. int nmatched = 0;
  1532. while (*from != 0) {
  1533. from += utf8_in((int *)&fc, from);
  1534. match_next += utf8_in((int *)&nc, match_next);
  1535. nc = towlower2(nc, tr);
  1536. if (nc != fc)
  1537. matched = false;
  1538. else
  1539. nmatched++;
  1540. }
  1541. if (*from == 0 && matched) {
  1542. *ignore_next_n = nmatched;
  1543. return from + 1;
  1544. }
  1545. }
  1546. // replacement 'from' string (skip the remaining part, if any)
  1547. while (*from != '\0') from++;
  1548. from++;
  1549. // replacement 'to' string
  1550. while (*from != '\0') from++;
  1551. from++;
  1552. }
  1553. return NULL;
  1554. }
  1555. // handle .replace rule in xx_rules file
  1556. static int SubstituteChar(Translator *tr, unsigned int c, unsigned int next_in, const char *next, int *insert, int *wordflags)
  1557. {
  1558. unsigned int new_c, c2 = ' ', c_lower;
  1559. int upper_case = 0;
  1560. static int ignore_next_n = 0;
  1561. if (ignore_next_n > 0) {
  1562. ignore_next_n--;
  1563. return 8;
  1564. }
  1565. if (c == 0) return 0;
  1566. const char *from = (const char *)tr->langopts.replace_chars;
  1567. if (from == NULL)
  1568. return c;
  1569. // there is a list of character codes to be substituted with alternative codes
  1570. if (iswupper(c_lower = c)) {
  1571. c_lower = towlower2(c, tr);
  1572. upper_case = 1;
  1573. }
  1574. const char *to = FindReplacementChars(tr, &from, c_lower, next, &ignore_next_n);
  1575. if (to == NULL)
  1576. return c; // no substitution
  1577. if (option_phonemes & espeakPHONEMES_TRACE)
  1578. fprintf(f_trans, "Replace: %s > %s\n", from, to);
  1579. to += utf8_in((int *)&new_c, to);
  1580. if (*to != 0) {
  1581. // there is a second character to be inserted
  1582. // don't convert the case of the second character unless the next letter is also upper case
  1583. to += utf8_in((int *)&c2, to);
  1584. if (upper_case && iswupper(next_in))
  1585. c2 = ucd_toupper(c2);
  1586. *insert = c2;
  1587. }
  1588. if (upper_case)
  1589. new_c = ucd_toupper(new_c);
  1590. *wordflags |= FLAG_CHAR_REPLACED;
  1591. return new_c;
  1592. }
  1593. static int TranslateChar(Translator *tr, char *ptr, int prev_in, unsigned int c, unsigned int next_in, int *insert, int *wordflags)
  1594. {
  1595. // To allow language specific examination and replacement of characters
  1596. int code;
  1597. int initial;
  1598. int medial;
  1599. int final;
  1600. int next2;
  1601. static const unsigned char hangul_compatibility[0x34] = {
  1602. 0, 0x00, 0x01, 0xaa, 0x02, 0xac, 0xad, 0x03,
  1603. 0x04, 0x05, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb4,
  1604. 0xb6, 0x06, 0x07, 0x08, 0xb9, 0x09, 0x0a, 0xbc,
  1605. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x61,
  1606. 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  1607. 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71,
  1608. 0x72, 0x73, 0x74, 0x75
  1609. };
  1610. // check for Korean Hangul letters
  1611. if (((code = c - 0xac00) >= 0) && (c <= 0xd7af)) {
  1612. // break a syllable hangul into 2 or 3 individual jamo
  1613. initial = (code/28)/21;
  1614. medial = (code/28) % 21;
  1615. final = code % 28;
  1616. if (initial == 11) {
  1617. // null initial
  1618. c = medial + 0x1161;
  1619. if (final > 0)
  1620. *insert = final + 0x11a7;
  1621. } else {
  1622. // extract the initial and insert the remainder with a null initial
  1623. c = initial + 0x1100;
  1624. *insert = (11*28*21) + (medial*28) + final + 0xac00;
  1625. }
  1626. return c;
  1627. } else if (((code = c - 0x3130) >= 0) && (code < 0x34)) {
  1628. // Hangul compatibility jamo
  1629. return hangul_compatibility[code] + 0x1100;
  1630. }
  1631. switch (tr->translator_name)
  1632. {
  1633. case L('a', 'f'):
  1634. case L('n', 'l'):
  1635. // look for 'n and replace by a special character (unicode: schwa)
  1636. if ((c == '\'') && !iswalpha(prev_in)) {
  1637. utf8_in(&next2, &ptr[1]);
  1638. if (IsSpace(next2)) {
  1639. if ((next_in == 'n') && (tr->translator_name == L('a', 'f'))) {
  1640. // n preceded by either apostrophe or U2019 "right single quotation mark"
  1641. ptr[0] = ' '; // delete the n
  1642. return 0x0259; // replace ' by unicode schwa character
  1643. }
  1644. if ((next_in == 'n') || (next_in == 't')) {
  1645. // Dutch, [@n] and [@t]
  1646. return 0x0259; // replace ' by unicode schwa character
  1647. }
  1648. }
  1649. }
  1650. break;
  1651. }
  1652. // handle .replace rule in xx_rules file
  1653. return SubstituteChar(tr, c, next_in, ptr, insert, wordflags);
  1654. }
  1655. static const char *UCase_ga[] = { "bp", "bhf", "dt", "gc", "hA", "mb", "nd", "ng", "ts", "tA", "nA", NULL };
  1656. static int UpperCaseInWord(Translator *tr, char *word, int c)
  1657. {
  1658. int ix;
  1659. int len;
  1660. const char *p;
  1661. if (tr->translator_name == L('g', 'a')) {
  1662. // Irish
  1663. for (ix = 0;; ix++) {
  1664. if ((p = UCase_ga[ix]) == NULL)
  1665. break;
  1666. len = strlen(p);
  1667. if ((word[-len] == ' ') && (memcmp(&word[-len+1], p, len-1) == 0)) {
  1668. if ((c == p[len-1]) || ((p[len-1] == 'A') && IsVowel(tr, c)))
  1669. return 1;
  1670. }
  1671. }
  1672. }
  1673. return 0;
  1674. }
  1675. void TranslateClause(Translator *tr, int *tone_out, char **voice_change)
  1676. {
  1677. int ix;
  1678. int c;
  1679. int cc = 0;
  1680. unsigned int source_index = 0;
  1681. unsigned int prev_source_index = 0;
  1682. int source_index_word = 0;
  1683. int prev_in;
  1684. int prev_out = ' ';
  1685. int prev_out2;
  1686. int prev_in_save = 0;
  1687. int next_in;
  1688. int next_in_nbytes;
  1689. int char_inserted = 0;
  1690. int clause_pause;
  1691. int pre_pause_add = 0;
  1692. int all_upper_case = FLAG_ALL_UPPER;
  1693. int alpha_count = 0;
  1694. bool finished = false;
  1695. bool single_quoted = false;
  1696. bool phoneme_mode = false;
  1697. int dict_flags = 0; // returned from dictionary lookup
  1698. int word_flags; // set here
  1699. int next_word_flags;
  1700. bool new_sentence2;
  1701. int embedded_count = 0;
  1702. int letter_count = 0;
  1703. bool space_inserted = false;
  1704. bool syllable_marked = false;
  1705. bool decimal_sep_count = false;
  1706. char *word;
  1707. char *p;
  1708. int j, k;
  1709. int n_digits;
  1710. int charix_top = 0;
  1711. short charix[N_TR_SOURCE+4];
  1712. WORD_TAB words[N_CLAUSE_WORDS];
  1713. static char voice_change_name[40];
  1714. int word_count = 0; // index into words
  1715. char sbuf[N_TR_SOURCE];
  1716. int terminator;
  1717. int tone;
  1718. if (tr == NULL)
  1719. return;
  1720. MAKE_MEM_UNDEFINED(&voice_change_name, sizeof(voice_change_name));
  1721. embedded_ix = 0;
  1722. embedded_read = 0;
  1723. pre_pause = 0;
  1724. any_stressed_words = false;
  1725. if ((clause_start_char = count_characters) < 0)
  1726. clause_start_char = 0;
  1727. clause_start_word = count_words + 1;
  1728. for (ix = 0; ix < N_TR_SOURCE; ix++)
  1729. charix[ix] = 0;
  1730. MAKE_MEM_UNDEFINED(&source, sizeof(source));
  1731. terminator = ReadClause(tr, source, charix, &charix_top, N_TR_SOURCE, &tone, voice_change_name);
  1732. if (tone_out != NULL) {
  1733. if (tone == 0)
  1734. *tone_out = (terminator & CLAUSE_INTONATION_TYPE) >> 12; // tone type not overridden in ReadClause, use default
  1735. else
  1736. *tone_out = tone; // override tone type
  1737. }
  1738. charix[charix_top+1] = 0;
  1739. charix[charix_top+2] = 0x7fff;
  1740. charix[charix_top+3] = 0;
  1741. clause_pause = (terminator & CLAUSE_PAUSE) * 10; // mS
  1742. if (terminator & CLAUSE_PAUSE_LONG)
  1743. clause_pause = clause_pause * 32; // pause value is *320mS not *10mS
  1744. for (p = source; *p != 0; p++) {
  1745. if (!isspace2(*p))
  1746. break;
  1747. }
  1748. if (*p == 0) {
  1749. // No characters except spaces. This is not a sentence.
  1750. // Don't add this pause, just make up the previous pause to this value;
  1751. clause_pause -= max_clause_pause;
  1752. if (clause_pause < 0)
  1753. clause_pause = 0;
  1754. if (new_sentence)
  1755. terminator |= CLAUSE_TYPE_SENTENCE; // carry forward an end-of-sentence indicator
  1756. max_clause_pause += clause_pause;
  1757. new_sentence2 = false;
  1758. } else {
  1759. max_clause_pause = clause_pause;
  1760. new_sentence2 = new_sentence;
  1761. }
  1762. tr->clause_terminator = terminator;
  1763. if (new_sentence2) {
  1764. count_sentences++;
  1765. if (skip_sentences > 0) {
  1766. skip_sentences--;
  1767. if (skip_sentences == 0)
  1768. skipping_text = false;
  1769. }
  1770. }
  1771. MAKE_MEM_UNDEFINED(&ph_list2, sizeof(ph_list2));
  1772. memset(&ph_list2[0], 0, sizeof(ph_list2[0]));
  1773. ph_list2[0].phcode = phonPAUSE_SHORT;
  1774. n_ph_list2 = 1;
  1775. tr->prev_last_stress = 0;
  1776. tr->prepause_timeout = 0;
  1777. tr->expect_verb = 0;
  1778. tr->expect_noun = 0;
  1779. tr->expect_past = 0;
  1780. tr->expect_verb_s = 0;
  1781. tr->phonemes_repeat_count = 0;
  1782. tr->end_stressed_vowel = 0;
  1783. tr->prev_dict_flags[0] = 0;
  1784. tr->prev_dict_flags[1] = 0;
  1785. word_count = 0;
  1786. word_flags = 0;
  1787. next_word_flags = 0;
  1788. sbuf[0] = 0;
  1789. sbuf[1] = ' ';
  1790. sbuf[2] = ' ';
  1791. ix = 3;
  1792. prev_in = ' ';
  1793. words[0].start = ix;
  1794. words[0].flags = 0;
  1795. for (j = 0; charix[j] <= 0; j++) ;
  1796. words[0].sourceix = charix[j];
  1797. k = 0;
  1798. while (charix[j] != 0) {
  1799. // count the number of characters (excluding multibyte continuation bytes)
  1800. if (charix[j++] != -1)
  1801. k++;
  1802. }
  1803. words[0].length = k;
  1804. while (!finished && (ix < (int)sizeof(sbuf) - 1)) {
  1805. prev_out2 = prev_out;
  1806. utf8_in2(&prev_out, &sbuf[ix-1], 1);
  1807. if (tr->langopts.tone_numbers && IsDigit09(prev_out) && IsAlpha(prev_out2)) {
  1808. // tone numbers can be part of a word, consider them as alphabetic
  1809. prev_out = 'a';
  1810. }
  1811. if (prev_in_save != 0) {
  1812. prev_in = prev_in_save;
  1813. prev_in_save = 0;
  1814. } else if (source_index > 0)
  1815. utf8_in2(&prev_in, &source[source_index-1], 1);
  1816. prev_source_index = source_index;
  1817. if (char_inserted) {
  1818. c = char_inserted;
  1819. char_inserted = 0;
  1820. } else {
  1821. source_index += utf8_in(&cc, &source[source_index]);
  1822. c = cc;
  1823. }
  1824. if (c == 0) {
  1825. finished = true;
  1826. c = ' ';
  1827. next_in = ' ';
  1828. next_in_nbytes = 0;
  1829. }
  1830. else
  1831. next_in_nbytes = utf8_in(&next_in, &source[source_index]);
  1832. if (c == CTRL_EMBEDDED) {
  1833. // start of embedded command in the text
  1834. int srcix = source_index-1;
  1835. if (prev_in != ' ') {
  1836. c = ' ';
  1837. prev_in_save = c;
  1838. source_index--;
  1839. } else {
  1840. embedded_count += EmbeddedCommand(&source_index);
  1841. prev_in_save = prev_in;
  1842. // replace the embedded command by spaces
  1843. memset(&source[srcix], ' ', source_index-srcix);
  1844. source_index = srcix;
  1845. continue;
  1846. }
  1847. }
  1848. if ((option_sayas2 == SAYAS_KEY) && (c != ' ')) {
  1849. if ((prev_in == ' ') && (next_in == ' '))
  1850. option_sayas2 = SAYAS_SINGLE_CHARS; // single character, speak its name
  1851. c = towlower2(c, tr);
  1852. }
  1853. if (phoneme_mode) {
  1854. all_upper_case = FLAG_PHONEMES;
  1855. if ((c == ']') && (next_in == ']')) {
  1856. phoneme_mode = false;
  1857. source_index++;
  1858. c = ' ';
  1859. }
  1860. } else if ((option_sayas2 & 0xf0) == SAYAS_DIGITS) {
  1861. if (iswdigit(c)) {
  1862. count_sayas_digits++;
  1863. if (count_sayas_digits > (option_sayas2 & 0xf)) {
  1864. // break after the specified number of digits
  1865. c = ' ';
  1866. space_inserted = true;
  1867. count_sayas_digits = 0;
  1868. }
  1869. } else {
  1870. count_sayas_digits = 0;
  1871. if (iswdigit(prev_out)) {
  1872. c = ' ';
  1873. space_inserted = true;
  1874. }
  1875. }
  1876. } else if ((option_sayas2 & 0x10) == 0) {
  1877. // speak as words
  1878. if ((c == 0x92) || (c == 0xb4) || (c == 0x2019) || (c == 0x2032))
  1879. c = '\''; // 'microsoft' quote or sexed closing single quote, or prime - possibly used as apostrophe
  1880. if (((c == 0x2018) || (c == '?')) && IsAlpha(prev_out) && IsAlpha(next_in)) {
  1881. // ? between two letters may be a smart-quote replaced by ?
  1882. c = '\'';
  1883. }
  1884. if (c == CHAR_EMPHASIS) {
  1885. // this character is a marker that the previous word is the focus of the clause
  1886. c = ' ';
  1887. word_flags |= FLAG_FOCUS;
  1888. }
  1889. if (c == CHAR_COMMA_BREAK) {
  1890. c = ' ';
  1891. word_flags |= FLAG_COMMA_AFTER;
  1892. }
  1893. // language specific character translations
  1894. c = TranslateChar(tr, &source[source_index], prev_in, c, next_in, &char_inserted, &word_flags);
  1895. if (c == 8)
  1896. continue; // ignore this character
  1897. if (char_inserted)
  1898. next_in = char_inserted;
  1899. // allow certain punctuation within a word (usually only apostrophe)
  1900. if (!IsAlpha(c) && !IsSpace(c) && (wcschr(tr->punct_within_word, c) == 0)) {
  1901. if (IsAlpha(prev_out)) {
  1902. if (tr->langopts.tone_numbers && IsDigit09(c) && !IsDigit09(next_in)) {
  1903. // allow a tone number as part of the word
  1904. } else {
  1905. c = ' '; // ensure we have an end-of-word terminator
  1906. space_inserted = true;
  1907. }
  1908. }
  1909. }
  1910. if (iswdigit(prev_out)) {
  1911. if (!iswdigit(c) && (c != '.') && (c != ',') && (c != ' ')) {
  1912. c = ' '; // terminate digit string with a space
  1913. space_inserted = true;
  1914. }
  1915. } else { // Prev output is not digit
  1916. if (prev_in == ',') {
  1917. // Workaround for several consecutive commas —
  1918. // replace current character with space
  1919. if (c == ',')
  1920. c = ' ';
  1921. } else {
  1922. decimal_sep_count = false;
  1923. }
  1924. }
  1925. if (c == '[') {
  1926. if ((next_in == '\002') || ((next_in == '[') && option_phoneme_input)) {
  1927. // "[\002" is used internally to start phoneme mode
  1928. phoneme_mode = true;
  1929. source_index++;
  1930. continue;
  1931. }
  1932. }
  1933. if (IsAlpha(c)) {
  1934. alpha_count++;
  1935. if (!IsAlpha(prev_out) || (tr->langopts.ideographs && ((c > 0x3040) || (prev_out > 0x3040)))) {
  1936. if (wcschr(tr->punct_within_word, prev_out) == 0)
  1937. letter_count = 0; // don't reset count for an apostrophy within a word
  1938. if ((prev_out != ' ') && (wcschr(tr->punct_within_word, prev_out) == 0)) {
  1939. // start of word, insert space if not one there already
  1940. c = ' ';
  1941. space_inserted = true;
  1942. if (!IsBracket(prev_out)) // ?? perhaps only set FLAG_NOSPACE for . - / (hyphenated words, URLs, etc)
  1943. next_word_flags |= FLAG_NOSPACE;
  1944. } else {
  1945. if (iswupper(c))
  1946. word_flags |= FLAG_FIRST_UPPER;
  1947. if ((prev_out == ' ') && iswdigit(sbuf[ix-2]) && !iswdigit(prev_in)) {
  1948. // word, following a number, but with a space between
  1949. // Add an extra space, to distinguish "2 a" from "2a"
  1950. sbuf[ix++] = ' ';
  1951. words[word_count].start++;
  1952. }
  1953. }
  1954. }
  1955. if (c != ' ') {
  1956. letter_count++;
  1957. if (tr->letter_bits_offset > 0) {
  1958. if (((c < 0x250) && (prev_out >= tr->letter_bits_offset)) ||
  1959. ((c >= tr->letter_bits_offset) && (letter_count > 1) && (prev_out < 0x250))) {
  1960. // Don't mix native and Latin characters in the same word
  1961. // Break into separate words
  1962. if (IsAlpha(prev_out)) {
  1963. c = ' ';
  1964. space_inserted = true;
  1965. word_flags |= FLAG_HYPHEN_AFTER;
  1966. next_word_flags |= FLAG_HYPHEN;
  1967. }
  1968. }
  1969. }
  1970. }
  1971. if (iswupper(c)) {
  1972. c = towlower2(c, tr);
  1973. if (tr->langopts.param[LOPT_CAPS_IN_WORD]) {
  1974. if (syllable_marked == false) {
  1975. char_inserted = c;
  1976. c = 0x2c8; // stress marker
  1977. syllable_marked = true;
  1978. }
  1979. } else {
  1980. if (iswlower(prev_in)) {
  1981. // lower case followed by upper case, possibly CamelCase
  1982. if (UpperCaseInWord(tr, &sbuf[ix], c) == 0) { // start a new word
  1983. c = ' ';
  1984. space_inserted = true;
  1985. prev_in_save = c;
  1986. }
  1987. } else if ((c != ' ') && iswupper(prev_in) && iswlower(next_in)) {
  1988. int next2_in;
  1989. utf8_in(&next2_in, &source[source_index + next_in_nbytes]);
  1990. if ((tr->translator_name == L('n', 'l')) && (letter_count == 2) && (c == 'j') && (prev_in == 'I')) {
  1991. // Dutch words may capitalise initial IJ, don't split
  1992. } else if (IsAlpha(next2_in)) {
  1993. // changing from upper to lower case, start new word at the last uppercase, if 3 or more letters
  1994. c = ' ';
  1995. space_inserted = true;
  1996. prev_in_save = c;
  1997. next_word_flags |= FLAG_NOSPACE;
  1998. }
  1999. }
  2000. }
  2001. } else {
  2002. if ((all_upper_case) && (letter_count > 2)) {
  2003. // Flag as plural only English
  2004. if (tr->translator_name == L('e', 'n') && (c == 's') && (next_in == ' ')) {
  2005. c = ' ';
  2006. all_upper_case |= FLAG_HAS_PLURAL;
  2007. if (sbuf[ix-1] == '\'')
  2008. sbuf[ix-1] = ' ';
  2009. } else
  2010. all_upper_case = 0; // current word contains lower case letters, not "'s"
  2011. } else
  2012. all_upper_case = 0;
  2013. }
  2014. } else if (c == '-') {
  2015. if (!IsSpace(prev_in) && IsAlpha(next_in)) {
  2016. if (prev_out != ' ') {
  2017. // previous 'word' not yet ended (not alpha or numeric), start new word now.
  2018. c = ' ';
  2019. space_inserted = true;
  2020. } else {
  2021. // '-' between two letters is a hyphen, treat as a space
  2022. word_flags |= FLAG_HYPHEN;
  2023. if (word_count > 0)
  2024. words[word_count-1].flags |= FLAG_HYPHEN_AFTER;
  2025. c = ' ';
  2026. }
  2027. } else if ((prev_in == ' ') && (next_in == ' ')) {
  2028. // ' - ' dash between two spaces, treat as pause
  2029. c = ' ';
  2030. pre_pause_add = 4;
  2031. } else if (next_in == '-') {
  2032. // double hyphen, treat as pause
  2033. source_index++;
  2034. c = ' ';
  2035. pre_pause_add = 4;
  2036. } else if ((prev_out == ' ') && IsAlpha(prev_out2) && !IsAlpha(prev_in)) {
  2037. // insert extra space between a word + space + hyphen, to distinguish 'a -2' from 'a-2'
  2038. sbuf[ix++] = ' ';
  2039. words[word_count].start++;
  2040. }
  2041. } else if (c == '.') {
  2042. if (prev_out == '.') {
  2043. // multiple dots, separate by spaces. Note >3 dots has been replaced by elipsis
  2044. c = ' ';
  2045. space_inserted = true;
  2046. } else if ((word_count > 0) && !(words[word_count-1].flags & FLAG_NOSPACE) && IsAlpha(prev_in)) {
  2047. // dot after a word, with space following, probably an abbreviation
  2048. words[word_count-1].flags |= FLAG_HAS_DOT;
  2049. if (IsSpace(next_in) || (next_in == '-'))
  2050. c = ' '; // remove the dot if it's followed by a space or hyphen, so that it's not pronounced
  2051. }
  2052. } else if (c == '\'') {
  2053. if (((prev_in == '.' && next_in == 's') || iswalnum(prev_in)) && IsAlpha(next_in)) {
  2054. // between two letters, or in an abbreviation (eg. u.s.a.'s). Consider the apostrophe as part of the word
  2055. single_quoted = false;
  2056. } else if ((tr->langopts.param[LOPT_APOSTROPHE] & 1) && IsAlpha(next_in))
  2057. single_quoted = false; // apostrophe at start of word is part of the word
  2058. else if ((tr->langopts.param[LOPT_APOSTROPHE] & 2) && IsAlpha(prev_in))
  2059. single_quoted = false; // apostrophe at end of word is part of the word
  2060. else if ((wcschr(tr->char_plus_apostrophe, prev_in) != 0) && (prev_out2 == ' ')) {
  2061. // consider single character plus apostrophe as a word
  2062. single_quoted = false;
  2063. if (next_in == ' ')
  2064. source_index++; // skip following space
  2065. } else {
  2066. if ((prev_out == 's') && (single_quoted == false)) {
  2067. // looks like apostrophe after an 's'
  2068. c = ' ';
  2069. } else {
  2070. if (IsSpace(prev_out))
  2071. single_quoted = true;
  2072. else
  2073. single_quoted = false;
  2074. pre_pause_add = 4; // single quote
  2075. c = ' ';
  2076. }
  2077. }
  2078. } else if (lookupwchar(breaks, c) != 0)
  2079. c = ' '; // various characters to treat as space
  2080. else if (iswdigit(c)) {
  2081. if (tr->langopts.tone_numbers && IsAlpha(prev_out) && !IsDigit(next_in)) {
  2082. } else if ((prev_out != ' ') && !iswdigit(prev_out)) {
  2083. if ((prev_out != tr->langopts.decimal_sep) || ((decimal_sep_count == true) && (tr->langopts.decimal_sep == ','))) {
  2084. c = ' ';
  2085. space_inserted = true;
  2086. } else
  2087. decimal_sep_count = true;
  2088. } else if ((prev_out == ' ') && IsAlpha(prev_out2) && !IsAlpha(prev_in)) {
  2089. // insert extra space between a word and a number, to distinguish 'a 2' from 'a2'
  2090. sbuf[ix++] = ' ';
  2091. words[word_count].start++;
  2092. }
  2093. }
  2094. }
  2095. if (IsSpace(c)) {
  2096. if (prev_out == ' ') {
  2097. word_flags |= FLAG_MULTIPLE_SPACES;
  2098. continue; // multiple spaces
  2099. }
  2100. if ((cc == 0x09) || (cc == 0x0a))
  2101. next_word_flags |= FLAG_MULTIPLE_SPACES; // tab or newline, not a simple space
  2102. if (space_inserted) {
  2103. // count the number of characters since the start of the word
  2104. j = 0;
  2105. k = source_index - 1;
  2106. while ((k >= source_index_word) && (charix[k] != 0)) {
  2107. if (charix[k] > 0) // don't count initial bytes of multi-byte character
  2108. j++;
  2109. k--;
  2110. }
  2111. words[word_count].length = j;
  2112. }
  2113. source_index_word = source_index;
  2114. // end of 'word'
  2115. sbuf[ix++] = ' ';
  2116. if ((word_count < N_CLAUSE_WORDS-1) && (ix > words[word_count].start)) {
  2117. if (embedded_count > 0) {
  2118. // there are embedded commands before this word
  2119. embedded_list[embedded_ix-1] |= 0x80; // terminate list of commands for this word
  2120. words[word_count].flags |= FLAG_EMBEDDED;
  2121. embedded_count = 0;
  2122. }
  2123. if (alpha_count == 0) {
  2124. all_upper_case &= ~FLAG_ALL_UPPER;
  2125. }
  2126. words[word_count].pre_pause = pre_pause;
  2127. words[word_count].flags |= (all_upper_case | word_flags | word_emphasis);
  2128. if (pre_pause > 0) {
  2129. // insert an extra space before the word, to prevent influence from previous word across the pause
  2130. for (j = ix; j > words[word_count].start; j--)
  2131. sbuf[j] = sbuf[j-1];
  2132. sbuf[j] = ' ';
  2133. words[word_count].start++;
  2134. ix++;
  2135. }
  2136. word_count++;
  2137. words[word_count].start = ix;
  2138. words[word_count].flags = 0;
  2139. for (j = source_index; j < charix_top && charix[j] <= 0; j++) // skip blanks
  2140. ;
  2141. words[word_count].sourceix = charix[j];
  2142. k = 0;
  2143. while (charix[j] != 0) {
  2144. // count the number of characters (excluding multibyte continuation bytes)
  2145. if (charix[j++] != -1)
  2146. k++;
  2147. }
  2148. words[word_count].length = k;
  2149. word_flags = next_word_flags;
  2150. next_word_flags = 0;
  2151. pre_pause = 0;
  2152. all_upper_case = FLAG_ALL_UPPER;
  2153. alpha_count = 0;
  2154. syllable_marked = false;
  2155. }
  2156. if (space_inserted) {
  2157. source_index = prev_source_index; // rewind to the previous character
  2158. char_inserted = 0;
  2159. space_inserted = false;
  2160. }
  2161. } else {
  2162. if ((ix < (N_TR_SOURCE - 4)))
  2163. ix += utf8_out(c, &sbuf[ix]);
  2164. }
  2165. if (pre_pause_add > pre_pause)
  2166. pre_pause = pre_pause_add;
  2167. pre_pause_add = 0;
  2168. }
  2169. if ((word_count == 0) && (embedded_count > 0)) {
  2170. // add a null 'word' to carry the embedded command flag
  2171. embedded_list[embedded_ix-1] |= 0x80;
  2172. words[word_count].flags |= FLAG_EMBEDDED;
  2173. word_count = 1;
  2174. }
  2175. tr->clause_end = &sbuf[ix-1];
  2176. sbuf[ix] = 0;
  2177. words[0].pre_pause = 0; // don't add extra pause at beginning of clause
  2178. words[word_count].pre_pause = 8;
  2179. if (word_count > 0) {
  2180. ix = word_count-1;
  2181. while ((ix > 0) && (IsBracket(sbuf[words[ix].start])))
  2182. ix--; // the last word is a bracket, mark the previous word as last
  2183. words[ix].flags |= FLAG_LAST_WORD;
  2184. // FLAG_NOSPACE check to avoid recognizing .mr -mr
  2185. if ((terminator & CLAUSE_DOT_AFTER_LAST_WORD) && !(words[word_count-1].flags & FLAG_NOSPACE))
  2186. words[word_count-1].flags |= FLAG_HAS_DOT;
  2187. }
  2188. words[0].flags |= FLAG_FIRST_WORD;
  2189. // Each TranslateWord2 may require up to 7 phonemes
  2190. // and after this loop we require 2 phonemes
  2191. for (ix = 0; ix < word_count && (n_ph_list2 < N_PHONEME_LIST-7-2); ix++) {
  2192. int nx;
  2193. int c_temp;
  2194. char *pn;
  2195. char *pw;
  2196. int nw;
  2197. char number_buf[150];
  2198. WORD_TAB num_wtab[50]; // copy of 'words', when splitting numbers into parts
  2199. // start speaking at a specified word position in the text?
  2200. count_words++;
  2201. if (skip_words > 0) {
  2202. skip_words--;
  2203. if (skip_words == 0)
  2204. skipping_text = false;
  2205. }
  2206. if (skipping_text)
  2207. continue;
  2208. current_alphabet = NULL;
  2209. // digits should have been converted to Latin alphabet ('0' to '9')
  2210. word = pw = &sbuf[words[ix].start];
  2211. if (iswdigit(word[0]) && (tr->langopts.break_numbers != BREAK_THOUSANDS)) {
  2212. // Languages with 100000 numbers. Remove thousands separators so that we can insert them again later
  2213. pn = number_buf;
  2214. while (pn < &number_buf[sizeof(number_buf)-20]) {
  2215. if (iswdigit(*pw))
  2216. *pn++ = *pw++;
  2217. else if ((*pw == tr->langopts.thousands_sep) && (pw[1] == ' ')
  2218. && iswdigit(pw[2]) && (pw[3] != ' ') && (pw[4] != ' ')) { // don't allow only 1 or 2 digits in the final part
  2219. pw += 2;
  2220. ix++; // skip "word"
  2221. } else {
  2222. nx = pw - word;
  2223. memset(word, ' ', nx);
  2224. nx = pn - number_buf;
  2225. memcpy(word, number_buf, nx);
  2226. break;
  2227. }
  2228. }
  2229. pw = word;
  2230. }
  2231. for (n_digits = 0; iswdigit(word[n_digits]); n_digits++) // count consecutive digits
  2232. ;
  2233. if (n_digits > 4) {
  2234. // word is entirely digits, insert commas and break into 3 digit "words"
  2235. number_buf[0] = ' ';
  2236. number_buf[1] = ' ';
  2237. number_buf[2] = ' ';
  2238. pn = &number_buf[3];
  2239. nx = n_digits;
  2240. nw = 0;
  2241. if ((n_digits > tr->langopts.max_digits) || (word[0] == '0'))
  2242. words[ix].flags |= FLAG_INDIVIDUAL_DIGITS;
  2243. while (pn < &number_buf[sizeof(number_buf)-20]) {
  2244. if (!IsDigit09(c = *pw++) && (c != tr->langopts.decimal_sep))
  2245. break;
  2246. *pn++ = c;
  2247. nx--;
  2248. if ((nx > 0) && (tr->langopts.break_numbers & (1 << nx))) {
  2249. memcpy(&num_wtab[nw++], &words[ix], sizeof(WORD_TAB)); // copy the 'words' entry for each word of numbers
  2250. if (tr->langopts.thousands_sep != ' ')
  2251. *pn++ = tr->langopts.thousands_sep;
  2252. *pn++ = ' ';
  2253. if ((words[ix].flags & FLAG_INDIVIDUAL_DIGITS) == 0) {
  2254. if (tr->langopts.break_numbers & (1 << (nx-1))) {
  2255. // the next group only has 1 digits, make it three
  2256. *pn++ = '0';
  2257. *pn++ = '0';
  2258. }
  2259. if (tr->langopts.break_numbers & (1 << (nx-2))) {
  2260. // the next group only has 2 digits (eg. Indian languages), make it three
  2261. *pn++ = '0';
  2262. }
  2263. }
  2264. }
  2265. }
  2266. pw--;
  2267. memcpy(&num_wtab[nw], &words[ix], sizeof(WORD_TAB)*2); // the original number word, and the word after it
  2268. for (j = 1; j <= nw; j++)
  2269. num_wtab[j].flags &= ~(FLAG_MULTIPLE_SPACES | FLAG_EMBEDDED); // don't use these flags for subsequent parts when splitting a number
  2270. // include the next few characters, in case there are an ordinal indicator or other suffix
  2271. memcpy(pn, pw, 16);
  2272. pn[16] = 0;
  2273. nw = 0;
  2274. for (pw = &number_buf[3]; pw < pn;) {
  2275. // keep wflags for each part, for FLAG_HYPHEN_AFTER
  2276. dict_flags = TranslateWord2(tr, pw, &num_wtab[nw++], words[ix].pre_pause);
  2277. while (*pw++ != ' ')
  2278. ;
  2279. words[ix].pre_pause = 0;
  2280. }
  2281. } else {
  2282. pre_pause = 0;
  2283. dict_flags = TranslateWord2(tr, word, &words[ix], words[ix].pre_pause);
  2284. if (pre_pause > words[ix+1].pre_pause) {
  2285. words[ix+1].pre_pause = pre_pause;
  2286. pre_pause = 0;
  2287. }
  2288. if (dict_flags & FLAG_SPELLWORD) {
  2289. // redo the word, speaking single letters
  2290. for (pw = word; *pw != ' ';) {
  2291. memset(number_buf, ' ', 9);
  2292. nx = utf8_in(&c_temp, pw);
  2293. memcpy(&number_buf[2], pw, nx);
  2294. TranslateWord2(tr, &number_buf[2], &words[ix], 0);
  2295. pw += nx;
  2296. }
  2297. }
  2298. if ((dict_flags & (FLAG_ALLOW_DOT | FLAG_NEEDS_DOT)) && (ix == word_count - 1 - dictionary_skipwords) && (terminator & CLAUSE_DOT_AFTER_LAST_WORD)) {
  2299. // probably an abbreviation such as Mr. or B. rather than end of sentence
  2300. clause_pause = 10;
  2301. if (tone_out != NULL)
  2302. *tone_out = 4;
  2303. }
  2304. }
  2305. if (dict_flags & FLAG_SKIPWORDS) {
  2306. // dictionary indicates skip next word(s)
  2307. while (dictionary_skipwords > 0) {
  2308. words[ix+dictionary_skipwords].flags |= FLAG_DELETE_WORD;
  2309. dictionary_skipwords--;
  2310. }
  2311. }
  2312. }
  2313. if (embedded_read < embedded_ix) {
  2314. // any embedded commands not yet processed?
  2315. Word_EmbeddedCmd();
  2316. }
  2317. for (ix = 0; ix < 2; ix++) {
  2318. // terminate the clause with 2 PAUSE phonemes
  2319. PHONEME_LIST2 *p2;
  2320. p2 = &ph_list2[n_ph_list2 + ix];
  2321. p2->phcode = phonPAUSE;
  2322. p2->stresslevel = 0;
  2323. p2->sourceix = source_index;
  2324. p2->synthflags = 0;
  2325. }
  2326. n_ph_list2 += 2;
  2327. if (count_words == 0)
  2328. clause_pause = 0;
  2329. if (Eof() && ((word_count == 0) || (option_endpause == 0)))
  2330. clause_pause = 10;
  2331. MakePhonemeList(tr, clause_pause, new_sentence2);
  2332. phoneme_list[N_PHONEME_LIST].ph = NULL; // recognize end of phoneme_list array, in Generate()
  2333. phoneme_list[N_PHONEME_LIST].sourceix = 1;
  2334. if (embedded_count) { // ???? is this needed
  2335. phoneme_list[n_phoneme_list-2].synthflags = SFLAG_EMBEDDED;
  2336. embedded_list[embedded_ix-1] |= 0x80;
  2337. embedded_list[embedded_ix] = 0x80;
  2338. }
  2339. prev_clause_pause = clause_pause;
  2340. new_sentence = false;
  2341. if (terminator & CLAUSE_TYPE_SENTENCE)
  2342. new_sentence = true; // next clause is a new sentence
  2343. if (voice_change != NULL) {
  2344. // return new voice name if an embedded voice change command terminated the clause
  2345. if (terminator & CLAUSE_TYPE_VOICE_CHANGE)
  2346. *voice_change = voice_change_name;
  2347. else
  2348. *voice_change = NULL;
  2349. }
  2350. }
  2351. void InitText(int control)
  2352. {
  2353. count_sentences = 0;
  2354. count_words = 0;
  2355. end_character_position = 0;
  2356. skip_sentences = 0;
  2357. skip_marker[0] = 0;
  2358. skip_words = 0;
  2359. skip_characters = 0;
  2360. skipping_text = false;
  2361. new_sentence = true;
  2362. prev_clause_pause = 0;
  2363. option_sayas = 0;
  2364. option_sayas2 = 0;
  2365. option_emphasis = 0;
  2366. word_emphasis = 0;
  2367. embedded_flag = 0;
  2368. InitText2();
  2369. if ((control & espeakKEEP_NAMEDATA) == 0)
  2370. InitNamedata();
  2371. }