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

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