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

translate.c 77KB

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