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

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