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

translate.cpp 73KB

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