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

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