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.cpp 83KB

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