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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2011 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 800
  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,0x2501,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,120,100,110,110,110,110,110,110, 110, /* a <- next2 */
  298. 110,120,100,110,110,110,110,110,110, 110, /* , */
  299. 110,120,100,110,100,110,110,110,100, 110, /* t */
  300. 110,120,100,110,110,110,110,110,110, 110, /* s */
  301. 110,120,100,110,110,110,110,110,110, 110, /* n */
  302. 110,120,100,110,110,110,110,110,110, 110, /* d */
  303. 110,120,100,110,110,110,110,110,110, 110, /* z */
  304. 110,120,100,110,110,110,110,110,110, 110, /* r */
  305. 110,120,100,110,110,110,110,110,110, 110, /* N */
  306. 110,120,100,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. if((c >= 0xd7a) && (c <= 0xd7f))
  340. return(1); // malaytalam chillu characters
  341. return(0);
  342. }
  343. if((c >= 0x64b) && (c <= 0x65e))
  344. return(1); // arabic vowel marks
  345. if((c >= 0x300) && (c <= 0x36f))
  346. return(1); // combining accents
  347. if((c >= 0x780) && (c <= 0x7b1))
  348. return(1); // taani/divehi (maldives)
  349. if((c >= 0xf40) && (c <= 0xfbc))
  350. return(1); // tibetan
  351. if((c >= 0x1100) && (c <= 0x11ff))
  352. return(1); //Korean jamo
  353. if((c > 0x3040) && (c <= 0xa700))
  354. return(1); // Chinese/Japanese. Should never get here, but Mac OS 10.4's iswalpha seems to be broken, so just make sure
  355. return(0);
  356. }
  357. static int IsDigit09(unsigned int c)
  358. {//=========================
  359. if((c >= '0') && (c <= '9'))
  360. return(1);
  361. return(0);
  362. }
  363. int IsDigit(unsigned int c)
  364. {//========================
  365. if(iswdigit(c))
  366. return(1);
  367. if((c >= 0x966) && (c <= 0x96f))
  368. return(1);
  369. return(0);
  370. }
  371. int IsSpace(unsigned int c)
  372. {//========================
  373. if(c == 0)
  374. return(0);
  375. if((c >= 0x2500) && (c < 0x25a0))
  376. return(1); // box drawing characters
  377. // if(wcschr(chars_space,c))
  378. // return(1);
  379. return(iswspace(c));
  380. }
  381. void DeleteTranslator(Translator *tr)
  382. {//==================================
  383. if(tr->data_dictlist != NULL)
  384. Free(tr->data_dictlist);
  385. Free(tr);
  386. }
  387. int lookupwchar(const unsigned short *list,int c)
  388. {//==============================================
  389. // Is the character c in the list ?
  390. int ix;
  391. for(ix=0; list[ix] != 0; ix++)
  392. {
  393. if(list[ix] == c)
  394. return(ix+1);
  395. }
  396. return(0);
  397. }
  398. int IsBracket(int c)
  399. {//=================
  400. if((c >= 0x2014) && (c <= 0x201f))
  401. return(1);
  402. return(lookupwchar(brackets,c));
  403. }
  404. int utf8_out(unsigned int c, char *buf)
  405. {//====================================
  406. // write a unicode character into a buffer as utf8
  407. // returns the number of bytes written
  408. int n_bytes;
  409. int j;
  410. int shift;
  411. static char unsigned code[4] = {0,0xc0,0xe0,0xf0};
  412. if(c < 0x80)
  413. {
  414. buf[0] = c;
  415. return(1);
  416. }
  417. if(c >= 0x110000)
  418. {
  419. buf[0] = ' '; // out of range character code
  420. return(1);
  421. }
  422. if(c < 0x0800)
  423. n_bytes = 1;
  424. else
  425. if(c < 0x10000)
  426. n_bytes = 2;
  427. else
  428. n_bytes = 3;
  429. shift = 6*n_bytes;
  430. buf[0] = code[n_bytes] | (c >> shift);
  431. for(j=0; j<n_bytes; j++)
  432. {
  433. shift -= 6;
  434. buf[j+1] = 0x80 + ((c >> shift) & 0x3f);
  435. }
  436. return(n_bytes+1);
  437. } // end of utf8_out
  438. int utf8_nbytes(const char *buf)
  439. {//=============================
  440. // Returns the number of bytes for the first UTF-8 character in buf
  441. unsigned char c = (unsigned char)buf[0];
  442. if(c < 0x80)
  443. return(1);
  444. if(c < 0xe0)
  445. return(2);
  446. if(c < 0xf0)
  447. return(3);
  448. return(4);
  449. }
  450. int utf8_in2(int *c, const char *buf, int backwards)
  451. {//=================================================
  452. // Read a unicode characater from a UTF8 string
  453. // Returns the number of UTF8 bytes used.
  454. // backwards: set if we are moving backwards through the UTF8 string
  455. int c1;
  456. int n_bytes;
  457. int ix;
  458. static const unsigned char mask[4] = {0xff,0x1f,0x0f,0x07};
  459. // find the start of the next/previous character
  460. while((*buf & 0xc0) == 0x80)
  461. {
  462. // skip over non-initial bytes of a multi-byte utf8 character
  463. if(backwards)
  464. buf--;
  465. else
  466. buf++;
  467. }
  468. n_bytes = 0;
  469. if((c1 = *buf++) & 0x80)
  470. {
  471. if((c1 & 0xe0) == 0xc0)
  472. n_bytes = 1;
  473. else
  474. if((c1 & 0xf0) == 0xe0)
  475. n_bytes = 2;
  476. else
  477. if((c1 & 0xf8) == 0xf0)
  478. n_bytes = 3;
  479. c1 &= mask[n_bytes];
  480. for(ix=0; ix<n_bytes; ix++)
  481. {
  482. c1 = (c1 << 6) + (*buf++ & 0x3f);
  483. }
  484. }
  485. *c = c1;
  486. return(n_bytes+1);
  487. }
  488. int utf8_in(int *c, const char *buf)
  489. {//=================================
  490. // Read a unicode characater from a UTF8 string
  491. // Returns the number of UTF8 bytes used.
  492. return(utf8_in2(c,buf,0));
  493. }
  494. char *strchr_w(const char *s, int c)
  495. {//=================================
  496. // return NULL for any non-ascii character
  497. if(c >= 0x80)
  498. return(NULL);
  499. return(strchr((char *)s,c)); // (char *) is needed for Borland compiler
  500. }
  501. int IsAllUpper(const char *word)
  502. {//=============================
  503. int c;
  504. while((*word != 0) && !isspace2(*word))
  505. {
  506. word += utf8_in(&c, word);
  507. if(!iswupper(c))
  508. return(0);
  509. }
  510. return(1);
  511. }
  512. static char *SpeakIndividualLetters(Translator *tr, char *word, char *phonemes, int spell_word)
  513. {//============================================================================================
  514. int posn = 0;
  515. int capitals = 0;
  516. int non_initial = 0;
  517. if (spell_word > 2)
  518. capitals = 2;
  519. while((*word != ' ') && (*word != 0))
  520. {
  521. word += TranslateLetter(tr, word, phonemes, capitals | non_initial);
  522. posn++;
  523. non_initial = 1;
  524. if(phonemes[0] == phonSWITCH)
  525. {
  526. // change to another language in order to translate this word
  527. strcpy(word_phonemes,phonemes);
  528. return(NULL);
  529. }
  530. }
  531. SetSpellingStress(tr,phonemes,spell_word,posn);
  532. return(word);
  533. } // end of SpeakIndividualLetters
  534. static int CheckDottedAbbrev(char *word1, WORD_TAB *wtab)
  535. {//=====================================================
  536. int wc;
  537. int count = 0;
  538. int nbytes;
  539. int ok;
  540. int ix;
  541. char *word;
  542. char *wbuf;
  543. char word_buf[80];
  544. word = word1;
  545. wbuf = word_buf;
  546. ix = 0;
  547. for(;;)
  548. {
  549. ok = 0;
  550. nbytes = utf8_in(&wc, word);
  551. if((word[nbytes] == ' ') && IsAlpha(wc))
  552. {
  553. if(word[nbytes+1] == '.')
  554. {
  555. if(word[nbytes+2] == ' ')
  556. ok = 1;
  557. else
  558. if(word[nbytes+2] =='\'')
  559. {
  560. nbytes += 2; // delete the final dot (eg. u.s.a.'s)
  561. ok = 2;
  562. }
  563. }
  564. else
  565. if((count > 0) && (word[nbytes] == ' '))
  566. ok = 2;
  567. }
  568. if(ok == 0)
  569. break;
  570. for(ix=0; ix < nbytes; ix++)
  571. *wbuf++ = word[ix];
  572. count++;
  573. if(ok == 2)
  574. {
  575. word += nbytes;
  576. break;
  577. }
  578. word += (nbytes + 3);
  579. }
  580. if(count > 1)
  581. {
  582. ix = wbuf - word_buf;
  583. memcpy(word1, word_buf, ix);
  584. while(&word1[ix] < word)
  585. word1[ix++] = ' ';
  586. dictionary_skipwords = (count - 1)*2;
  587. }
  588. return(count);
  589. } // end of CheckDottedAbbrev
  590. extern char *phondata_ptr;
  591. int ChangeEquivalentPhonemes(Translator *tr, int lang2, char *phonemes)
  592. {//====================================================================
  593. // tr: the original language
  594. // lang2: phoneme table number for the temporary language
  595. // phonemes: the phonemes to be replaced
  596. int ix;
  597. int len;
  598. char phon;
  599. char *p;
  600. unsigned char *pb;
  601. char *eqlist;
  602. char *p_out;
  603. char *p_in;
  604. int remove_stress = 0;
  605. char phonbuf[N_WORD_PHONEMES];
  606. // has a phoneme equivalence table been specified for thus language pair?
  607. if((ix = phoneme_tab_list[tr->phoneme_tab_ix].equivalence_tables) == 0)
  608. return(0);
  609. pb = (unsigned char *)&phondata_ptr[ix];
  610. for(;;)
  611. {
  612. if(pb[0] == 0)
  613. return(0); // table not found
  614. if(pb[0] == lang2)
  615. break;
  616. len = (pb[2] << 8) + pb[3]; // size of this table in words
  617. pb += (len * 4);
  618. }
  619. remove_stress = pb[1];
  620. if(option_phonemes == 2)
  621. {
  622. DecodePhonemes(phonemes, phonbuf);
  623. fprintf(f_trans,"(%s) %s -> (%s) ", phoneme_tab_list[lang2].name, phonbuf, phoneme_tab_list[tr->phoneme_tab_ix].name);
  624. }
  625. p_in = phonemes;
  626. eqlist = (char *)&pb[8];
  627. p_out = phonbuf;
  628. while((phon = *p_in++) != 0)
  629. {
  630. if(remove_stress && ((phon & 0xff) < phonSTRESS_PREV))
  631. continue; // remove stress marks
  632. // is there a translation for this phoneme code?
  633. p = eqlist;
  634. while(*p != 0)
  635. {
  636. len = strlen(&p[1]);
  637. if(*p == phon)
  638. {
  639. strcpy(p_out, &p[1]);
  640. p_out += len;
  641. break;
  642. }
  643. p += (len + 2);
  644. }
  645. if(*p == 0)
  646. {
  647. // no translation found
  648. *p_out++ = phon;
  649. }
  650. }
  651. *p_out = 0;
  652. if(remove_stress)
  653. {
  654. SetWordStress(tr, phonbuf, NULL, -1, 0);
  655. }
  656. strcpy(phonemes, phonbuf);
  657. if(option_phonemes == 2)
  658. {
  659. SelectPhonemeTable(tr->phoneme_tab_ix);
  660. DecodePhonemes(phonemes, phonbuf);
  661. fprintf(f_trans,"%s\n\n", phonbuf);
  662. }
  663. return(1);
  664. } // end of ChangeEquivalentPhonemes
  665. int TranslateWord(Translator *tr, char *word_start, int next_pause, WORD_TAB *wtab)
  666. {//===========================================================================
  667. // word1 is terminated by space (0x20) character
  668. char *word1;
  669. int word_length;
  670. int ix;
  671. char *p;
  672. int pfix;
  673. int n_chars;
  674. unsigned int dictionary_flags[2];
  675. unsigned int dictionary_flags2[2];
  676. int end_type=0;
  677. int prefix_type=0;
  678. int prefix_stress;
  679. char *wordx;
  680. char phonemes[N_WORD_PHONEMES];
  681. char prefix_phonemes[N_WORD_PHONEMES];
  682. char unpron_phonemes[N_WORD_PHONEMES];
  683. char end_phonemes[N_WORD_PHONEMES];
  684. char word_copy[N_WORD_BYTES];
  685. char word_copy2[N_WORD_BYTES];
  686. int word_copy_length;
  687. char prefix_chars[0x3f + 2];
  688. int found=0;
  689. int end_flags;
  690. char c_temp; // save a character byte while we temporarily replace it with space
  691. int first_char;
  692. int last_char = 0;
  693. int add_plural_suffix = 0;
  694. int prefix_flags = 0;
  695. int confirm_prefix;
  696. int spell_word;
  697. int stress_bits;
  698. int emphasize_allcaps = 0;
  699. int wflags;
  700. int wmark;
  701. int was_unpronouncable = 0;
  702. WORD_TAB wtab_null[8];
  703. // translate these to get pronunciations of plural 's' suffix (different forms depending on
  704. // the preceding letter
  705. static char word_zz[4] = {0,'z','z',0};
  706. static char word_iz[4] = {0,'i','z',0};
  707. static char word_ss[4] = {0,'s','s',0};
  708. if(wtab == NULL)
  709. {
  710. memset(wtab_null, 0, sizeof(wtab_null));
  711. wtab = wtab_null;
  712. }
  713. wflags = wtab->flags;
  714. wmark = wtab->wmark;
  715. dictionary_flags[0] = 0;
  716. dictionary_flags[1] = 0;
  717. dictionary_flags2[0] = 0;
  718. dictionary_flags2[1] = 0;
  719. dictionary_skipwords = 0;
  720. phonemes[0] = 0;
  721. unpron_phonemes[0] = 0;
  722. prefix_phonemes[0] = 0;
  723. end_phonemes[0] = 0;
  724. if(tr->data_dictlist == NULL)
  725. {
  726. // dictionary is not loaded
  727. word_phonemes[0] = 0;
  728. return(0);
  729. }
  730. // count the length of the word
  731. word1 = word_start;
  732. if(*word1 == ' ') word1++; // possibly a dot was replaced by space: $dot
  733. wordx = word1;
  734. utf8_in(&first_char,wordx);
  735. word_length = 0;
  736. while((*wordx != 0) && (*wordx != ' '))
  737. {
  738. wordx += utf8_in(&last_char,wordx);
  739. word_length++;
  740. }
  741. word_copy_length = wordx - word_start;
  742. if(word_copy_length >= N_WORD_BYTES)
  743. word_copy_length = N_WORD_BYTES-1;
  744. memcpy(word_copy2, word_start, word_copy_length);
  745. spell_word = 0;
  746. if(option_sayas == SAYAS_KEY)
  747. {
  748. if(word_length == 1)
  749. spell_word = 4;
  750. else
  751. {
  752. // is there a translation for this keyname ?
  753. word1--;
  754. *word1 = '_'; // prefix keyname with '_'
  755. found = LookupDictList(tr, &word1, phonemes, dictionary_flags, 0, wtab);
  756. }
  757. }
  758. // try an initial lookup in the dictionary list, we may find a pronunciation specified, or
  759. // we may just find some flags
  760. if(option_sayas & 0x10)
  761. {
  762. // SAYAS_CHAR, SAYAS_GYLPH, or SAYAS_SINGLE_CHAR
  763. spell_word = option_sayas & 0xf; // 2,3,4
  764. }
  765. else
  766. {
  767. if(!found)
  768. found = LookupDictList(tr, &word1, phonemes, dictionary_flags, FLAG_ALLOW_TEXTMODE, wtab); // the original word
  769. if((dictionary_flags[0] & (FLAG_ALLOW_DOT || FLAG_NEEDS_DOT)) && (wordx[1] == '.'))
  770. {
  771. wordx[1] = ' '; // remove a Dot after this word
  772. }
  773. if(dictionary_flags[0] & FLAG_TEXTMODE)
  774. {
  775. first_char = word1[0];
  776. stress_bits = dictionary_flags[0] & 0x7f;
  777. found = LookupDictList(tr, &word1, phonemes, dictionary_flags2, 0, wtab); // the text replacement
  778. if(dictionary_flags2[0]!=0)
  779. {
  780. dictionary_flags[0] = dictionary_flags2[0];
  781. dictionary_flags[1] = dictionary_flags2[1];
  782. if(stress_bits != 0)
  783. {
  784. // keep any stress information from the original word
  785. dictionary_flags[0] = (dictionary_flags[0] & ~0x7f) | stress_bits;
  786. }
  787. }
  788. }
  789. else
  790. if((found==0) && (dictionary_flags[0] & FLAG_SKIPWORDS))
  791. {
  792. // grouped words, but no translation. Join the words with hyphens.
  793. wordx = word1;
  794. ix = 0;
  795. while(ix < dictionary_skipwords)
  796. {
  797. if(*wordx == ' ')
  798. {
  799. *wordx = '-';
  800. ix++;
  801. }
  802. wordx++;
  803. }
  804. }
  805. if((word_length == 1) && (dictionary_skipwords == 0))
  806. {
  807. // is this a series of single letters separated by dots?
  808. if(CheckDottedAbbrev(word1, wtab))
  809. {
  810. dictionary_flags[0] = 0;
  811. dictionary_flags[1] = 0;
  812. spell_word = 1;
  813. if(dictionary_skipwords)
  814. dictionary_flags[0] = FLAG_SKIPWORDS;
  815. }
  816. }
  817. // if textmode, LookupDictList() replaces word1 by the new text and returns found=0
  818. if(phonemes[0] == phonSWITCH)
  819. {
  820. // change to another language in order to translate this word
  821. strcpy(word_phonemes,phonemes);
  822. return(0);
  823. }
  824. if((wmark > 0) && (wmark < 8))
  825. {
  826. // the stressed syllable has been specified in the text (TESTING)
  827. dictionary_flags[0] = (dictionary_flags[0] & ~0xf) | wmark;
  828. }
  829. if(!found && (dictionary_flags[0] & FLAG_ABBREV))
  830. {
  831. // the word has $abbrev flag, but no pronunciation specified. Speak as individual letters
  832. spell_word = 1;
  833. }
  834. if(!found && iswdigit(first_char))
  835. {
  836. Lookup(tr,"_0lang",word_phonemes);
  837. if(word_phonemes[0] == phonSWITCH)
  838. return(0);
  839. found = TranslateNumber(tr, word1, phonemes, dictionary_flags, wtab, 0);
  840. }
  841. if(!found & ((wflags & FLAG_UPPERS) != FLAG_FIRST_UPPER))
  842. {
  843. // either all upper or all lower case
  844. if((tr->langopts.numbers & NUM_ROMAN) || ((tr->langopts.numbers & NUM_ROMAN_CAPITALS) && (wflags & FLAG_ALL_UPPER)))
  845. {
  846. if((wflags & FLAG_LAST_WORD) || !(wtab[1].flags & FLAG_NOSPACE))
  847. {
  848. // don't use Roman number if this word is not separated from the next word (eg. "XLTest")
  849. if((found = TranslateRoman(tr, word1, phonemes, wtab)) != 0)
  850. dictionary_flags[0] |= FLAG_ABBREV; // prevent emphasis if capitals
  851. }
  852. }
  853. }
  854. if((wflags & FLAG_ALL_UPPER) && (word_length > 1)&& iswalpha(first_char))
  855. {
  856. if((option_tone_flags & OPTION_EMPHASIZE_ALLCAPS) && !(dictionary_flags[0] & FLAG_ABBREV))
  857. {
  858. // emphasize words which are in capitals
  859. emphasize_allcaps = FLAG_EMPHASIZED;
  860. }
  861. else
  862. if(!found && !(dictionary_flags[0] & FLAG_SKIPWORDS) && (word_length<4) && (tr->clause_lower_count > 3)
  863. && (tr->clause_upper_count <= tr->clause_lower_count))
  864. {
  865. // An upper case word in a lower case clause. This could be an abbreviation.
  866. spell_word = 1;
  867. }
  868. }
  869. }
  870. if(spell_word > 0)
  871. {
  872. // Speak as individual letters
  873. phonemes[0] = 0;
  874. end_type = 0;
  875. if(SpeakIndividualLetters(tr, word1, phonemes, spell_word) == NULL)
  876. {
  877. if(word_length > 1)
  878. return(FLAG_SPELLWORD); // a mixture of languages, retranslate as individual letters, separated by spaces
  879. return(0);
  880. }
  881. }
  882. else
  883. if(found == 0)
  884. {
  885. int posn;
  886. int non_initial;
  887. int length;
  888. // word's pronunciation is not given in the dictionary list, although
  889. // dictionary_flags may have ben set there
  890. posn = 0;
  891. non_initial = 0;
  892. length = 999;
  893. wordx = word1;
  894. while(((length < 3) && (length > 0))|| (word_length > 1 && Unpronouncable(tr, wordx, posn)))
  895. {
  896. // This word looks "unpronouncable", so speak letters individually until we
  897. // find a remainder that we can pronounce.
  898. was_unpronouncable = FLAG_WAS_UNPRONOUNCABLE;
  899. emphasize_allcaps = 0;
  900. if(wordx[0] == '\'')
  901. break;
  902. if(posn > 0)
  903. non_initial = 1;
  904. wordx += TranslateLetter(tr, wordx, unpron_phonemes, non_initial);
  905. posn++;
  906. if(unpron_phonemes[0] == phonSWITCH)
  907. {
  908. // change to another language in order to translate this word
  909. strcpy(word_phonemes,unpron_phonemes);
  910. if(strcmp(&unpron_phonemes[1],"en")==0)
  911. return(FLAG_SPELLWORD); // _^_en must have been set in TranslateLetter(), not *_rules
  912. return(0);
  913. }
  914. #ifdef deleted
  915. p = &wordx[word_length-3]; // this looks wrong. Doesn't consider multi-byte chars.
  916. if(memcmp(p,"'s ",3) == 0)
  917. {
  918. // remove a 's suffix and pronounce this separately (not as an individual letter)
  919. add_plural_suffix = 1;
  920. p[0] = ' ';
  921. p[1] = ' ';
  922. last_char = p[-1];
  923. }
  924. #endif
  925. length=0;
  926. while(wordx[length] != ' ') length++;
  927. }
  928. SetSpellingStress(tr,unpron_phonemes,0,posn);
  929. // anything left ?
  930. if(*wordx != ' ')
  931. {
  932. if((unpron_phonemes[0] != 0) && (wordx[0] != '\''))
  933. {
  934. // letters which have been spoken individually from affecting the pronunciation of the pronuncable part
  935. wordx[-1] = ' ';
  936. }
  937. // Translate the stem
  938. end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags, dictionary_flags);
  939. if(phonemes[0] == phonSWITCH)
  940. {
  941. // change to another language in order to translate this word
  942. strcpy(word_phonemes,phonemes);
  943. return(0);
  944. }
  945. if((phonemes[0] == 0) && (end_phonemes[0] == 0))
  946. {
  947. int wc;
  948. // characters not recognised, speak them individually
  949. utf8_in(&wc, wordx);
  950. if((word_length == 1) && IsAlpha(wc))
  951. {
  952. if((wordx = SpeakIndividualLetters(tr, wordx, phonemes, spell_word)) == NULL)
  953. {
  954. return(0);
  955. }
  956. }
  957. }
  958. c_temp = wordx[-1];
  959. found = 0;
  960. confirm_prefix = 1;
  961. for (int loopcount = 0; (loopcount < 50) && (end_type & SUFX_P); loopcount++)
  962. {
  963. // Found a standard prefix, remove it and retranslate
  964. // loopcount guards against an endless loop
  965. if(confirm_prefix && !(end_type & SUFX_B))
  966. {
  967. int end2;
  968. char phonemes2[N_WORD_PHONEMES];
  969. char end_phonemes2[N_WORD_PHONEMES];
  970. // remove any standard suffix and confirm that the prefix is still recognised
  971. phonemes2[0] = 0;
  972. end2 = TranslateRules(tr, wordx, phonemes2, N_WORD_PHONEMES, end_phonemes2, wflags|FLAG_NO_PREFIX|FLAG_NO_TRACE, dictionary_flags);
  973. if(end2)
  974. {
  975. RemoveEnding(tr, wordx, end2, word_copy);
  976. end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, wflags|FLAG_NO_TRACE, dictionary_flags);
  977. memcpy(wordx,word_copy,strlen(word_copy));
  978. if((end_type & SUFX_P) == 0)
  979. {
  980. // after removing the suffix, the prefix is no longer recognised.
  981. // Keep the suffix, but don't use the prefix
  982. end_type = end2;
  983. strcpy(phonemes,phonemes2);
  984. strcpy(end_phonemes,end_phonemes2);
  985. if(option_phonemes == 2)
  986. {
  987. DecodePhonemes(end_phonemes,end_phonemes2);
  988. fprintf(f_trans," suffix [%s]\n\n",end_phonemes2);
  989. }
  990. }
  991. confirm_prefix = 0;
  992. continue;
  993. }
  994. }
  995. prefix_type = end_type;
  996. if(prefix_type & SUFX_V)
  997. {
  998. tr->expect_verb = 1; // use the verb form of the word
  999. }
  1000. wordx[-1] = c_temp;
  1001. if((prefix_type & SUFX_B) == 0)
  1002. {
  1003. for(ix=(prefix_type & 0xf); ix>0; ix--) // num. of characters to remove
  1004. {
  1005. wordx++;
  1006. while((*wordx & 0xc0) == 0x80) wordx++; // for multibyte characters
  1007. }
  1008. }
  1009. else
  1010. {
  1011. pfix = 1;
  1012. prefix_chars[0] = 0;
  1013. n_chars = prefix_type & 0x3f;
  1014. for(ix=0; ix < n_chars; ix++) // num. of bytes to remove
  1015. {
  1016. prefix_chars[pfix++] = *wordx++;
  1017. if((prefix_type & SUFX_B) && (ix == (n_chars-1)))
  1018. {
  1019. prefix_chars[pfix-1] = 0; // discard the last character of the prefix, this is the separator character
  1020. }
  1021. }
  1022. prefix_chars[pfix] = 0;
  1023. }
  1024. c_temp = wordx[-1];
  1025. wordx[-1] = ' ';
  1026. confirm_prefix = 1;
  1027. if(prefix_type & SUFX_B)
  1028. {
  1029. // SUFX_B is used for Turkish, tr_rules contains " ' (Pb"
  1030. // examine the prefix part
  1031. char *wordpf;
  1032. char prefix_phonemes2[12];
  1033. strncpy0(prefix_phonemes2,end_phonemes,sizeof(prefix_phonemes2));
  1034. wordpf = &prefix_chars[1];
  1035. strcpy(prefix_phonemes, phonemes);
  1036. // look for stress marker or $abbrev
  1037. found = LookupDictList(tr, &wordpf, phonemes, dictionary_flags, 0, wtab);
  1038. if(found)
  1039. {
  1040. strcpy(prefix_phonemes, phonemes);
  1041. }
  1042. if(dictionary_flags[0] & FLAG_ABBREV)
  1043. {
  1044. prefix_phonemes[0] = 0;
  1045. SpeakIndividualLetters(tr, wordpf, prefix_phonemes, 1);
  1046. }
  1047. }
  1048. else
  1049. {
  1050. strcat(prefix_phonemes,end_phonemes);
  1051. }
  1052. end_phonemes[0] = 0;
  1053. end_type = 0;
  1054. found = LookupDictList(tr, &wordx, phonemes, dictionary_flags2, SUFX_P, wtab); // without prefix
  1055. if(dictionary_flags[0]==0)
  1056. {
  1057. dictionary_flags[0] = dictionary_flags2[0];
  1058. dictionary_flags[1] = dictionary_flags2[1];
  1059. }
  1060. else
  1061. prefix_flags = 1;
  1062. if(found == 0)
  1063. {
  1064. end_type = TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, end_phonemes, 0, dictionary_flags);
  1065. if(phonemes[0] == phonSWITCH)
  1066. {
  1067. // change to another language in order to translate this word
  1068. wordx[-1] = c_temp;
  1069. strcpy(word_phonemes,phonemes);
  1070. return(0);
  1071. }
  1072. }
  1073. }
  1074. if((end_type != 0) && !(end_type & SUFX_P))
  1075. {
  1076. char phonemes2[N_WORD_PHONEMES];
  1077. strcpy(phonemes2,phonemes);
  1078. // The word has a standard ending, re-translate without this ending
  1079. end_flags = RemoveEnding(tr, wordx, end_type, word_copy);
  1080. phonemes[0] = 0;
  1081. if(prefix_phonemes[0] != 0)
  1082. {
  1083. // lookup the stem without the prefix removed
  1084. wordx[-1] = c_temp;
  1085. found = LookupDictList(tr, &word1, phonemes, dictionary_flags2, end_flags, wtab); // include prefix, but not suffix
  1086. wordx[-1] = ' ';
  1087. if(phonemes[0] == phonSWITCH)
  1088. {
  1089. // change to another language in order to translate this word
  1090. memcpy(wordx,word_copy,strlen(word_copy));
  1091. strcpy(word_phonemes,phonemes);
  1092. return(0);
  1093. }
  1094. if(dictionary_flags[0]==0)
  1095. {
  1096. dictionary_flags[0] = dictionary_flags2[0];
  1097. dictionary_flags[1] = dictionary_flags2[1];
  1098. }
  1099. if(found)
  1100. prefix_phonemes[0] = 0; // matched whole word, don't need prefix now
  1101. if((found==0) && (dictionary_flags2[0] != 0))
  1102. prefix_flags = 1;
  1103. }
  1104. if(found == 0)
  1105. {
  1106. found = LookupDictList(tr, &wordx, phonemes, dictionary_flags2, end_flags, wtab); // without prefix and suffix
  1107. if(phonemes[0] == phonSWITCH)
  1108. {
  1109. // change to another language in order to translate this word
  1110. memcpy(wordx,word_copy,strlen(word_copy));
  1111. strcpy(word_phonemes,phonemes);
  1112. return(0);
  1113. }
  1114. if(dictionary_flags2[0] & FLAG_ABBREV)
  1115. {
  1116. // Removing the suffix leaves a word which should be spoken as individual letters
  1117. // Not yet implemented
  1118. }
  1119. if(dictionary_flags[0]==0)
  1120. {
  1121. dictionary_flags[0] = dictionary_flags2[0];
  1122. dictionary_flags[1] = dictionary_flags2[1];
  1123. }
  1124. }
  1125. if(found == 0)
  1126. {
  1127. if(end_type & SUFX_Q)
  1128. {
  1129. // don't retranslate, use the original lookup result
  1130. strcpy(phonemes,phonemes2);
  1131. // language specific changes
  1132. ApplySpecialAttribute(tr,phonemes,dictionary_flags[0]);
  1133. }
  1134. else
  1135. {
  1136. if(end_flags & FLAG_SUFX)
  1137. wflags |= FLAG_SUFFIX_REMOVED;
  1138. if(end_type & SUFX_A)
  1139. wflags |= FLAG_SUFFIX_VOWEL;
  1140. TranslateRules(tr, wordx, phonemes, N_WORD_PHONEMES, NULL, wflags, dictionary_flags);
  1141. if(phonemes[0] == phonSWITCH)
  1142. {
  1143. // change to another language in order to translate this word
  1144. strcpy(word_phonemes,phonemes);
  1145. memcpy(wordx,word_copy,strlen(word_copy));
  1146. wordx[-1] = c_temp;
  1147. return(0);
  1148. }
  1149. }
  1150. }
  1151. if((end_type & SUFX_T) == 0)
  1152. {
  1153. // the default is to add the suffix and then determine the word's stress pattern
  1154. AppendPhonemes(tr,phonemes, N_WORD_PHONEMES, end_phonemes);
  1155. end_phonemes[0] = 0;
  1156. }
  1157. memcpy(wordx,word_copy,strlen(word_copy));
  1158. }
  1159. wordx[-1] = c_temp;
  1160. }
  1161. }
  1162. if((add_plural_suffix) || (wflags & FLAG_HAS_PLURAL))
  1163. {
  1164. // s or 's suffix, append [s], [z] or [Iz] depending on previous letter
  1165. if(last_char == 'f')
  1166. TranslateRules(tr, &word_ss[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
  1167. else
  1168. if((last_char==0) || (strchr_w("hsx",last_char)==NULL))
  1169. TranslateRules(tr, &word_zz[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
  1170. else
  1171. TranslateRules(tr, &word_iz[1], phonemes, N_WORD_PHONEMES, NULL, 0, NULL);
  1172. }
  1173. wflags |= emphasize_allcaps;
  1174. /* determine stress pattern for this word */
  1175. /******************************************/
  1176. prefix_stress = 0;
  1177. for(p = prefix_phonemes; *p != 0; p++)
  1178. {
  1179. if((*p == phonSTRESS_P) || (*p == phonSTRESS_P2))
  1180. {
  1181. prefix_stress = *p;
  1182. }
  1183. }
  1184. if(prefix_flags || (prefix_stress != 0))
  1185. {
  1186. if((tr->langopts.param[LOPT_PREFIXES]) || (prefix_type & SUFX_T))
  1187. {
  1188. char *p;
  1189. // German, keep a secondary stress on the stem
  1190. SetWordStress(tr, phonemes, dictionary_flags, 3, 0);
  1191. // reduce all but the first primary stress
  1192. ix=0;
  1193. for(p=prefix_phonemes; *p != 0; p++)
  1194. {
  1195. if(*p == phonSTRESS_P)
  1196. {
  1197. if(ix==0)
  1198. ix=1;
  1199. else
  1200. *p = phonSTRESS_3;
  1201. }
  1202. }
  1203. #ifdef PLATFORM_WINDOWS
  1204. sprintf(word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1205. #else
  1206. snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1207. #endif
  1208. word_phonemes[N_WORD_PHONEMES-1] = 0;
  1209. SetWordStress(tr, word_phonemes, dictionary_flags, -1, 0);
  1210. }
  1211. else
  1212. {
  1213. // stress position affects the whole word, including prefix
  1214. #ifdef PLATFORM_WINDOWS
  1215. sprintf(word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1216. #else
  1217. snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1218. #endif
  1219. word_phonemes[N_WORD_PHONEMES-1] = 0;
  1220. SetWordStress(tr, word_phonemes, dictionary_flags, -1, 0);
  1221. }
  1222. }
  1223. else
  1224. {
  1225. if(prefix_phonemes[0] == 0)
  1226. SetWordStress(tr, phonemes, dictionary_flags, -1, 0);
  1227. else
  1228. SetWordStress(tr, phonemes, dictionary_flags, -1, 0);
  1229. #ifdef PLATFORM_WINDOWS
  1230. sprintf(word_phonemes, "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1231. #else
  1232. snprintf(word_phonemes, sizeof(word_phonemes), "%s%s%s", unpron_phonemes, prefix_phonemes, phonemes);
  1233. #endif
  1234. word_phonemes[N_WORD_PHONEMES-1] = 0;
  1235. }
  1236. if(end_phonemes[0] != 0)
  1237. {
  1238. // a suffix had the SUFX_T option set, add the suffix after the stress pattern has been determined
  1239. ix = strlen(word_phonemes);
  1240. end_phonemes[N_WORD_PHONEMES-1-ix] = 0; // ensure no buffer overflow
  1241. strcpy(&word_phonemes[ix], end_phonemes);
  1242. }
  1243. if(wflags & FLAG_LAST_WORD)
  1244. {
  1245. // don't use $brk pause before the last word of a sentence
  1246. // (but allow it for emphasis, see below
  1247. dictionary_flags[0] &= ~FLAG_PAUSE1;
  1248. }
  1249. #ifdef deleted
  1250. // but it causes problems if these are not a person name
  1251. if(tr->translator_name == L('h','u'))
  1252. {
  1253. // lang=hu, If the last two words of a clause have capital letters (eg. a person name), unstress the last word.
  1254. if((wflags & (FLAG_LAST_WORD | FLAG_FIRST_UPPER | FLAG_ALL_UPPER | FLAG_FIRST_WORD)) == (FLAG_LAST_WORD | FLAG_FIRST_UPPER))
  1255. {
  1256. if(((wtab[-1].flags & (FLAG_FIRST_UPPER | FLAG_ALL_UPPER)) == FLAG_FIRST_UPPER) && ((tr->clause_terminator != 0x90028) || (wflags & FLAG_HAS_DOT)))
  1257. {
  1258. ChangeWordStress(tr,word_phonemes,3);
  1259. }
  1260. }
  1261. }
  1262. #endif
  1263. if((wflags & FLAG_HYPHEN) && (tr->langopts.stress_flags & S_HYPEN_UNSTRESS))
  1264. {
  1265. ChangeWordStress(tr,word_phonemes,3);
  1266. }
  1267. else
  1268. if(wflags & FLAG_EMPHASIZED2)
  1269. {
  1270. // A word is indicated in the source text as stressed
  1271. // Give it stress level 6 (for the intonation module)
  1272. ChangeWordStress(tr,word_phonemes,6);
  1273. if(wflags & FLAG_EMPHASIZED)
  1274. dictionary_flags[0] |= FLAG_PAUSE1; // precede by short pause
  1275. }
  1276. else
  1277. if(wtab[dictionary_skipwords].flags & FLAG_LAST_WORD)
  1278. {
  1279. // the word has attribute to stress or unstress when at end of clause
  1280. if(dictionary_flags[0] & (FLAG_STRESS_END | FLAG_STRESS_END2))
  1281. ChangeWordStress(tr,word_phonemes,4);
  1282. else
  1283. if((dictionary_flags[0] & FLAG_UNSTRESS_END) && (any_stressed_words))
  1284. ChangeWordStress(tr,word_phonemes,3);
  1285. }
  1286. // dictionary flags for this word give a clue about which alternative pronunciations of
  1287. // following words to use.
  1288. if(end_type & SUFX_F)
  1289. {
  1290. // expect a verb form, with or without -s suffix
  1291. tr->expect_verb = 2;
  1292. tr->expect_verb_s = 2;
  1293. }
  1294. if(dictionary_flags[1] & FLAG_PASTF)
  1295. {
  1296. /* expect perfect tense in next two words */
  1297. tr->expect_past = 3;
  1298. tr->expect_verb = 0;
  1299. tr->expect_noun = 0;
  1300. }
  1301. else
  1302. if(dictionary_flags[1] & FLAG_VERBF)
  1303. {
  1304. /* expect a verb in the next word */
  1305. tr->expect_verb = 2;
  1306. tr->expect_verb_s = 0; /* verb won't have -s suffix */
  1307. tr->expect_noun = 0;
  1308. }
  1309. else
  1310. if(dictionary_flags[1] & FLAG_VERBSF)
  1311. {
  1312. // expect a verb, must have a -s suffix
  1313. tr->expect_verb = 0;
  1314. tr->expect_verb_s = 2;
  1315. tr->expect_past = 0;
  1316. tr->expect_noun = 0;
  1317. }
  1318. else
  1319. if(dictionary_flags[1] & FLAG_NOUNF)
  1320. {
  1321. /* not expecting a verb next */
  1322. tr->expect_noun = 2;
  1323. tr->expect_verb = 0;
  1324. tr->expect_verb_s = 0;
  1325. tr->expect_past = 0;
  1326. }
  1327. if((wordx[0] != 0) && (!(dictionary_flags[1] & FLAG_VERB_EXT)))
  1328. {
  1329. if(tr->expect_verb > 0)
  1330. tr->expect_verb--;
  1331. if(tr->expect_verb_s > 0)
  1332. tr->expect_verb_s--;
  1333. if(tr->expect_noun >0)
  1334. tr->expect_noun--;
  1335. if(tr->expect_past > 0)
  1336. tr->expect_past--;
  1337. }
  1338. if((word_length == 1) && (tr->translator_name == L('e','n')) && iswalpha(first_char) && (first_char != 'i'))
  1339. {
  1340. // English Specific !!!!
  1341. // any single letter before a dot is an abbreviation, except 'I'
  1342. dictionary_flags[0] |= FLAG_ALLOW_DOT;
  1343. }
  1344. if((tr->langopts.param[LOPT_ALT] & 2) && ((dictionary_flags[0] & (FLAG_ALT_TRANS | FLAG_ALT2_TRANS)) != 0))
  1345. {
  1346. ApplySpecialAttribute2(tr,word_phonemes,dictionary_flags[0]);
  1347. }
  1348. dictionary_flags[0] |= was_unpronouncable;
  1349. memcpy(word_start, word_copy2, word_copy_length);
  1350. return(dictionary_flags[0]);
  1351. } // end of TranslateWord
  1352. static void SetPlist2(PHONEME_LIST2 *p, unsigned char phcode)
  1353. {//==========================================================
  1354. p->phcode = phcode;
  1355. p->stresslevel = 0;
  1356. p->tone_ph = 0;
  1357. p->synthflags = embedded_flag;
  1358. p->sourceix = 0;
  1359. embedded_flag = 0;
  1360. }
  1361. static int CountSyllables(unsigned char *phonemes)
  1362. {//===============================================
  1363. int count = 0;
  1364. int phon;
  1365. while((phon = *phonemes++) != 0)
  1366. {
  1367. if(phoneme_tab[phon]->type == phVOWEL)
  1368. count++;
  1369. }
  1370. return(count);
  1371. }
  1372. void Word_EmbeddedCmd()
  1373. {//====================
  1374. // Process embedded commands for emphasis, sayas, and break
  1375. int embedded_cmd;
  1376. int value;
  1377. do
  1378. {
  1379. embedded_cmd = embedded_list[embedded_read++];
  1380. value = embedded_cmd >> 8;
  1381. switch(embedded_cmd & 0x1f)
  1382. {
  1383. case EMBED_Y:
  1384. option_sayas = value;
  1385. break;
  1386. case EMBED_F:
  1387. option_emphasis = value;
  1388. break;
  1389. case EMBED_B:
  1390. // break command
  1391. if(value == 0)
  1392. pre_pause = 0; // break=none
  1393. else
  1394. pre_pause += value;
  1395. break;
  1396. }
  1397. } while(((embedded_cmd & 0x80) == 0) && (embedded_read < embedded_ix));
  1398. } // end of Word_EmbeddedCmd
  1399. int SetTranslator2(const char *new_language)
  1400. {//=========================================
  1401. // Set translator2 to a second language
  1402. int new_phoneme_tab;
  1403. if((new_phoneme_tab = SelectPhonemeTableName(new_language)) >= 0)
  1404. {
  1405. if((translator2 != NULL) && (strcmp(new_language,translator2_language) != 0))
  1406. {
  1407. // we already have an alternative translator, but not for the required language, delete it
  1408. DeleteTranslator(translator2);
  1409. translator2 = NULL;
  1410. }
  1411. if(translator2 == NULL)
  1412. {
  1413. translator2 = SelectTranslator(new_language);
  1414. strcpy(translator2_language,new_language);
  1415. if(LoadDictionary(translator2, new_language, 0) != 0)
  1416. {
  1417. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  1418. new_phoneme_tab = -1;
  1419. translator2_language[0] = 0;
  1420. }
  1421. }
  1422. }
  1423. return(new_phoneme_tab);
  1424. } // end of SetTranslator2
  1425. static int TranslateWord2(Translator *tr, char *word, WORD_TAB *wtab, int pre_pause, int next_pause)
  1426. {//=================================================================================================
  1427. int flags=0;
  1428. int stress;
  1429. int next_stress;
  1430. int next_tone=0;
  1431. unsigned char *p;
  1432. int srcix;
  1433. int found_dict_flag;
  1434. unsigned char ph_code;
  1435. PHONEME_LIST2 *plist2;
  1436. PHONEME_TAB *ph;
  1437. int max_stress;
  1438. int max_stress_ix=0;
  1439. int prev_vowel = -1;
  1440. int pitch_raised = 0;
  1441. int switch_phonemes = -1;
  1442. int first_phoneme = 1;
  1443. int source_ix;
  1444. int len;
  1445. int ix;
  1446. int sylimit; // max. number of syllables in a word to be combined with a preceding preposition
  1447. const char *new_language;
  1448. unsigned char bad_phoneme[4];
  1449. int word_flags;
  1450. int word_copy_len;
  1451. char word_copy[N_WORD_BYTES+1];
  1452. char old_dictionary_name[40];
  1453. if((f_logespeak != NULL) && (logging_type & 8))
  1454. {
  1455. fprintf(f_logespeak,"WORD: flg=%.5x len=%d '",wtab->flags,wtab->length);
  1456. for(ix=0; ix<40; ix++)
  1457. {
  1458. if(word[ix]==0) break;
  1459. fputc(word[ix], f_logespeak);
  1460. }
  1461. fprintf(f_logespeak,"'\n");
  1462. }
  1463. len = wtab->length;
  1464. if(len > 31) len = 31;
  1465. source_ix = (wtab->sourceix & 0x7ff) | (len << 11); // bits 0-10 sourceix, bits 11-15 word length
  1466. word_flags = wtab[0].flags;
  1467. if(word_flags & FLAG_EMBEDDED)
  1468. {
  1469. embedded_flag = SFLAG_EMBEDDED;
  1470. Word_EmbeddedCmd();
  1471. }
  1472. if((word[0] == 0) || (word_flags & FLAG_DELETE_WORD))
  1473. {
  1474. // nothing to translate. Add a dummy phoneme to carry any embedded commands
  1475. if(embedded_flag)
  1476. {
  1477. ph_list2[n_ph_list2].phcode = phonEND_WORD;
  1478. ph_list2[n_ph_list2].stresslevel = 0;
  1479. ph_list2[n_ph_list2].wordstress = 0;
  1480. ph_list2[n_ph_list2].tone_ph = 0;
  1481. ph_list2[n_ph_list2].synthflags = embedded_flag;
  1482. ph_list2[n_ph_list2].sourceix = 0;
  1483. n_ph_list2++;
  1484. embedded_flag = 0;
  1485. }
  1486. word_phonemes[0] = 0;
  1487. return(0);
  1488. }
  1489. // after a $pause word attribute, ignore a $pause attribute on the next two words
  1490. if(tr->prepause_timeout > 0)
  1491. tr->prepause_timeout--;
  1492. if((option_sayas & 0xf0) == 0x10)
  1493. {
  1494. if(!(word_flags & FLAG_FIRST_WORD))
  1495. {
  1496. // SAYAS_CHARS, SAYAS_GLYPHS, or SAYAS_SINGLECHARS. Pause between each word.
  1497. pre_pause += 4;
  1498. }
  1499. }
  1500. if(word_flags & FLAG_FIRST_UPPER)
  1501. {
  1502. if((option_capitals > 2) && (embedded_ix < N_EMBEDDED_LIST-6))
  1503. {
  1504. // indicate capital letter by raising pitch
  1505. if(embedded_flag)
  1506. embedded_list[embedded_ix-1] &= ~0x80; // already embedded command before this word, remove terminator
  1507. if((pitch_raised = option_capitals) == 3)
  1508. pitch_raised = 20; // default pitch raise for capitals
  1509. embedded_list[embedded_ix++] = EMBED_P+0x40+0x80 + (pitch_raised << 8); // raise pitch
  1510. embedded_flag = SFLAG_EMBEDDED;
  1511. }
  1512. }
  1513. p = (unsigned char *)word_phonemes;
  1514. if(word_flags & FLAG_PHONEMES)
  1515. {
  1516. // The input is in phoneme mnemonics, not language text
  1517. int c1;
  1518. char lang_name[12];
  1519. if(memcmp(word,"_^_",3)==0)
  1520. {
  1521. // switch languages
  1522. word+=3;
  1523. for(ix=0;;)
  1524. {
  1525. c1 = *word++;
  1526. if((c1==' ') || (c1==0))
  1527. break;
  1528. lang_name[ix++] = tolower(c1);
  1529. }
  1530. lang_name[ix] = 0;
  1531. if((ix = LookupPhonemeTable(lang_name)) > 0)
  1532. {
  1533. SelectPhonemeTable(ix);
  1534. word_phonemes[0] = phonSWITCH;
  1535. word_phonemes[1] = ix;
  1536. word_phonemes[2] = 0;
  1537. }
  1538. }
  1539. else
  1540. {
  1541. EncodePhonemes(word,word_phonemes,bad_phoneme);
  1542. }
  1543. flags = FLAG_FOUND;
  1544. }
  1545. else
  1546. {
  1547. int c2;
  1548. ix = 0;
  1549. while(((c2 = word_copy[ix] = word[ix]) != ' ') && (c2 != 0) && (ix < N_WORD_BYTES)) ix++;
  1550. word_copy_len = ix;
  1551. flags = TranslateWord(translator, word, next_pause, wtab);
  1552. if(flags & FLAG_SPELLWORD)
  1553. {
  1554. // re-translate the word as individual letters, separated by spaces
  1555. memcpy(word, word_copy, word_copy_len);
  1556. return(flags);
  1557. }
  1558. if((flags & FLAG_COMBINE) && !(wtab[1].flags & FLAG_PHONEMES))
  1559. {
  1560. char *p2;
  1561. int ok = 1;
  1562. unsigned int flags2[2];
  1563. int c_word2;
  1564. char ph_buf[N_WORD_PHONEMES];
  1565. flags2[0] = 0;
  1566. sylimit = tr->langopts.param[LOPT_COMBINE_WORDS];
  1567. // LANG=cs,sk
  1568. // combine a preposition with the following word
  1569. p2 = word;
  1570. while(*p2 != ' ') p2++;
  1571. utf8_in(&c_word2, p2+1); // first character of the next word;
  1572. if(!iswalpha(c_word2))
  1573. {
  1574. ok =0;
  1575. }
  1576. if(ok != 0)
  1577. {
  1578. strcpy(ph_buf,word_phonemes);
  1579. flags2[0] = TranslateWord(translator, p2+1, 0, wtab+1);
  1580. if((flags2[0] & FLAG_WAS_UNPRONOUNCABLE) || (word_phonemes[0] == phonSWITCH))
  1581. ok = 0;
  1582. if(sylimit & 0x100)
  1583. {
  1584. // only if the second word has $alt attribute
  1585. if((flags2[0] & FLAG_ALT_TRANS) == 0)
  1586. {
  1587. ok = 0;
  1588. }
  1589. }
  1590. if((sylimit & 0x200) && ((wtab+1)->flags & FLAG_LAST_WORD))
  1591. {
  1592. // not if the next word is end-of-sentence
  1593. ok = 0;
  1594. }
  1595. if(ok == 0)
  1596. {
  1597. strcpy(word_phonemes,ph_buf);
  1598. }
  1599. }
  1600. if(ok)
  1601. {
  1602. *p2 = '-'; // replace next space by hyphen
  1603. flags = TranslateWord(translator, word, next_pause, wtab); // translate the combined word
  1604. if((sylimit > 0) && (CountSyllables(p) > (sylimit & 0x1f)))
  1605. {
  1606. // revert to separate words
  1607. *p2 = ' ';
  1608. flags = TranslateWord(translator, word, next_pause, wtab);
  1609. }
  1610. else
  1611. {
  1612. if(flags == 0)
  1613. flags = flags2[0]; // no flags for the combined word, so use flags from the second word eg. lang-hu "nem december 7-e"
  1614. flags |= FLAG_SKIPWORDS;
  1615. dictionary_skipwords = 1;
  1616. }
  1617. }
  1618. }
  1619. if(p[0] == phonSWITCH)
  1620. {
  1621. // this word uses a different language
  1622. memcpy(word, word_copy, word_copy_len);
  1623. new_language = (char *)(&p[1]);
  1624. if(new_language[0]==0)
  1625. new_language = "en";
  1626. strcpy(old_dictionary_name, dictionary_name);
  1627. switch_phonemes = SetTranslator2(new_language);
  1628. if(switch_phonemes >= 0)
  1629. {
  1630. // re-translate the word using the new translator
  1631. flags = TranslateWord(translator2, word, next_pause, wtab);
  1632. // strcpy((char *)p,translator2->word_phonemes);
  1633. if(p[0] == phonSWITCH)
  1634. {
  1635. // the second translator doesn't want to process this word
  1636. switch_phonemes = -1;
  1637. }
  1638. }
  1639. if(switch_phonemes < 0)
  1640. {
  1641. // language code is not recognised or 2nd translator won't translate it
  1642. p[0] = phonSCHWA; // just say something
  1643. p[1] = phonSCHWA;
  1644. p[2] = 0;
  1645. }
  1646. // ?? Option to convert from language2 phonemes to the equivalent language1 phonemes
  1647. // ?? Option to set the word-stress according to language1 rules eg. lang=fr)
  1648. if(ChangeEquivalentPhonemes(tr, switch_phonemes, (char *)p))
  1649. {
  1650. // Phonemes have been converted from the foreign language to the native language
  1651. switch_phonemes = -1;
  1652. }
  1653. if(switch_phonemes == -1)
  1654. {
  1655. strcpy(dictionary_name, old_dictionary_name);
  1656. SelectPhonemeTable(voice->phoneme_tab_ix);
  1657. // leave switch_phonemes set, but use the original phoneme table number.
  1658. // This will suppress LOPT_REGRESSIVE_VOICING
  1659. switch_phonemes = voice->phoneme_tab_ix; // original phoneme table
  1660. }
  1661. }
  1662. if(!(word_flags & FLAG_HYPHEN))
  1663. {
  1664. if(flags & FLAG_PAUSE1)
  1665. {
  1666. if(pre_pause < 1)
  1667. pre_pause = 1;
  1668. }
  1669. if((flags & FLAG_PREPAUSE) && !(word_flags && (FLAG_LAST_WORD | FLAG_FIRST_WORD)) && !(wtab[-1].flags & FLAG_FIRST_WORD) && (tr->prepause_timeout == 0))
  1670. {
  1671. // the word is marked in the dictionary list with $pause
  1672. if(pre_pause < 4) pre_pause = 4;
  1673. tr->prepause_timeout = 3;
  1674. }
  1675. }
  1676. if((option_emphasis >= 3) && (pre_pause < 1))
  1677. pre_pause = 1;
  1678. }
  1679. plist2 = &ph_list2[n_ph_list2];
  1680. stress = 0;
  1681. next_stress = 1;
  1682. srcix = 0;
  1683. max_stress = -1;
  1684. found_dict_flag = 0;
  1685. if((flags & FLAG_FOUND) && !(flags & FLAG_TEXTMODE))
  1686. found_dict_flag = SFLAG_DICTIONARY;
  1687. while((pre_pause > 0) && (n_ph_list2 < N_PHONEME_LIST-4))
  1688. {
  1689. // add pause phonemes here. Either because of punctuation (brackets or quotes) in the
  1690. // text, or because the word is marked in the dictionary lookup as a conjunction
  1691. if(pre_pause > 1)
  1692. {
  1693. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE);
  1694. pre_pause -= 2;
  1695. }
  1696. else
  1697. {
  1698. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_NOLINK);
  1699. pre_pause--;
  1700. }
  1701. tr->end_stressed_vowel = 0; // forget about the previous word
  1702. tr->prev_dict_flags = 0;
  1703. }
  1704. if((option_capitals==1) && (word_flags & FLAG_FIRST_UPPER))
  1705. {
  1706. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_SHORT);
  1707. SetPlist2(&ph_list2[n_ph_list2++],phonCAPITAL);
  1708. if((word_flags & FLAG_ALL_UPPER) && IsAlpha(word[1]))
  1709. {
  1710. // word > 1 letter and all capitals
  1711. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_SHORT);
  1712. SetPlist2(&ph_list2[n_ph_list2++],phonCAPITAL);
  1713. }
  1714. }
  1715. if(switch_phonemes >= 0)
  1716. {
  1717. // this word uses a different phoneme table
  1718. SetPlist2(&ph_list2[n_ph_list2],phonSWITCH);
  1719. ph_list2[n_ph_list2++].tone_ph = switch_phonemes; // temporary phoneme table number
  1720. }
  1721. // remove initial pause from a word if it follows a hyphen
  1722. if((word_flags & FLAG_HYPHEN) && (phoneme_tab[*p]->type == phPAUSE))
  1723. p++;
  1724. if((p[0] == 0) && (embedded_flag))
  1725. {
  1726. // no phonemes. Insert a very short pause to carry an embedded command
  1727. p[0] = phonPAUSE_VSHORT;
  1728. p[1] = 0;
  1729. }
  1730. while(((ph_code = *p++) != 0) && (n_ph_list2 < N_PHONEME_LIST-4))
  1731. {
  1732. if(ph_code == 255)
  1733. continue; // unknown phoneme
  1734. // Add the phonemes to the first stage phoneme list (ph_list2)
  1735. ph = phoneme_tab[ph_code];
  1736. if(ph_code == phonSWITCH)
  1737. {
  1738. ph_list2[n_ph_list2].phcode = ph_code;
  1739. ph_list2[n_ph_list2].sourceix = 0;
  1740. ph_list2[n_ph_list2].synthflags = embedded_flag;
  1741. ph_list2[n_ph_list2++].tone_ph = *p++;
  1742. }
  1743. else
  1744. if(ph->type == phSTRESS)
  1745. {
  1746. // don't add stress phonemes codes to the list, but give their stress
  1747. // value to the next vowel phoneme
  1748. // std_length is used to hold stress number or (if >10) a tone number for a tone language
  1749. if(ph->program == 0)
  1750. next_stress = ph->std_length;
  1751. else
  1752. {
  1753. // for tone languages, the tone number for a syllable follows the vowel
  1754. if(prev_vowel >= 0)
  1755. {
  1756. ph_list2[prev_vowel].tone_ph = ph_code;
  1757. }
  1758. else
  1759. {
  1760. next_tone = ph_code; // no previous vowel, apply to the next vowel
  1761. }
  1762. }
  1763. }
  1764. else
  1765. if(ph_code == phonSYLLABIC)
  1766. {
  1767. // mark the previous phoneme as a syllabic consonant
  1768. prev_vowel = n_ph_list2-1;
  1769. ph_list2[prev_vowel].synthflags |= SFLAG_SYLLABLE;
  1770. ph_list2[prev_vowel].stresslevel = next_stress;
  1771. }
  1772. else
  1773. if(ph_code == phonLENGTHEN)
  1774. {
  1775. ph_list2[n_ph_list2-1].synthflags |= SFLAG_LENGTHEN;
  1776. }
  1777. else
  1778. if(ph_code == phonEND_WORD)
  1779. {
  1780. // a || symbol in a phoneme string was used to indicate a word boundary
  1781. // Don't add this phoneme to the list, but make sure the next phoneme has
  1782. // a newword indication
  1783. srcix = source_ix+1;
  1784. }
  1785. else
  1786. if(ph_code == phonX1)
  1787. {
  1788. // a language specific action
  1789. if(tr->langopts.param[LOPT_IT_DOUBLING])
  1790. {
  1791. flags |= FLAG_DOUBLING;
  1792. }
  1793. }
  1794. else
  1795. {
  1796. ph_list2[n_ph_list2].phcode = ph_code;
  1797. ph_list2[n_ph_list2].tone_ph = 0;
  1798. ph_list2[n_ph_list2].synthflags = embedded_flag | found_dict_flag;
  1799. embedded_flag = 0;
  1800. ph_list2[n_ph_list2].sourceix = srcix;
  1801. srcix = 0;
  1802. if(ph->type == phVOWEL)
  1803. {
  1804. stress = next_stress;
  1805. next_stress = 1; // default is 'unstressed'
  1806. if(stress >= 4)
  1807. {
  1808. any_stressed_words = 1;
  1809. }
  1810. if((prev_vowel >= 0) && (n_ph_list2-1) != prev_vowel)
  1811. ph_list2[n_ph_list2-1].stresslevel = stress; // set stress for previous consonant
  1812. ph_list2[n_ph_list2].synthflags |= SFLAG_SYLLABLE;
  1813. prev_vowel = n_ph_list2;
  1814. if(stress > max_stress)
  1815. {
  1816. max_stress = stress;
  1817. max_stress_ix = n_ph_list2;
  1818. }
  1819. if(next_tone != 0)
  1820. {
  1821. ph_list2[n_ph_list2].tone_ph = next_tone;
  1822. next_tone=0;
  1823. }
  1824. }
  1825. else
  1826. {
  1827. if(first_phoneme && tr->langopts.param[LOPT_IT_DOUBLING])
  1828. {
  1829. if(((tr->prev_dict_flags & FLAG_DOUBLING) && (tr->langopts.param[LOPT_IT_DOUBLING] & 1)) ||
  1830. (tr->end_stressed_vowel && (tr->langopts.param[LOPT_IT_DOUBLING] & 2)))
  1831. {
  1832. // italian, double the initial consonant if the previous word ends with a
  1833. // stressed vowel, or is marked with a flag
  1834. ph_list2[n_ph_list2].synthflags |= SFLAG_LENGTHEN;
  1835. }
  1836. }
  1837. }
  1838. ph_list2[n_ph_list2].stresslevel = stress;
  1839. n_ph_list2++;
  1840. first_phoneme = 0;
  1841. }
  1842. }
  1843. if(word_flags & FLAG_COMMA_AFTER)
  1844. {
  1845. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_CLAUSE);
  1846. }
  1847. // don't set new-word if there is a hyphen before it
  1848. if((word_flags & FLAG_HYPHEN) == 0)
  1849. {
  1850. plist2->sourceix = source_ix;
  1851. }
  1852. tr->end_stressed_vowel = 0;
  1853. if((stress >= 4) && (phoneme_tab[ph_list2[n_ph_list2-1].phcode]->type == phVOWEL))
  1854. {
  1855. tr->end_stressed_vowel = 1; // word ends with a stressed vowel
  1856. }
  1857. if(switch_phonemes >= 0)
  1858. {
  1859. // this word uses a different phoneme table, now switch back
  1860. strcpy(dictionary_name, old_dictionary_name);
  1861. SelectPhonemeTable(voice->phoneme_tab_ix);
  1862. SetPlist2(&ph_list2[n_ph_list2],phonSWITCH);
  1863. ph_list2[n_ph_list2++].tone_ph = voice->phoneme_tab_ix; // original phoneme table number
  1864. }
  1865. if(pitch_raised > 0)
  1866. {
  1867. embedded_list[embedded_ix++] = EMBED_P+0x60+0x80 + (pitch_raised << 8); // lower pitch
  1868. SetPlist2(&ph_list2[n_ph_list2],phonPAUSE_SHORT);
  1869. ph_list2[n_ph_list2++].synthflags = SFLAG_EMBEDDED;
  1870. }
  1871. if(flags & FLAG_STRESS_END2)
  1872. {
  1873. // this's word's stress could be increased later
  1874. ph_list2[max_stress_ix].synthflags |= SFLAG_PROMOTE_STRESS;
  1875. }
  1876. tr->prev_dict_flags = flags;
  1877. return(flags);
  1878. } // end of TranslateWord2
  1879. static int EmbeddedCommand(unsigned int &source_index)
  1880. {//===================================================
  1881. // An embedded command to change the pitch, volume, etc.
  1882. // returns number of commands added to embedded_list
  1883. // pitch,speed,amplitude,expression,reverb,tone,voice,sayas
  1884. const char *commands = "PSARHTIVYMUBF";
  1885. int value = -1;
  1886. int sign = 0;
  1887. unsigned char c;
  1888. char *p;
  1889. int cmd;
  1890. c = source[source_index];
  1891. if(c == '+')
  1892. {
  1893. sign = 0x40;
  1894. source_index++;
  1895. }
  1896. else
  1897. if(c == '-')
  1898. {
  1899. sign = 0x60;
  1900. source_index++;
  1901. }
  1902. if(isdigit(source[source_index]))
  1903. {
  1904. value = atoi(&source[source_index]);
  1905. while(isdigit(source[source_index]))
  1906. source_index++;
  1907. }
  1908. c = source[source_index++];
  1909. if(embedded_ix >= (N_EMBEDDED_LIST - 2))
  1910. return(0); // list is full
  1911. if((p = strchr_w(commands,c)) == NULL)
  1912. return(0);
  1913. cmd = (p - commands)+1;
  1914. if(value == -1)
  1915. {
  1916. value = embedded_default[cmd];
  1917. sign = 0;
  1918. }
  1919. if(cmd == EMBED_Y)
  1920. {
  1921. option_sayas2 = value;
  1922. count_sayas_digits = 0;
  1923. }
  1924. if(cmd == EMBED_F)
  1925. {
  1926. if(value >= 3)
  1927. word_emphasis = FLAG_EMPHASIZED;
  1928. else
  1929. word_emphasis = 0;
  1930. }
  1931. embedded_list[embedded_ix++] = cmd + sign + (value << 8);
  1932. return(1);
  1933. } // end of EmbeddedCommand
  1934. static int SubstituteChar(Translator *tr, unsigned int c, unsigned int next_in, int *insert)
  1935. {//=========================================================================================
  1936. int ix;
  1937. unsigned int word;
  1938. unsigned int new_c, c2, c_lower;
  1939. int upper_case = 0;
  1940. static int ignore_next = 0;
  1941. const unsigned int *replace_chars;
  1942. if(ignore_next)
  1943. {
  1944. ignore_next = 0;
  1945. return(8);
  1946. }
  1947. if(c == 0) return(0);
  1948. if((replace_chars = tr->langopts.replace_chars) == NULL)
  1949. return(c);
  1950. // there is a list of character codes to be substituted with alternative codes
  1951. if(iswupper(c_lower = c))
  1952. {
  1953. c_lower = towlower(c);
  1954. upper_case = 1;
  1955. }
  1956. new_c = 0;
  1957. for(ix=0; (word = replace_chars[ix]) != 0; ix+=2)
  1958. {
  1959. if(c_lower == (word & 0xffff))
  1960. {
  1961. if((word >> 16) == 0)
  1962. {
  1963. new_c = replace_chars[ix+1];
  1964. break;
  1965. }
  1966. if((word >> 16) == (unsigned int)towlower(next_in))
  1967. {
  1968. new_c = replace_chars[ix+1];
  1969. ignore_next = 1;
  1970. break;
  1971. }
  1972. }
  1973. }
  1974. if(new_c == 0)
  1975. return(c); // no substitution
  1976. if(new_c & 0xffe00000)
  1977. {
  1978. // there is a second character to be inserted
  1979. // don't convert the case of the second character unless the next letter is also upper case
  1980. c2 = new_c >> 16;
  1981. if(upper_case && iswupper(next_in))
  1982. c2 = towupper(c2);
  1983. *insert = c2;
  1984. new_c &= 0xffff;
  1985. }
  1986. if(upper_case)
  1987. new_c = towupper(new_c);
  1988. return(new_c);
  1989. }
  1990. static int TranslateChar(Translator *tr, char *ptr, int prev_in, unsigned int c, unsigned int next_in, int *insert)
  1991. {//================================================================================================================
  1992. // To allow language specific examination and replacement of characters
  1993. int code;
  1994. int initial;
  1995. int medial;
  1996. int final;
  1997. int next2;
  1998. static const unsigned char hangul_compatibility[0x34] = {
  1999. 0, 0x00,0x01,0xaa,0x02,0xac,0xad,0x03,
  2000. 0x04,0x05,0xb0,0xb1,0xb2,0xb3,0xb4,0xb4,
  2001. 0xb6,0x06,0x07,0x08,0xb9,0x09,0x0a,0xbc,
  2002. 0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x61,
  2003. 0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
  2004. 0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,
  2005. 0x72,0x73,0x74,0x75 };
  2006. switch(tr->translator_name)
  2007. {
  2008. case L('a','f'):
  2009. case L('n','l'):
  2010. // look for 'n and replace by a special character (unicode: schwa)
  2011. if(!iswalpha(prev_in))
  2012. {
  2013. utf8_in(&next2, &ptr[1]);
  2014. if((c == '\'') && IsSpace(next2))
  2015. {
  2016. if((next_in == 'n') && (tr->translator_name == L('a','f')))
  2017. {
  2018. // n preceded by either apostrophe or U2019 "right single quotation mark"
  2019. ptr[0] = ' '; // delete the n
  2020. return(0x0259); // replace ' by unicode schwa character
  2021. }
  2022. if((next_in == 'n') || (next_in == 't'))
  2023. {
  2024. // Dutch, [@n] and [@t]
  2025. return(0x0259); // replace ' by unicode schwa character
  2026. }
  2027. }
  2028. }
  2029. break;
  2030. case L('k','o'):
  2031. if(((code = c - 0xac00) >= 0) && (c <= 0xd7af))
  2032. {
  2033. // break a syllable hangul into 2 or 3 individual jamo
  2034. initial = (code/28)/21;
  2035. medial = (code/28) % 21;
  2036. final = code % 28;
  2037. if(initial == 11)
  2038. {
  2039. // null initial
  2040. c = medial + 0x1161;
  2041. if(final > 0)
  2042. *insert = final + 0x11a7;
  2043. }
  2044. else
  2045. {
  2046. // extact the initial and insert the remainder with a null initial
  2047. c = initial + 0x1100;
  2048. *insert = (11*28*21) + (medial*28) + final + 0xac00;
  2049. }
  2050. return(c);
  2051. }
  2052. else
  2053. if(((code = c - 0x3130) >= 0) && (code < 0x34))
  2054. {
  2055. // Hangul compatibility jamo
  2056. return(hangul_compatibility[code] + 0x1100);
  2057. }
  2058. break;
  2059. }
  2060. return(SubstituteChar(tr,c,next_in,insert));
  2061. }
  2062. void *TranslateClause(Translator *tr, FILE *f_text, const void *vp_input, int *tone_out, char **voice_change)
  2063. {//==========================================================================================================
  2064. int ix;
  2065. int c;
  2066. int cc;
  2067. unsigned int source_index=0;
  2068. unsigned int prev_source_index=0;
  2069. int source_index_word=0;
  2070. int prev_in;
  2071. int prev_out=' ';
  2072. int prev_out2;
  2073. int prev_in_save=0;
  2074. int next_in;
  2075. int next_in_nbytes;
  2076. int char_inserted=0;
  2077. int clause_pause;
  2078. int pre_pause_add=0;
  2079. int word_mark = 0;
  2080. int all_upper_case=FLAG_ALL_UPPER;
  2081. int finished;
  2082. int single_quoted;
  2083. int phoneme_mode = 0;
  2084. int dict_flags = 0; // returned from dictionary lookup
  2085. int word_flags; // set here
  2086. int next_word_flags;
  2087. int embedded_count = 0;
  2088. int letter_count = 0;
  2089. int space_inserted = 0;
  2090. int syllable_marked = 0;
  2091. int decimal_sep_count = 0;
  2092. char *word;
  2093. char *p;
  2094. int j, k;
  2095. int n_digits;
  2096. int charix_top=0;
  2097. short charix[N_TR_SOURCE+4];
  2098. WORD_TAB words[N_CLAUSE_WORDS];
  2099. static char voice_change_name[40];
  2100. int word_count=0; // index into words
  2101. char sbuf[N_TR_SOURCE];
  2102. int terminator;
  2103. int tone;
  2104. int tone2;
  2105. if(tr==NULL)
  2106. {
  2107. return(NULL);
  2108. }
  2109. p_textinput = (unsigned char *)vp_input;
  2110. p_wchar_input = (wchar_t *)vp_input;
  2111. embedded_ix = 0;
  2112. embedded_read = 0;
  2113. pre_pause = 0;
  2114. any_stressed_words = 0;
  2115. if((clause_start_char = count_characters) < 0)
  2116. clause_start_char = 0;
  2117. clause_start_word = count_words + 1;
  2118. for(ix=0; ix<N_TR_SOURCE; ix++)
  2119. charix[ix] = 0;
  2120. terminator = ReadClause(tr, f_text, source, charix, &charix_top, N_TR_SOURCE, &tone2, voice_change_name);
  2121. if((f_logespeak != NULL) && (logging_type & 4))
  2122. {
  2123. fprintf(f_logespeak,"CLAUSE %x:\n",terminator);
  2124. for(p=source; *p != 0; p++)
  2125. fputc(*p, f_logespeak);
  2126. fprintf(f_logespeak,"ENDCLAUSE\n");
  2127. fflush(f_logespeak);
  2128. }
  2129. p = source;
  2130. charix[charix_top+1] = 0;
  2131. charix[charix_top+2] = 0x7fff;
  2132. charix[charix_top+3] = 0;
  2133. clause_pause = (terminator & 0xfff) * 10; // mS
  2134. tone = (terminator >> 12) & 0x7;
  2135. if(tone2 != 0)
  2136. {
  2137. // override the tone type
  2138. tone = tone2;
  2139. }
  2140. for(p=source; *p != 0; p++)
  2141. {
  2142. if(!isspace2(*p))
  2143. {
  2144. break;
  2145. }
  2146. }
  2147. if(*p == 0)
  2148. {
  2149. // No characters except spaces. This is not a sentence.
  2150. // Don't add this pause, just make up the previous pause to this value;
  2151. clause_pause -= max_clause_pause;
  2152. if(clause_pause < 0)
  2153. clause_pause = 0;
  2154. terminator &= ~CLAUSE_BIT_SENTENCE; // clear sentence bit
  2155. max_clause_pause += clause_pause;
  2156. }
  2157. else
  2158. {
  2159. max_clause_pause = clause_pause;
  2160. }
  2161. tr->clause_terminator = terminator;
  2162. if(new_sentence)
  2163. {
  2164. count_sentences++;
  2165. if(skip_sentences > 0)
  2166. {
  2167. skip_sentences--;
  2168. if(skip_sentences == 0)
  2169. skipping_text = 0;
  2170. }
  2171. }
  2172. memset(&ph_list2[0],0,sizeof(ph_list2[0]));
  2173. ph_list2[0].phcode = phonPAUSE_SHORT;
  2174. n_ph_list2 = 1;
  2175. tr->prev_last_stress = 0;
  2176. tr->prepause_timeout = 0;
  2177. tr->expect_verb=0;
  2178. tr->expect_noun=0;
  2179. tr->expect_past=0;
  2180. tr->expect_verb_s=0;
  2181. tr->phonemes_repeat_count = 0;
  2182. tr->end_stressed_vowel=0;
  2183. tr->prev_dict_flags = 0;
  2184. word_count = 0;
  2185. single_quoted = 0;
  2186. word_flags = 0;
  2187. next_word_flags = 0;
  2188. sbuf[0] = 0;
  2189. sbuf[1] = ' ';
  2190. sbuf[2] = ' ';
  2191. ix = 3;
  2192. prev_in = ' ';
  2193. words[0].start = ix;
  2194. words[0].flags = 0;
  2195. finished = 0;
  2196. for(j=0; charix[j]<=0; j++);
  2197. words[0].sourceix = charix[j];
  2198. k = 0;
  2199. while(charix[j] != 0)
  2200. {
  2201. // count the number of characters (excluding multibyte continuation bytes)
  2202. if(charix[j++] != -1)
  2203. k++;
  2204. }
  2205. words[0].length = k;
  2206. while(!finished && (ix < (int)sizeof(sbuf))&& (n_ph_list2 < N_PHONEME_LIST-4))
  2207. {
  2208. prev_out2 = prev_out;
  2209. utf8_in2(&prev_out,&sbuf[ix-1],1); // prev_out = sbuf[ix-1];
  2210. if(tr->langopts.tone_numbers && IsDigit09(prev_out) && IsAlpha(prev_out2))
  2211. {
  2212. // tone numbers can be part of a word, consider them as alphabetic
  2213. prev_out = 'a';
  2214. }
  2215. if(prev_in_save != 0)
  2216. {
  2217. prev_in = prev_in_save;
  2218. prev_in_save = 0;
  2219. }
  2220. else
  2221. if(source_index > 0)
  2222. {
  2223. utf8_in2(&prev_in,&source[source_index-1],1); // prev_in = source[source_index-1];
  2224. }
  2225. prev_source_index = source_index;
  2226. if(char_inserted)
  2227. {
  2228. c = char_inserted;
  2229. char_inserted = 0;
  2230. }
  2231. else
  2232. {
  2233. source_index += utf8_in(&cc,&source[source_index]); // cc = source[source_index++];
  2234. c = cc;
  2235. }
  2236. next_in_nbytes = utf8_in(&next_in,&source[source_index]);
  2237. if(c == 0)
  2238. {
  2239. finished = 1;
  2240. c = ' ';
  2241. }
  2242. if((c == CTRL_EMBEDDED) || (c == ctrl_embedded))
  2243. {
  2244. // start of embedded command in the text
  2245. int srcix = source_index-1;
  2246. if(prev_in != ' ')
  2247. {
  2248. c = ' ';
  2249. prev_in_save = c;
  2250. source_index--;
  2251. }
  2252. else
  2253. {
  2254. embedded_count += EmbeddedCommand(source_index);
  2255. prev_in_save = prev_in;
  2256. // replace the embedded command by spaces
  2257. memset(&source[srcix],' ',source_index-srcix);
  2258. source_index = srcix;
  2259. continue;
  2260. }
  2261. }
  2262. if((option_sayas2 == SAYAS_KEY) && (c != ' '))
  2263. {
  2264. if((prev_in == ' ') && (next_in == ' '))
  2265. option_sayas2 = SAYAS_SINGLE_CHARS; // single character, speak its name
  2266. c = towlower2(c);
  2267. }
  2268. if(phoneme_mode)
  2269. {
  2270. all_upper_case = FLAG_PHONEMES;
  2271. if((c == ']') && (next_in == ']'))
  2272. {
  2273. phoneme_mode = 0;
  2274. source_index++;
  2275. c = ' ';
  2276. }
  2277. }
  2278. else
  2279. if((option_sayas2 & 0xf0) == SAYAS_DIGITS)
  2280. {
  2281. if(iswdigit(c))
  2282. {
  2283. count_sayas_digits++;
  2284. if(count_sayas_digits > (option_sayas2 & 0xf))
  2285. {
  2286. // break after the specified number of digits
  2287. c = ' ';
  2288. space_inserted = 1;
  2289. count_sayas_digits = 0;
  2290. }
  2291. }
  2292. else
  2293. {
  2294. count_sayas_digits = 0;
  2295. if(iswdigit(prev_out))
  2296. {
  2297. c = ' ';
  2298. space_inserted = 1;
  2299. }
  2300. }
  2301. }
  2302. else
  2303. if((option_sayas2 & 0x10) == 0)
  2304. {
  2305. // speak as words
  2306. #ifdef deleted
  2307. if((c == '/') && (tr->langopts.testing & 2) && IsDigit09(next_in) && IsAlpha(prev_out))
  2308. {
  2309. // TESTING, explicit indication of stressed syllable by /2 after the word
  2310. word_mark = next_in-'0';
  2311. source_index++;
  2312. c = ' ';
  2313. }
  2314. #endif
  2315. if((c == 0x92) || (c == 0xb4) || (c == 0x2019) || (c == 0x2032))
  2316. c = '\''; // 'microsoft' quote or sexed closing single quote, or prime - possibly used as apostrophe
  2317. if(((c == 0x2018) || (c == '?')) && IsAlpha(prev_out) && IsAlpha(next_in))
  2318. {
  2319. // ? between two letters may be a smart-quote replaced by ?
  2320. c = '\'';
  2321. }
  2322. if(c == CHAR_EMPHASIS)
  2323. {
  2324. // this character is a marker that the previous word is the focus of the clause
  2325. c = ' ';
  2326. word_flags |= FLAG_FOCUS;
  2327. }
  2328. if(c == CHAR_COMMA_BREAK)
  2329. {
  2330. c = ' ';
  2331. word_flags |= FLAG_COMMA_AFTER;
  2332. }
  2333. c = TranslateChar(tr, &source[source_index], prev_in,c, next_in, &char_inserted); // optional language specific function
  2334. if(c == 8)
  2335. continue; // ignore this character
  2336. if(char_inserted)
  2337. next_in = char_inserted;
  2338. // allow certain punctuation within a word (usually only apostrophe)
  2339. if(!IsAlpha(c) && !IsSpace(c) && (wcschr(tr->punct_within_word,c) == 0))
  2340. {
  2341. if(IsAlpha(prev_out))
  2342. {
  2343. if(tr->langopts.tone_numbers && IsDigit09(c) && !IsDigit09(next_in))
  2344. {
  2345. // allow a tone number as part of the word
  2346. }
  2347. else
  2348. {
  2349. c = ' '; // ensure we have an end-of-word terminator
  2350. space_inserted = 1;
  2351. }
  2352. }
  2353. }
  2354. if(iswdigit(prev_out))
  2355. {
  2356. if(!iswdigit(c) && (c != '.') && (c != ',') && (c != ' '))
  2357. {
  2358. c = ' '; // terminate digit string with a space
  2359. space_inserted = 1;
  2360. }
  2361. }
  2362. else
  2363. {
  2364. if(prev_in != ',')
  2365. {
  2366. decimal_sep_count = 0;
  2367. }
  2368. }
  2369. if(c == '[')
  2370. {
  2371. if((next_in == '\002') || ((next_in == '[') && option_phoneme_input))
  2372. {
  2373. // "[\002" is used internally to start phoneme mode
  2374. phoneme_mode = FLAG_PHONEMES;
  2375. source_index++;
  2376. continue;
  2377. }
  2378. }
  2379. if(IsAlpha(c))
  2380. {
  2381. if(!IsAlpha(prev_out) || (tr->langopts.ideographs && ((c > 0x3040) || (prev_out > 0x3040))))
  2382. {
  2383. if(wcschr(tr->punct_within_word,prev_out) == 0)
  2384. letter_count = 0; // don't reset count for an apostrophy within a word
  2385. if((prev_out != ' ') && (wcschr(tr->punct_within_word,prev_out) == 0))
  2386. {
  2387. // start of word, insert space if not one there already
  2388. c = ' ';
  2389. space_inserted = 1;
  2390. if(!IsBracket(prev_out)) // ?? perhaps only set FLAG_NOSPACE for . - / (hyphenated words, URLs, etc)
  2391. {
  2392. next_word_flags |= FLAG_NOSPACE;
  2393. }
  2394. }
  2395. else
  2396. {
  2397. if(iswupper(c))
  2398. word_flags |= FLAG_FIRST_UPPER;
  2399. if((prev_out == ' ') && iswdigit(sbuf[ix-2]) && !iswdigit(prev_in))
  2400. {
  2401. // word, following a number, but with a space between
  2402. // Add an extra space, to distinguish "2 a" from "2a"
  2403. sbuf[ix++] = ' ';
  2404. words[word_count].start++;
  2405. }
  2406. }
  2407. }
  2408. letter_count++;
  2409. if(iswupper(c))
  2410. {
  2411. c = towlower2(c);
  2412. if((j = tr->langopts.param[LOPT_CAPS_IN_WORD]) > 0)
  2413. {
  2414. if((j == 2) && (syllable_marked == 0))
  2415. {
  2416. char_inserted = c;
  2417. c = 0x2c8; // stress marker
  2418. syllable_marked = 1;
  2419. }
  2420. }
  2421. else
  2422. {
  2423. if(iswlower(prev_in))
  2424. {
  2425. c = ' '; // lower case followed by upper case, treat as new word
  2426. space_inserted = 1;
  2427. prev_in_save = c;
  2428. // next_word_flags |= FLAG_NOSPACE; // problem: prevents FLAG_HAS_DOT being set
  2429. }
  2430. else
  2431. if((c != ' ') && iswupper(prev_in) && iswlower(next_in))
  2432. {
  2433. int next2_in;
  2434. utf8_in(&next2_in,&source[source_index + next_in_nbytes]);
  2435. if(IsAlpha(next2_in))
  2436. {
  2437. // changing from upper to lower case, start new word at the last uppercase, if 3 or more letters
  2438. c = ' ';
  2439. space_inserted = 1;
  2440. prev_in_save = c;
  2441. next_word_flags |= FLAG_NOSPACE;
  2442. }
  2443. }
  2444. }
  2445. }
  2446. else
  2447. {
  2448. if((all_upper_case) && (letter_count > 2))
  2449. {
  2450. if((c == 's') && (next_in==' '))
  2451. {
  2452. c = ' ';
  2453. all_upper_case |= FLAG_HAS_PLURAL;
  2454. if(sbuf[ix-1] == '\'')
  2455. sbuf[ix-1] = ' ';
  2456. }
  2457. else
  2458. all_upper_case = 0; // current word contains lower case letters, not "'s"
  2459. }
  2460. else
  2461. all_upper_case = 0;
  2462. }
  2463. }
  2464. else
  2465. if(c=='-')
  2466. {
  2467. if(!IsSpace(prev_in) && IsAlpha(next_in))
  2468. {
  2469. if(prev_out != ' ')
  2470. {
  2471. // previous 'word' not yet ended (not alpha or numeric), start new word now.
  2472. c = ' ';
  2473. space_inserted = 1;
  2474. }
  2475. else
  2476. {
  2477. // '-' between two letters is a hyphen, treat as a space
  2478. word_flags |= FLAG_HYPHEN;
  2479. if(word_count > 0)
  2480. words[word_count-1].flags |= FLAG_HYPHEN_AFTER;
  2481. c = ' ';
  2482. }
  2483. }
  2484. else
  2485. if((prev_in==' ') && (next_in==' '))
  2486. {
  2487. // ' - ' dash between two spaces, treat as pause
  2488. c = ' ';
  2489. pre_pause_add = 4;
  2490. }
  2491. else
  2492. if(next_in=='-')
  2493. {
  2494. // double hyphen, treat as pause
  2495. source_index++;
  2496. c = ' ';
  2497. pre_pause_add = 4;
  2498. }
  2499. else
  2500. if((prev_out == ' ') && IsAlpha(sbuf[ix-2]) && !IsAlpha(prev_in))
  2501. {
  2502. // insert extra space between a word + space + hyphen, to distinguish 'a -2' from 'a-2'
  2503. sbuf[ix++] = ' ';
  2504. words[word_count].start++;
  2505. }
  2506. }
  2507. else
  2508. if(c == '.')
  2509. {
  2510. if(prev_out == '.')
  2511. {
  2512. // multiple dots, separate by spaces. Note >3 dots has been replaced by elipsis
  2513. c = ' ';
  2514. space_inserted = 1;
  2515. }
  2516. else
  2517. if((word_count > 0) && !(words[word_count-1].flags & FLAG_NOSPACE) && IsAlpha(prev_in))
  2518. {
  2519. // dot after a word, with space following, probably an abbreviation
  2520. words[word_count-1].flags |= FLAG_HAS_DOT;
  2521. if(IsSpace(next_in) || (next_in == '-'))
  2522. c = ' '; // remove the dot if it's followed by a space or hyphen, so that it's not pronounced
  2523. }
  2524. }
  2525. else
  2526. if(c == '\'')
  2527. {
  2528. if(((prev_in == '.') || iswalnum(prev_in)) && IsAlpha(next_in))
  2529. {
  2530. // between two letters, or in an abbreviation (eg. u.s.a.'s). Consider the apostrophe as part of the word
  2531. single_quoted = 0;
  2532. }
  2533. else
  2534. if((wcschr(tr->char_plus_apostrophe,prev_in) != 0) && (prev_out2 == ' '))
  2535. {
  2536. // consider single character plus apostrophe as a word
  2537. single_quoted = 0;
  2538. if(next_in == ' ')
  2539. {
  2540. source_index++; // skip following space
  2541. }
  2542. }
  2543. else
  2544. {
  2545. if((prev_out == 's') && (single_quoted==0))
  2546. {
  2547. // looks like apostrophe after an 's'
  2548. c = ' ';
  2549. }
  2550. else
  2551. {
  2552. if(IsSpace(prev_out))
  2553. single_quoted = 1;
  2554. else
  2555. single_quoted = 0;
  2556. pre_pause_add = 4; // single quote
  2557. c = ' ';
  2558. }
  2559. }
  2560. }
  2561. else
  2562. #ifdef deleted
  2563. // Brackets are now recognised in TranslateRules()
  2564. if(IsBracket(c))
  2565. {
  2566. pre_pause_add = 4;
  2567. c = ' ';
  2568. }
  2569. else
  2570. #endif
  2571. if(lookupwchar(breaks,c) != 0)
  2572. {
  2573. c = ' '; // various characters to treat as space
  2574. }
  2575. else
  2576. if(iswdigit(c))
  2577. {
  2578. if(tr->langopts.tone_numbers && IsAlpha(prev_out) && !IsDigit(next_in))
  2579. {
  2580. }
  2581. else
  2582. if((prev_out != ' ') && !iswdigit(prev_out))
  2583. {
  2584. if((prev_out != tr->langopts.decimal_sep) || ((decimal_sep_count > 0) && (tr->langopts.decimal_sep == ',')))
  2585. {
  2586. c = ' ';
  2587. space_inserted = 1;
  2588. }
  2589. else
  2590. {
  2591. decimal_sep_count = 1;
  2592. }
  2593. }
  2594. else
  2595. if((prev_out == ' ') && IsAlpha(sbuf[ix-2]) && !IsAlpha(prev_in))
  2596. {
  2597. // insert extra space between a word and a number, to distinguish 'a 2' from 'a2'
  2598. sbuf[ix++] = ' ';
  2599. words[word_count].start++;
  2600. }
  2601. }
  2602. }
  2603. if(IsSpace(c))
  2604. {
  2605. if(prev_out == ' ')
  2606. {
  2607. word_flags |= FLAG_MULTIPLE_SPACES;
  2608. continue; // multiple spaces
  2609. }
  2610. if((cc == 0x09) || (cc == 0x0a))
  2611. {
  2612. next_word_flags |= FLAG_MULTIPLE_SPACES; // tab or newline, not a simple space
  2613. }
  2614. if(space_inserted)
  2615. {
  2616. // count the number of characters since the start of the word
  2617. j = 0;
  2618. k = source_index - 1;
  2619. while((k >= source_index_word) && (charix[k] != 0))
  2620. {
  2621. if(charix[k] > 0) // don't count initial bytes of multi-byte character
  2622. j++;
  2623. k--;
  2624. }
  2625. words[word_count].length = j;
  2626. }
  2627. source_index_word = source_index;
  2628. // end of 'word'
  2629. sbuf[ix++] = ' ';
  2630. if((word_count < N_CLAUSE_WORDS-1) && (ix > words[word_count].start))
  2631. {
  2632. if(embedded_count > 0)
  2633. {
  2634. // there are embedded commands before this word
  2635. embedded_list[embedded_ix-1] |= 0x80; // terminate list of commands for this word
  2636. words[word_count].flags |= FLAG_EMBEDDED;
  2637. embedded_count = 0;
  2638. }
  2639. words[word_count].pre_pause = pre_pause;
  2640. words[word_count].flags |= (all_upper_case | word_flags | word_emphasis);
  2641. words[word_count].wmark = word_mark;
  2642. if(pre_pause > 0)
  2643. {
  2644. // insert an extra space before the word, to prevent influence from previous word across the pause
  2645. for(j=ix; j>words[word_count].start; j--)
  2646. {
  2647. sbuf[j] = sbuf[j-1];
  2648. }
  2649. sbuf[j] = ' ';
  2650. words[word_count].start++;
  2651. ix++;
  2652. }
  2653. word_count++;
  2654. words[word_count].start = ix;
  2655. words[word_count].flags = 0;
  2656. for(j=source_index; charix[j] <= 0; j++); // skip blanks
  2657. words[word_count].sourceix = charix[j];
  2658. k = 0;
  2659. while(charix[j] != 0)
  2660. {
  2661. // count the number of characters (excluding multibyte continuation bytes)
  2662. if(charix[j++] != -1)
  2663. k++;
  2664. }
  2665. words[word_count].length = k;
  2666. word_flags = next_word_flags;
  2667. next_word_flags = 0;
  2668. pre_pause = 0;
  2669. word_mark = 0;
  2670. all_upper_case = FLAG_ALL_UPPER;
  2671. syllable_marked = 0;
  2672. }
  2673. if(space_inserted)
  2674. {
  2675. source_index = prev_source_index; // rewind to the previous character
  2676. char_inserted = 0;
  2677. space_inserted = 0;
  2678. }
  2679. }
  2680. else
  2681. {
  2682. if((ix < (N_TR_SOURCE - 4)))
  2683. ix += utf8_out(c,&sbuf[ix]); // sbuf[ix++] = c;
  2684. }
  2685. if(pre_pause_add > pre_pause)
  2686. pre_pause = pre_pause_add;
  2687. pre_pause_add = 0;
  2688. }
  2689. if((word_count==0) && (embedded_count > 0))
  2690. {
  2691. // add a null 'word' to carry the embedded command flag
  2692. embedded_list[embedded_ix-1] |= 0x80;
  2693. words[word_count].flags |= FLAG_EMBEDDED;
  2694. word_count = 1;
  2695. }
  2696. tr->clause_end = &sbuf[ix-1];
  2697. sbuf[ix] = 0;
  2698. words[0].pre_pause = 0; // don't add extra pause at beginning of clause
  2699. words[word_count].pre_pause = 8;
  2700. if(word_count > 0)
  2701. {
  2702. ix = word_count-1;
  2703. while((ix > 0) && (IsBracket(sbuf[words[ix].start])))
  2704. ix--; // the last word is a bracket, mark the previous word as last
  2705. words[ix].flags |= FLAG_LAST_WORD;
  2706. // FLAG_NOSPACE check to avoid recognizing .mr -mr
  2707. if((terminator & CLAUSE_DOT) && !(words[word_count-1].flags & FLAG_NOSPACE))
  2708. words[word_count-1].flags |= FLAG_HAS_DOT;
  2709. }
  2710. words[0].flags |= FLAG_FIRST_WORD;
  2711. for(ix=0; ix < word_count; ix++)
  2712. {
  2713. int nx;
  2714. int c_temp;
  2715. char *pn;
  2716. char *pw;
  2717. int nw;
  2718. char number_buf[150];
  2719. WORD_TAB num_wtab[50]; // copy of 'words', when splitting numbers into parts
  2720. // start speaking at a specified word position in the text?
  2721. count_words++;
  2722. if(skip_words > 0)
  2723. {
  2724. skip_words--;
  2725. if(skip_words == 0)
  2726. skipping_text = 0;
  2727. }
  2728. if(skipping_text)
  2729. continue;
  2730. // digits should have been converted to Latin alphabet ('0' to '9')
  2731. word = pw = &sbuf[words[ix].start];
  2732. if(iswdigit(word[0]) && (tr->langopts.break_numbers != BREAK_THOUSANDS))
  2733. {
  2734. // Languages with 100000 numbers. Remove thousands separators so that we can insert them again later
  2735. pn = number_buf;
  2736. while(pn < &number_buf[sizeof(number_buf)-20])
  2737. {
  2738. if(iswdigit(*pw))
  2739. {
  2740. *pn++ = *pw++;
  2741. }
  2742. else
  2743. if((*pw == tr->langopts.thousands_sep) && (pw[1] == ' ')
  2744. && iswdigit(pw[2]) && (pw[3] != ' ') && (pw[4] != ' ')) // don't allow only 1 or 2 digits in the final part
  2745. {
  2746. pw += 2;
  2747. ix++; // skip "word"
  2748. }
  2749. else
  2750. {
  2751. nx = pw - word;
  2752. memset(word,' ',nx);
  2753. nx = pn - number_buf;
  2754. memcpy(word,number_buf,nx);
  2755. break;
  2756. }
  2757. }
  2758. pw = word;
  2759. }
  2760. for(n_digits=0; iswdigit(word[n_digits]); n_digits++); // count consecutive digits
  2761. if(n_digits > 4)
  2762. {
  2763. // word is entirely digits, insert commas and break into 3 digit "words"
  2764. number_buf[0] = ' ';
  2765. pn = &number_buf[1];
  2766. nx = n_digits;
  2767. nw = 0;
  2768. if((n_digits > tr->langopts.max_digits) || (word[0] == '0'))
  2769. words[ix].flags |= FLAG_INDIVIDUAL_DIGITS;
  2770. while(pn < &number_buf[sizeof(number_buf)-20])
  2771. {
  2772. if(!isdigit(c = *pw++) && (c != tr->langopts.decimal_sep))
  2773. break;
  2774. *pn++ = c;
  2775. if((--nx > 0) && (tr->langopts.break_numbers & (1 << nx)))
  2776. {
  2777. memcpy(&num_wtab[nw++], &words[ix], sizeof(WORD_TAB)); // copy the 'words' entry for each word of numbers
  2778. if(tr->langopts.thousands_sep != ' ')
  2779. {
  2780. *pn++ = tr->langopts.thousands_sep;
  2781. }
  2782. *pn++ = ' ';
  2783. if(tr->langopts.break_numbers & (1 << (nx-1)))
  2784. {
  2785. // the next group only has 1 digits (i.e. NUM2_10000), make it three
  2786. *pn++ = '0';
  2787. *pn++ = '0';
  2788. }
  2789. if(tr->langopts.break_numbers & (1 << (nx-2)))
  2790. {
  2791. // the next group only has 2 digits (i.e. NUM2_10000), make it three
  2792. *pn++ = '0';
  2793. }
  2794. }
  2795. }
  2796. pw--;
  2797. memcpy(&num_wtab[nw], &words[ix], sizeof(WORD_TAB)*2); // the original number word, and the word after it
  2798. for(j=1; j<=nw; j++)
  2799. {
  2800. num_wtab[j].flags &= ~(FLAG_MULTIPLE_SPACES | FLAG_EMBEDDED); // don't use these flags for subsequent parts when splitting a number
  2801. }
  2802. // include the next few characters, in case there are an ordinal indicator or other suffix
  2803. memcpy(pn, pw, 16);
  2804. pn[16] = 0;
  2805. nw = 0;
  2806. for(pw = &number_buf[1]; pw < pn;)
  2807. {
  2808. // keep wflags for each part, for FLAG_HYPHEN_AFTER
  2809. dict_flags = TranslateWord2(tr, pw, &num_wtab[nw++], words[ix].pre_pause,0 );
  2810. while(*pw++ != ' ');
  2811. words[ix].pre_pause = 0;
  2812. }
  2813. }
  2814. else
  2815. {
  2816. pre_pause = 0;
  2817. dict_flags = TranslateWord2(tr, word, &words[ix], words[ix].pre_pause, words[ix+1].pre_pause);
  2818. if(pre_pause > words[ix+1].pre_pause)
  2819. {
  2820. words[ix+1].pre_pause = pre_pause;
  2821. pre_pause = 0;
  2822. }
  2823. if(dict_flags & FLAG_SPELLWORD)
  2824. {
  2825. // redo the word, speaking single letters
  2826. for(pw = word; *pw != ' ';)
  2827. {
  2828. memset(number_buf,' ',9);
  2829. nx = utf8_in(&c_temp, pw);
  2830. memcpy(&number_buf[2],pw,nx);
  2831. TranslateWord2(tr, &number_buf[2], &words[ix], 0, 0 );
  2832. pw += nx;
  2833. }
  2834. }
  2835. if((dict_flags & (FLAG_ALLOW_DOT | FLAG_NEEDS_DOT)) && (ix == word_count - 1 - dictionary_skipwords) && (terminator & CLAUSE_DOT))
  2836. {
  2837. // probably an abbreviation such as Mr. or B. rather than end of sentence
  2838. clause_pause = 10;
  2839. tone = 4;
  2840. }
  2841. }
  2842. if(dict_flags & FLAG_SKIPWORDS)
  2843. {
  2844. // dictionary indicates skip next word(s)
  2845. while(dictionary_skipwords > 0)
  2846. {
  2847. words[ix+dictionary_skipwords].flags |= FLAG_DELETE_WORD;
  2848. dictionary_skipwords--;
  2849. }
  2850. }
  2851. }
  2852. if(embedded_read < embedded_ix)
  2853. {
  2854. // any embedded commands not yet processed?
  2855. Word_EmbeddedCmd();
  2856. }
  2857. for(ix=0; ix<2; ix++)
  2858. {
  2859. // terminate the clause with 2 PAUSE phonemes
  2860. PHONEME_LIST2 *p2;
  2861. p2 = &ph_list2[n_ph_list2 + ix];
  2862. p2->phcode = phonPAUSE;
  2863. p2->stresslevel = 0;
  2864. p2->sourceix = source_index;
  2865. p2->synthflags = 0;
  2866. }
  2867. n_ph_list2 += 2;
  2868. if(count_words == 0)
  2869. {
  2870. clause_pause = 0;
  2871. }
  2872. if(Eof() && ((word_count == 0) || (option_endpause==0)))
  2873. {
  2874. clause_pause = 10;
  2875. }
  2876. MakePhonemeList(tr, clause_pause, new_sentence);
  2877. if(embedded_count) // ???? is this needed
  2878. {
  2879. phoneme_list[n_phoneme_list-2].synthflags = SFLAG_EMBEDDED;
  2880. embedded_list[embedded_ix-1] |= 0x80;
  2881. embedded_list[embedded_ix] = 0x80;
  2882. }
  2883. prev_clause_pause = clause_pause;
  2884. *tone_out = tone;
  2885. new_sentence = 0;
  2886. if(terminator & CLAUSE_BIT_SENTENCE)
  2887. {
  2888. new_sentence = 1; // next clause is a new sentence
  2889. }
  2890. if(voice_change != NULL)
  2891. {
  2892. // return new voice name if an embedded voice change command terminated the clause
  2893. if(terminator & CLAUSE_BIT_VOICE)
  2894. *voice_change = voice_change_name;
  2895. else
  2896. *voice_change = NULL;
  2897. }
  2898. if(Eof() || (vp_input==NULL))
  2899. return(NULL);
  2900. if(option_multibyte == espeakCHARS_WCHAR)
  2901. return((void *)p_wchar_input);
  2902. else
  2903. return((void *)p_textinput);
  2904. } // end of TranslateClause
  2905. void InitText(int control)
  2906. {//=======================
  2907. count_sentences = 0;
  2908. count_words = 0;
  2909. end_character_position = 0;
  2910. skip_sentences = 0;
  2911. skip_marker[0] = 0;
  2912. skip_words = 0;
  2913. skip_characters = 0;
  2914. skipping_text = 0;
  2915. new_sentence = 1;
  2916. prev_clause_pause = 0;
  2917. option_sayas = 0;
  2918. option_sayas2 = 0;
  2919. option_emphasis = 0;
  2920. word_emphasis = 0;
  2921. embedded_flag = 0;
  2922. InitText2();
  2923. if((control & espeakKEEP_NAMEDATA) == 0)
  2924. {
  2925. InitNamedata();
  2926. }
  2927. }