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

translate.c 89KB

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