translate.cpp 77 KB

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