eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

translate.c 79KB

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