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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089
  1. /*
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015 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 <stdio.h>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #if HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #include <wctype.h>
  28. #include <wchar.h>
  29. #include "speak_lib.h"
  30. #include "speech.h"
  31. #include "phoneme.h"
  32. #include "synthesize.h"
  33. #include "voice.h"
  34. #include "translate.h"
  35. #define WORD_STRESS_CHAR '*'
  36. Translator *translator = NULL; // the main translator
  37. Translator *translator2 = NULL; // secondary translator for certain words
  38. static char translator2_language[20] = { 0 };
  39. FILE *f_trans = NULL; // phoneme output text
  40. int option_tone2 = 0;
  41. int option_tone_flags = 0; // bit 8=emphasize allcaps, bit 9=emphasize penultimate stress
  42. int option_phonemes = 0;
  43. int option_phoneme_events = 0;
  44. int option_quiet = 0;
  45. int option_endpause = 0; // suppress pause after end of text
  46. int option_capitals = 0;
  47. int option_punctuation = 0;
  48. int option_sayas = 0;
  49. static int option_sayas2 = 0; // used in translate_clause()
  50. static int option_emphasis = 0; // 0=normal, 1=normal, 2=weak, 3=moderate, 4=strong
  51. int option_ssml = 0;
  52. int option_phoneme_input = 0; // allow [[phonemes]] in input
  53. int option_phoneme_variants = 0; // 0= don't display phoneme variant mnemonics
  54. int option_wordgap = 0;
  55. static int count_sayas_digits;
  56. int skip_sentences;
  57. int skip_words;
  58. int skip_characters;
  59. char skip_marker[N_MARKER_LENGTH];
  60. int skipping_text; // waiting until word count, sentence count, or named marker is reached
  61. int end_character_position;
  62. int count_sentences;
  63. int count_words;
  64. int clause_start_char;
  65. int clause_start_word;
  66. int new_sentence;
  67. static int word_emphasis = 0; // set if emphasis level 3 or 4
  68. static int embedded_flag = 0; // there are embedded commands to be applied to the next phoneme, used in TranslateWord2()
  69. static int prev_clause_pause = 0;
  70. static int max_clause_pause = 0;
  71. static int any_stressed_words;
  72. int pre_pause;
  73. ALPHABET *current_alphabet;
  74. // these were previously in translator class
  75. #ifdef PLATFORM_WINDOWS
  76. char word_phonemes[N_WORD_PHONEMES*2]; // longer, because snprint() is not available
  77. #else
  78. char word_phonemes[N_WORD_PHONEMES]; // a word translated into phoneme codes
  79. #endif
  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. char ctrl_embedded = '\001'; // to allow an alternative CTRL for embedded commands
  84. int option_multibyte = espeakCHARS_AUTO; // 0=auto, 1=utf8, 2=8bit, 3=wchar, 4=16bit
  85. // these are overridden by defaults set in the "speak" file
  86. int option_linelength = 0;
  87. #define N_EMBEDDED_LIST 250
  88. static int embedded_ix;
  89. static int embedded_read;
  90. unsigned int embedded_list[N_EMBEDDED_LIST];
  91. // the source text of a single clause (UTF8 bytes)
  92. static char source[N_TR_SOURCE+40]; // extra space for embedded command & voice change info at end
  93. int n_replace_phonemes;
  94. REPLACE_PHONEMES replace_phonemes[N_REPLACE_PHONEMES];
  95. // brackets, also 0x2014 to 0x021f which don't need to be in this list
  96. static const unsigned short brackets[] = {
  97. '(', ')', '[', ']', '{', '}', '<', '>', '"', '\'', '`',
  98. 0xab, 0xbb, // double angle brackets
  99. 0x300a, 0x300b, // double angle brackets (ideograph)
  100. 0xe000+'<', // private usage area
  101. 0
  102. };
  103. // other characters which break a word, but don't produce a pause
  104. static const unsigned short breaks[] = { '_', 0 };
  105. // Translate character codes 0xA0 to 0xFF into their unicode values
  106. // ISO_8859_1 is set as default
  107. static const unsigned short ISO_8859_1[0x60] = {
  108. 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, // a0
  109. 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, // a8
  110. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, // b0
  111. 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, // b8
  112. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
  113. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  114. 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, // d0
  115. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, // d8
  116. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
  117. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  118. 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, // f0
  119. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff, // f8
  120. };
  121. static const unsigned short ISO_8859_2[0x60] = {
  122. 0x00a0, 0x0104, 0x02d8, 0x0141, 0x00a4, 0x013d, 0x015a, 0x00a7, // a0
  123. 0x00a8, 0x0160, 0x015e, 0x0164, 0x0179, 0x00ad, 0x017d, 0x017b, // a8
  124. 0x00b0, 0x0105, 0x02db, 0x0142, 0x00b4, 0x013e, 0x015b, 0x02c7, // b0
  125. 0x00b8, 0x0161, 0x015f, 0x0165, 0x017a, 0x02dd, 0x017e, 0x017c, // b8
  126. 0x0154, 0x00c1, 0x00c2, 0x0102, 0x00c4, 0x0139, 0x0106, 0x00c7, // c0
  127. 0x010c, 0x00c9, 0x0118, 0x00cb, 0x011a, 0x00cd, 0x00ce, 0x010e, // c8
  128. 0x0110, 0x0143, 0x0147, 0x00d3, 0x00d4, 0x0150, 0x00d6, 0x00d7, // d0
  129. 0x0158, 0x016e, 0x00da, 0x0170, 0x00dc, 0x00dd, 0x0162, 0x00df, // d8
  130. 0x0155, 0x00e1, 0x00e2, 0x0103, 0x00e4, 0x013a, 0x0107, 0x00e7, // e0
  131. 0x010d, 0x00e9, 0x0119, 0x00eb, 0x011b, 0x00ed, 0x00ee, 0x010f, // e8
  132. 0x0111, 0x0144, 0x0148, 0x00f3, 0x00f4, 0x0151, 0x00f6, 0x00f7, // f0
  133. 0x0159, 0x016f, 0x00fa, 0x0171, 0x00fc, 0x00fd, 0x0163, 0x02d9, // f8
  134. };
  135. static const unsigned short ISO_8859_3[0x60] = {
  136. 0x00a0, 0x0126, 0x02d8, 0x00a3, 0x00a4, 0x0000, 0x0124, 0x00a7, // a0
  137. 0x00a8, 0x0130, 0x015e, 0x011e, 0x0134, 0x00ad, 0x0000, 0x017b, // a8
  138. 0x00b0, 0x0127, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x0125, 0x00b7, // b0
  139. 0x00b8, 0x0131, 0x015f, 0x011f, 0x0135, 0x00bd, 0x0000, 0x017c, // b8
  140. 0x00c0, 0x00c1, 0x00c2, 0x0000, 0x00c4, 0x010a, 0x0108, 0x00c7, // c0
  141. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  142. 0x0000, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x0120, 0x00d6, 0x00d7, // d0
  143. 0x011c, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x016c, 0x015c, 0x00df, // d8
  144. 0x00e0, 0x00e1, 0x00e2, 0x0000, 0x00e4, 0x010b, 0x0109, 0x00e7, // e0
  145. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  146. 0x0000, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x0121, 0x00f6, 0x00f7, // f0
  147. 0x011d, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x016d, 0x015d, 0x02d9, // f8
  148. };
  149. static const unsigned short ISO_8859_4[0x60] = {
  150. 0x00a0, 0x0104, 0x0138, 0x0156, 0x00a4, 0x0128, 0x013b, 0x00a7, // a0
  151. 0x00a8, 0x0160, 0x0112, 0x0122, 0x0166, 0x00ad, 0x017d, 0x00af, // a8
  152. 0x00b0, 0x0105, 0x02db, 0x0157, 0x00b4, 0x0129, 0x013c, 0x02c7, // b0
  153. 0x00b8, 0x0161, 0x0113, 0x0123, 0x0167, 0x014a, 0x017e, 0x014b, // b8
  154. 0x0100, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x012e, // c0
  155. 0x010c, 0x00c9, 0x0118, 0x00cb, 0x0116, 0x00cd, 0x00ce, 0x012a, // c8
  156. 0x0110, 0x0145, 0x014c, 0x0136, 0x00d4, 0x00d5, 0x00d6, 0x00d7, // d0
  157. 0x00d8, 0x0172, 0x00da, 0x00db, 0x00dc, 0x0168, 0x016a, 0x00df, // d8
  158. 0x0101, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x012f, // e0
  159. 0x010d, 0x00e9, 0x0119, 0x00eb, 0x0117, 0x00ed, 0x00ee, 0x012b, // e8
  160. 0x0111, 0x0146, 0x014d, 0x0137, 0x00f4, 0x00f5, 0x00f6, 0x00f7, // f0
  161. 0x00f8, 0x0173, 0x00fa, 0x00fb, 0x00fc, 0x0169, 0x016b, 0x02d9, // f8
  162. };
  163. static const unsigned short ISO_8859_5[0x60] = {
  164. 0x00a0, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407, // a0 Cyrillic
  165. 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x00ad, 0x040e, 0x040f, // a8
  166. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, // b0
  167. 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f, // b8
  168. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, // c0
  169. 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f, // c8
  170. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, // d0
  171. 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f, // d8
  172. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, // e0
  173. 0x0448, 0x0449, 0x044a, 0x044b, 0x044c, 0x044d, 0x044e, 0x044f, // e8
  174. 0x2116, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457, // f0
  175. 0x0458, 0x0459, 0x045a, 0x045b, 0x045c, 0x00a7, 0x045e, 0x045f, // f8
  176. };
  177. static const unsigned short ISO_8859_7[0x60] = {
  178. 0x00a0, 0x2018, 0x2019, 0x00a3, 0x20ac, 0x20af, 0x00a6, 0x00a7, // a0 Greek
  179. 0x00a8, 0x00a9, 0x037a, 0x00ab, 0x00ac, 0x00ad, 0x0000, 0x2015, // a8
  180. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x0384, 0x0385, 0x0386, 0x00b7, // b0
  181. 0x0388, 0x0389, 0x038a, 0x00bb, 0x038c, 0x00bd, 0x038e, 0x038f, // b8
  182. 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, // c0
  183. 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, // c8
  184. 0x03a0, 0x03a1, 0x0000, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7, // d0
  185. 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af, // d8
  186. 0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7, // e0
  187. 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf, // e8
  188. 0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6, 0x03c7, // f0
  189. 0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce, 0x0000, // f8
  190. };
  191. static const unsigned short ISO_8859_9[0x60] = {
  192. 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, // a0
  193. 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, // a8
  194. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, // b0
  195. 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, // b8
  196. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
  197. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  198. 0x011e, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, // d0
  199. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x0130, 0x015e, 0x00df, // d8
  200. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
  201. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  202. 0x011f, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, // f0
  203. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x0131, 0x015f, 0x00ff, // f8
  204. };
  205. static const unsigned short ISO_8859_14[0x60] = {
  206. 0x00a0, 0x1e02, 0x1e03, 0x00a3, 0x010a, 0x010b, 0x1e0a, 0x00a7, // a0 Welsh
  207. 0x1e80, 0x00a9, 0x1e82, 0x1e0b, 0x1ef2, 0x00ad, 0x00ae, 0x0178, // a8
  208. 0x1e1e, 0x1e1f, 0x0120, 0x0121, 0x1e40, 0x1e41, 0x00b6, 0x1e56, // b0
  209. 0x1e81, 0x1e57, 0x1e83, 0x1e60, 0x1ef3, 0x1e84, 0x1e85, 0x1e61, // b8
  210. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
  211. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  212. 0x0174, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x1e6a, // d0
  213. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x0176, 0x00df, // d8
  214. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
  215. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  216. 0x0175, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x1e6b, // f0
  217. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x0177, 0x00ff, // f8
  218. };
  219. static const unsigned short KOI8_R[0x60] = {
  220. 0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556, // a0 Russian
  221. 0x2557, 0x2558, 0x2559, 0x255a, 0x255b, 0x255c, 0x255d, 0x255e, // a8
  222. 0x255f, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565, // b0
  223. 0x2566, 0x2567, 0x2568, 0x2569, 0x256a, 0x256b, 0x256c, 0x00a9, // b8
  224. 0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, // c0
  225. 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, // c8
  226. 0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, // d0
  227. 0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a, // d8
  228. 0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, // e0
  229. 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, // e8
  230. 0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, // f0
  231. 0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a, // f8
  232. };
  233. static const unsigned short ISCII[0x60] = {
  234. 0x0020, 0x0901, 0x0902, 0x0903, 0x0905, 0x0906, 0x0907, 0x0908, // a0
  235. 0x0909, 0x090a, 0x090b, 0x090e, 0x090f, 0x0910, 0x090d, 0x0912, // a8
  236. 0x0913, 0x0914, 0x0911, 0x0915, 0x0916, 0x0917, 0x0918, 0x0919, // b0
  237. 0x091a, 0x091b, 0x091c, 0x091d, 0x091e, 0x091f, 0x0920, 0x0921, // b8
  238. 0x0922, 0x0923, 0x0924, 0x0925, 0x0926, 0x0927, 0x0928, 0x0929, // c0
  239. 0x092a, 0x092b, 0x092c, 0x092d, 0x092e, 0x092f, 0x095f, 0x0930, // c8
  240. 0x0931, 0x0932, 0x0933, 0x0934, 0x0935, 0x0936, 0x0937, 0x0938, // d0
  241. 0x0939, 0x0020, 0x093e, 0x093f, 0x0940, 0x0941, 0x0942, 0x0943, // d8
  242. 0x0946, 0x0947, 0x0948, 0x0945, 0x094a, 0x094b, 0x094c, 0x0949, // e0
  243. 0x094d, 0x093c, 0x0964, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, // e8
  244. 0x0020, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, // f0
  245. 0x0037, 0x0038, 0x0039, 0x20, 0x20, 0x20, 0x20, 0x20, // f8
  246. };
  247. const unsigned short *charsets[N_CHARSETS] = {
  248. ISO_8859_1,
  249. ISO_8859_1,
  250. ISO_8859_2,
  251. ISO_8859_3,
  252. ISO_8859_4,
  253. ISO_8859_5,
  254. ISO_8859_1,
  255. ISO_8859_7,
  256. ISO_8859_1,
  257. ISO_8859_9,
  258. ISO_8859_1,
  259. ISO_8859_1,
  260. ISO_8859_1,
  261. ISO_8859_1,
  262. ISO_8859_14,
  263. ISO_8859_1,
  264. ISO_8859_1,
  265. ISO_8859_1,
  266. KOI8_R, // 18
  267. ISCII
  268. };
  269. // Tables of the relative lengths of vowels, depending on the
  270. // type of the two phonemes that follow
  271. // indexes are the "length_mod" value for the following phonemes
  272. // use this table if vowel is not the last in the word
  273. static unsigned char length_mods_en[100] = {
  274. /* a , t s n d z r N <- next */
  275. 100, 120, 100, 105, 100, 110, 110, 100, 95, 100, /* a <- next2 */
  276. 105, 120, 105, 110, 125, 130, 135, 115, 125, 100, /* , */
  277. 105, 120, 75, 100, 75, 105, 120, 85, 75, 100, /* t */
  278. 105, 120, 85, 105, 95, 115, 120, 100, 95, 100, /* s */
  279. 110, 120, 95, 105, 100, 115, 120, 100, 100, 100, /* n */
  280. 105, 120, 100, 105, 95, 115, 120, 110, 95, 100, /* d */
  281. 105, 120, 100, 105, 105, 122, 125, 110, 105, 100, /* z */
  282. 105, 120, 100, 105, 105, 122, 125, 110, 105, 100, /* r */
  283. 105, 120, 95, 105, 100, 115, 120, 110, 100, 100, /* N */
  284. 100, 120, 100, 100, 100, 100, 100, 100, 100, 100
  285. };
  286. // as above, but for the last syllable in a word
  287. static unsigned char length_mods_en0[100] = {
  288. /* a , t s n d z r N <- next */
  289. 100, 150, 100, 105, 110, 115, 110, 110, 110, 100, /* a <- next2 */
  290. 105, 150, 105, 110, 125, 135, 140, 115, 135, 100, /* , */
  291. 105, 150, 90, 105, 90, 122, 135, 100, 90, 100, /* t */
  292. 105, 150, 100, 105, 100, 122, 135, 100, 100, 100, /* s */
  293. 105, 150, 100, 105, 105, 115, 135, 110, 105, 100, /* n */
  294. 105, 150, 100, 105, 105, 122, 130, 120, 125, 100, /* d */
  295. 105, 150, 100, 105, 110, 122, 125, 115, 110, 100, /* z */
  296. 105, 150, 100, 105, 105, 122, 135, 120, 105, 100, /* r */
  297. 105, 150, 100, 105, 105, 115, 135, 110, 105, 100, /* N */
  298. 100, 100, 100, 100, 100, 100, 100, 100, 100, 100
  299. };
  300. static unsigned char length_mods_equal[100] = {
  301. /* a , t s n d z r N <- next */
  302. 110, 120, 100, 110, 110, 110, 110, 110, 110, 110, /* a <- next2 */
  303. 110, 120, 100, 110, 110, 110, 110, 110, 110, 110, /* , */
  304. 110, 120, 100, 110, 100, 110, 110, 110, 100, 110, /* t */
  305. 110, 120, 100, 110, 110, 110, 110, 110, 110, 110, /* s */
  306. 110, 120, 100, 110, 110, 110, 110, 110, 110, 110, /* n */
  307. 110, 120, 100, 110, 110, 110, 110, 110, 110, 110, /* d */
  308. 110, 120, 100, 110, 110, 110, 110, 110, 110, 110, /* z */
  309. 110, 120, 100, 110, 110, 110, 110, 110, 110, 110, /* r */
  310. 110, 120, 100, 110, 110, 110, 110, 110, 110, 110, /* N */
  311. 110, 120, 100, 110, 110, 110, 110, 110, 110, 110
  312. };
  313. static unsigned char *length_mod_tabs[6] = {
  314. length_mods_en,
  315. length_mods_en, // 1
  316. length_mods_en0, // 2
  317. length_mods_equal, // 3
  318. length_mods_equal, // 4
  319. length_mods_equal // 5
  320. };
  321. void SetLengthMods(Translator *tr, int value)
  322. {
  323. int value2;
  324. tr->langopts.length_mods0 = tr->langopts.length_mods = length_mod_tabs[value % 100];
  325. if ((value2 = value / 100) != 0) {
  326. tr->langopts.length_mods0 = length_mod_tabs[value2];
  327. }
  328. }
  329. int IsAlpha(unsigned int c)
  330. {
  331. // Replacement for iswalph() which also checks for some in-word symbols
  332. static const unsigned short extra_indic_alphas[] = {
  333. 0xa70, 0xa71, // Gurmukhi: tippi, addak
  334. 0
  335. };
  336. if (iswalpha2(c))
  337. return 1;
  338. if (c < 0x300)
  339. return 0;
  340. if ((c >= 0x901) && (c <= 0xdf7)) {
  341. // Indic scripts: Devanagari, Tamil, etc
  342. if ((c & 0x7f) < 0x64)
  343. return 1;
  344. if (lookupwchar(extra_indic_alphas, c) != 0)
  345. return 1;
  346. if ((c >= 0xd7a) && (c <= 0xd7f))
  347. return 1; // malaytalam chillu characters
  348. return 0;
  349. }
  350. if ((c >= 0x5b0) && (c <= 0x5c2))
  351. return 1; // Hebrew vowel marks
  352. if (c == 0x0605)
  353. return 1;
  354. if ((c == 0x670) || ((c >= 0x64b) && (c <= 0x65e)))
  355. return 1; // arabic vowel marks
  356. if ((c >= 0x300) && (c <= 0x36f))
  357. return 1; // combining accents
  358. if ((c >= 0x780) && (c <= 0x7b1))
  359. return 1; // taani/divehi (maldives)
  360. if ((c >= 0xf40) && (c <= 0xfbc))
  361. return 1; // tibetan
  362. if ((c >= 0x1100) && (c <= 0x11ff))
  363. return 1; // Korean jamo
  364. if ((c >= 0x2800) && (c <= 0x28ff))
  365. return 1; // braille
  366. if ((c > 0x3040) && (c <= 0xa700))
  367. return 1; // Chinese/Japanese. Should never get here, but Mac OS 10.4's iswalpha seems to be broken, so just make sure
  368. return 0;
  369. }
  370. int IsDigit09(unsigned int c)
  371. {
  372. if ((c >= '0') && (c <= '9'))
  373. return 1;
  374. return 0;
  375. }
  376. int IsDigit(unsigned int c)
  377. {
  378. if (iswdigit(c))
  379. return 1;
  380. if ((c >= 0x966) && (c <= 0x96f))
  381. return 1;
  382. return 0;
  383. }
  384. int IsSpace(unsigned int c)
  385. {
  386. if (c == 0)
  387. return 0;
  388. if ((c >= 0x2500) && (c < 0x25a0))
  389. return 1; // box drawing characters
  390. if ((c >= 0xfff9) && (c <= 0xffff))
  391. return 1; // unicode specials
  392. return iswspace(c);
  393. }
  394. void DeleteTranslator(Translator *tr)
  395. {
  396. if (tr->data_dictlist != NULL)
  397. Free(tr->data_dictlist);
  398. Free(tr);
  399. }
  400. int lookupwchar(const unsigned short *list, int c)
  401. {
  402. // Is the character c in the list ?
  403. int ix;
  404. for (ix = 0; list[ix] != 0; ix++) {
  405. if (list[ix] == c)
  406. return ix+1;
  407. }
  408. return 0;
  409. }
  410. int lookupwchar2(const unsigned short *list, int c)
  411. {
  412. // Replace character c by another character.
  413. // Returns 0 = not found, 1 = delete character
  414. int ix;
  415. for (ix = 0; list[ix] != 0; ix += 2) {
  416. if (list[ix] == c)
  417. return list[ix+1];
  418. }
  419. return 0;
  420. }
  421. int IsBracket(int c)
  422. {
  423. if ((c >= 0x2014) && (c <= 0x201f))
  424. return 1;
  425. return lookupwchar(brackets, c);
  426. }
  427. int utf8_out(unsigned int c, char *buf)
  428. {
  429. // write a unicode character into a buffer as utf8
  430. // returns the number of bytes written
  431. int n_bytes;
  432. int j;
  433. int shift;
  434. static char unsigned code[4] = { 0, 0xc0, 0xe0, 0xf0 };
  435. if (c < 0x80) {
  436. buf[0] = c;
  437. return 1;
  438. }
  439. if (c >= 0x110000) {
  440. buf[0] = ' '; // out of range character code
  441. return 1;
  442. }
  443. if (c < 0x0800)
  444. n_bytes = 1;
  445. else if (c < 0x10000)
  446. n_bytes = 2;
  447. else
  448. n_bytes = 3;
  449. shift = 6*n_bytes;
  450. buf[0] = code[n_bytes] | (c >> shift);
  451. for (j = 0; j < n_bytes; j++) {
  452. shift -= 6;
  453. buf[j+1] = 0x80 + ((c >> shift) & 0x3f);
  454. }
  455. return n_bytes+1;
  456. }
  457. int utf8_nbytes(const char *buf)
  458. {
  459. // Returns the number of bytes for the first UTF-8 character in buf
  460. unsigned char c = (unsigned char)buf[0];
  461. if (c < 0x80)
  462. return 1;
  463. if (c < 0xe0)
  464. return 2;
  465. if (c < 0xf0)
  466. return 3;
  467. return 4;
  468. }
  469. int utf8_in2(int *c, const char *buf, int backwards)
  470. {
  471. // Read a unicode characater from a UTF8 string
  472. // Returns the number of UTF8 bytes used.
  473. // backwards: set if we are moving backwards through the UTF8 string
  474. int c1;
  475. int n_bytes;
  476. int ix;
  477. static const unsigned char mask[4] = { 0xff, 0x1f, 0x0f, 0x07 };
  478. // find the start of the next/previous character
  479. while ((*buf & 0xc0) == 0x80) {
  480. // skip over non-initial bytes of a multi-byte utf8 character
  481. if (backwards)
  482. buf--;
  483. else
  484. buf++;
  485. }
  486. n_bytes = 0;
  487. if ((c1 = *buf++) & 0x80) {
  488. if ((c1 & 0xe0) == 0xc0)
  489. n_bytes = 1;
  490. else if ((c1 & 0xf0) == 0xe0)
  491. n_bytes = 2;
  492. else if ((c1 & 0xf8) == 0xf0)
  493. n_bytes = 3;
  494. c1 &= mask[n_bytes];
  495. for (ix = 0; ix < n_bytes; ix++) {
  496. c1 = (c1 << 6) + (*buf++ & 0x3f);
  497. }
  498. }
  499. *c = c1;
  500. return n_bytes+1;
  501. }
  502. #pragma GCC visibility push(default)
  503. int utf8_in(int *c, const char *buf)
  504. {
  505. // Read a unicode characater from a UTF8 string
  506. // Returns the number of UTF8 bytes used.
  507. return utf8_in2(c, buf, 0);
  508. }
  509. #pragma GCC visibility pop
  510. char *strchr_w(const char *s, int c)
  511. {
  512. // return NULL for any non-ascii character
  513. if (c >= 0x80)
  514. return NULL;
  515. return strchr((char *)s, c); // (char *) is needed for Borland compiler
  516. }
  517. int IsAllUpper(const char *word)
  518. {
  519. int c;
  520. while ((*word != 0) && !isspace2(*word)) {
  521. word += utf8_in(&c, word);
  522. if (!iswupper2(c))
  523. return 0;
  524. }
  525. return 1;
  526. }
  527. static char *SpeakIndividualLetters(Translator *tr, char *word, char *phonemes, int spell_word)
  528. {
  529. int posn = 0;
  530. int capitals = 0;
  531. int non_initial = 0;
  532. if (spell_word > 2)
  533. capitals = 2; // speak 'capital'
  534. if (spell_word > 1)
  535. capitals |= 4; // speak charater code for unknown letters
  536. while ((*word != ' ') && (*word != 0)) {
  537. word += TranslateLetter(tr, word, phonemes, capitals | non_initial);
  538. posn++;
  539. non_initial = 1;
  540. if (phonemes[0] == phonSWITCH) {
  541. // change to another language in order to translate this word
  542. strcpy(word_phonemes, phonemes);
  543. return NULL;
  544. }
  545. }
  546. SetSpellingStress(tr, phonemes, spell_word, posn);
  547. return word;
  548. }
  549. static int CheckDottedAbbrev(char *word1, WORD_TAB *wtab)
  550. {
  551. int wc;
  552. int count = 0;
  553. int nbytes;
  554. int ok;
  555. int ix;
  556. char *word;
  557. char *wbuf;
  558. char word_buf[80];
  559. word = word1;
  560. wbuf = word_buf;
  561. ix = 0;
  562. for (;;) {
  563. ok = 0;
  564. nbytes = utf8_in(&wc, word);
  565. if ((word[nbytes] == ' ') && IsAlpha(wc)) {
  566. if (word[nbytes+1] == '.') {
  567. if (word[nbytes+2] == ' ')
  568. ok = 1;
  569. else if (word[nbytes+2] == '\'') {
  570. nbytes += 2; // delete the final dot (eg. u.s.a.'s)
  571. ok = 2;
  572. }
  573. } else if ((count > 0) && (word[nbytes] == ' '))
  574. ok = 2;
  575. }
  576. if (ok == 0)
  577. break;
  578. for (ix = 0; ix < nbytes; ix++)
  579. *wbuf++ = word[ix];
  580. count++;
  581. if (ok == 2) {
  582. word += nbytes;
  583. break;
  584. }
  585. word += (nbytes + 3);
  586. }
  587. if (count > 1) {
  588. ix = wbuf - word_buf;
  589. memcpy(word1, word_buf, ix);
  590. while (&word1[ix] < word)
  591. word1[ix++] = ' ';
  592. dictionary_skipwords = (count - 1)*2;
  593. }
  594. return count;
  595. }
  596. extern char *phondata_ptr;
  597. int ChangeEquivalentPhonemes(Translator *tr, int lang2, char *phonemes)
  598. {
  599. // tr: the original language
  600. // lang2: phoneme table number for the temporary language
  601. // phonemes: the phonemes to be replaced
  602. int ix;
  603. int len;
  604. char phon;
  605. char *p;
  606. unsigned char *pb;
  607. char *eqlist;
  608. char *p_out;
  609. char *p_in;
  610. int remove_stress = 0;
  611. char phonbuf[N_WORD_PHONEMES];
  612. // has a phoneme equivalence table been specified for thus language pair?
  613. if ((ix = phoneme_tab_list[tr->phoneme_tab_ix].equivalence_tables) == 0)
  614. return 0;
  615. pb = (unsigned char *)&phondata_ptr[ix];
  616. for (;;) {
  617. if (pb[0] == 0)
  618. return 0; // table not found
  619. if (pb[0] == lang2)
  620. break;
  621. len = (pb[2] << 8) + pb[3]; // size of this table in words
  622. pb += (len * 4);
  623. }
  624. remove_stress = pb[1];
  625. if (option_phonemes & espeakPHONEMES_TRACE) {
  626. DecodePhonemes(phonemes, phonbuf);
  627. fprintf(f_trans, "(%s) %s -> (%s) ", phoneme_tab_list[lang2].name, phonbuf, phoneme_tab_list[tr->phoneme_tab_ix].name);
  628. }
  629. p_in = phonemes;
  630. eqlist = (char *)&pb[8];
  631. p_out = phonbuf;
  632. while ((phon = *p_in++) != 0) {
  633. if (remove_stress && ((phon & 0xff) < phonSTRESS_PREV))
  634. continue; // remove stress marks
  635. // is there a translation for this phoneme code?
  636. p = eqlist;
  637. while (*p != 0) {
  638. len = strlen(&p[1]);
  639. if (*p == phon) {
  640. strcpy(p_out, &p[1]);
  641. p_out += len;
  642. break;
  643. }
  644. p += (len + 2);
  645. }
  646. if (*p == 0) {
  647. // no translation found
  648. *p_out++ = phon;
  649. }
  650. }
  651. *p_out = 0;
  652. if (remove_stress) {
  653. SetWordStress(tr, phonbuf, NULL, -1, 0);
  654. }
  655. strcpy(phonemes, phonbuf);
  656. if (option_phonemes & espeakPHONEMES_TRACE) {
  657. SelectPhonemeTable(tr->phoneme_tab_ix);
  658. DecodePhonemes(phonemes, phonbuf);
  659. fprintf(f_trans, "%s\n\n", phonbuf);
  660. }
  661. return 1;
  662. }
  663. int TranslateWord(Translator *tr, char *word_start, int next_pause, WORD_TAB *wtab, char *word_out)
  664. {
  665. // word1 is terminated by space (0x20) character
  666. char *word1;
  667. int word_length;
  668. int ix;
  669. char *p;
  670. int pfix;
  671. int n_chars;
  672. unsigned int dictionary_flags[2];
  673. unsigned int dictionary_flags2[2];
  674. int end_type = 0;
  675. int end_type1 = 0;
  676. int prefix_type = 0;
  677. int prefix_stress;
  678. char *wordx;
  679. char phonemes[N_WORD_PHONEMES];
  680. char phonemes2[N_WORD_PHONEMES];
  681. char prefix_phonemes[N_WORD_PHONEMES];
  682. char unpron_phonemes[N_WORD_PHONEMES];
  683. char end_phonemes[N_WORD_PHONEMES];
  684. char end_phonemes2[N_WORD_PHONEMES];
  685. char word_copy[N_WORD_BYTES];
  686. char word_copy2[N_WORD_BYTES];
  687. int word_copy_length;
  688. char prefix_chars[0x3f + 2];
  689. int found = 0;
  690. int end_flags;
  691. int c_temp; // save a character byte while we temporarily replace it with space
  692. int first_char;
  693. int last_char = 0;
  694. int add_plural_suffix = 0;
  695. int prefix_flags = 0;
  696. int more_suffixes;
  697. int confirm_prefix;
  698. int spell_word;
  699. int stress_bits;
  700. int emphasize_allcaps = 0;
  701. int wflags;
  702. int wmark;
  703. int was_unpronouncable = 0;
  704. int loopcount;
  705. int add_suffix_phonemes = 0;
  706. WORD_TAB wtab_null[8];
  707. // translate these to get pronunciations of plural 's' suffix (different forms depending on
  708. // the preceding letter
  709. static char word_zz[4] = { 0, 'z', 'z', 0 };
  710. static char word_iz[4] = { 0, 'i', 'z', 0 };
  711. static char word_ss[4] = { 0, 's', 's', 0 };
  712. if (wtab == NULL) {
  713. memset(wtab_null, 0, sizeof(wtab_null));
  714. wtab = wtab_null;
  715. }
  716. wflags = wtab->flags;
  717. wmark = wtab->wmark;
  718. dictionary_flags[0] = 0;
  719. dictionary_flags[1] = 0;
  720. dictionary_flags2[0] = 0;
  721. dictionary_flags2[1] = 0;
  722. dictionary_skipwords = 0;
  723. phonemes[0] = 0;
  724. unpron_phonemes[0] = 0;
  725. prefix_phonemes[0] = 0;
  726. end_phonemes[0] = 0;
  727. if (tr->data_dictlist == NULL) {
  728. // dictionary is not loaded
  729. word_phonemes[0] = 0;
  730. return 0;
  731. }
  732. // count the length of the word
  733. word1 = word_start;
  734. if (*word1 == ' ') word1++; // possibly a dot was replaced by space: $dot
  735. wordx = word1;
  736. utf8_in(&first_char, wordx);
  737. word_length = 0;
  738. while ((*wordx != 0) && (*wordx != ' ')) {
  739. wordx += utf8_in(&last_char, wordx);
  740. word_length++;
  741. }
  742. word_copy_length = wordx - word_start;
  743. if (word_copy_length >= N_WORD_BYTES)
  744. word_copy_length = N_WORD_BYTES-1;
  745. memcpy(word_copy2, word_start, word_copy_length);
  746. spell_word = 0;
  747. if ((word_length == 1) && (wflags & FLAG_TRANSLATOR2)) {
  748. // retranslating a 1-character word using a different language, say its name
  749. utf8_in(&c_temp, wordx+1); // the next character
  750. if (!IsAlpha(c_temp) || (AlphabetFromChar(last_char) != AlphabetFromChar(c_temp)))
  751. spell_word = 1;
  752. }
  753. if (option_sayas == SAYAS_KEY) {
  754. if (word_length == 1)
  755. spell_word = 4;
  756. else {
  757. // is there a translation for this keyname ?
  758. word1--;
  759. *word1 = '_'; // prefix keyname with '_'
  760. found = LookupDictList(tr, &word1, phonemes, dictionary_flags, 0, wtab);
  761. }
  762. }
  763. // try an initial lookup in the dictionary list, we may find a pronunciation specified, or
  764. // we may just find some flags
  765. if (option_sayas & 0x10) {
  766. // SAYAS_CHAR, SAYAS_GYLPH, or SAYAS_SINGLE_CHAR
  767. spell_word = option_sayas & 0xf; // 2,3,4
  768. } else {
  769. if (!found)
  770. found = LookupDictList(tr, &word1, phonemes, dictionary_flags, FLAG_ALLOW_TEXTMODE, wtab); // the original word
  771. if ((dictionary_flags[0] & (FLAG_ALLOW_DOT | FLAG_NEEDS_DOT)) && (wordx[1] == '.')) {
  772. wordx[1] = ' '; // remove a Dot after this word
  773. }
  774. if (dictionary_flags[0] & FLAG_TEXTMODE) {
  775. if (word_out != NULL)
  776. strcpy(word_out, word1);
  777. first_char = word1[0];
  778. stress_bits = dictionary_flags[0] & 0x7f;
  779. found = LookupDictList(tr, &word1, phonemes, dictionary_flags2, 0, wtab); // the text replacement
  780. if (dictionary_flags2[0] != 0) {
  781. dictionary_flags[0] = dictionary_flags2[0];
  782. dictionary_flags[1] = dictionary_flags2[1];
  783. if (stress_bits != 0) {
  784. // keep any stress information from the original word
  785. dictionary_flags[0] = (dictionary_flags[0] & ~0x7f) | stress_bits;
  786. }
  787. }
  788. } else if ((found == 0) && (dictionary_flags[0] & FLAG_SKIPWORDS) && !(dictionary_flags[0] & FLAG_ABBREV)) {
  789. // grouped words, but no translation. Join the words with hyphens.
  790. wordx = word1;
  791. ix = 0;
  792. while (ix < dictionary_skipwords) {
  793. if (*wordx == ' ') {
  794. *wordx = '-';
  795. ix++;
  796. }
  797. wordx++;
  798. }
  799. }
  800. if ((word_length == 1) && (dictionary_skipwords == 0)) {
  801. // is this a series of single letters separated by dots?
  802. if (CheckDottedAbbrev(word1, wtab)) {
  803. dictionary_flags[0] = 0;
  804. dictionary_flags[1] = 0;
  805. spell_word = 1;
  806. if (dictionary_skipwords)
  807. dictionary_flags[0] = FLAG_SKIPWORDS;
  808. }
  809. }
  810. // if textmode, LookupDictList() replaces word1 by the new text and returns found=0
  811. if (phonemes[0] == phonSWITCH) {
  812. // change to another language in order to translate this word
  813. strcpy(word_phonemes, phonemes);
  814. return 0;
  815. }
  816. if ((wmark > 0) && (wmark < 8)) {
  817. // the stressed syllable has been specified in the text (TESTING)
  818. dictionary_flags[0] = (dictionary_flags[0] & ~0xf) | wmark;
  819. }
  820. if (!found && (dictionary_flags[0] & FLAG_ABBREV)) {
  821. // the word has $abbrev flag, but no pronunciation specified. Speak as individual letters
  822. spell_word = 1;
  823. }
  824. if (!found && iswdigit(first_char)) {
  825. Lookup(tr, "_0lang", word_phonemes);
  826. if (word_phonemes[0] == phonSWITCH)
  827. return 0;
  828. if ((tr->langopts.numbers2 & NUM2_ENGLISH_NUMERALS) && !(wtab->flags & FLAG_CHAR_REPLACED)) {
  829. // for this language, speak English numerals (0-9) with the English voice
  830. sprintf(word_phonemes, "%c", phonSWITCH);
  831. return 0;
  832. }
  833. found = TranslateNumber(tr, word1, phonemes, dictionary_flags, wtab, 0);
  834. }
  835. if (!found && ((wflags & FLAG_UPPERS) != FLAG_FIRST_UPPER)) {
  836. // either all upper or all lower case
  837. if ((tr->langopts.numbers & NUM_ROMAN) || ((tr->langopts.numbers & NUM_ROMAN_CAPITALS) && (wflags & FLAG_ALL_UPPER))) {
  838. if ((wflags & FLAG_LAST_WORD) || !(wtab[1].flags & FLAG_NOSPACE)) {
  839. // don't use Roman number if this word is not separated from the next word (eg. "XLTest")
  840. if ((found = TranslateRoman(tr, word1, phonemes, wtab)) != 0)
  841. dictionary_flags[0] |= FLAG_ABBREV; // prevent emphasis if capitals
  842. }
  843. }
  844. }
  845. if ((wflags & FLAG_ALL_UPPER) && (word_length > 1) && iswalpha2(first_char)) {
  846. if ((option_tone_flags & OPTION_EMPHASIZE_ALLCAPS) && !(dictionary_flags[0] & FLAG_ABBREV)) {
  847. // emphasize words which are in capitals
  848. emphasize_allcaps = FLAG_EMPHASIZED;
  849. } else if (!found && !(dictionary_flags[0] & FLAG_SKIPWORDS) && (word_length < 4) && (tr->clause_lower_count > 3)
  850. && (tr->clause_upper_count <= tr->clause_lower_count)) {
  851. // An upper case word in a lower case clause. This could be an abbreviation.
  852. spell_word = 1;
  853. }
  854. }
  855. }
  856. if (spell_word > 0) {
  857. // Speak as individual letters
  858. phonemes[0] = 0;
  859. end_type = 0;
  860. if (SpeakIndividualLetters(tr, word1, phonemes, spell_word) == NULL) {
  861. if (word_length > 1)
  862. return FLAG_SPELLWORD; // a mixture of languages, retranslate as individual letters, separated by spaces
  863. return 0;
  864. }
  865. strcpy(word_phonemes, phonemes);
  866. if (wflags & FLAG_TRANSLATOR2)
  867. return 0;
  868. return dictionary_flags[0] & FLAG_SKIPWORDS; // for "b.c.d"
  869. } else if (found == 0) {
  870. int posn;
  871. int non_initial;
  872. int length;
  873. // word's pronunciation is not given in the dictionary list, although
  874. // dictionary_flags may have ben set there
  875. posn = 0;
  876. non_initial = 0;
  877. length = 999;
  878. wordx = word1;
  879. while (((length < 3) && (length > 0)) || (word_length > 1 && Unpronouncable(tr, wordx, posn))) {
  880. // This word looks "unpronouncable", so speak letters individually until we
  881. // find a remainder that we can pronounce.
  882. was_unpronouncable = FLAG_WAS_UNPRONOUNCABLE;
  883. emphasize_allcaps = 0;
  884. if (wordx[0] == '\'')
  885. break;
  886. if (posn > 0)
  887. non_initial = 1;
  888. wordx += TranslateLetter(tr, wordx, unpron_phonemes, non_initial);
  889. posn++;
  890. if (unpron_phonemes[0] == phonSWITCH) {
  891. // change to another language in order to translate this word
  892. strcpy(word_phonemes, unpron_phonemes);
  893. if (strcmp(&unpron_phonemes[1], "en") == 0)
  894. return FLAG_SPELLWORD; // _^_en must have been set in TranslateLetter(), not *_rules which uses only _^_
  895. return 0;
  896. }
  897. length = 0;
  898. while (wordx[length] != ' ') length++;
  899. }
  900. SetSpellingStress(tr, unpron_phonemes, 0, posn);
  901. // anything left ?
  902. if (*wordx != ' ') {
  903. if ((unpron_phonemes[0] != 0) && (wordx[0] != '\'')) {
  904. // letters which have been spoken individually from affecting the pronunciation of the pronuncable part
  905. wordx[-1] = ' ';
  906. }
  907. // Translate the stem
  908. end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags, dictionary_flags);
  909. if (phonemes[0] == phonSWITCH) {
  910. // change to another language in order to translate this word
  911. strcpy(word_phonemes, phonemes);
  912. return 0;
  913. }
  914. if ((phonemes[0] == 0) && (end_phonemes[0] == 0)) {
  915. int wc;
  916. // characters not recognised, speak them individually
  917. // ?? should we say super/sub-script numbers and letters here?
  918. utf8_in(&wc, wordx);
  919. if ((word_length == 1) && (IsAlpha(wc) || IsSuperscript(wc))) {
  920. if ((wordx = SpeakIndividualLetters(tr, wordx, phonemes, spell_word)) == NULL) {
  921. return 0;
  922. }
  923. strcpy(word_phonemes, phonemes);
  924. return 0;
  925. }
  926. }
  927. c_temp = wordx[-1];
  928. found = 0;
  929. confirm_prefix = 1;
  930. for (loopcount = 0; (loopcount < 50) && (end_type & SUFX_P); loopcount++) {
  931. // Found a standard prefix, remove it and retranslate
  932. // loopcount guards against an endless loop
  933. if (confirm_prefix && !(end_type & SUFX_B)) {
  934. int end2;
  935. char end_phonemes2[N_WORD_PHONEMES];
  936. // remove any standard suffix and confirm that the prefix is still recognised
  937. phonemes2[0] = 0;
  938. end2 = TranslateRules(tr, wordx, phonemes2, N_WORD_PHONEMES, end_phonemes2, wflags|FLAG_NO_PREFIX|FLAG_NO_TRACE, dictionary_flags);
  939. if (end2) {
  940. RemoveEnding(tr, wordx, end2, word_copy);
  941. end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags|FLAG_NO_TRACE, dictionary_flags);
  942. memcpy(wordx, word_copy, strlen(word_copy));
  943. if ((end_type & SUFX_P) == 0) {
  944. // after removing the suffix, the prefix is no longer recognised.
  945. // Keep the suffix, but don't use the prefix
  946. end_type = end2;
  947. strcpy(phonemes, phonemes2);
  948. strcpy(end_phonemes, end_phonemes2);
  949. if (option_phonemes & espeakPHONEMES_TRACE) {
  950. DecodePhonemes(end_phonemes, end_phonemes2);
  951. fprintf(f_trans, " suffix [%s]\n\n", end_phonemes2);
  952. }
  953. }
  954. confirm_prefix = 0;
  955. continue;
  956. }
  957. }
  958. prefix_type = end_type;
  959. if (prefix_type & SUFX_V) {
  960. tr->expect_verb = 1; // use the verb form of the word
  961. }
  962. wordx[-1] = c_temp;
  963. if ((prefix_type & SUFX_B) == 0) {
  964. for (ix = (prefix_type & 0xf); ix > 0; ix--) { // num. of characters to remove
  965. wordx++;
  966. while ((*wordx & 0xc0) == 0x80) wordx++; // for multibyte characters
  967. }
  968. } else {
  969. pfix = 1;
  970. prefix_chars[0] = 0;
  971. n_chars = prefix_type & 0x3f;
  972. for (ix = 0; ix < n_chars; ix++) { // num. of bytes to remove
  973. prefix_chars[pfix++] = *wordx++;
  974. if ((prefix_type & SUFX_B) && (ix == (n_chars-1))) {
  975. prefix_chars[pfix-1] = 0; // discard the last character of the prefix, this is the separator character
  976. }
  977. }
  978. prefix_chars[pfix] = 0;
  979. }
  980. c_temp = wordx[-1];
  981. wordx[-1] = ' ';
  982. confirm_prefix = 1;
  983. wflags |= FLAG_PREFIX_REMOVED;
  984. if (prefix_type & SUFX_B) {
  985. // SUFX_B is used for Turkish, tr_rules contains " ' (Pb"
  986. // examine the prefix part
  987. char *wordpf;
  988. char prefix_phonemes2[12];
  989. strncpy0(prefix_phonemes2, end_phonemes, sizeof(prefix_phonemes2));
  990. wordpf = &prefix_chars[1];
  991. strcpy(prefix_phonemes, phonemes);
  992. // look for stress marker or $abbrev
  993. found = LookupDictList(tr, &wordpf, phonemes, dictionary_flags, 0, wtab);
  994. if (found) {
  995. strcpy(prefix_phonemes, phonemes);
  996. }
  997. if (dictionary_flags[0] & FLAG_ABBREV) {
  998. prefix_phonemes[0] = 0;
  999. SpeakIndividualLetters(tr, wordpf, prefix_phonemes, 1);
  1000. }
  1001. } else {
  1002. strcat(prefix_phonemes, end_phonemes);
  1003. }
  1004. end_phonemes[0] = 0;
  1005. end_type = 0;
  1006. found = LookupDictList(tr, &wordx, phonemes, dictionary_flags2, SUFX_P, wtab); // without prefix
  1007. if (dictionary_flags[0] == 0) {
  1008. dictionary_flags[0] = dictionary_flags2[0];
  1009. dictionary_flags[1] = dictionary_flags2[1];
  1010. } else
  1011. prefix_flags = 1;
  1012. if (found == 0) {
  1013. end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags & (FLAG_HYPHEN_AFTER | FLAG_PREFIX_REMOVED), dictionary_flags);
  1014. if (phonemes[0] == phonSWITCH) {
  1015. // change to another language in order to translate this word
  1016. wordx[-1] = c_temp;
  1017. strcpy(word_phonemes, phonemes);
  1018. return 0;
  1019. }
  1020. }
  1021. }
  1022. if ((end_type != 0) && !(end_type & SUFX_P)) {
  1023. end_type1 = end_type;
  1024. strcpy(phonemes2, phonemes);
  1025. // The word has a standard ending, re-translate without this ending
  1026. end_flags = RemoveEnding(tr, wordx, end_type, word_copy);
  1027. more_suffixes = 1;
  1028. while (more_suffixes) {
  1029. more_suffixes = 0;
  1030. phonemes[0] = 0;
  1031. if (prefix_phonemes[0] != 0) {
  1032. // lookup the stem without the prefix removed
  1033. wordx[-1] = c_temp;
  1034. found = LookupDictList(tr, &word1, phonemes, dictionary_flags2, end_flags, wtab); // include prefix, but not suffix
  1035. wordx[-1] = ' ';
  1036. if (phonemes[0] == phonSWITCH) {
  1037. // change to another language in order to translate this word
  1038. memcpy(wordx, word_copy, strlen(word_copy));
  1039. strcpy(word_phonemes, phonemes);
  1040. return 0;
  1041. }
  1042. if (dictionary_flags[0] == 0) {
  1043. dictionary_flags[0] = dictionary_flags2[0];
  1044. dictionary_flags[1] = dictionary_flags2[1];
  1045. }
  1046. if (found)
  1047. prefix_phonemes[0] = 0; // matched whole word, don't need prefix now
  1048. if ((found == 0) && (dictionary_flags2[0] != 0))
  1049. prefix_flags = 1;
  1050. }
  1051. if (found == 0) {
  1052. found = LookupDictList(tr, &wordx, phonemes, dictionary_flags2, end_flags, wtab); // without prefix and suffix
  1053. if (phonemes[0] == phonSWITCH) {
  1054. // change to another language in order to translate this word
  1055. memcpy(wordx, word_copy, strlen(word_copy));
  1056. strcpy(word_phonemes, phonemes);
  1057. return 0;
  1058. }
  1059. if (dictionary_flags2[0] & FLAG_ABBREV) {
  1060. // Removing the suffix leaves a word which should be spoken as individual letters
  1061. // Not yet implemented
  1062. }
  1063. if (dictionary_flags[0] == 0) {
  1064. dictionary_flags[0] = dictionary_flags2[0];
  1065. dictionary_flags[1] = dictionary_flags2[1];
  1066. }
  1067. }
  1068. if (found == 0) {
  1069. if (end_type & SUFX_Q) {
  1070. // don't retranslate, use the original lookup result
  1071. strcpy(phonemes, phonemes2);
  1072. } else {
  1073. if (end_flags & FLAG_SUFX)
  1074. wflags |= FLAG_SUFFIX_REMOVED;
  1075. if (end_type & SUFX_A)
  1076. wflags |= FLAG_SUFFIX_VOWEL;
  1077. if (end_type & SUFX_M) {
  1078. // allow more suffixes before this suffix
  1079. strcpy(end_phonemes2, end_phonemes);
  1080. end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags, dictionary_flags);
  1081. strcat(end_phonemes, end_phonemes2); // add the phonemes for the previous suffixes after this one
  1082. if ((end_type != 0) && !(end_type & SUFX_P)) {
  1083. // there is another suffix
  1084. end_flags = RemoveEnding(tr, wordx, end_type, NULL);
  1085. more_suffixes = 1;
  1086. }
  1087. } else {
  1088. // don't remove any previous suffix
  1089. TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, NULL, wflags, dictionary_flags);
  1090. end_type = 0;
  1091. }
  1092. if (phonemes[0] == phonSWITCH) {
  1093. // change to another language in order to translate this word
  1094. strcpy(word_phonemes, phonemes);
  1095. memcpy(wordx, word_copy, strlen(word_copy));
  1096. wordx[-1] = c_temp;
  1097. return 0;
  1098. }
  1099. }
  1100. }
  1101. }
  1102. if ((end_type1 & SUFX_T) == 0) {
  1103. // the default is to add the suffix and then determine the word's stress pattern
  1104. AppendPhonemes(tr, phonemes, N_WORD_PHONEMES, end_phonemes);
  1105. end_phonemes[0] = 0;
  1106. }
  1107. memcpy(wordx, word_copy, strlen(word_copy));
  1108. }
  1109. wordx[-1] = c_temp;
  1110. }
  1111. }
  1112. if ((add_plural_suffix) || (wflags & FLAG_HAS_PLURAL)) {
  1113. // s or 's suffix, append [s], [z] or [Iz] depending on previous letter
  1114. if (last_char == 'f')
  1115. TranslateRules(tr, &word_ss[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
  1116. else if ((last_char == 0) || (strchr_w("hsx", last_char) == NULL))
  1117. TranslateRules(tr, &word_zz[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
  1118. else
  1119. TranslateRules(tr, &word_iz[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
  1120. }
  1121. wflags |= emphasize_allcaps;
  1122. /* determine stress pattern for this word */
  1123. /******************************************/
  1124. add_suffix_phonemes = 0;
  1125. if (end_phonemes[0] != 0) {
  1126. add_suffix_phonemes = 2;
  1127. }
  1128. prefix_stress = 0;
  1129. for (p = prefix_phonemes; *p != 0; p++) {
  1130. if ((*p == phonSTRESS_P) || (*p == phonSTRESS_P2)) {
  1131. prefix_stress = *p;
  1132. }
  1133. }
  1134. if (prefix_flags || (prefix_stress != 0)) {
  1135. if ((tr->langopts.param[LOPT_PREFIXES]) || (prefix_type & SUFX_T)) {
  1136. char *p;
  1137. // German, keep a secondary stress on the stem
  1138. SetWordStress(tr, phonemes, dictionary_flags, 3, 0);
  1139. // reduce all but the first primary stress
  1140. ix = 0;
  1141. for (p = prefix_phonemes; *p != 0; p++) {
  1142. if (*p == phonSTRESS_P) {
  1143. if (ix == 0)
  1144. ix = 1;
  1145. else
  1146. *p = phonSTRESS_3;
  1147. }
  1148. }
  1149. #ifdef PLATFORM_WINDOWS
  1150. sprintf(word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1151. #else
  1152. snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1153. #endif
  1154. word_phonemes[N_WORD_PHONEMES-1] = 0;
  1155. SetWordStress(tr, word_phonemes, dictionary_flags, -1, 0);
  1156. } else {
  1157. // stress position affects the whole word, including prefix
  1158. #ifdef PLATFORM_WINDOWS
  1159. sprintf(word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1160. #else
  1161. snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1162. #endif
  1163. word_phonemes[N_WORD_PHONEMES-1] = 0;
  1164. SetWordStress(tr, word_phonemes, dictionary_flags, -1, 0);
  1165. }
  1166. } else {
  1167. if (prefix_phonemes[0] == 0)
  1168. SetWordStress(tr, phonemes, dictionary_flags, -1, add_suffix_phonemes);
  1169. else
  1170. SetWordStress(tr, phonemes, dictionary_flags, -1, add_suffix_phonemes);
  1171. #ifdef PLATFORM_WINDOWS
  1172. sprintf(word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1173. #else
  1174. snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1175. #endif
  1176. word_phonemes[N_WORD_PHONEMES-1] = 0;
  1177. }
  1178. if (end_phonemes[0] != 0) {
  1179. // a suffix had the SUFX_T option set, add the suffix after the stress pattern has been determined
  1180. ix = strlen(word_phonemes);
  1181. end_phonemes[N_WORD_PHONEMES-1-ix] = 0; // ensure no buffer overflow
  1182. strcpy(&word_phonemes[ix], end_phonemes);
  1183. }
  1184. if (wflags & FLAG_LAST_WORD) {
  1185. // don't use $brk pause before the last word of a sentence
  1186. // (but allow it for emphasis, see below
  1187. dictionary_flags[0] &= ~FLAG_PAUSE1;
  1188. }
  1189. if ((wflags & FLAG_HYPHEN) && (tr->langopts.stress_flags & S_HYPEN_UNSTRESS)) {
  1190. ChangeWordStress(tr, word_phonemes, 3);
  1191. } else if (wflags & FLAG_EMPHASIZED2) {
  1192. // A word is indicated in the source text as stressed
  1193. // Give it stress level 6 (for the intonation module)
  1194. ChangeWordStress(tr, word_phonemes, 6);
  1195. if (wflags & FLAG_EMPHASIZED)
  1196. dictionary_flags[0] |= FLAG_PAUSE1; // precede by short pause
  1197. } else if (wtab[dictionary_skipwords].flags & FLAG_LAST_WORD) {
  1198. // the word has attribute to stress or unstress when at end of clause
  1199. if (dictionary_flags[0] & (FLAG_STRESS_END | FLAG_STRESS_END2))
  1200. ChangeWordStress(tr, word_phonemes, 4);
  1201. else if ((dictionary_flags[0] & FLAG_UNSTRESS_END) && (any_stressed_words))
  1202. ChangeWordStress(tr, word_phonemes, 3);
  1203. }
  1204. // dictionary flags for this word give a clue about which alternative pronunciations of
  1205. // following words to use.
  1206. if (end_type1 & SUFX_F) {
  1207. // expect a verb form, with or without -s suffix
  1208. tr->expect_verb = 2;
  1209. tr->expect_verb_s = 2;
  1210. }
  1211. if (dictionary_flags[1] & FLAG_PASTF) {
  1212. /* expect perfect tense in next two words */
  1213. tr->expect_past = 3;
  1214. tr->expect_verb = 0;
  1215. tr->expect_noun = 0;
  1216. } else if (dictionary_flags[1] & FLAG_VERBF) {
  1217. /* expect a verb in the next word */
  1218. tr->expect_verb = 2;
  1219. tr->expect_verb_s = 0; /* verb won't have -s suffix */
  1220. tr->expect_noun = 0;
  1221. } else if (dictionary_flags[1] & FLAG_VERBSF) {
  1222. // expect a verb, must have a -s suffix
  1223. tr->expect_verb = 0;
  1224. tr->expect_verb_s = 2;
  1225. tr->expect_past = 0;
  1226. tr->expect_noun = 0;
  1227. } else if (dictionary_flags[1] & FLAG_NOUNF) {
  1228. /* not expecting a verb next */
  1229. tr->expect_noun = 2;
  1230. tr->expect_verb = 0;
  1231. tr->expect_verb_s = 0;
  1232. tr->expect_past = 0;
  1233. }
  1234. if ((wordx[0] != 0) && (!(dictionary_flags[1] & FLAG_VERB_EXT))) {
  1235. if (tr->expect_verb > 0)
  1236. tr->expect_verb--;
  1237. if (tr->expect_verb_s > 0)
  1238. tr->expect_verb_s--;
  1239. if (tr->expect_noun > 0)
  1240. tr->expect_noun--;
  1241. if (tr->expect_past > 0)
  1242. tr->expect_past--;
  1243. }
  1244. if ((word_length == 1) && (tr->translator_name == L('e', 'n')) && iswalpha2(first_char) && (first_char != 'i')) {
  1245. // English Specific !!!!
  1246. // any single letter before a dot is an abbreviation, except 'I'
  1247. dictionary_flags[0] |= FLAG_ALLOW_DOT;
  1248. }
  1249. if ((tr->langopts.param[LOPT_ALT] & 2) && ((dictionary_flags[0] & (FLAG_ALT_TRANS | FLAG_ALT2_TRANS)) != 0)) {
  1250. ApplySpecialAttribute2(tr, word_phonemes, dictionary_flags[0]);
  1251. }
  1252. dictionary_flags[0] |= was_unpronouncable;
  1253. memcpy(word_start, word_copy2, word_copy_length);
  1254. return dictionary_flags[0];
  1255. }
  1256. static void SetPlist2(PHONEME_LIST2 *p, unsigned char phcode)
  1257. {
  1258. p->phcode = phcode;
  1259. p->stresslevel = 0;
  1260. p->tone_ph = 0;
  1261. p->synthflags = embedded_flag;
  1262. p->sourceix = 0;
  1263. embedded_flag = 0;
  1264. }
  1265. static int CountSyllables(unsigned char *phonemes)
  1266. {
  1267. int count = 0;
  1268. int phon;
  1269. while ((phon = *phonemes++) != 0) {
  1270. if (phoneme_tab[phon]->type == phVOWEL)
  1271. count++;
  1272. }
  1273. return count;
  1274. }
  1275. void Word_EmbeddedCmd()
  1276. {
  1277. // Process embedded commands for emphasis, sayas, and break
  1278. int embedded_cmd;
  1279. int value;
  1280. do {
  1281. embedded_cmd = embedded_list[embedded_read++];
  1282. value = embedded_cmd >> 8;
  1283. switch (embedded_cmd & 0x1f)
  1284. {
  1285. case EMBED_Y:
  1286. option_sayas = value;
  1287. break;
  1288. case EMBED_F:
  1289. option_emphasis = value;
  1290. break;
  1291. case EMBED_B:
  1292. // break command
  1293. if (value == 0)
  1294. pre_pause = 0; // break=none
  1295. else
  1296. pre_pause += value;
  1297. break;
  1298. }
  1299. } while (((embedded_cmd & 0x80) == 0) && (embedded_read < embedded_ix));
  1300. }
  1301. int SetTranslator2(const char *new_language)
  1302. {
  1303. // Set translator2 to a second language
  1304. int new_phoneme_tab;
  1305. const char *new_phtab_name;
  1306. int bitmap;
  1307. int dialect = 0;
  1308. new_phtab_name = new_language;
  1309. if ((bitmap = translator->langopts.dict_dialect) != 0) {
  1310. if ((bitmap & (1 << DICTDIALECT_EN_US)) && (strcmp(new_language, "en") == 0)) {
  1311. new_phtab_name = "en-us";
  1312. dialect = DICTDIALECT_EN_US;
  1313. }
  1314. if ((bitmap & (1 << DICTDIALECT_ES_LA)) && (strcmp(new_language, "es") == 0)) {
  1315. new_phtab_name = "es-la";
  1316. dialect = DICTDIALECT_ES_LA;
  1317. }
  1318. }
  1319. if ((new_phoneme_tab = SelectPhonemeTableName(new_phtab_name)) >= 0) {
  1320. if ((translator2 != NULL) && (strcmp(new_language, translator2_language) != 0)) {
  1321. // we already have an alternative translator, but not for the required language, delete it
  1322. DeleteTranslator(translator2);
  1323. translator2 = NULL;
  1324. }
  1325. if (translator2 == NULL) {
  1326. translator2 = SelectTranslator(new_language);
  1327. strcpy(translator2_language, new_language);
  1328. if (LoadDictionary(translator2, translator2->dictionary_name, 0) != 0) {
  1329. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  1330. new_phoneme_tab = -1;
  1331. translator2_language[0] = 0;
  1332. } else {
  1333. if (dialect == DICTDIALECT_EN_US) {
  1334. // en-us
  1335. translator2->dict_condition = 0x48; // bits 3, 6
  1336. translator2->langopts.param[LOPT_REDUCE_T] = 1;
  1337. }
  1338. if (dialect == DICTDIALECT_ES_LA) {
  1339. translator2->dict_condition = 0x04; // bit 2
  1340. }
  1341. }
  1342. translator2->phoneme_tab_ix = new_phoneme_tab;
  1343. }
  1344. }
  1345. if (translator2 != NULL)
  1346. translator2->phonemes_repeat[0] = 0;
  1347. return new_phoneme_tab;
  1348. }
  1349. static int TranslateWord2(Translator *tr, char *word, WORD_TAB *wtab, int pre_pause, int next_pause)
  1350. {
  1351. int flags = 0;
  1352. int stress;
  1353. int next_stress;
  1354. int next_tone = 0;
  1355. unsigned char *p;
  1356. int srcix;
  1357. int found_dict_flag;
  1358. unsigned char ph_code;
  1359. PHONEME_LIST2 *plist2;
  1360. PHONEME_TAB *ph;
  1361. int max_stress;
  1362. int max_stress_ix = 0;
  1363. int prev_vowel = -1;
  1364. int pitch_raised = 0;
  1365. int switch_phonemes = -1;
  1366. int first_phoneme = 1;
  1367. int source_ix;
  1368. int len;
  1369. int ix;
  1370. int sylimit; // max. number of syllables in a word to be combined with a preceding preposition
  1371. const char *new_language;
  1372. int bad_phoneme;
  1373. int word_flags;
  1374. int word_copy_len;
  1375. char word_copy[N_WORD_BYTES+1];
  1376. char word_replaced[N_WORD_BYTES+1];
  1377. char old_dictionary_name[40];
  1378. if ((f_logespeak != NULL) && (logging_type & 8)) {
  1379. fprintf(f_logespeak, "WORD: flg=%.5x len=%d '", wtab->flags, wtab->length);
  1380. for (ix = 0; ix < 40; ix++) {
  1381. if (word[ix] == 0) break;
  1382. fputc(word[ix], f_logespeak);
  1383. }
  1384. fprintf(f_logespeak, "'\n");
  1385. }
  1386. len = wtab->length;
  1387. if (len > 31) len = 31;
  1388. source_ix = (wtab->sourceix & 0x7ff) | (len << 11); // bits 0-10 sourceix, bits 11-15 word length
  1389. word_flags = wtab[0].flags;
  1390. if (word_flags & FLAG_EMBEDDED) {
  1391. wtab[0].flags &= ~FLAG_EMBEDDED; // clear it in case we call TranslateWord2() again for the same word
  1392. embedded_flag = SFLAG_EMBEDDED;
  1393. Word_EmbeddedCmd();
  1394. }
  1395. if ((word[0] == 0) || (word_flags & FLAG_DELETE_WORD)) {
  1396. // nothing to translate. Add a dummy phoneme to carry any embedded commands
  1397. if (embedded_flag) {
  1398. ph_list2[n_ph_list2].phcode = phonEND_WORD;
  1399. ph_list2[n_ph_list2].stresslevel = 0;
  1400. ph_list2[n_ph_list2].wordstress = 0;
  1401. ph_list2[n_ph_list2].tone_ph = 0;
  1402. ph_list2[n_ph_list2].synthflags = embedded_flag;
  1403. ph_list2[n_ph_list2].sourceix = 0;
  1404. n_ph_list2++;
  1405. embedded_flag = 0;
  1406. }
  1407. word_phonemes[0] = 0;
  1408. return 0;
  1409. }
  1410. // after a $pause word attribute, ignore a $pause attribute on the next two words
  1411. if (tr->prepause_timeout > 0)
  1412. tr->prepause_timeout--;
  1413. if ((option_sayas & 0xf0) == 0x10) {
  1414. if (!(word_flags & FLAG_FIRST_WORD)) {
  1415. // SAYAS_CHARS, SAYAS_GLYPHS, or SAYAS_SINGLECHARS. Pause between each word.
  1416. pre_pause += 4;
  1417. }
  1418. }
  1419. if (word_flags & FLAG_FIRST_UPPER) {
  1420. if ((option_capitals > 2) && (embedded_ix < N_EMBEDDED_LIST-6)) {
  1421. // indicate capital letter by raising pitch
  1422. if (embedded_flag)
  1423. embedded_list[embedded_ix-1] &= ~0x80; // already embedded command before this word, remove terminator
  1424. if ((pitch_raised = option_capitals) == 3)
  1425. pitch_raised = 20; // default pitch raise for capitals
  1426. embedded_list[embedded_ix++] = EMBED_P+0x40+0x80 + (pitch_raised << 8); // raise pitch
  1427. embedded_flag = SFLAG_EMBEDDED;
  1428. }
  1429. }
  1430. p = (unsigned char *)word_phonemes;
  1431. if (word_flags & FLAG_PHONEMES) {
  1432. // The input is in phoneme mnemonics, not language text
  1433. int c1;
  1434. char lang_name[12];
  1435. if (memcmp(word, "_^_", 3) == 0) {
  1436. // switch languages
  1437. word += 3;
  1438. for (ix = 0;;) {
  1439. c1 = *word++;
  1440. if ((c1 == ' ') || (c1 == 0))
  1441. break;
  1442. lang_name[ix++] = tolower(c1);
  1443. }
  1444. lang_name[ix] = 0;
  1445. if ((ix = LookupPhonemeTable(lang_name)) > 0) {
  1446. SelectPhonemeTable(ix);
  1447. word_phonemes[0] = phonSWITCH;
  1448. word_phonemes[1] = ix;
  1449. word_phonemes[2] = 0;
  1450. }
  1451. } else {
  1452. EncodePhonemes(word, word_phonemes, &bad_phoneme);
  1453. }
  1454. flags = FLAG_FOUND;
  1455. } else {
  1456. int c2;
  1457. ix = 0;
  1458. while (((c2 = word_copy[ix] = word[ix]) != ' ') && (c2 != 0) && (ix < N_WORD_BYTES)) ix++;
  1459. word_copy_len = ix;
  1460. word_replaced[2] = 0;
  1461. flags = TranslateWord(translator, word, next_pause, wtab, &word_replaced[2]);
  1462. if (flags & FLAG_SPELLWORD) {
  1463. // re-translate the word as individual letters, separated by spaces
  1464. memcpy(word, word_copy, word_copy_len);
  1465. return flags;
  1466. }
  1467. if ((flags & FLAG_COMBINE) && !(wtab[1].flags & FLAG_PHONEMES)) {
  1468. char *p2;
  1469. int ok = 1;
  1470. unsigned int flags2[2];
  1471. int c_word2;
  1472. char ph_buf[N_WORD_PHONEMES];
  1473. flags2[0] = 0;
  1474. sylimit = tr->langopts.param[LOPT_COMBINE_WORDS];
  1475. // LANG=cs,sk
  1476. // combine a preposition with the following word
  1477. p2 = word;
  1478. while (*p2 != ' ') p2++;
  1479. utf8_in(&c_word2, p2+1); // first character of the next word;
  1480. if (!iswalpha2(c_word2)) {
  1481. ok = 0;
  1482. }
  1483. if (ok != 0) {
  1484. strcpy(ph_buf, word_phonemes);
  1485. flags2[0] = TranslateWord(translator, p2+1, 0, wtab+1, NULL);
  1486. if ((flags2[0] & FLAG_WAS_UNPRONOUNCABLE) || (word_phonemes[0] == phonSWITCH))
  1487. ok = 0;
  1488. if (sylimit & 0x100) {
  1489. // only if the second word has $alt attribute
  1490. if ((flags2[0] & FLAG_ALT_TRANS) == 0) {
  1491. ok = 0;
  1492. }
  1493. }
  1494. if ((sylimit & 0x200) && ((wtab+1)->flags & FLAG_LAST_WORD)) {
  1495. // not if the next word is end-of-sentence
  1496. ok = 0;
  1497. }
  1498. if (ok == 0) {
  1499. strcpy(word_phonemes, ph_buf);
  1500. }
  1501. }
  1502. if (ok) {
  1503. *p2 = '-'; // replace next space by hyphen
  1504. wtab[0].flags &= ~FLAG_ALL_UPPER; // prevent it being considered an abbreviation
  1505. flags = TranslateWord(translator, word, next_pause, wtab, NULL); // translate the combined word
  1506. if ((sylimit > 0) && (CountSyllables(p) > (sylimit & 0x1f))) {
  1507. // revert to separate words
  1508. *p2 = ' ';
  1509. flags = TranslateWord(translator, word, next_pause, wtab, NULL);
  1510. } else {
  1511. if (flags == 0)
  1512. flags = flags2[0]; // no flags for the combined word, so use flags from the second word eg. lang-hu "nem december 7-e"
  1513. flags |= FLAG_SKIPWORDS;
  1514. dictionary_skipwords = 1;
  1515. }
  1516. }
  1517. }
  1518. if (p[0] == phonSWITCH) {
  1519. int switch_attempt;
  1520. strcpy(old_dictionary_name, dictionary_name);
  1521. for (switch_attempt = 0; switch_attempt < 2; switch_attempt++) {
  1522. // this word uses a different language
  1523. memcpy(word, word_copy, word_copy_len);
  1524. new_language = (char *)(&p[1]);
  1525. if (new_language[0] == 0)
  1526. new_language = "en";
  1527. switch_phonemes = SetTranslator2(new_language);
  1528. if (switch_phonemes >= 0) {
  1529. // re-translate the word using the new translator
  1530. wtab[0].flags |= FLAG_TRANSLATOR2;
  1531. if (word_replaced[2] != 0) {
  1532. word_replaced[0] = 0; // byte before the start of the word
  1533. word_replaced[1] = ' ';
  1534. flags = TranslateWord(translator2, &word_replaced[1], next_pause, wtab, NULL);
  1535. } else
  1536. flags = TranslateWord(translator2, word, next_pause, wtab, &word_replaced[2]);
  1537. }
  1538. if (p[0] != phonSWITCH)
  1539. break;
  1540. }
  1541. // strcpy((char *)p,translator2->word_phonemes);
  1542. if (p[0] == phonSWITCH)
  1543. return FLAG_SPELLWORD;
  1544. if (switch_phonemes < 0) {
  1545. // language code is not recognised or 2nd translator won't translate it
  1546. p[0] = phonSCHWA; // just say something
  1547. p[1] = phonSCHWA;
  1548. p[2] = 0;
  1549. }
  1550. // ?? Option to convert from language2 phonemes to the equivalent language1 phonemes
  1551. // ?? Option to set the word-stress according to language1 rules eg. lang=fr)
  1552. if (ChangeEquivalentPhonemes(tr, switch_phonemes, (char *)p)) {
  1553. // Phonemes have been converted from the foreign language to the native language
  1554. switch_phonemes = -1;
  1555. }
  1556. if (switch_phonemes == -1) {
  1557. strcpy(dictionary_name, old_dictionary_name);
  1558. SelectPhonemeTable(voice->phoneme_tab_ix);
  1559. // leave switch_phonemes set, but use the original phoneme table number.
  1560. // This will suppress LOPT_REGRESSIVE_VOICING
  1561. switch_phonemes = voice->phoneme_tab_ix; // original phoneme table
  1562. }
  1563. }
  1564. if (!(word_flags & FLAG_HYPHEN)) {
  1565. if (flags & FLAG_PAUSE1) {
  1566. if (pre_pause < 1)
  1567. pre_pause = 1;
  1568. }
  1569. if ((flags & FLAG_PREPAUSE) && !(word_flags & (FLAG_LAST_WORD | FLAG_FIRST_WORD)) && !(wtab[-1].flags & FLAG_FIRST_WORD) && (tr->prepause_timeout == 0)) {
  1570. // the word is marked in the dictionary list with $pause
  1571. if (pre_pause < 4) pre_pause = 4;
  1572. tr->prepause_timeout = 3;
  1573. }
  1574. }
  1575. if ((option_emphasis >= 3) && (pre_pause < 1))
  1576. pre_pause = 1;
  1577. }
  1578. stress = 0;
  1579. next_stress = 1;
  1580. srcix = 0;
  1581. max_stress = -1;
  1582. found_dict_flag = 0;
  1583. if ((flags & FLAG_FOUND) && !(flags & FLAG_TEXTMODE))
  1584. found_dict_flag = SFLAG_DICTIONARY;
  1585. while ((pre_pause > 0) && (n_ph_list2 < N_PHONEME_LIST-4)) {
  1586. // add pause phonemes here. Either because of punctuation (brackets or quotes) in the
  1587. // text, or because the word is marked in the dictionary lookup as a conjunction
  1588. if (pre_pause > 1) {
  1589. SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE);
  1590. pre_pause -= 2;
  1591. } else {
  1592. SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_NOLINK);
  1593. pre_pause--;
  1594. }
  1595. tr->end_stressed_vowel = 0; // forget about the previous word
  1596. tr->prev_dict_flags[0] = 0;
  1597. tr->prev_dict_flags[1] = 0;
  1598. }
  1599. plist2 = &ph_list2[n_ph_list2];
  1600. if ((option_capitals == 1) && (word_flags & FLAG_FIRST_UPPER)) {
  1601. SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_SHORT);
  1602. SetPlist2(&ph_list2[n_ph_list2++], phonCAPITAL);
  1603. if ((word_flags & FLAG_ALL_UPPER) && IsAlpha(word[1])) {
  1604. // word > 1 letter and all capitals
  1605. SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_SHORT);
  1606. SetPlist2(&ph_list2[n_ph_list2++], phonCAPITAL);
  1607. }
  1608. }
  1609. if (switch_phonemes >= 0) {
  1610. if ((p[0] == phonPAUSE) && (p[1] == phonSWITCH)) {
  1611. // the new word starts with a phoneme table switch, so there's no need to switch before it.
  1612. if (ph_list2[n_ph_list2-1].phcode == phonSWITCH) {
  1613. // previous phoneme is also a phonSWITCH, delete it
  1614. n_ph_list2--;
  1615. }
  1616. } else {
  1617. // this word uses a different phoneme table
  1618. if (ph_list2[n_ph_list2-1].phcode == phonSWITCH) {
  1619. // previous phoneme is also a phonSWITCH, just change its phoneme table number
  1620. n_ph_list2--;
  1621. } else {
  1622. SetPlist2(&ph_list2[n_ph_list2], phonSWITCH);
  1623. }
  1624. ph_list2[n_ph_list2++].tone_ph = switch_phonemes; // temporary phoneme table number
  1625. }
  1626. }
  1627. // remove initial pause from a word if it follows a hyphen
  1628. if ((word_flags & FLAG_HYPHEN) && (phoneme_tab[*p]->type == phPAUSE))
  1629. p++;
  1630. if ((p[0] == 0) && (embedded_flag)) {
  1631. // no phonemes. Insert a very short pause to carry an embedded command
  1632. p[0] = phonPAUSE_VSHORT;
  1633. p[1] = 0;
  1634. }
  1635. while (((ph_code = *p++) != 0) && (n_ph_list2 < N_PHONEME_LIST-4)) {
  1636. if (ph_code == 255)
  1637. continue; // unknown phoneme
  1638. // Add the phonemes to the first stage phoneme list (ph_list2)
  1639. ph = phoneme_tab[ph_code];
  1640. if (ph_code == phonSWITCH) {
  1641. ph_list2[n_ph_list2].phcode = ph_code;
  1642. ph_list2[n_ph_list2].sourceix = 0;
  1643. ph_list2[n_ph_list2].synthflags = 0;
  1644. ph_list2[n_ph_list2++].tone_ph = *p;
  1645. SelectPhonemeTable(*p);
  1646. p++;
  1647. } else if (ph->type == phSTRESS) {
  1648. // don't add stress phonemes codes to the list, but give their stress
  1649. // value to the next vowel phoneme
  1650. // std_length is used to hold stress number or (if >10) a tone number for a tone language
  1651. if (ph->program == 0)
  1652. next_stress = ph->std_length;
  1653. else {
  1654. // for tone languages, the tone number for a syllable follows the vowel
  1655. if (prev_vowel >= 0) {
  1656. ph_list2[prev_vowel].tone_ph = ph_code;
  1657. } else {
  1658. next_tone = ph_code; // no previous vowel, apply to the next vowel
  1659. }
  1660. }
  1661. } else if (ph_code == phonSYLLABIC) {
  1662. // mark the previous phoneme as a syllabic consonant
  1663. prev_vowel = n_ph_list2-1;
  1664. ph_list2[prev_vowel].synthflags |= SFLAG_SYLLABLE;
  1665. ph_list2[prev_vowel].stresslevel = next_stress;
  1666. } else if (ph_code == phonLENGTHEN) {
  1667. ph_list2[n_ph_list2-1].synthflags |= SFLAG_LENGTHEN;
  1668. } else if (ph_code == phonEND_WORD) {
  1669. // a || symbol in a phoneme string was used to indicate a word boundary
  1670. // Don't add this phoneme to the list, but make sure the next phoneme has
  1671. // a newword indication
  1672. srcix = source_ix+1;
  1673. } else if (ph_code == phonX1) {
  1674. // a language specific action
  1675. if (tr->langopts.param[LOPT_IT_DOUBLING]) {
  1676. flags |= FLAG_DOUBLING;
  1677. }
  1678. } else {
  1679. ph_list2[n_ph_list2].phcode = ph_code;
  1680. ph_list2[n_ph_list2].tone_ph = 0;
  1681. ph_list2[n_ph_list2].synthflags = embedded_flag | found_dict_flag;
  1682. embedded_flag = 0;
  1683. ph_list2[n_ph_list2].sourceix = srcix;
  1684. srcix = 0;
  1685. if (ph->type == phVOWEL) {
  1686. stress = next_stress;
  1687. next_stress = 1; // default is 'unstressed'
  1688. if (stress >= 4) {
  1689. any_stressed_words = 1;
  1690. }
  1691. if ((prev_vowel >= 0) && (n_ph_list2-1) != prev_vowel)
  1692. ph_list2[n_ph_list2-1].stresslevel = stress; // set stress for previous consonant
  1693. ph_list2[n_ph_list2].synthflags |= SFLAG_SYLLABLE;
  1694. prev_vowel = n_ph_list2;
  1695. if (stress > max_stress) {
  1696. max_stress = stress;
  1697. max_stress_ix = n_ph_list2;
  1698. }
  1699. if (next_tone != 0) {
  1700. ph_list2[n_ph_list2].tone_ph = next_tone;
  1701. next_tone = 0;
  1702. }
  1703. } else {
  1704. if (first_phoneme && tr->langopts.param[LOPT_IT_DOUBLING]) {
  1705. if (((tr->prev_dict_flags[0] & FLAG_DOUBLING) && (tr->langopts.param[LOPT_IT_DOUBLING] & 1)) ||
  1706. (tr->end_stressed_vowel && (tr->langopts.param[LOPT_IT_DOUBLING] & 2))) {
  1707. // italian, double the initial consonant if the previous word ends with a
  1708. // stressed vowel, or is marked with a flag
  1709. ph_list2[n_ph_list2].synthflags |= SFLAG_LENGTHEN;
  1710. }
  1711. }
  1712. }
  1713. ph_list2[n_ph_list2].stresslevel = stress;
  1714. n_ph_list2++;
  1715. first_phoneme = 0;
  1716. }
  1717. }
  1718. if (word_flags & FLAG_COMMA_AFTER) {
  1719. SetPlist2(&ph_list2[n_ph_list2++], phonPAUSE_CLAUSE);
  1720. }
  1721. // don't set new-word if there is a hyphen before it
  1722. if ((word_flags & FLAG_HYPHEN) == 0) {
  1723. plist2->sourceix = source_ix;
  1724. }
  1725. tr->end_stressed_vowel = 0;
  1726. if ((stress >= 4) && (phoneme_tab[ph_list2[n_ph_list2-1].phcode]->type == phVOWEL)) {
  1727. tr->end_stressed_vowel = 1; // word ends with a stressed vowel
  1728. }
  1729. if (switch_phonemes >= 0) {
  1730. // this word uses a different phoneme table, now switch back
  1731. strcpy(dictionary_name, old_dictionary_name);
  1732. SelectPhonemeTable(voice->phoneme_tab_ix);
  1733. SetPlist2(&ph_list2[n_ph_list2], phonSWITCH);
  1734. ph_list2[n_ph_list2++].tone_ph = voice->phoneme_tab_ix; // original phoneme table number
  1735. }
  1736. if (pitch_raised > 0) {
  1737. embedded_list[embedded_ix++] = EMBED_P+0x60+0x80 + (pitch_raised << 8); // lower pitch
  1738. SetPlist2(&ph_list2[n_ph_list2], phonPAUSE_SHORT);
  1739. ph_list2[n_ph_list2++].synthflags = SFLAG_EMBEDDED;
  1740. }
  1741. if (flags & FLAG_STRESS_END2) {
  1742. // this's word's stress could be increased later
  1743. ph_list2[max_stress_ix].synthflags |= SFLAG_PROMOTE_STRESS;
  1744. }
  1745. tr->prev_dict_flags[0] = flags;
  1746. return flags;
  1747. }
  1748. static int EmbeddedCommand(unsigned int *source_index_out)
  1749. {
  1750. // An embedded command to change the pitch, volume, etc.
  1751. // returns number of commands added to embedded_list
  1752. // pitch,speed,amplitude,expression,reverb,tone,voice,sayas
  1753. const char *commands = "PSARHTIVYMUBF";
  1754. int value = -1;
  1755. int sign = 0;
  1756. unsigned char c;
  1757. char *p;
  1758. int cmd;
  1759. int source_index = *source_index_out;
  1760. c = source[source_index];
  1761. if (c == '+') {
  1762. sign = 0x40;
  1763. source_index++;
  1764. } else if (c == '-') {
  1765. sign = 0x60;
  1766. source_index++;
  1767. }
  1768. if (IsDigit09(source[source_index])) {
  1769. value = atoi(&source[source_index]);
  1770. while (IsDigit09(source[source_index]))
  1771. source_index++;
  1772. }
  1773. c = source[source_index++];
  1774. if (embedded_ix >= (N_EMBEDDED_LIST - 2))
  1775. return 0; // list is full
  1776. if ((p = strchr_w(commands, c)) == NULL)
  1777. return 0;
  1778. cmd = (p - commands)+1;
  1779. if (value == -1) {
  1780. value = embedded_default[cmd];
  1781. sign = 0;
  1782. }
  1783. if (cmd == EMBED_Y) {
  1784. option_sayas2 = value;
  1785. count_sayas_digits = 0;
  1786. }
  1787. if (cmd == EMBED_F) {
  1788. if (value >= 3)
  1789. word_emphasis = FLAG_EMPHASIZED;
  1790. else
  1791. word_emphasis = 0;
  1792. }
  1793. embedded_list[embedded_ix++] = cmd + sign + (value << 8);
  1794. *source_index_out = source_index;
  1795. return 1;
  1796. }
  1797. static int SubstituteChar(Translator *tr, unsigned int c, unsigned int next_in, int *insert, int *wordflags)
  1798. {
  1799. int ix;
  1800. unsigned int word;
  1801. unsigned int new_c, c2, c_lower;
  1802. int upper_case = 0;
  1803. static int ignore_next = 0;
  1804. const unsigned int *replace_chars;
  1805. if (ignore_next) {
  1806. ignore_next = 0;
  1807. return 8;
  1808. }
  1809. if (c == 0) return 0;
  1810. if ((replace_chars = tr->langopts.replace_chars) == NULL)
  1811. return c;
  1812. // there is a list of character codes to be substituted with alternative codes
  1813. if (iswupper2(c_lower = c)) {
  1814. c_lower = towlower2(c);
  1815. upper_case = 1;
  1816. }
  1817. new_c = 0;
  1818. for (ix = 0; (word = replace_chars[ix]) != 0; ix += 2) {
  1819. if (c_lower == (word & 0xffff)) {
  1820. if ((word >> 16) == 0) {
  1821. new_c = replace_chars[ix+1];
  1822. break;
  1823. }
  1824. if ((word >> 16) == (unsigned int)towlower2(next_in)) {
  1825. new_c = replace_chars[ix+1];
  1826. ignore_next = 1;
  1827. break;
  1828. }
  1829. }
  1830. }
  1831. if (new_c == 0)
  1832. return c; // no substitution
  1833. if (new_c & 0xffe00000) {
  1834. // there is a second character to be inserted
  1835. // don't convert the case of the second character unless the next letter is also upper case
  1836. c2 = new_c >> 16;
  1837. if (upper_case && iswupper2(next_in))
  1838. c2 = towupper2(c2);
  1839. *insert = c2;
  1840. new_c &= 0xffff;
  1841. }
  1842. if (upper_case)
  1843. new_c = towupper2(new_c);
  1844. *wordflags |= FLAG_CHAR_REPLACED;
  1845. return new_c;
  1846. }
  1847. static int TranslateChar(Translator *tr, char *ptr, int prev_in, unsigned int c, unsigned int next_in, int *insert, int *wordflags)
  1848. {
  1849. // To allow language specific examination and replacement of characters
  1850. int code;
  1851. int initial;
  1852. int medial;
  1853. int final;
  1854. int next2;
  1855. static const unsigned char hangul_compatibility[0x34] = {
  1856. 0, 0x00, 0x01, 0xaa, 0x02, 0xac, 0xad, 0x03,
  1857. 0x04, 0x05, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb4,
  1858. 0xb6, 0x06, 0x07, 0x08, 0xb9, 0x09, 0x0a, 0xbc,
  1859. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x61,
  1860. 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  1861. 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71,
  1862. 0x72, 0x73, 0x74, 0x75
  1863. };
  1864. // check for Korean Hangul letters
  1865. if (((code = c - 0xac00) >= 0) && (c <= 0xd7af)) {
  1866. // break a syllable hangul into 2 or 3 individual jamo
  1867. initial = (code/28)/21;
  1868. medial = (code/28) % 21;
  1869. final = code % 28;
  1870. if (initial == 11) {
  1871. // null initial
  1872. c = medial + 0x1161;
  1873. if (final > 0)
  1874. *insert = final + 0x11a7;
  1875. } else {
  1876. // extact the initial and insert the remainder with a null initial
  1877. c = initial + 0x1100;
  1878. *insert = (11*28*21) + (medial*28) + final + 0xac00;
  1879. }
  1880. return c;
  1881. } else if (((code = c - 0x3130) >= 0) && (code < 0x34)) {
  1882. // Hangul compatibility jamo
  1883. return hangul_compatibility[code] + 0x1100;
  1884. }
  1885. switch (tr->translator_name)
  1886. {
  1887. case L('a', 'f'):
  1888. case L('n', 'l'):
  1889. // look for 'n and replace by a special character (unicode: schwa)
  1890. if (!iswalpha2(prev_in)) {
  1891. utf8_in(&next2, &ptr[1]);
  1892. if ((c == '\'') && IsSpace(next2)) {
  1893. if ((next_in == 'n') && (tr->translator_name == L('a', 'f'))) {
  1894. // n preceded by either apostrophe or U2019 "right single quotation mark"
  1895. ptr[0] = ' '; // delete the n
  1896. return 0x0259; // replace ' by unicode schwa character
  1897. }
  1898. if ((next_in == 'n') || (next_in == 't')) {
  1899. // Dutch, [@n] and [@t]
  1900. return 0x0259; // replace ' by unicode schwa character
  1901. }
  1902. }
  1903. }
  1904. break;
  1905. }
  1906. return SubstituteChar(tr, c, next_in, insert, wordflags);
  1907. }
  1908. static const char *UCase_ga[] = { "bp", "bhf", "dt", "gc", "hA", "mb", "nd", "ng", "ts", "tA", "nA", NULL };
  1909. int UpperCaseInWord(Translator *tr, char *word, int c)
  1910. {
  1911. int ix;
  1912. int len;
  1913. const char *p;
  1914. if (tr->translator_name == L('g', 'a')) {
  1915. // Irish
  1916. for (ix = 0;; ix++) {
  1917. if ((p = UCase_ga[ix]) == NULL)
  1918. break;
  1919. len = strlen(p);
  1920. if ((word[-len] == ' ') && (memcmp(&word[-len+1], p, len-1) == 0)) {
  1921. if ((c == p[len-1]) || ((p[len-1] == 'A') && IsVowel(tr, c)))
  1922. return 1;
  1923. }
  1924. }
  1925. }
  1926. return 0;
  1927. }
  1928. void *TranslateClause(Translator *tr, FILE *f_text, const void *vp_input, int *tone_out, char **voice_change)
  1929. {
  1930. int ix;
  1931. int c;
  1932. int cc;
  1933. unsigned int source_index = 0;
  1934. unsigned int prev_source_index = 0;
  1935. int source_index_word = 0;
  1936. int prev_in;
  1937. int prev_out = ' ';
  1938. int prev_out2;
  1939. int prev_in_save = 0;
  1940. int next_in;
  1941. int next_in_nbytes;
  1942. int char_inserted = 0;
  1943. int clause_pause;
  1944. int pre_pause_add = 0;
  1945. int word_mark = 0;
  1946. int all_upper_case = FLAG_ALL_UPPER;
  1947. int finished;
  1948. int single_quoted;
  1949. int phoneme_mode = 0;
  1950. int dict_flags = 0; // returned from dictionary lookup
  1951. int word_flags; // set here
  1952. int next_word_flags;
  1953. int new_sentence2;
  1954. int embedded_count = 0;
  1955. int letter_count = 0;
  1956. int space_inserted = 0;
  1957. int syllable_marked = 0;
  1958. int decimal_sep_count = 0;
  1959. char *word;
  1960. char *p;
  1961. int j, k;
  1962. int n_digits;
  1963. int charix_top = 0;
  1964. short charix[N_TR_SOURCE+4];
  1965. WORD_TAB words[N_CLAUSE_WORDS];
  1966. static char voice_change_name[40];
  1967. int word_count = 0; // index into words
  1968. char sbuf[N_TR_SOURCE];
  1969. int terminator;
  1970. int tone;
  1971. int tone2;
  1972. if (tr == NULL) {
  1973. return NULL;
  1974. }
  1975. p_textinput = (unsigned char *)vp_input;
  1976. p_wchar_input = (wchar_t *)vp_input;
  1977. embedded_ix = 0;
  1978. embedded_read = 0;
  1979. pre_pause = 0;
  1980. any_stressed_words = 0;
  1981. if ((clause_start_char = count_characters) < 0)
  1982. clause_start_char = 0;
  1983. clause_start_word = count_words + 1;
  1984. for (ix = 0; ix < N_TR_SOURCE; ix++)
  1985. charix[ix] = 0;
  1986. terminator = ReadClause(tr, f_text, source, charix, &charix_top, N_TR_SOURCE, &tone2, voice_change_name);
  1987. if ((f_logespeak != NULL) && (logging_type & 4)) {
  1988. fprintf(f_logespeak, "CLAUSE %x:\n", terminator);
  1989. for (p = source; *p != 0; p++)
  1990. fputc(*p, f_logespeak);
  1991. fprintf(f_logespeak, "ENDCLAUSE\n");
  1992. fflush(f_logespeak);
  1993. }
  1994. p = source;
  1995. charix[charix_top+1] = 0;
  1996. charix[charix_top+2] = 0x7fff;
  1997. charix[charix_top+3] = 0;
  1998. clause_pause = (terminator & 0xfff) * 10; // mS
  1999. if (terminator & CLAUSE_PAUSE_LONG)
  2000. clause_pause = clause_pause * 32; // pause value is *320mS not *10mS
  2001. tone = (terminator >> 12) & 0x7;
  2002. if (tone2 != 0) {
  2003. // override the tone type
  2004. tone = tone2;
  2005. }
  2006. for (p = source; *p != 0; p++) {
  2007. if (!isspace2(*p)) {
  2008. break;
  2009. }
  2010. }
  2011. if (*p == 0) {
  2012. // No characters except spaces. This is not a sentence.
  2013. // Don't add this pause, just make up the previous pause to this value;
  2014. clause_pause -= max_clause_pause;
  2015. if (clause_pause < 0)
  2016. clause_pause = 0;
  2017. if (new_sentence)
  2018. terminator |= CLAUSE_BIT_SENTENCE; // carry forward an end-of-sentence indicator
  2019. max_clause_pause += clause_pause;
  2020. new_sentence2 = 0;
  2021. } else {
  2022. max_clause_pause = clause_pause;
  2023. new_sentence2 = new_sentence;
  2024. }
  2025. tr->clause_terminator = terminator;
  2026. if (new_sentence2) {
  2027. count_sentences++;
  2028. if (skip_sentences > 0) {
  2029. skip_sentences--;
  2030. if (skip_sentences == 0)
  2031. skipping_text = 0;
  2032. }
  2033. }
  2034. memset(&ph_list2[0], 0, sizeof(ph_list2[0]));
  2035. ph_list2[0].phcode = phonPAUSE_SHORT;
  2036. n_ph_list2 = 1;
  2037. tr->prev_last_stress = 0;
  2038. tr->prepause_timeout = 0;
  2039. tr->expect_verb = 0;
  2040. tr->expect_noun = 0;
  2041. tr->expect_past = 0;
  2042. tr->expect_verb_s = 0;
  2043. tr->phonemes_repeat_count = 0;
  2044. tr->end_stressed_vowel = 0;
  2045. tr->prev_dict_flags[0] = 0;
  2046. tr->prev_dict_flags[1] = 0;
  2047. word_count = 0;
  2048. single_quoted = 0;
  2049. word_flags = 0;
  2050. next_word_flags = 0;
  2051. sbuf[0] = 0;
  2052. sbuf[1] = ' ';
  2053. sbuf[2] = ' ';
  2054. ix = 3;
  2055. prev_in = ' ';
  2056. words[0].start = ix;
  2057. words[0].flags = 0;
  2058. finished = 0;
  2059. for (j = 0; charix[j] <= 0; j++) ;
  2060. words[0].sourceix = charix[j];
  2061. k = 0;
  2062. while (charix[j] != 0) {
  2063. // count the number of characters (excluding multibyte continuation bytes)
  2064. if (charix[j++] != -1)
  2065. k++;
  2066. }
  2067. words[0].length = k;
  2068. while (!finished && (ix < (int)sizeof(sbuf)) && (n_ph_list2 < N_PHONEME_LIST-4)) {
  2069. prev_out2 = prev_out;
  2070. utf8_in2(&prev_out, &sbuf[ix-1], 1); // prev_out = sbuf[ix-1];
  2071. if (tr->langopts.tone_numbers && IsDigit09(prev_out) && IsAlpha(prev_out2)) {
  2072. // tone numbers can be part of a word, consider them as alphabetic
  2073. prev_out = 'a';
  2074. }
  2075. if (prev_in_save != 0) {
  2076. prev_in = prev_in_save;
  2077. prev_in_save = 0;
  2078. } else if (source_index > 0) {
  2079. utf8_in2(&prev_in, &source[source_index-1], 1); // prev_in = source[source_index-1];
  2080. }
  2081. prev_source_index = source_index;
  2082. if (char_inserted) {
  2083. c = char_inserted;
  2084. char_inserted = 0;
  2085. } else {
  2086. source_index += utf8_in(&cc, &source[source_index]); // cc = source[source_index++];
  2087. c = cc;
  2088. }
  2089. next_in_nbytes = utf8_in(&next_in, &source[source_index]);
  2090. if (c == 0) {
  2091. finished = 1;
  2092. c = ' ';
  2093. }
  2094. if ((c == CTRL_EMBEDDED) || (c == ctrl_embedded)) {
  2095. // start of embedded command in the text
  2096. int srcix = source_index-1;
  2097. if (prev_in != ' ') {
  2098. c = ' ';
  2099. prev_in_save = c;
  2100. source_index--;
  2101. } else {
  2102. embedded_count += EmbeddedCommand(&source_index);
  2103. prev_in_save = prev_in;
  2104. // replace the embedded command by spaces
  2105. memset(&source[srcix], ' ', source_index-srcix);
  2106. source_index = srcix;
  2107. continue;
  2108. }
  2109. }
  2110. if ((option_sayas2 == SAYAS_KEY) && (c != ' ')) {
  2111. if ((prev_in == ' ') && (next_in == ' '))
  2112. option_sayas2 = SAYAS_SINGLE_CHARS; // single character, speak its name
  2113. c = towlower2(c);
  2114. }
  2115. if (phoneme_mode) {
  2116. all_upper_case = FLAG_PHONEMES;
  2117. if ((c == ']') && (next_in == ']')) {
  2118. phoneme_mode = 0;
  2119. source_index++;
  2120. c = ' ';
  2121. }
  2122. } else if ((option_sayas2 & 0xf0) == SAYAS_DIGITS) {
  2123. if (iswdigit(c)) {
  2124. count_sayas_digits++;
  2125. if (count_sayas_digits > (option_sayas2 & 0xf)) {
  2126. // break after the specified number of digits
  2127. c = ' ';
  2128. space_inserted = 1;
  2129. count_sayas_digits = 0;
  2130. }
  2131. } else {
  2132. count_sayas_digits = 0;
  2133. if (iswdigit(prev_out)) {
  2134. c = ' ';
  2135. space_inserted = 1;
  2136. }
  2137. }
  2138. } else if ((option_sayas2 & 0x10) == 0) {
  2139. // speak as words
  2140. if ((c == 0x92) || (c == 0xb4) || (c == 0x2019) || (c == 0x2032))
  2141. c = '\''; // 'microsoft' quote or sexed closing single quote, or prime - possibly used as apostrophe
  2142. if (((c == 0x2018) || (c == '?')) && IsAlpha(prev_out) && IsAlpha(next_in)) {
  2143. // ? between two letters may be a smart-quote replaced by ?
  2144. c = '\'';
  2145. }
  2146. if (c == CHAR_EMPHASIS) {
  2147. // this character is a marker that the previous word is the focus of the clause
  2148. c = ' ';
  2149. word_flags |= FLAG_FOCUS;
  2150. }
  2151. if (c == CHAR_COMMA_BREAK) {
  2152. c = ' ';
  2153. word_flags |= FLAG_COMMA_AFTER;
  2154. }
  2155. c = TranslateChar(tr, &source[source_index], prev_in, c, next_in, &char_inserted, &word_flags); // optional language specific function
  2156. if (c == 8)
  2157. continue; // ignore this character
  2158. if (char_inserted)
  2159. next_in = char_inserted;
  2160. // allow certain punctuation within a word (usually only apostrophe)
  2161. if (!IsAlpha(c) && !IsSpace(c) && (wcschr(tr->punct_within_word, c) == 0)) {
  2162. if (IsAlpha(prev_out)) {
  2163. if (tr->langopts.tone_numbers && IsDigit09(c) && !IsDigit09(next_in)) {
  2164. // allow a tone number as part of the word
  2165. } else {
  2166. c = ' '; // ensure we have an end-of-word terminator
  2167. space_inserted = 1;
  2168. }
  2169. }
  2170. }
  2171. if (iswdigit(prev_out)) {
  2172. if (!iswdigit(c) && (c != '.') && (c != ',') && (c != ' ')) {
  2173. c = ' '; // terminate digit string with a space
  2174. space_inserted = 1;
  2175. }
  2176. } else {
  2177. if (prev_in != ',') {
  2178. decimal_sep_count = 0;
  2179. }
  2180. }
  2181. if (c == '[') {
  2182. if ((next_in == '\002') || ((next_in == '[') && option_phoneme_input)) {
  2183. // "[\002" is used internally to start phoneme mode
  2184. phoneme_mode = FLAG_PHONEMES;
  2185. source_index++;
  2186. continue;
  2187. }
  2188. }
  2189. if (IsAlpha(c)) {
  2190. if (!IsAlpha(prev_out) || (tr->langopts.ideographs && ((c > 0x3040) || (prev_out > 0x3040)))) {
  2191. if (wcschr(tr->punct_within_word, prev_out) == 0)
  2192. letter_count = 0; // don't reset count for an apostrophy within a word
  2193. if ((prev_out != ' ') && (wcschr(tr->punct_within_word, prev_out) == 0)) {
  2194. // start of word, insert space if not one there already
  2195. c = ' ';
  2196. space_inserted = 1;
  2197. if (!IsBracket(prev_out)) { // ?? perhaps only set FLAG_NOSPACE for . - / (hyphenated words, URLs, etc)
  2198. next_word_flags |= FLAG_NOSPACE;
  2199. }
  2200. } else {
  2201. if (iswupper2(c))
  2202. word_flags |= FLAG_FIRST_UPPER;
  2203. if ((prev_out == ' ') && iswdigit(sbuf[ix-2]) && !iswdigit(prev_in)) {
  2204. // word, following a number, but with a space between
  2205. // Add an extra space, to distinguish "2 a" from "2a"
  2206. sbuf[ix++] = ' ';
  2207. words[word_count].start++;
  2208. }
  2209. }
  2210. }
  2211. if (c != ' ') {
  2212. letter_count++;
  2213. if (tr->letter_bits_offset > 0) {
  2214. if (((c < 0x250) && (prev_out >= tr->letter_bits_offset)) ||
  2215. ((c >= tr->letter_bits_offset) && (letter_count > 1) && (prev_out < 0x250))) {
  2216. // Don't mix native and Latin characters in the same word
  2217. // Break into separate words
  2218. if (IsAlpha(prev_out)) {
  2219. c = ' ';
  2220. space_inserted = 1;
  2221. word_flags |= FLAG_HYPHEN_AFTER;
  2222. next_word_flags |= FLAG_HYPHEN;
  2223. }
  2224. }
  2225. }
  2226. }
  2227. if (iswupper2(c)) {
  2228. c = towlower2(c);
  2229. if ((j = tr->langopts.param[LOPT_CAPS_IN_WORD]) > 0) {
  2230. if ((j == 2) && (syllable_marked == 0)) {
  2231. char_inserted = c;
  2232. c = 0x2c8; // stress marker
  2233. syllable_marked = 1;
  2234. }
  2235. } else {
  2236. if (iswlower2(prev_in)) {
  2237. // lower case followed by upper case in a word
  2238. if (UpperCaseInWord(tr, &sbuf[ix], c) == 1) {
  2239. // convert to lower case and continue
  2240. c = towlower2(c);
  2241. } else {
  2242. c = ' '; // lower case followed by upper case, treat as new word
  2243. space_inserted = 1;
  2244. prev_in_save = c;
  2245. }
  2246. } else if ((c != ' ') && iswupper2(prev_in) && iswlower2(next_in)) {
  2247. int next2_in;
  2248. utf8_in(&next2_in, &source[source_index + next_in_nbytes]);
  2249. if ((tr->translator_name == L('n', 'l')) && (letter_count == 2) && (c == 'j') && (prev_in == 'I')) {
  2250. // Dutch words may capitalise initial IJ, don't split
  2251. } else if (IsAlpha(next2_in)) {
  2252. // changing from upper to lower case, start new word at the last uppercase, if 3 or more letters
  2253. c = ' ';
  2254. space_inserted = 1;
  2255. prev_in_save = c;
  2256. next_word_flags |= FLAG_NOSPACE;
  2257. }
  2258. }
  2259. }
  2260. } else {
  2261. if ((all_upper_case) && (letter_count > 2)) {
  2262. if ((c == 's') && (next_in == ' ')) {
  2263. c = ' ';
  2264. all_upper_case |= FLAG_HAS_PLURAL;
  2265. if (sbuf[ix-1] == '\'')
  2266. sbuf[ix-1] = ' ';
  2267. } else
  2268. all_upper_case = 0; // current word contains lower case letters, not "'s"
  2269. } else
  2270. all_upper_case = 0;
  2271. }
  2272. } else if (c == '-') {
  2273. if (!IsSpace(prev_in) && IsAlpha(next_in)) {
  2274. if (prev_out != ' ') {
  2275. // previous 'word' not yet ended (not alpha or numeric), start new word now.
  2276. c = ' ';
  2277. space_inserted = 1;
  2278. } else {
  2279. // '-' between two letters is a hyphen, treat as a space
  2280. word_flags |= FLAG_HYPHEN;
  2281. if (word_count > 0)
  2282. words[word_count-1].flags |= FLAG_HYPHEN_AFTER;
  2283. c = ' ';
  2284. }
  2285. } else if ((prev_in == ' ') && (next_in == ' ')) {
  2286. // ' - ' dash between two spaces, treat as pause
  2287. c = ' ';
  2288. pre_pause_add = 4;
  2289. } else if (next_in == '-') {
  2290. // double hyphen, treat as pause
  2291. source_index++;
  2292. c = ' ';
  2293. pre_pause_add = 4;
  2294. } else if ((prev_out == ' ') && IsAlpha(prev_out2) && !IsAlpha(prev_in)) {
  2295. // insert extra space between a word + space + hyphen, to distinguish 'a -2' from 'a-2'
  2296. sbuf[ix++] = ' ';
  2297. words[word_count].start++;
  2298. }
  2299. } else if (c == '.') {
  2300. if (prev_out == '.') {
  2301. // multiple dots, separate by spaces. Note >3 dots has been replaced by elipsis
  2302. c = ' ';
  2303. space_inserted = 1;
  2304. } else if ((word_count > 0) && !(words[word_count-1].flags & FLAG_NOSPACE) && IsAlpha(prev_in)) {
  2305. // dot after a word, with space following, probably an abbreviation
  2306. words[word_count-1].flags |= FLAG_HAS_DOT;
  2307. if (IsSpace(next_in) || (next_in == '-'))
  2308. c = ' '; // remove the dot if it's followed by a space or hyphen, so that it's not pronounced
  2309. }
  2310. } else if (c == '\'') {
  2311. if (((prev_in == '.') || iswalnum(prev_in)) && IsAlpha(next_in)) {
  2312. // between two letters, or in an abbreviation (eg. u.s.a.'s). Consider the apostrophe as part of the word
  2313. single_quoted = 0;
  2314. } else if ((tr->langopts.param[LOPT_APOSTROPHE] & 1) && IsAlpha(next_in)) {
  2315. single_quoted = 0; // apostrophe at start of word is part of the word
  2316. } else if ((tr->langopts.param[LOPT_APOSTROPHE] & 2) && IsAlpha(prev_in)) {
  2317. single_quoted = 0; // apostrophe at end of word is part of the word
  2318. } else if ((wcschr(tr->char_plus_apostrophe, prev_in) != 0) && (prev_out2 == ' ')) {
  2319. // consider single character plus apostrophe as a word
  2320. single_quoted = 0;
  2321. if (next_in == ' ') {
  2322. source_index++; // skip following space
  2323. }
  2324. } else {
  2325. if ((prev_out == 's') && (single_quoted == 0)) {
  2326. // looks like apostrophe after an 's'
  2327. c = ' ';
  2328. } else {
  2329. if (IsSpace(prev_out))
  2330. single_quoted = 1;
  2331. else
  2332. single_quoted = 0;
  2333. pre_pause_add = 4; // single quote
  2334. c = ' ';
  2335. }
  2336. }
  2337. } else if (lookupwchar(breaks, c) != 0) {
  2338. c = ' '; // various characters to treat as space
  2339. } else if (iswdigit(c)) {
  2340. if (tr->langopts.tone_numbers && IsAlpha(prev_out) && !IsDigit(next_in)) {
  2341. } else if ((prev_out != ' ') && !iswdigit(prev_out)) {
  2342. if ((prev_out != tr->langopts.decimal_sep) || ((decimal_sep_count > 0) && (tr->langopts.decimal_sep == ','))) {
  2343. c = ' ';
  2344. space_inserted = 1;
  2345. } else {
  2346. decimal_sep_count = 1;
  2347. }
  2348. } else if ((prev_out == ' ') && IsAlpha(prev_out2) && !IsAlpha(prev_in)) {
  2349. // insert extra space between a word and a number, to distinguish 'a 2' from 'a2'
  2350. sbuf[ix++] = ' ';
  2351. words[word_count].start++;
  2352. }
  2353. }
  2354. }
  2355. if (IsSpace(c)) {
  2356. if (prev_out == ' ') {
  2357. word_flags |= FLAG_MULTIPLE_SPACES;
  2358. continue; // multiple spaces
  2359. }
  2360. if ((cc == 0x09) || (cc == 0x0a)) {
  2361. next_word_flags |= FLAG_MULTIPLE_SPACES; // tab or newline, not a simple space
  2362. }
  2363. if (space_inserted) {
  2364. // count the number of characters since the start of the word
  2365. j = 0;
  2366. k = source_index - 1;
  2367. while ((k >= source_index_word) && (charix[k] != 0)) {
  2368. if (charix[k] > 0) // don't count initial bytes of multi-byte character
  2369. j++;
  2370. k--;
  2371. }
  2372. words[word_count].length = j;
  2373. }
  2374. source_index_word = source_index;
  2375. // end of 'word'
  2376. sbuf[ix++] = ' ';
  2377. if ((word_count < N_CLAUSE_WORDS-1) && (ix > words[word_count].start)) {
  2378. if (embedded_count > 0) {
  2379. // there are embedded commands before this word
  2380. embedded_list[embedded_ix-1] |= 0x80; // terminate list of commands for this word
  2381. words[word_count].flags |= FLAG_EMBEDDED;
  2382. embedded_count = 0;
  2383. }
  2384. words[word_count].pre_pause = pre_pause;
  2385. words[word_count].flags |= (all_upper_case | word_flags | word_emphasis);
  2386. words[word_count].wmark = word_mark;
  2387. if (pre_pause > 0) {
  2388. // insert an extra space before the word, to prevent influence from previous word across the pause
  2389. for (j = ix; j > words[word_count].start; j--) {
  2390. sbuf[j] = sbuf[j-1];
  2391. }
  2392. sbuf[j] = ' ';
  2393. words[word_count].start++;
  2394. ix++;
  2395. }
  2396. word_count++;
  2397. words[word_count].start = ix;
  2398. words[word_count].flags = 0;
  2399. for (j = source_index; charix[j] <= 0; j++) ; // skip blanks
  2400. words[word_count].sourceix = charix[j];
  2401. k = 0;
  2402. while (charix[j] != 0) {
  2403. // count the number of characters (excluding multibyte continuation bytes)
  2404. if (charix[j++] != -1)
  2405. k++;
  2406. }
  2407. words[word_count].length = k;
  2408. word_flags = next_word_flags;
  2409. next_word_flags = 0;
  2410. pre_pause = 0;
  2411. word_mark = 0;
  2412. all_upper_case = FLAG_ALL_UPPER;
  2413. syllable_marked = 0;
  2414. }
  2415. if (space_inserted) {
  2416. source_index = prev_source_index; // rewind to the previous character
  2417. char_inserted = 0;
  2418. space_inserted = 0;
  2419. }
  2420. } else {
  2421. if ((ix < (N_TR_SOURCE - 4)))
  2422. ix += utf8_out(c, &sbuf[ix]); // sbuf[ix++] = c;
  2423. }
  2424. if (pre_pause_add > pre_pause)
  2425. pre_pause = pre_pause_add;
  2426. pre_pause_add = 0;
  2427. }
  2428. if ((word_count == 0) && (embedded_count > 0)) {
  2429. // add a null 'word' to carry the embedded command flag
  2430. embedded_list[embedded_ix-1] |= 0x80;
  2431. words[word_count].flags |= FLAG_EMBEDDED;
  2432. word_count = 1;
  2433. }
  2434. tr->clause_end = &sbuf[ix-1];
  2435. sbuf[ix] = 0;
  2436. words[0].pre_pause = 0; // don't add extra pause at beginning of clause
  2437. words[word_count].pre_pause = 8;
  2438. if (word_count > 0) {
  2439. ix = word_count-1;
  2440. while ((ix > 0) && (IsBracket(sbuf[words[ix].start])))
  2441. ix--; // the last word is a bracket, mark the previous word as last
  2442. words[ix].flags |= FLAG_LAST_WORD;
  2443. // FLAG_NOSPACE check to avoid recognizing .mr -mr
  2444. if ((terminator & CLAUSE_DOT) && !(words[word_count-1].flags & FLAG_NOSPACE))
  2445. words[word_count-1].flags |= FLAG_HAS_DOT;
  2446. }
  2447. words[0].flags |= FLAG_FIRST_WORD;
  2448. for (ix = 0; ix < word_count; ix++) {
  2449. int nx;
  2450. int c_temp;
  2451. char *pn;
  2452. char *pw;
  2453. int nw;
  2454. char number_buf[150];
  2455. WORD_TAB num_wtab[50]; // copy of 'words', when splitting numbers into parts
  2456. // start speaking at a specified word position in the text?
  2457. count_words++;
  2458. if (skip_words > 0) {
  2459. skip_words--;
  2460. if (skip_words == 0)
  2461. skipping_text = 0;
  2462. }
  2463. if (skipping_text)
  2464. continue;
  2465. current_alphabet = NULL;
  2466. // digits should have been converted to Latin alphabet ('0' to '9')
  2467. word = pw = &sbuf[words[ix].start];
  2468. if (iswdigit(word[0]) && (tr->langopts.break_numbers != BREAK_THOUSANDS)) {
  2469. // Languages with 100000 numbers. Remove thousands separators so that we can insert them again later
  2470. pn = number_buf;
  2471. while (pn < &number_buf[sizeof(number_buf)-20]) {
  2472. if (iswdigit(*pw)) {
  2473. *pn++ = *pw++;
  2474. } else if ((*pw == tr->langopts.thousands_sep) && (pw[1] == ' ')
  2475. && iswdigit(pw[2]) && (pw[3] != ' ') && (pw[4] != ' ')) { // don't allow only 1 or 2 digits in the final part
  2476. pw += 2;
  2477. ix++; // skip "word"
  2478. } else {
  2479. nx = pw - word;
  2480. memset(word, ' ', nx);
  2481. nx = pn - number_buf;
  2482. memcpy(word, number_buf, nx);
  2483. break;
  2484. }
  2485. }
  2486. pw = word;
  2487. }
  2488. for (n_digits = 0; iswdigit(word[n_digits]); n_digits++) ; // count consecutive digits
  2489. if (n_digits > 4) {
  2490. // word is entirely digits, insert commas and break into 3 digit "words"
  2491. number_buf[0] = ' ';
  2492. pn = &number_buf[1];
  2493. nx = n_digits;
  2494. nw = 0;
  2495. if ((n_digits > tr->langopts.max_digits) || (word[0] == '0'))
  2496. words[ix].flags |= FLAG_INDIVIDUAL_DIGITS;
  2497. while (pn < &number_buf[sizeof(number_buf)-20]) {
  2498. if (!IsDigit09(c = *pw++) && (c != tr->langopts.decimal_sep))
  2499. break;
  2500. *pn++ = c;
  2501. nx--;
  2502. if ((nx > 0) && (tr->langopts.break_numbers & (1 << nx))) {
  2503. memcpy(&num_wtab[nw++], &words[ix], sizeof(WORD_TAB)); // copy the 'words' entry for each word of numbers
  2504. if (tr->langopts.thousands_sep != ' ') {
  2505. *pn++ = tr->langopts.thousands_sep;
  2506. }
  2507. *pn++ = ' ';
  2508. if ((words[ix].flags & FLAG_INDIVIDUAL_DIGITS) == 0) {
  2509. if (tr->langopts.break_numbers & (1 << (nx-1))) {
  2510. // the next group only has 1 digits, make it three
  2511. *pn++ = '0';
  2512. *pn++ = '0';
  2513. }
  2514. if (tr->langopts.break_numbers & (1 << (nx-2))) {
  2515. // the next group only has 2 digits (eg. Indian languages), make it three
  2516. *pn++ = '0';
  2517. }
  2518. }
  2519. }
  2520. }
  2521. pw--;
  2522. memcpy(&num_wtab[nw], &words[ix], sizeof(WORD_TAB)*2); // the original number word, and the word after it
  2523. for (j = 1; j <= nw; j++) {
  2524. num_wtab[j].flags &= ~(FLAG_MULTIPLE_SPACES | FLAG_EMBEDDED); // don't use these flags for subsequent parts when splitting a number
  2525. }
  2526. // include the next few characters, in case there are an ordinal indicator or other suffix
  2527. memcpy(pn, pw, 16);
  2528. pn[16] = 0;
  2529. nw = 0;
  2530. for (pw = &number_buf[1]; pw < pn;) {
  2531. // keep wflags for each part, for FLAG_HYPHEN_AFTER
  2532. dict_flags = TranslateWord2(tr, pw, &num_wtab[nw++], words[ix].pre_pause, 0);
  2533. while (*pw++ != ' ') ;
  2534. words[ix].pre_pause = 0;
  2535. }
  2536. } else {
  2537. pre_pause = 0;
  2538. dict_flags = TranslateWord2(tr, word, &words[ix], words[ix].pre_pause, words[ix+1].pre_pause);
  2539. if (pre_pause > words[ix+1].pre_pause) {
  2540. words[ix+1].pre_pause = pre_pause;
  2541. pre_pause = 0;
  2542. }
  2543. if (dict_flags & FLAG_SPELLWORD) {
  2544. // redo the word, speaking single letters
  2545. for (pw = word; *pw != ' ';) {
  2546. memset(number_buf, ' ', 9);
  2547. nx = utf8_in(&c_temp, pw);
  2548. memcpy(&number_buf[2], pw, nx);
  2549. TranslateWord2(tr, &number_buf[2], &words[ix], 0, 0);
  2550. pw += nx;
  2551. }
  2552. }
  2553. if ((dict_flags & (FLAG_ALLOW_DOT | FLAG_NEEDS_DOT)) && (ix == word_count - 1 - dictionary_skipwords) && (terminator & CLAUSE_DOT)) {
  2554. // probably an abbreviation such as Mr. or B. rather than end of sentence
  2555. clause_pause = 10;
  2556. tone = 4;
  2557. }
  2558. }
  2559. if (dict_flags & FLAG_SKIPWORDS) {
  2560. // dictionary indicates skip next word(s)
  2561. while (dictionary_skipwords > 0) {
  2562. words[ix+dictionary_skipwords].flags |= FLAG_DELETE_WORD;
  2563. dictionary_skipwords--;
  2564. }
  2565. }
  2566. }
  2567. if (embedded_read < embedded_ix) {
  2568. // any embedded commands not yet processed?
  2569. Word_EmbeddedCmd();
  2570. }
  2571. for (ix = 0; ix < 2; ix++) {
  2572. // terminate the clause with 2 PAUSE phonemes
  2573. PHONEME_LIST2 *p2;
  2574. p2 = &ph_list2[n_ph_list2 + ix];
  2575. p2->phcode = phonPAUSE;
  2576. p2->stresslevel = 0;
  2577. p2->sourceix = source_index;
  2578. p2->synthflags = 0;
  2579. }
  2580. n_ph_list2 += 2;
  2581. if (count_words == 0) {
  2582. clause_pause = 0;
  2583. }
  2584. if (Eof() && ((word_count == 0) || (option_endpause == 0))) {
  2585. clause_pause = 10;
  2586. }
  2587. MakePhonemeList(tr, clause_pause, new_sentence2);
  2588. phoneme_list[N_PHONEME_LIST].ph = NULL; // recognize end of phoneme_list array, in Generate()
  2589. phoneme_list[N_PHONEME_LIST].sourceix = 1;
  2590. if (embedded_count) { // ???? is this needed
  2591. phoneme_list[n_phoneme_list-2].synthflags = SFLAG_EMBEDDED;
  2592. embedded_list[embedded_ix-1] |= 0x80;
  2593. embedded_list[embedded_ix] = 0x80;
  2594. }
  2595. prev_clause_pause = clause_pause;
  2596. if (tone_out != NULL)
  2597. *tone_out = tone;
  2598. new_sentence = 0;
  2599. if (terminator & CLAUSE_BIT_SENTENCE) {
  2600. new_sentence = 1; // next clause is a new sentence
  2601. }
  2602. if (voice_change != NULL) {
  2603. // return new voice name if an embedded voice change command terminated the clause
  2604. if (terminator & CLAUSE_BIT_VOICE)
  2605. *voice_change = voice_change_name;
  2606. else
  2607. *voice_change = NULL;
  2608. }
  2609. if (Eof() || (vp_input == NULL))
  2610. return NULL;
  2611. if (option_multibyte == espeakCHARS_WCHAR)
  2612. return (void *)p_wchar_input;
  2613. else
  2614. return (void *)p_textinput;
  2615. }
  2616. void InitText(int control)
  2617. {
  2618. count_sentences = 0;
  2619. count_words = 0;
  2620. end_character_position = 0;
  2621. skip_sentences = 0;
  2622. skip_marker[0] = 0;
  2623. skip_words = 0;
  2624. skip_characters = 0;
  2625. skipping_text = 0;
  2626. new_sentence = 1;
  2627. prev_clause_pause = 0;
  2628. option_sayas = 0;
  2629. option_sayas2 = 0;
  2630. option_emphasis = 0;
  2631. word_emphasis = 0;
  2632. embedded_flag = 0;
  2633. InitText2();
  2634. if ((control & espeakKEEP_NAMEDATA) == 0) {
  2635. InitNamedata();
  2636. }
  2637. }