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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  1. /***************************************************************************
  2. * Copyright (C) 2005, 2006 by Jonathan Duddington *
  3. * [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 2 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, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include "StdAfx.h"
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <wctype.h>
  26. #include <wchar.h>
  27. #include "speak_lib.h"
  28. #include "speech.h"
  29. #include "voice.h"
  30. #include "phoneme.h"
  31. #include "synthesize.h"
  32. #include "translate.h"
  33. #define WORD_STRESS_CHAR '*'
  34. Translator *translator = NULL; // the main translator
  35. static Translator *translator2 = NULL; // secondary translator for certain words
  36. static char translator2_language[20] = {0};
  37. FILE *f_trans = NULL; // phoneme output text
  38. int option_tone1 = 0;
  39. int option_tone2 = 0;
  40. int option_phonemes = 0;
  41. int option_quiet = 0;
  42. int option_endpause = 0; // suppress pause after end of text
  43. int option_capitals = 0;
  44. int option_punctuation = 0;
  45. int option_sayas = 0;
  46. int option_sayas2 = 0; // used in translate_clause()
  47. int option_emphasis = 0;
  48. int option_ssml = 0;
  49. int option_phoneme_input = 1; // allow [[phonemes]] in input
  50. int option_phoneme_variants = 0; // 0= don't display phoneme variant mnemonics
  51. int count_sayas_digits;
  52. int skip_sentences;
  53. int skip_words;
  54. int skip_characters;
  55. char skip_marker[N_MARKER_LENGTH];
  56. int skipping_text; // waiting until word count, sentence count, or named marker is reached
  57. int end_character_position;
  58. int count_sentences;
  59. int count_words;
  60. int clause_start_char;
  61. int clause_start_word;
  62. int new_sentence;
  63. int prev_clause_pause=0;
  64. int max_clause_pause = 0;
  65. wchar_t option_punctlist[N_PUNCTLIST]={0};
  66. char ctrl_embedded = '\001'; // to allow an alternative CTRL for embedded commands
  67. int option_multibyte=espeakCHARS_AUTO; // 0=auto, 1=utf8, 2=8bit, 3=wchar
  68. // these are overridden by defaults set in the "speak" file
  69. int option_linelength = 0;
  70. #define N_EMBEDDED_LIST 250
  71. static int embedded_ix;
  72. static int embedded_read;
  73. unsigned int embedded_list[N_EMBEDDED_LIST];
  74. // the source text of a single clause
  75. #define N_TR_SOURCE 350
  76. static char source[N_TR_SOURCE+40]; // extra space for embedded command & voice change info at end
  77. int n_replace_phonemes;
  78. REPLACE_PHONEMES replace_phonemes[N_REPLACE_PHONEMES];
  79. // brackets, also 0x2014 to 0x021f which don't need to be in this list
  80. static const unsigned short brackets[] = {
  81. '(',')','[',']','{','}','<','>','"','\'','`',
  82. 0xab,0xbb, // double angle brackets
  83. 0x300a,0x300b, // double angle brackets (ideograph)
  84. 0};
  85. // other characters which break a word, but don't produce a pause
  86. static const unsigned short breaks[] = {'_', 0};
  87. // Translate character codes 0xA0 to 0xFF into their unicode values
  88. // ISO_8859_1 is set as default
  89. static const unsigned short ISO_8859_1[0x60] = {
  90. 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, // a0
  91. 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, // a8
  92. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, // b0
  93. 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, // b8
  94. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
  95. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  96. 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, // d0
  97. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, // d8
  98. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
  99. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  100. 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, // f0
  101. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff, // f8
  102. };
  103. static const unsigned short ISO_8859_2[0x60] = {
  104. 0x00a0, 0x0104, 0x02d8, 0x0141, 0x00a4, 0x013d, 0x015a, 0x00a7, // a0
  105. 0x00a8, 0x0160, 0x015e, 0x0164, 0x0179, 0x00ad, 0x017d, 0x017b, // a8
  106. 0x00b0, 0x0105, 0x02db, 0x0142, 0x00b4, 0x013e, 0x015b, 0x02c7, // b0
  107. 0x00b8, 0x0161, 0x015f, 0x0165, 0x017a, 0x02dd, 0x017e, 0x017c, // b8
  108. 0x0154, 0x00c1, 0x00c2, 0x0102, 0x00c4, 0x0139, 0x0106, 0x00c7, // c0
  109. 0x010c, 0x00c9, 0x0118, 0x00cb, 0x011a, 0x00cd, 0x00ce, 0x010e, // c8
  110. 0x0110, 0x0143, 0x0147, 0x00d3, 0x00d4, 0x0150, 0x00d6, 0x00d7, // d0
  111. 0x0158, 0x016e, 0x00da, 0x0170, 0x00dc, 0x00dd, 0x0162, 0x00df, // d8
  112. 0x0155, 0x00e1, 0x00e2, 0x0103, 0x00e4, 0x013a, 0x0107, 0x00e7, // e0
  113. 0x010d, 0x00e9, 0x0119, 0x00eb, 0x011b, 0x00ed, 0x00ee, 0x010f, // e8
  114. 0x0111, 0x0144, 0x0148, 0x00f3, 0x00f4, 0x0151, 0x00f6, 0x00f7, // f0
  115. 0x0159, 0x016f, 0x00fa, 0x0171, 0x00fc, 0x00fd, 0x0163, 0x02d9, // f8
  116. };
  117. static const unsigned short ISO_8859_3[0x60] = {
  118. 0x00a0, 0x0126, 0x02d8, 0x00a3, 0x00a4, 0x0000, 0x0124, 0x00a7, // a0
  119. 0x00a8, 0x0130, 0x015e, 0x011e, 0x0134, 0x00ad, 0x0000, 0x017b, // a8
  120. 0x00b0, 0x0127, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x0125, 0x00b7, // b0
  121. 0x00b8, 0x0131, 0x015f, 0x011f, 0x0135, 0x00bd, 0x0000, 0x017c, // b8
  122. 0x00c0, 0x00c1, 0x00c2, 0x0000, 0x00c4, 0x010a, 0x0108, 0x00c7, // c0
  123. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  124. 0x0000, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x0120, 0x00d6, 0x00d7, // d0
  125. 0x011c, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x016c, 0x015c, 0x00df, // d8
  126. 0x00e0, 0x00e1, 0x00e2, 0x0000, 0x00e4, 0x010b, 0x0109, 0x00e7, // e0
  127. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  128. 0x0000, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x0121, 0x00f6, 0x00f7, // f0
  129. 0x011d, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x016d, 0x015d, 0x02d9, // f8
  130. };
  131. static const unsigned short ISO_8859_5[0x60] = {
  132. 0x00a0, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407, // a0 Cyrillic
  133. 0x0408, 0x0409, 0x040a, 0x040b, 0x040c, 0x00ad, 0x040e, 0x040f, // a8
  134. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, // b0
  135. 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f, // b8
  136. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, // c0
  137. 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f, // c8
  138. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, // d0
  139. 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f, // d8
  140. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, // e0
  141. 0x0448, 0x0449, 0x044a, 0x044b, 0x044c, 0x044d, 0x044e, 0x044f, // e8
  142. 0x2116, 0x0451, 0x0452, 0x0453, 0x0454, 0x0455, 0x0456, 0x0457, // f0
  143. 0x0458, 0x0459, 0x045a, 0x045b, 0x045c, 0x00a7, 0x045e, 0x045f, // f8
  144. };
  145. static const unsigned short ISO_8859_7[0x60] = {
  146. 0x00a0, 0x2018, 0x2019, 0x00a3, 0x20ac, 0x20af, 0x00a6, 0x00a7, // a0 Greek
  147. 0x00a8, 0x00a9, 0x037a, 0x00ab, 0x00ac, 0x00ad, 0x0000, 0x2015, // a8
  148. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x0384, 0x0385, 0x0386, 0x00b7, // b0
  149. 0x0388, 0x0389, 0x038a, 0x00bb, 0x038c, 0x00bd, 0x038e, 0x038f, // b8
  150. 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, // c0
  151. 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e, 0x039f, // c8
  152. 0x03a0, 0x03a1, 0x0000, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7, // d0
  153. 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af, // d8
  154. 0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7, // e0
  155. 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf, // e8
  156. 0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6, 0x03c7, // f0
  157. 0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce, 0x0000, // f8
  158. };
  159. static const unsigned short ISO_8859_9[0x60] = {
  160. 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, // a0
  161. 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, // a8
  162. 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, // b0
  163. 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, // b8
  164. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
  165. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  166. 0x011e, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, // d0
  167. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x0130, 0x015e, 0x00df, // d8
  168. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
  169. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  170. 0x011f, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, // f0
  171. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x0131, 0x015f, 0x00ff, // f8
  172. };
  173. static const unsigned short ISO_8859_14[0x60] = {
  174. 0x00a0, 0x1e02, 0x1e03, 0x00a3, 0x010a, 0x010b, 0x1e0a, 0x00a7, // a0 Welsh
  175. 0x1e80, 0x00a9, 0x1e82, 0x1e0b, 0x1ef2, 0x00ad, 0x00ae, 0x0178, // a8
  176. 0x1e1e, 0x1e1f, 0x0120, 0x0121, 0x1e40, 0x1e41, 0x00b6, 0x1e56, // b0
  177. 0x1e81, 0x1e57, 0x1e83, 0x1e60, 0x1ef3, 0x1e84, 0x1e85, 0x1e61, // b8
  178. 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, // c0
  179. 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, // c8
  180. 0x0174, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x1e6a, // d0
  181. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x0176, 0x00df, // d8
  182. 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, // e0
  183. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, // e8
  184. 0x0175, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x1e6b, // f0
  185. 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x0177, 0x00ff, // f8
  186. };
  187. static const unsigned short KOI8_R[0x60] = {
  188. 0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556, // a0 Russian
  189. 0x2557, 0x2558, 0x2559, 0x255a, 0x255b, 0x255c, 0x255d, 0x255e, // a8
  190. 0x255f, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565, // b0
  191. 0x2566, 0x2567, 0x2568, 0x2569, 0x256a, 0x256b, 0x256c, 0x00a9, // b8
  192. 0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, // c0
  193. 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, // c8
  194. 0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, // d0
  195. 0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a, // d8
  196. 0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, // e0
  197. 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, // e8
  198. 0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, // f0
  199. 0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a, // f8
  200. };
  201. static const unsigned short ISCII[0x60] = {
  202. 0x0020, 0x0901, 0x0902, 0x0903, 0x0905, 0x0906, 0x0907, 0x0908, // a0
  203. 0x0909, 0x090a, 0x090b, 0x090e, 0x090f, 0x0910, 0x090d, 0x0912, // a8
  204. 0x0913, 0x0914, 0x0911, 0x0915, 0x0916, 0x0917, 0x0918, 0x0919, // b0
  205. 0x091a, 0x091b, 0x091c, 0x091d, 0x091e, 0x091f, 0x0920, 0x0921, // b8
  206. 0x0922, 0x0923, 0x0924, 0x0925, 0x0926, 0x0927, 0x0928, 0x0929, // c0
  207. 0x092a, 0x092b, 0x092c, 0x092d, 0x092e, 0x092f, 0x095f, 0x0930, // c8
  208. 0x0931, 0x0932, 0x0933, 0x0934, 0x0935, 0x0936, 0x0937, 0x0938, // d0
  209. 0x0939, 0x0020, 0x093e, 0x093f, 0x0940, 0x0941, 0x0942, 0x0943, // d8
  210. 0x0946, 0x0947, 0x0948, 0x0945, 0x094a, 0x094b, 0x094c, 0x0949, // e0
  211. 0x094d, 0x093c, 0x0964, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, // e8
  212. 0x0020, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, // f0
  213. 0x0037, 0x0038, 0x0039, 0x20, 0x20, 0x20, 0x20, 0x20, // f8
  214. };
  215. const unsigned short *charsets[N_CHARSETS] = {
  216. ISO_8859_1,
  217. ISO_8859_1,
  218. ISO_8859_2,
  219. ISO_8859_3,
  220. ISO_8859_1,
  221. ISO_8859_5,
  222. ISO_8859_1,
  223. ISO_8859_7,
  224. ISO_8859_1,
  225. ISO_8859_9,
  226. ISO_8859_1,
  227. ISO_8859_1,
  228. ISO_8859_1,
  229. ISO_8859_1,
  230. ISO_8859_14,
  231. ISO_8859_1,
  232. ISO_8859_1,
  233. ISO_8859_1,
  234. KOI8_R, // 18
  235. ISCII };
  236. // Tables of the relative lengths of vowels, depending on the
  237. // type of the two phonemes that follow
  238. // indexes are the "length_mod" value for the following phonemes
  239. // use this table if vowel is not the last in the word
  240. static unsigned char length_mods_en[100] = {
  241. /* a , t s n d z r N <- next */
  242. 100,120,100,105, 95,110,110,100, 95, 100, /* a <- next2 */
  243. 105,120,105,110,125,130,135,115,125, 100, /* , */
  244. 105,120, 75,100, 75,105,120, 85, 75, 100, /* t */
  245. 105,120, 85,105, 95,115,120,100, 95, 100, /* s */
  246. 110,120, 95,105,100,115,120,100,100, 100, /* n */
  247. 105,120,100,105, 95,115,120,110, 95, 100, /* d */
  248. 105,120,100,105,105,122,125,110,105, 100, /* z */
  249. 105,120,100,105,105,122,125,110,105, 100, /* r */
  250. 105,120, 95,105,100,115,120,110,100, 100, /* N */
  251. 100,120,100,100,100,100,100,100,100, 100 }; // SPARE
  252. // as above, but for the last syllable in a word
  253. static unsigned char length_mods_en0[100] = {
  254. /* a , t s n d z r N <- next */
  255. 100,150,100,105,110,115,110,110,110, 100, /* a <- next2 */
  256. 105,150,105,110,125,135,140,115,135, 100, /* , */
  257. 105,150, 90,105, 90,122,135,100, 90, 100, /* t */
  258. 105,150,100,105,100,122,135,100,100, 100, /* s */
  259. 105,150,100,105,105,115,135,110,105, 100, /* n */
  260. 105,150,100,105,105,122,130,120,125, 100, /* d */
  261. 105,150,100,105,110,122,125,115,110, 100, /* z */
  262. 105,150,100,105,105,122,135,120,105, 100, /* r */
  263. 105,150,100,105,105,115,135,110,105, 100, /* N */
  264. 100,100,100,100,100,100,100,100,100, 100 }; // SPARE
  265. static unsigned char length_mods_equal[100] = {
  266. /* a , t s n d z r N <- next */
  267. 100,100,100,100,100,100,100,100,100, 100, /* a <- next2 */
  268. 100,100,100,100,100,100,100,100,100, 100, /* , */
  269. 100,100,100,100,100,100,100,100,100, 100, /* t */
  270. 100,100,100,100,100,100,100,100,100, 100, /* s */
  271. 100,100,100,100,100,100,100,100,100, 100, /* n */
  272. 100,100,100,100,100,100,100,100,100, 100, /* d */
  273. 100,100,100,100,100,100,100,100,100, 100, /* z */
  274. 100,100,100,100,100,100,100,100,100, 100, /* r */
  275. 100,100,100,100,100,100,100,100,100, 100, /* N */
  276. 100,100,100,100,100,100,100,100,100, 100 }; // SPARE
  277. unsigned char *length_mod_tabs[6] = {
  278. length_mods_en,
  279. length_mods_en, // 1
  280. length_mods_en0, // 2
  281. length_mods_equal, // 3
  282. length_mods_equal, // 4
  283. length_mods_equal // 5
  284. };
  285. /* index by 0=. 1=, 2=?, 3=! 4=none */
  286. static unsigned char punctuation_to_tone[4][5] = {
  287. {0,1,1,2,0},
  288. {3,4,4,5,3},
  289. {6,7,7,8,6},
  290. {9,10,10,11,9} };
  291. void SetLengthMods(Translator *tr, int value)
  292. {//==========================================
  293. int value2;
  294. tr->langopts.length_mods0 = tr->langopts.length_mods = length_mod_tabs[value % 100];
  295. if((value2 = value / 100) != 0)
  296. {
  297. tr->langopts.length_mods0 = length_mod_tabs[value2];
  298. }
  299. }
  300. int IsAlpha(unsigned int c)
  301. {//========================
  302. // Replacement for iswalph() which also checks for some in-word symbols
  303. if(iswalpha(c))
  304. return(1);
  305. if((c >= 0x901) && (c <= 0x957))
  306. return(1); // Devanagari vowel signs and other signs
  307. return(0);
  308. }
  309. int IsDigit(unsigned int c)
  310. {//========================
  311. if(iswdigit(c))
  312. return(1);
  313. if((c >= 0x966) && (c <= 0x96f))
  314. return(1);
  315. return(0);
  316. }
  317. Translator::Translator()
  318. {//=====================
  319. int ix;
  320. static int stress_amps2[] = {16,16, 20,20, 20,24, 24,21 };
  321. static int stress_lengths2[8] = {182,140, 220,220, 220,240, 260,280};
  322. static const wchar_t empty_wstring[1] = {0};
  323. charset_a0 = charsets[1]; // ISO-8859-1, this is for when the input is not utf8
  324. dictionary_name[0] = 0;
  325. dict_condition=0;
  326. data_dictrules = NULL; // language_1 translation rules file
  327. data_dictlist = NULL; // language_2 dictionary lookup file
  328. transpose_offset = 0;
  329. // only need lower case
  330. letter_bits_offset = 0;
  331. memset(letter_bits,0,sizeof(letter_bits));
  332. memset(letter_groups,0,sizeof(letter_groups));
  333. // 0-5 sets of characters matched by A B C E F G in pronunciation rules
  334. // these may be set differently for different languages
  335. SetLetterBits(this,0,"aeiou"); // A vowels, except y
  336. SetLetterBits(this,1,"bcdfgjklmnpqstvxz"); // B hard consonants, excluding h,r,w
  337. SetLetterBits(this,2,"bcdfghjklmnpqrstvwxz"); // C all consonants
  338. SetLetterBits(this,3,"hlmnr"); // H 'soft' consonants
  339. SetLetterBits(this,4,"cfhkpqstx"); // F voiceless consonants
  340. SetLetterBits(this,5,"bdgjlmnrvwyz"); // G voiced
  341. SetLetterBits(this,6,"eiy"); // Letter group Y, front vowels
  342. SetLetterBits(this,7,"aeiouy"); // vowels, including y
  343. char_plus_apostrophe = empty_wstring;
  344. for(ix=0; ix<8; ix++)
  345. {
  346. stress_amps[ix] = stress_amps2[ix];
  347. stress_amps_r[ix] = stress_amps2[ix] - 1;
  348. stress_lengths[ix] = stress_lengths2[ix];
  349. }
  350. memset(&langopts,0,sizeof(langopts));
  351. langopts.stress_rule = 2;
  352. langopts.unstressed_wd1 = 1;
  353. langopts.unstressed_wd2 = 3;
  354. langopts.param[LOPT_SONORANT_MIN] = 95;
  355. langopts.param[LOPT_MAXAMP_EOC] = 19;
  356. langopts.param[LOPT_UNPRONOUNCABLE] = 's'; // don't count this character at start of word
  357. langopts.max_initial_consonants = 3;
  358. langopts.replace_chars = NULL;
  359. langopts.length_mods = length_mods_en;
  360. langopts.length_mods0 = length_mods_en0;
  361. langopts.long_stop = 100;
  362. langopts.thousands_sep = ',';
  363. langopts.decimal_sep = '.';
  364. memcpy(punct_to_tone,punctuation_to_tone,sizeof(punct_to_tone));
  365. }
  366. Translator::~Translator(void)
  367. {//==========================
  368. if(data_dictlist != NULL)
  369. Free(data_dictlist);
  370. }
  371. int lookupwchar(const unsigned short *list,int c)
  372. {//==============================================
  373. // Is the character c in the list ?
  374. int ix;
  375. for(ix=0; list[ix] != 0; ix++)
  376. {
  377. if(list[ix] == c)
  378. return(ix+1);
  379. }
  380. return(0);
  381. }
  382. int IsBracket(int c)
  383. {//=================
  384. if((c >= 0x2014) && (c <= 0x201f))
  385. return(1);
  386. return(lookupwchar(brackets,c));
  387. }
  388. int utf8_out(unsigned int c, char *buf)
  389. {//====================================
  390. // write a unicode character into a buffer as utf8
  391. // returns the number of bytes written
  392. int n_bytes;
  393. int j;
  394. int shift;
  395. static char unsigned code[4] = {0,0xc0,0xe0,0xf0};
  396. if(c < 0x80)
  397. {
  398. buf[0] = c;
  399. return(1);
  400. }
  401. if(c >= 0x110000)
  402. {
  403. buf[0] = ' '; // out of range character code
  404. return(1);
  405. }
  406. if(c < 0x0800)
  407. n_bytes = 1;
  408. else
  409. if(c < 0x10000)
  410. n_bytes = 2;
  411. else
  412. n_bytes = 3;
  413. shift = 6*n_bytes;
  414. buf[0] = code[n_bytes] | (c >> shift);
  415. for(j=0; j<n_bytes; j++)
  416. {
  417. shift -= 6;
  418. buf[j+1] = 0x80 + ((c >> shift) & 0x3f);
  419. }
  420. return(n_bytes+1);
  421. } // end of utf8_out
  422. int utf8_in(int *c, char *buf, int backwards)
  423. {//==========================================
  424. int c1;
  425. int n_bytes;
  426. int ix;
  427. static const unsigned char mask[4] = {0xff,0x1f,0x0f,0x07};
  428. // find the start of the next/previous character
  429. while((*buf & 0xc0) == 0x80)
  430. {
  431. // skip over non-initial bytes of a multi-byte utf8 character
  432. if(backwards)
  433. buf--;
  434. else
  435. buf++;
  436. }
  437. n_bytes = 0;
  438. if((c1 = *buf++) & 0x80)
  439. {
  440. if((c1 & 0xe0) == 0xc0)
  441. n_bytes = 1;
  442. else
  443. if((c1 & 0xf0) == 0xe0)
  444. n_bytes = 2;
  445. else
  446. if((c1 & 0xf8) == 0xf0)
  447. n_bytes = 3;
  448. c1 &= mask[n_bytes];
  449. for(ix=0; ix<n_bytes; ix++)
  450. {
  451. c1 = (c1 << 6) + (*buf++ & 0x3f);
  452. }
  453. }
  454. *c = c1;
  455. return(n_bytes+1);
  456. }
  457. char *strchr_w(const char *s, int c)
  458. {//=================================
  459. // return NULL for any non-ascii character
  460. if(c >= 0x80)
  461. return(NULL);
  462. return(strchr((char *)s,c)); // (char *) is needed for Borland compiler
  463. }
  464. int PhonemeCode(unsigned int mnem)
  465. {//===============================
  466. int ix;
  467. for(ix=0; ix<n_phoneme_tab; ix++)
  468. {
  469. if(phoneme_tab[ix]->mnemonic == mnem)
  470. return(phoneme_tab[ix]->code);
  471. }
  472. return(phonSCHWA);
  473. }
  474. int Translator::TranslateWord(char *word1, int next_pause, WORD_TAB *wtab)
  475. {//=======================================================================
  476. // word1 is terminated by space (0x20) character
  477. int length;
  478. int word_length;
  479. int ix;
  480. int posn;
  481. unsigned int dictionary_flags=0;
  482. unsigned int dictionary_flags2=0;
  483. int end_type=0;
  484. int prefix_type=0;
  485. char *word;
  486. char phonemes[N_WORD_PHONEMES];
  487. char *ph_limit;
  488. char *phonemes_ptr;
  489. char prefix_phonemes[N_WORD_PHONEMES];
  490. char end_phonemes[N_WORD_PHONEMES];
  491. char word_copy[N_WORD_BYTES];
  492. int found=0;
  493. int end_flags;
  494. char c_temp; // save a character byte while we temporarily replace it with space
  495. int first_char;
  496. int last_char = 0;
  497. int unpron_length;
  498. int add_plural_suffix = 0;
  499. int prefix_flags = 0;
  500. int spell_word;
  501. int wflags = wtab->flags;
  502. int wmark = wtab->wmark;
  503. // translate these to get pronunciations of plural 's' suffix (different forms depending on
  504. // the preceding letter
  505. static char word_zz[4] = {0,'z','z',0};
  506. static char word_iz[4] = {0,'i','z',0};
  507. static char word_ss[4] = {0,'s','s',0};
  508. word = word1;
  509. prefix_phonemes[0] = 0;
  510. end_phonemes[0] = 0;
  511. ph_limit = &phonemes[N_WORD_PHONEMES];
  512. // count the length of the word
  513. utf8_in(&first_char,word,0);
  514. word_length = 0;
  515. while((*word != 0) && (*word != ' '))
  516. {
  517. word += utf8_in(&last_char,word,0);
  518. word_length++;
  519. }
  520. word = word1;
  521. //#define DEBUG3
  522. #ifdef DEBUG3
  523. printf("TR1 wlen=%d, %x %x %x %x %x %x %x %x\n",word_length,word[0],word[1],word[2],word[3],word[4],word[5],word[6],word[7]);
  524. #endif
  525. // try an initial lookup in the dictionary list, we may find a pronunciation specified, or
  526. // we may just find some flags
  527. if((option_sayas & 0xf0) == 0x10)
  528. {
  529. // SAYAS_CHAR, SAYAS_GYLPH, or SAYAS_SINGLE_CHAR
  530. spell_word = option_sayas & 0xf; // 2,3,4
  531. }
  532. else
  533. {
  534. spell_word = 0;
  535. found = LookupDictList(word,phonemes,&dictionary_flags,wflags << 16);
  536. if(phonemes[0] == phonSWITCH)
  537. {
  538. // change to another language in order to translate this word
  539. strcpy(word_phonemes,phonemes);
  540. return(0);
  541. }
  542. if((wmark > 0) && (wmark < 8))
  543. {
  544. // the stressed syllable has been specified in the text (TESTING)
  545. dictionary_flags = (dictionary_flags & ~0xf) | wmark;
  546. }
  547. if(!found && (dictionary_flags & FLAG_ABBREV))
  548. {
  549. // the word has $abbrev flag, but no pronunciation specified. Speak as individual letters
  550. spell_word = 1;
  551. }
  552. if(!found && iswdigit(first_char))
  553. {
  554. found = TranslateNumber(word,phonemes,&dictionary_flags,wflags);
  555. }
  556. if((wflags & FLAG_ALL_UPPER) && (clause_upper_count <= clause_lower_count) &&
  557. !(dictionary_flags & (FLAG_ABBREV | FLAG_SKIPWORDS)) && (word_length>1) && (word_length<4) && iswalpha(first_char))
  558. {
  559. // An upper case word in a lower case clause. This could be an abbreviation.
  560. spell_word = 1;
  561. }
  562. }
  563. #ifdef DEBUG3
  564. printf("TR2 spell_word=%d\n",spell_word);
  565. #endif
  566. if(spell_word > 0)
  567. {
  568. // Speak as individual letters
  569. word = word1;
  570. posn = 0;
  571. phonemes[0] = 0;
  572. end_type = 0;
  573. while(*word != ' ')
  574. {
  575. word += TranslateLetter(word, phonemes,spell_word);
  576. if(phonemes[0] == phonSWITCH)
  577. {
  578. // change to another language in order to translate this word
  579. strcpy(word_phonemes,phonemes);
  580. return(0);
  581. }
  582. }
  583. SetSpellingStress(phonemes,spell_word);
  584. }
  585. else
  586. if(found == 0)
  587. {
  588. // word's pronunciation is not given in the dictionary list, although
  589. // dictionary_flags may have ben set there
  590. posn = 0;
  591. length = 999;
  592. while(((length < 3) && (length > 0))|| (word_length > 1 && Unpronouncable(word)))
  593. {
  594. // This word looks "unpronouncable", so speak letters individually until we
  595. // find a remainder that we can pronounce.
  596. #ifdef DEBUG3
  597. printf("TR3 length=%d\n",length);
  598. #endif
  599. word += TranslateLetter(word,phonemes,0);
  600. if(phonemes[0] == phonSWITCH)
  601. {
  602. // change to another language in order to translate this word
  603. strcpy(word_phonemes,phonemes);
  604. return(0);
  605. }
  606. if(memcmp(word,"'s ",3) == 0)
  607. {
  608. // remove a 's suffix and pronounce this separately (not as an individual letter)
  609. add_plural_suffix = 1;
  610. word[0] = ' ';
  611. word[1] = ' ';
  612. break;
  613. }
  614. length=0;
  615. while(word[length] != ' ') length++;
  616. word[-1] = ' '; // prevent this affecting the pronunciation of the pronuncable part
  617. }
  618. SetSpellingStress(phonemes,0);
  619. // anything left ?
  620. if(*word != ' ')
  621. {
  622. // Translate the stem
  623. unpron_length = strlen(phonemes);
  624. end_type = TranslateRules(word, phonemes, N_WORD_PHONEMES, end_phonemes, wflags, dictionary_flags);
  625. if(phonemes[0] == phonSWITCH)
  626. {
  627. // change to another language in order to translate this word
  628. strcpy(word_phonemes,phonemes);
  629. return(0);
  630. }
  631. c_temp = word[-1];
  632. found = 0;
  633. while(end_type & SUFX_P)
  634. {
  635. // Found a standard prefix, remove it and retranslate
  636. strcat(prefix_phonemes,end_phonemes);
  637. end_phonemes[0] = 0;
  638. prefix_type = end_type;
  639. if(prefix_type & SUFX_V)
  640. {
  641. expect_verb = 1; // use the verb form of the word
  642. }
  643. word[-1] = c_temp;
  644. for(ix=(prefix_type & 0xf); ix>0; ix--) // num. of characters to remove
  645. {
  646. word++;
  647. while((*word & 0xc0) == 0x80) word++; // for multibyte characters
  648. }
  649. c_temp = word[-1];
  650. word[-1] = ' ';
  651. end_type = 0;
  652. found = LookupDictList(word,phonemes,&dictionary_flags2,SUFX_P | (wflags << 16));
  653. if(dictionary_flags==0)
  654. dictionary_flags = dictionary_flags2;
  655. else
  656. prefix_flags = 1;
  657. if(found == 0)
  658. {
  659. end_type = TranslateRules(word, phonemes, N_WORD_PHONEMES, end_phonemes, 0, dictionary_flags);
  660. if(phonemes[0] == phonSWITCH)
  661. {
  662. // change to another language in order to translate this word
  663. word[-1] = c_temp;
  664. strcpy(word_phonemes,phonemes);
  665. return(0);
  666. }
  667. }
  668. }
  669. if((end_type != 0) && !(end_type & SUFX_P))
  670. {
  671. char phonemes2[N_WORD_PHONEMES];
  672. strcpy(phonemes2,phonemes);
  673. // The word has a standard ending, re-translate without this ending
  674. end_flags = RemoveEnding(word,end_type,word_copy);
  675. phonemes_ptr = &phonemes[unpron_length];
  676. phonemes_ptr[0] = 0;
  677. if(prefix_phonemes[0] != 0)
  678. {
  679. // lookup the stem without the prefix removed
  680. word[-1] = c_temp;
  681. found = LookupDictList(word1,phonemes_ptr,&dictionary_flags2,end_flags | (wflags << 16));
  682. word[-1] = ' ';
  683. if(dictionary_flags==0)
  684. dictionary_flags = dictionary_flags2;
  685. if(found)
  686. prefix_phonemes[0] = 0; // matched whole word, don't need prefix now
  687. if(found || (dictionary_flags2 != 0))
  688. prefix_flags = 1;
  689. }
  690. if(found == 0)
  691. {
  692. found = LookupDictList(word,phonemes_ptr,&dictionary_flags2,end_flags | (wflags << 16));
  693. if(phonemes_ptr[0] == phonSWITCH)
  694. {
  695. // change to another language in order to translate this word
  696. memcpy(word,word_copy,strlen(word_copy));
  697. strcpy(word_phonemes,phonemes_ptr);
  698. return(0);
  699. }
  700. if(dictionary_flags==0)
  701. dictionary_flags = dictionary_flags2;
  702. }
  703. if(found == 0)
  704. {
  705. if(end_type & SUFX_Q)
  706. {
  707. // don't retranslate, use the original lookup result
  708. strcpy(phonemes,phonemes2);
  709. // language specific changes
  710. ApplySpecialAttribute(phonemes,dictionary_flags);
  711. }
  712. else
  713. {
  714. if(end_flags & FLAG_SUFX)
  715. TranslateRules(word, phonemes, N_WORD_PHONEMES, NULL,wflags | FLAG_SUFFIX_REMOVED, dictionary_flags);
  716. else
  717. TranslateRules(word, phonemes, N_WORD_PHONEMES, NULL,wflags,dictionary_flags);
  718. if(phonemes[0] == phonSWITCH)
  719. {
  720. // change to another language in order to translate this word
  721. strcpy(word_phonemes,phonemes);
  722. memcpy(word,word_copy,strlen(word_copy));
  723. word[-1] = c_temp;
  724. return(0);
  725. }
  726. }
  727. }
  728. if((end_type & SUFX_T) == 0)
  729. {
  730. // the default is to add the suffix and then determine the word's stress pattern
  731. AppendPhonemes(phonemes, N_WORD_PHONEMES, end_phonemes);
  732. end_phonemes[0] = 0;
  733. }
  734. }
  735. word[-1] = c_temp;
  736. }
  737. }
  738. if((add_plural_suffix) || (wflags & FLAG_HAS_PLURAL))
  739. {
  740. // s or 's suffix, append [s], [z] or [Iz] depending on previous letter
  741. if(last_char == 'f')
  742. TranslateRules(&word_ss[1], phonemes, N_WORD_PHONEMES, NULL, 0, 0);
  743. else
  744. if(strchr_w("hsx",last_char)==NULL)
  745. TranslateRules(&word_zz[1], phonemes, N_WORD_PHONEMES, NULL, 0, 0);
  746. else
  747. TranslateRules(&word_iz[1], phonemes, N_WORD_PHONEMES, NULL, 0, 0);
  748. }
  749. /* determine stress pattern for this word */
  750. /******************************************/
  751. /* NOTE: this also adds a single PAUSE if the previous word ended
  752. in a primary stress, and this one starts with one */
  753. if(prefix_flags || (strchr(prefix_phonemes,phonSTRESS_P)!=NULL))
  754. {
  755. if((langopts.param[LOPT_PREFIXES]) || (prefix_type & SUFX_T))
  756. {
  757. // German, keep a secondary stress on the stem
  758. SetWordStress(phonemes,dictionary_flags,3,0);
  759. strcpy(word_phonemes,prefix_phonemes);
  760. strcat(word_phonemes,phonemes);
  761. SetWordStress(word_phonemes,dictionary_flags,-1,0);
  762. }
  763. else
  764. {
  765. // stress position affects the whole word, including prefix
  766. strcpy(word_phonemes,prefix_phonemes);
  767. strcat(word_phonemes,phonemes);
  768. SetWordStress(word_phonemes,dictionary_flags,-1,prev_last_stress);
  769. }
  770. }
  771. else
  772. {
  773. if(prefix_phonemes[0] == 0)
  774. SetWordStress(phonemes,dictionary_flags,-1,prev_last_stress);
  775. else
  776. SetWordStress(phonemes,dictionary_flags,-1,0);
  777. strcpy(word_phonemes,prefix_phonemes);
  778. strcat(word_phonemes,phonemes);
  779. }
  780. if(end_phonemes[0] != 0)
  781. {
  782. // a suffix had the SUFX_T option set, add the suffix after the stress pattern has been determined
  783. strcat(word_phonemes,end_phonemes);
  784. }
  785. // if(next_pause > 2)
  786. ix = ((dictionary_flags >> 5) & 7); // dictionary indicates skip next word(s)
  787. if(wtab[ix].flags & FLAG_LAST_WORD)
  788. {
  789. // the word has attribute to stress or unstress when at end of clause
  790. if(dictionary_flags & (FLAG_STRESS_END | FLAG_STRESS_END2))
  791. ChangeWordStress(word_phonemes,4);
  792. // SetWordStress(word_phonemes,0,4,prev_last_stress);
  793. else
  794. if(dictionary_flags & FLAG_UNSTRESS_END)
  795. ChangeWordStress(word_phonemes,3);
  796. // SetWordStress(word_phonemes,0,3,prev_last_stress);
  797. }
  798. if(wflags & FLAG_STRESSED_WORD)
  799. {
  800. // A word is indicated in the source text as stressed
  801. // we need to improve the intonation module to deal better with a clauses tonic
  802. // stress being early in the clause, before enabling this
  803. //SetWordStress(word_phonemes,0,5,prev_last_stress);
  804. }
  805. // dictionary flags for this word give a clue about which alternative pronunciations of
  806. // following words to use.
  807. if(end_type & SUFX_F)
  808. {
  809. // expect a verb form, with or without -s suffix
  810. expect_verb = 2;
  811. expect_verb_s = 2;
  812. }
  813. if(dictionary_flags & FLAG_PASTF)
  814. {
  815. /* expect perfect tense in next two words */
  816. expect_past = 3;
  817. expect_verb = 0;
  818. }
  819. else
  820. if(dictionary_flags & FLAG_VERBF)
  821. {
  822. /* expect a verb in the next word */
  823. expect_verb = 2;
  824. expect_verb_s = 0; /* verb won't have -s suffix */
  825. }
  826. else
  827. if(dictionary_flags & FLAG_VERBSF)
  828. {
  829. // expect a verb, must have a -s suffix
  830. expect_verb = 0;
  831. expect_verb_s = 2;
  832. expect_past = 0;
  833. }
  834. else
  835. if(dictionary_flags & FLAG_NOUNF)
  836. {
  837. /* not expecting a verb next */
  838. expect_verb = 0;
  839. expect_verb_s = 0;
  840. expect_past = 0;
  841. }
  842. if((word[0] != 0) && (!(dictionary_flags & FLAG_VERB_EXT)))
  843. {
  844. if(expect_verb > 0)
  845. expect_verb -= 1;
  846. if(expect_verb_s > 0)
  847. expect_verb_s -= 1;
  848. if(expect_past > 0)
  849. expect_past -= 1;
  850. }
  851. if((word_length == 1) && iswalpha(first_char) && (first_char != 'i'))
  852. {
  853. // English Specific !!!!
  854. // any single letter before a dot is an abbreviation, except 'I'
  855. dictionary_flags |= FLAG_DOT;
  856. }
  857. return(dictionary_flags);
  858. } // end of TranslateWord
  859. static void SetPlist2(PHONEME_LIST2 *p, unsigned char phcode)
  860. {//==========================================================
  861. p->phcode = phcode;
  862. p->stress = 0;
  863. p->tone_number = 0;
  864. p->synthflags = 0;
  865. p->sourceix = 0;
  866. }
  867. static int CountSyllables(unsigned char *phonemes)
  868. {//===============================================
  869. int count = 0;
  870. int phon;
  871. while((phon = *phonemes++) != 0)
  872. {
  873. if(phoneme_tab[phon]->type == phVOWEL)
  874. count++;
  875. }
  876. return(count);
  877. }
  878. int Translator::TranslateWord2(char *word, WORD_TAB *wtab, int pre_pause, int next_pause)
  879. {//======================================================================================
  880. int flags=0;
  881. int stress;
  882. int next_stress;
  883. int next_tone=0;
  884. unsigned char *p;
  885. int srcix;
  886. int embedded_flag=0;
  887. int embedded_cmd;
  888. int value;
  889. int found_dict_flag;
  890. unsigned char ph_code;
  891. PHONEME_LIST2 *plist2;
  892. PHONEME_TAB *ph;
  893. int max_stress;
  894. int max_stress_ix=0;
  895. int prev_vowel = -1;
  896. int pitch_raised = 0;
  897. int switch_phonemes = -1;
  898. int first_phoneme = 1;
  899. int source_ix;
  900. int len;
  901. int sylimit; // max. number of syllables in a word to be combined with a preceding preposition
  902. char *new_language;
  903. unsigned char bad_phoneme[4];
  904. len = wtab->length;
  905. if(len > 31) len = 31;
  906. source_ix = (wtab->sourceix & 0x7ff) | (len << 11); // bits 0-10 sourceix, bits 11-15 word length
  907. word_flags = wtab[0].flags;
  908. if(word_flags & FLAG_EMBEDDED)
  909. {
  910. embedded_flag = SFLAG_EMBEDDED;
  911. value = embedded_list[embedded_read] >> 8;
  912. switch(embedded_cmd = embedded_list[embedded_read] & 0x1f)
  913. {
  914. case EMBED_Y:
  915. option_sayas = value;
  916. break;
  917. case EMBED_F:
  918. option_emphasis = value;
  919. break;
  920. case EMBED_B:
  921. // break command
  922. if(value == 0)
  923. pre_pause = 0; // break=none
  924. else
  925. pre_pause += value;
  926. break;
  927. }
  928. while((embedded_list[embedded_read++] & 0x80) == 0); // read over the embedded commands for this word
  929. }
  930. if(word[0] == 0)
  931. {
  932. // nothing to translate
  933. word_phonemes[0] = 0;
  934. return(0);
  935. }
  936. // after a $pause word attribute, ignore a $pause attribute on the next two words
  937. if(prepause_timeout > 0)
  938. prepause_timeout--;
  939. if((option_sayas & 0xf0) == 0x10)
  940. {
  941. // SAYAS_CHARS, SAYAS_GLYPHS, or SAYAS_SINGLECHARS. Pause between each word.
  942. pre_pause += 4;
  943. }
  944. if(word_flags & FLAG_FIRST_UPPER)
  945. {
  946. if((option_capitals > 2) && (embedded_ix < N_EMBEDDED_LIST-6))
  947. {
  948. // indicate capital letter by raising pitch
  949. if(embedded_flag)
  950. embedded_list[embedded_ix-1] &= ~0x80; // already embedded command before this word, remove terminator
  951. if((pitch_raised = option_capitals) == 3)
  952. pitch_raised = 20; // default pitch raise for capitals
  953. embedded_list[embedded_ix++] = EMBED_P+0x40+0x80 + (pitch_raised << 8); // raise pitch
  954. embedded_flag = SFLAG_EMBEDDED;
  955. }
  956. }
  957. p = (unsigned char *)translator->word_phonemes;
  958. if(word_flags & FLAG_PHONEMES)
  959. {
  960. // The input is in phoneme mnemonics, not language text
  961. EncodePhonemes(word,word_phonemes,bad_phoneme);
  962. flags = FLAG_FOUND;
  963. }
  964. else
  965. {
  966. flags = translator->TranslateWord(word, next_pause, wtab);
  967. if((flags & FLAG_ALT2_TRANS) && ((sylimit = langopts.param[LOPT_COMBINE_WORDS]) > 0))
  968. {
  969. char *p2;
  970. int ok = 1;
  971. int flags2;
  972. int c_word2;
  973. char ph_buf[N_WORD_PHONEMES];
  974. // LANG=cs,sk
  975. // combine a preposition with the following word
  976. p2 = word;
  977. while(*p2 != ' ') p2++;
  978. utf8_in(&c_word2, p2+1, 0); // first character of the next word;
  979. if(!iswalpha(c_word2))
  980. {
  981. ok =0;
  982. }
  983. if(sylimit & 0x100)
  984. {
  985. // only if the second word has $alt attribute
  986. strcpy(ph_buf,word_phonemes);
  987. flags2 = translator->TranslateWord(p2+1, 0, wtab+1);
  988. if((flags2 & FLAG_ALT_TRANS) == 0)
  989. {
  990. ok = 0;
  991. strcpy(word_phonemes,ph_buf);
  992. }
  993. }
  994. if(ok)
  995. {
  996. *p2 = '-'; // replace next space by hyphen
  997. flags = translator->TranslateWord(word, next_pause, wtab); // translate the combined word
  998. if(CountSyllables(p) > (sylimit & 0xf))
  999. {
  1000. // revert to separate words
  1001. *p2 = ' ';
  1002. flags = translator->TranslateWord(word, next_pause, wtab);
  1003. }
  1004. else
  1005. {
  1006. flags |= FLAG_SKIPWORDS_1;
  1007. }
  1008. }
  1009. }
  1010. if(p[0] == phonSWITCH)
  1011. {
  1012. // this word uses a different language
  1013. new_language = (char *)(&p[1]);
  1014. if(new_language[0]==0)
  1015. new_language = "en";
  1016. if((switch_phonemes = SelectPhonemeTableName(new_language)) >= 0)
  1017. {
  1018. if((translator2 != NULL) && (strcmp(new_language,translator2_language) != 0))
  1019. {
  1020. // we already have an alternative translator, but not for the required language, delete it
  1021. delete translator2;
  1022. translator2 = NULL;
  1023. }
  1024. if(translator2 == NULL)
  1025. {
  1026. translator2 = SelectTranslator(new_language);
  1027. strcpy(translator2_language,new_language);
  1028. if(translator2->LoadDictionary(new_language,0) != 0)
  1029. {
  1030. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  1031. switch_phonemes = -1;
  1032. translator2_language[0] = 0;
  1033. }
  1034. }
  1035. }
  1036. if(switch_phonemes >= 0)
  1037. {
  1038. // re-translate the word using the new translator
  1039. flags = translator2->TranslateWord(word, next_pause, wtab);
  1040. strcpy((char *)p,translator2->word_phonemes);
  1041. if(p[0] == phonSWITCH)
  1042. {
  1043. // the second translator doesn't want to process this word
  1044. switch_phonemes = -1;
  1045. }
  1046. }
  1047. if(switch_phonemes < 0)
  1048. {
  1049. // language code is not recognised or 2nd translator won't translate it
  1050. p[0] = phonSCHWA; // just say something
  1051. p[1] = phonSCHWA;
  1052. p[2] = 0;
  1053. }
  1054. }
  1055. if(!(word_flags & FLAG_LAST_WORD) && !(word_flags & FLAG_HYPHEN))
  1056. {
  1057. if(flags & FLAG_PAUSE1)
  1058. {
  1059. if(pre_pause < 1)
  1060. pre_pause = 1;
  1061. }
  1062. if((flags & FLAG_PREPAUSE) && (prepause_timeout == 0))
  1063. {
  1064. // the word is marked in the dictionary list with $pause
  1065. if(pre_pause < 4) pre_pause = 4;
  1066. prepause_timeout = 3;
  1067. }
  1068. }
  1069. if((option_emphasis >= 3) && (pre_pause < 2))
  1070. pre_pause = 2;
  1071. }
  1072. plist2 = &ph_list2[n_ph_list2];
  1073. stress = 0;
  1074. next_stress = 0;
  1075. srcix = 0;
  1076. max_stress = -1;
  1077. found_dict_flag = 0;
  1078. if(flags & FLAG_FOUND)
  1079. found_dict_flag = SFLAG_DICTIONARY;
  1080. while((pre_pause > 0) && (n_ph_list2 < N_PHONEME_LIST-4))
  1081. {
  1082. // add pause phonemes here. Either because of punctuation (brackets or quotes) in the
  1083. // text, or because the word is marked in the dictionary lookup as a conjunction
  1084. if(pre_pause > 1)
  1085. {
  1086. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE);
  1087. pre_pause -= 2;
  1088. }
  1089. else
  1090. {
  1091. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_NOLINK);
  1092. pre_pause--;
  1093. }
  1094. end_stressed_vowel = 0; // forget about the previous word
  1095. prev_dict_flags = 0;
  1096. }
  1097. if((option_capitals==1) && (word_flags & FLAG_FIRST_UPPER))
  1098. {
  1099. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_SHORT);
  1100. SetPlist2(&ph_list2[n_ph_list2++],phonCAPITAL);
  1101. if((word_flags & FLAG_ALL_UPPER) && IsAlpha(word[1]))
  1102. {
  1103. // word > 1 letter and all capitals
  1104. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_SHORT);
  1105. SetPlist2(&ph_list2[n_ph_list2++],phonCAPITAL);
  1106. }
  1107. }
  1108. if(switch_phonemes >= 0)
  1109. {
  1110. // this word uses a different phoneme table
  1111. SetPlist2(&ph_list2[n_ph_list2],phonSWITCH);
  1112. ph_list2[n_ph_list2++].tone_number = switch_phonemes; // temporary phoneme table number
  1113. }
  1114. // remove initial pause from a word if it follows a hyphen
  1115. if((word_flags & FLAG_HYPHEN) && (phoneme_tab[*p]->type == phPAUSE))
  1116. p++;
  1117. while(((ph_code = *p++) != 0) && (n_ph_list2 < N_PHONEME_LIST-4))
  1118. {
  1119. if(ph_code == 255)
  1120. continue; // unknown phoneme
  1121. // Add the phonemes to the first stage phoneme list (ph_list2)
  1122. ph = phoneme_tab[ph_code];
  1123. if(ph->type == phSTRESS)
  1124. {
  1125. // don't add stress phonemes codes to the list, but give their stress
  1126. // value to the next vowel phoneme
  1127. // std_length is used to hold stress number or (if >10) a tone number for a tone language
  1128. if(ph->spect == 0)
  1129. next_stress = ph->std_length;
  1130. else
  1131. {
  1132. // for tone languages, the tone number for a syllable follows the vowel
  1133. if(prev_vowel >= 0)
  1134. {
  1135. ph_list2[prev_vowel].tone_number = ph_code;
  1136. }
  1137. else
  1138. {
  1139. next_tone = ph_code; // no previous vowel, apply to the next vowel
  1140. }
  1141. }
  1142. }
  1143. else
  1144. if(ph_code == phonSYLLABIC)
  1145. {
  1146. // mark the previous phoneme as a syllabic consonant
  1147. ph_list2[n_ph_list2-1].synthflags |= SFLAG_SYLLABLE;
  1148. ph_list2[n_ph_list2-1].stress = next_stress;
  1149. }
  1150. else
  1151. if(ph_code == phonLENGTHEN)
  1152. {
  1153. ph_list2[n_ph_list2-1].synthflags |= SFLAG_LENGTHEN;
  1154. }
  1155. else
  1156. if(ph_code == phonEND_WORD)
  1157. {
  1158. // a || symbol in a phoneme string was used to indicate a word boundary
  1159. // Don't add this phoneme to the list, but make sure the next phoneme has
  1160. // a newword indication
  1161. srcix = source_ix+1;
  1162. }
  1163. else
  1164. if(ph_code == phonX1)
  1165. {
  1166. // a language specific action
  1167. if(langopts.param[LOPT_IT_DOUBLING])
  1168. {
  1169. flags |= FLAG_DOUBLING;
  1170. }
  1171. }
  1172. else
  1173. {
  1174. ph_list2[n_ph_list2].phcode = ph_code;
  1175. ph_list2[n_ph_list2].tone_number = 0;
  1176. ph_list2[n_ph_list2].synthflags = embedded_flag | found_dict_flag;
  1177. embedded_flag = 0;
  1178. ph_list2[n_ph_list2].sourceix = srcix;
  1179. srcix = 0;
  1180. if(ph->type == phVOWEL)
  1181. {
  1182. stress = next_stress;
  1183. next_stress = 0;
  1184. if((prev_vowel >= 0) && (n_ph_list2-1) != prev_vowel)
  1185. ph_list2[n_ph_list2-1].stress = stress; // set stress for previous consonant
  1186. ph_list2[n_ph_list2].synthflags |= SFLAG_SYLLABLE;
  1187. prev_vowel = n_ph_list2;
  1188. if(stress > max_stress)
  1189. {
  1190. max_stress = stress;
  1191. max_stress_ix = n_ph_list2;
  1192. }
  1193. if(next_tone != 0)
  1194. {
  1195. ph_list2[n_ph_list2].tone_number = next_tone;
  1196. next_tone=0;
  1197. }
  1198. }
  1199. else
  1200. {
  1201. if(first_phoneme && langopts.param[LOPT_IT_DOUBLING])
  1202. {
  1203. if((prev_dict_flags & FLAG_DOUBLING) || (end_stressed_vowel && (langopts.param[LOPT_IT_DOUBLING] == 2)))
  1204. {
  1205. // italian, double the initial consonant if the previous word ends with a
  1206. // stressed vowel, or is marked with a flag
  1207. ph_list2[n_ph_list2].synthflags |= SFLAG_LENGTHEN;
  1208. }
  1209. }
  1210. }
  1211. ph_list2[n_ph_list2].stress = stress;
  1212. n_ph_list2++;
  1213. first_phoneme = 0;
  1214. }
  1215. }
  1216. plist2->sourceix = source_ix; // ????
  1217. end_stressed_vowel = 0;
  1218. if((stress >= 4) && (phoneme_tab[ph_list2[n_ph_list2-1].phcode]->type == phVOWEL))
  1219. {
  1220. end_stressed_vowel = 1; // word ends with a stressed vowel
  1221. }
  1222. if(switch_phonemes >= 0)
  1223. {
  1224. // this word uses a different phoneme table, now switch back
  1225. SelectPhonemeTable(voice->phoneme_tab_ix);
  1226. SetPlist2(&ph_list2[n_ph_list2],phonSWITCH);
  1227. ph_list2[n_ph_list2++].tone_number = voice->phoneme_tab_ix; // original phoneme table number
  1228. }
  1229. if(pitch_raised > 0)
  1230. {
  1231. embedded_list[embedded_ix++] = EMBED_P+0x60+0x80 + (pitch_raised << 8); // lower pitch
  1232. SetPlist2(&ph_list2[n_ph_list2],phonPAUSE_SHORT);
  1233. ph_list2[n_ph_list2++].synthflags = SFLAG_EMBEDDED;
  1234. }
  1235. if(flags & FLAG_STRESS_END2)
  1236. {
  1237. // this's word's stress could be increased later
  1238. ph_list2[max_stress_ix].stress |= 0x80;
  1239. }
  1240. prev_dict_flags = flags;
  1241. return(flags);
  1242. } // end of TranslateWord2
  1243. static int EmbeddedCommand(unsigned int &source_index)
  1244. {//===================================================
  1245. // An embedded command to change the pitch, volume, etc.
  1246. // returns number of commands added to embedded_list
  1247. // pitch,speed,amplitude,expression,reverb,tone,voice,sayas
  1248. const char *commands = "PSARHTIVYMUBF";
  1249. int value = -1;
  1250. int sign = 0;
  1251. unsigned char c;
  1252. char *p;
  1253. int cmd;
  1254. c = source[source_index];
  1255. if(c == '+')
  1256. {
  1257. sign = 0x40;
  1258. source_index++;
  1259. }
  1260. else
  1261. if(c == '-')
  1262. {
  1263. sign = 0x60;
  1264. source_index++;
  1265. }
  1266. if(isdigit(source[source_index]))
  1267. {
  1268. value = atoi(&source[source_index]);
  1269. while(isdigit(source[source_index]))
  1270. source_index++;
  1271. }
  1272. c = source[source_index++];
  1273. if(embedded_ix >= (N_EMBEDDED_LIST - 2))
  1274. return(0); // list is full
  1275. if((p = strchr_w(commands,c)) == NULL)
  1276. return(0);
  1277. cmd = (p - commands)+1;
  1278. if(value == -1)
  1279. {
  1280. value = embedded_default[cmd];
  1281. sign = 0;
  1282. }
  1283. if(cmd == EMBED_Y)
  1284. {
  1285. option_sayas2 = value;
  1286. count_sayas_digits = 0;
  1287. }
  1288. embedded_list[embedded_ix++] = cmd + sign + (value << 8);
  1289. return(1);
  1290. } // end of EmbeddedCommand
  1291. int Translator::TranslateChar(char *ptr, int prev_in, int c, int next_in, int *insert)
  1292. {//===================================================================================
  1293. // To allow language specific examination and replacement of characters
  1294. const wchar_t *p;
  1295. unsigned int new_c;
  1296. int upper_case = 0;
  1297. if(c == 0) return(0);
  1298. if(langopts.replace_chars != NULL)
  1299. {
  1300. // there is a list of character codes to be substituted with alternative codes
  1301. if((p = wcschr(langopts.replace_chars,c)) == NULL)
  1302. {
  1303. // Try converting to lower case
  1304. if((p = wcschr(langopts.replace_chars,towlower(c))) != NULL)
  1305. upper_case =1;
  1306. }
  1307. if(p != NULL)
  1308. {
  1309. new_c = langopts.replacement_chars[p - langopts.replace_chars];
  1310. if(new_c & 0xffe00000)
  1311. {
  1312. // there is a second character to be inserted (don't convert the case of the second character)
  1313. *insert = (new_c >> 16);
  1314. new_c &= 0xffff;
  1315. }
  1316. if(upper_case)
  1317. new_c = towupper(new_c);
  1318. return(new_c);
  1319. }
  1320. }
  1321. return(c);
  1322. }
  1323. void *Translator::TranslateClause(FILE *f_text, const void *vp_input, int *tone_out, char **voice_change)
  1324. {//======================================================================================================
  1325. int ix;
  1326. int c;
  1327. int cc;
  1328. unsigned int source_index=0;
  1329. unsigned int prev_source_index=0;
  1330. int prev_in;
  1331. int prev_out=' ';
  1332. int prev_out2;
  1333. int prev_in2=0;
  1334. int next_in;
  1335. int char_inserted=0;
  1336. int clause_pause;
  1337. int pre_pause=0;
  1338. int pre_pause_add=0;
  1339. int word_mark = 0;
  1340. int all_upper_case=FLAG_ALL_UPPER;
  1341. int finished;
  1342. int single_quoted;
  1343. int phoneme_mode = 0;
  1344. int dict_flags; // returned from dictionary lookup
  1345. int word_flags; // set here
  1346. int embedded_count = 0;
  1347. char *word;
  1348. char *p;
  1349. int j, k;
  1350. int n_digits;
  1351. unsigned short charix[N_TR_SOURCE+1];
  1352. WORD_TAB words[N_CLAUSE_WORDS];
  1353. int word_count=0; // index into words
  1354. char sbuf[512];
  1355. int terminator;
  1356. int tone;
  1357. p_textinput = (char *)vp_input;
  1358. p_wchar_input = (wchar_t *)vp_input;
  1359. embedded_ix = 0;
  1360. embedded_read = 0;
  1361. if((clause_start_char = count_characters) < 0)
  1362. clause_start_char = 0;
  1363. clause_start_word = count_words + 1;
  1364. for(ix=0; ix<N_TR_SOURCE; ix++)
  1365. charix[ix] = 0;
  1366. terminator = translator->ReadClause(f_text,source,charix,N_TR_SOURCE);
  1367. charix[N_TR_SOURCE] = count_characters;
  1368. clause_pause = (terminator & 0xff) * 10; // mS
  1369. tone = (terminator & 0xf00) >> 8;
  1370. for(p=source; *p != 0; p++)
  1371. {
  1372. if(!isspace2(*p))
  1373. {
  1374. break;
  1375. }
  1376. }
  1377. if(*p == 0)
  1378. {
  1379. // No characters except spaces. This is not a sentence.
  1380. // Don't add this pause, just make up the previous pause to this value;
  1381. clause_pause -= max_clause_pause;
  1382. if(clause_pause < 0)
  1383. clause_pause = 0;
  1384. terminator &= ~CLAUSE_BIT_SENTENCE; // clear sentence bit
  1385. max_clause_pause += clause_pause;
  1386. }
  1387. else
  1388. {
  1389. max_clause_pause = clause_pause;
  1390. }
  1391. if(new_sentence)
  1392. {
  1393. count_sentences++;
  1394. if(skip_sentences > 0)
  1395. {
  1396. skip_sentences--;
  1397. if(skip_sentences == 0)
  1398. skipping_text = 0;
  1399. }
  1400. }
  1401. ph_list2[0].phcode = phonPAUSE_SHORT;
  1402. ph_list2[0].stress = 0;
  1403. ph_list2[0].tone_number = 0;
  1404. ph_list2[0].sourceix = 0;
  1405. n_ph_list2 = 1;
  1406. prev_last_stress = 0;
  1407. prepause_timeout = 0;
  1408. word_count = 0;
  1409. single_quoted = 0;
  1410. word_flags = 0;
  1411. expect_verb=0;
  1412. expect_past=0;
  1413. expect_verb_s=0;
  1414. end_stressed_vowel=0;
  1415. prev_dict_flags = 0;
  1416. sbuf[0] = 0;
  1417. sbuf[1] = ' ';
  1418. sbuf[2] = ' ';
  1419. ix = 3;
  1420. prev_in = ' ';
  1421. words[0].start = ix;
  1422. words[0].flags = 0;
  1423. finished = 0;
  1424. for(j=0; charix[j]==0; j++);
  1425. words[0].sourceix = charix[j];
  1426. for(k=j; charix[k]!=0; k++);
  1427. words[0].length = k-j;
  1428. while(!finished && (ix < (int)sizeof(sbuf))&& (n_ph_list2 < N_PHONEME_LIST-4))
  1429. {
  1430. prev_out2 = prev_out;
  1431. utf8_in(&prev_out,&sbuf[ix-1],1); // prev_out = sbuf[ix-1];
  1432. if(prev_in2 != 0)
  1433. {
  1434. prev_in = prev_in2;
  1435. prev_in2 = 0;
  1436. }
  1437. else
  1438. if(source_index > 0)
  1439. {
  1440. utf8_in(&prev_in,&source[source_index-1],1); // prev_in = source[source_index-1];
  1441. }
  1442. if(prev_source_index == source_index)
  1443. char_inserted = 0; // have done an unget, so discard any character to be inserted
  1444. prev_source_index = source_index;
  1445. if(char_inserted)
  1446. {
  1447. c = char_inserted;
  1448. char_inserted = 0;
  1449. }
  1450. else
  1451. {
  1452. source_index += utf8_in(&cc,&source[source_index],0); // cc = source[source_index++];
  1453. c = cc;
  1454. }
  1455. utf8_in(&next_in,&source[source_index],0);
  1456. if((c == CTRL_EMBEDDED) || (c == ctrl_embedded))
  1457. {
  1458. // start of embedded command in the text
  1459. int srcix = source_index-1;
  1460. if(prev_in != ' ')
  1461. {
  1462. c = ' ';
  1463. prev_in2 = c;
  1464. source_index--;
  1465. }
  1466. else
  1467. {
  1468. embedded_count += EmbeddedCommand(source_index);
  1469. prev_in2 = prev_in;
  1470. // replace the embedded command by spaces
  1471. memset(&source[srcix],' ',source_index-srcix);
  1472. source_index = srcix;
  1473. continue;
  1474. }
  1475. }
  1476. if(phoneme_mode)
  1477. {
  1478. all_upper_case = FLAG_PHONEMES;
  1479. if((c == ']') && (next_in == ']'))
  1480. {
  1481. phoneme_mode = 0;
  1482. source_index++;
  1483. c = ' ';
  1484. }
  1485. }
  1486. else
  1487. if((option_sayas2 & 0xf0) == SAYAS_DIGITS)
  1488. {
  1489. if(iswdigit(c))
  1490. {
  1491. count_sayas_digits++;
  1492. if(count_sayas_digits > (option_sayas2 & 0xf))
  1493. {
  1494. // break after the specified number of digits
  1495. c = ' ';
  1496. source_index = prev_source_index; // unget
  1497. count_sayas_digits = 0;
  1498. }
  1499. }
  1500. else
  1501. {
  1502. count_sayas_digits = 0;
  1503. if(iswdigit(prev_out))
  1504. {
  1505. c = ' ';
  1506. source_index = prev_source_index; // unget
  1507. }
  1508. }
  1509. }
  1510. else
  1511. if((option_sayas2 & 0xf0) != 1)
  1512. {
  1513. if((c == '/') && (langopts.testing & 2) && isdigit(next_in) && IsAlpha(prev_out))
  1514. {
  1515. // TESTING, explicit indication of stressed syllable by /2 after the word
  1516. word_mark = next_in-'0';
  1517. source_index++;
  1518. c = ' ';
  1519. }
  1520. if((c == 0x92) || (c == 0xb4) || (c == 0x2019))
  1521. c = '\''; // 'microsoft' quote or sexed closing single quote - possibly used as apostrophe
  1522. if((c == '?') && IsAlpha(prev_out) && IsAlpha(next_in))
  1523. {
  1524. // ? between two letters may be a smart-quote replaced by ?
  1525. c = '\'';
  1526. }
  1527. c = TranslateChar(&source[source_index], prev_in,c, next_in, &char_inserted); // optional language specific function
  1528. if(char_inserted)
  1529. next_in = char_inserted;
  1530. if(!IsAlpha(c) && !iswspace(c) && (c != '\''))
  1531. {
  1532. if(IsAlpha(prev_out))
  1533. {
  1534. if(langopts.tone_numbers && isdigit(c) && !isdigit(next_in))
  1535. {
  1536. // allow a tone number as part of the word
  1537. }
  1538. else
  1539. {
  1540. c = ' '; // ensure we have an end-of-word terminator
  1541. source_index = prev_source_index;
  1542. }
  1543. }
  1544. }
  1545. if(iswdigit(prev_out))
  1546. {
  1547. if(!iswdigit(c) && (c != '.') && (c != ','))
  1548. {
  1549. c = ' '; // terminate digit string with a space
  1550. source_index = prev_source_index;
  1551. }
  1552. }
  1553. if((c == '[') && (next_in == '[') && option_phoneme_input)
  1554. {
  1555. phoneme_mode = FLAG_PHONEMES;
  1556. source_index++;
  1557. continue;
  1558. }
  1559. if(c == 0)
  1560. {
  1561. finished = 1;
  1562. c = ' ';
  1563. }
  1564. else
  1565. if(IsAlpha(c))
  1566. {
  1567. if(!IsAlpha(prev_out) || (langopts.ideographs && (c >= 0x3000)))
  1568. {
  1569. if((prev_out != ' ') && (prev_out != '\''))
  1570. {
  1571. // start of word, insert space if not one there already
  1572. c = ' ';
  1573. source_index = prev_source_index; // unget
  1574. }
  1575. else
  1576. {
  1577. if(iswupper(c))
  1578. word_flags |= FLAG_FIRST_UPPER;
  1579. if((prev_out == ' ') && iswdigit(sbuf[ix-2]) && !iswdigit(prev_in))
  1580. {
  1581. // word, following a number, but with a space between
  1582. // Add an extra space, to distinguish "2 a" from "2a"
  1583. sbuf[ix++] = ' ';
  1584. words[word_count].start++;
  1585. }
  1586. }
  1587. }
  1588. if(iswupper(c))
  1589. {
  1590. c = towlower(c);
  1591. if(iswlower(prev_in))
  1592. {
  1593. c = ' '; // lower case followed by upper case, treat as new word
  1594. prev_in2 = c;
  1595. source_index = prev_source_index; // unget
  1596. }
  1597. //#ifdef deleted
  1598. // changed to break after the last uppercase letter, not before. See below
  1599. else
  1600. if((c != ' ') && iswupper(prev_in) && iswlower(next_in) &&
  1601. (memcmp(&source[source_index],"s ",2) != 0)) // ENGLISH specific plural
  1602. {
  1603. c = ' '; // change from upper to lower case, start new word at the last uppercase
  1604. prev_in2 = c;
  1605. source_index = prev_source_index; // unget
  1606. }
  1607. //#endif
  1608. }
  1609. else
  1610. {
  1611. #ifdef deleted
  1612. if(iswupper(prev_in) && iswalpha(prev_out2) &&
  1613. (memcmp(&source[source_index-1],"s ",2) != 0)) // ENGLISH specific plural
  1614. {
  1615. // change to break after the last uppercase letter, not before.
  1616. c = ' '; // more than one upper case followed by lower case, treat as new word
  1617. prev_in2 = c;
  1618. source_index = prev_source_index; // unget
  1619. }
  1620. #endif
  1621. if((all_upper_case) && ((ix - words[word_count].start) > 1))
  1622. {
  1623. if((c == 's') && (next_in==' '))
  1624. {
  1625. c = ' ';
  1626. all_upper_case |= FLAG_HAS_PLURAL;
  1627. if(sbuf[ix-1] == '\'')
  1628. sbuf[ix-1] = ' ';
  1629. }
  1630. else
  1631. all_upper_case = 0; // current word contains lower case letters, not "'s"
  1632. }
  1633. else
  1634. all_upper_case = 0;
  1635. }
  1636. }
  1637. else
  1638. if(c=='-')
  1639. {
  1640. if(IsAlpha(prev_in) && IsAlpha(next_in))
  1641. {
  1642. // '-' between two letters is a hyphen, treat as a space
  1643. word_flags |= FLAG_HYPHEN;
  1644. words[word_count-1].flags |= FLAG_HYPHEN_AFTER;
  1645. c = ' ';
  1646. }
  1647. else
  1648. if((prev_in==' ') && (next_in==' '))
  1649. {
  1650. // ' - ' dash between two spaces, treat as pause
  1651. c = ' ';
  1652. pre_pause_add = 4;
  1653. }
  1654. else
  1655. if(next_in=='-')
  1656. {
  1657. // double hyphen, treat as pause
  1658. source_index++;
  1659. c = ' ';
  1660. pre_pause_add = 4;
  1661. }
  1662. else
  1663. if((prev_out == ' ') && IsAlpha(sbuf[ix-2]) && !IsAlpha(prev_in))
  1664. {
  1665. // insert extra space between a word + space + hyphen, to distinguish 'a -2' from 'a-2'
  1666. sbuf[ix++] = ' ';
  1667. words[word_count].start++;
  1668. }
  1669. }
  1670. else
  1671. if(c == '\'')
  1672. {
  1673. if(iswalnum(prev_in) && IsAlpha(next_in))
  1674. {
  1675. // between two letters, consider apostrophe as part of the word
  1676. single_quoted = 0;
  1677. }
  1678. else
  1679. if((wcschr(char_plus_apostrophe,prev_in) != 0) && (prev_out2 == ' '))
  1680. {
  1681. // consider single character plus apostrophe as a word
  1682. single_quoted = 0;
  1683. if(next_in == ' ')
  1684. {
  1685. source_index++; // skip following space
  1686. }
  1687. }
  1688. else
  1689. {
  1690. if((prev_out == 's') && (single_quoted==0))
  1691. {
  1692. // looks like apostrophe after an 's'
  1693. c = ' ';
  1694. }
  1695. else
  1696. {
  1697. if(iswspace(prev_out))
  1698. single_quoted = 1;
  1699. else
  1700. single_quoted = 0;
  1701. pre_pause_add = 4; // single quote
  1702. c = ' ';
  1703. }
  1704. }
  1705. }
  1706. else
  1707. if(IsBracket(c))
  1708. {
  1709. pre_pause_add = 4;
  1710. c = ' ';
  1711. }
  1712. else
  1713. if(lookupwchar(breaks,c) != 0)
  1714. {
  1715. c = ' '; // various characters to treat as space
  1716. }
  1717. else
  1718. if(iswdigit(c))
  1719. {
  1720. if(langopts.tone_numbers && IsAlpha(prev_out) && !isdigit(next_in))
  1721. {
  1722. }
  1723. else
  1724. if((prev_out != ' ') && !iswdigit(prev_out) && (prev_out != langopts.decimal_sep)) // TEST2
  1725. {
  1726. c = ' ';
  1727. source_index = prev_source_index;
  1728. }
  1729. else
  1730. if((prev_out == ' ') && IsAlpha(sbuf[ix-2]) && !IsAlpha(prev_in))
  1731. {
  1732. // insert extra space between a word and a number, to distinguish 'a 2' from 'a2'
  1733. sbuf[ix++] = ' ';
  1734. words[word_count].start++;
  1735. }
  1736. }
  1737. }
  1738. if(iswspace(c))
  1739. {
  1740. if(prev_out == ' ')
  1741. {
  1742. continue; // multiple spaces
  1743. }
  1744. // end of 'word'
  1745. sbuf[ix++] = ' ';
  1746. if((ix > words[word_count].start) && (word_count < N_CLAUSE_WORDS-1))
  1747. {
  1748. if(embedded_count > 0)
  1749. {
  1750. // there are embedded commands before this word
  1751. embedded_list[embedded_ix-1] |= 0x80; // terminate list of commands for this word
  1752. words[word_count].flags |= FLAG_EMBEDDED;
  1753. embedded_count = 0;
  1754. }
  1755. words[word_count].pre_pause = pre_pause;
  1756. words[word_count].flags |= (all_upper_case | word_flags);
  1757. words[word_count].wmark = word_mark;
  1758. if(pre_pause > 0)
  1759. {
  1760. // insert an extra space before the word, to prevent influence from previous word across the pause
  1761. for(j=ix; j>words[word_count].start; j--)
  1762. {
  1763. sbuf[j] = sbuf[j-1];
  1764. }
  1765. sbuf[j] = ' ';
  1766. words[word_count].start++;
  1767. ix++;
  1768. }
  1769. word_count++;
  1770. words[word_count].start = ix;
  1771. words[word_count].flags = 0;
  1772. for(j=source_index; charix[j] == 0; j++);
  1773. words[word_count].sourceix = charix[j];
  1774. for(k=j; charix[k]!=0; k++);
  1775. words[word_count].length = k-j;
  1776. word_flags = 0;
  1777. pre_pause = 0;
  1778. word_mark = 0;
  1779. all_upper_case = FLAG_ALL_UPPER;
  1780. }
  1781. }
  1782. else
  1783. {
  1784. ix += utf8_out(c,&sbuf[ix]); // sbuf[ix++] = c;
  1785. }
  1786. if(pre_pause_add > pre_pause)
  1787. pre_pause = pre_pause_add;
  1788. pre_pause_add = 0;
  1789. }
  1790. if((word_count==0) && (embedded_count > 0))
  1791. {
  1792. // add a null 'word' to carry the embedded command flag
  1793. embedded_list[embedded_ix-1] |= 0x80;
  1794. words[word_count].flags |= FLAG_EMBEDDED;
  1795. word_count = 1;
  1796. }
  1797. clause_end = &sbuf[ix-1];
  1798. sbuf[ix] = 0;
  1799. words[0].pre_pause = 0; // don't add extra pause at beginning of clause
  1800. words[word_count].pre_pause = 8;
  1801. if(word_count > 0)
  1802. words[word_count-1].flags |= FLAG_LAST_WORD;
  1803. for(ix=0; ix<word_count; ix++)
  1804. {
  1805. int j;
  1806. char *pn;
  1807. char *pw;
  1808. char number_buf[80];
  1809. // start speaking at a specified word position in the text?
  1810. count_words++;
  1811. if(skip_words > 0)
  1812. {
  1813. skip_words--;
  1814. if(skip_words == 0)
  1815. skipping_text = 0;
  1816. }
  1817. if(skipping_text)
  1818. continue;
  1819. word = pw = &sbuf[words[ix].start];
  1820. for(n_digits=0; iswdigit(word[n_digits]); n_digits++); // count consecutive digits
  1821. if((n_digits > 4) && (word[0] != '0'))
  1822. {
  1823. // word is entirely digits, insert commas and break into 3 digit "words"
  1824. number_buf[0] = ' ';
  1825. pn = &number_buf[1];
  1826. j = n_digits;
  1827. while(pn < &number_buf[sizeof(number_buf)-3])
  1828. {
  1829. if(!isdigit(c = *pw++) && (c != langopts.decimal_sep))
  1830. break;
  1831. *pn++ = c;
  1832. if((--j > 0) && (j % 3)==0)
  1833. {
  1834. if(langopts.thousands_sep != ' ')
  1835. *pn++ = langopts.thousands_sep;
  1836. *pn++ = ' ';
  1837. }
  1838. }
  1839. pn[0] = ' ';
  1840. pn[1] = 0;
  1841. word = pw;
  1842. for(pw = &number_buf[1]; pw < pn;)
  1843. {
  1844. TranslateWord2(pw, &words[ix], words[ix].pre_pause,0 );
  1845. while(*pw++ != ' ');
  1846. words[ix].pre_pause = 0;
  1847. words[ix].flags = 0;
  1848. }
  1849. }
  1850. else
  1851. {
  1852. dict_flags = TranslateWord2(word, &words[ix], words[ix].pre_pause, words[ix+1].pre_pause);
  1853. ix += ((dict_flags >> 5) & 7); // dictionary indicates skip next word(s)
  1854. if((dict_flags & FLAG_DOT) && (ix == word_count-1) && (terminator == CLAUSE_PERIOD))
  1855. {
  1856. // probably an abbreviation such as Mr. or B. rather than end of sentence
  1857. clause_pause = 10;
  1858. tone = 4;
  1859. }
  1860. }
  1861. }
  1862. for(ix=0; ix<2; ix++)
  1863. {
  1864. // terminate the clause with 2 PAUSE phonemes
  1865. ph_list2[n_ph_list2+ix].phcode = phonPAUSE;
  1866. ph_list2[n_ph_list2+ix].stress = 0;
  1867. ph_list2[n_ph_list2+ix].sourceix = 0;
  1868. ph_list2[n_ph_list2+ix].synthflags = 0;
  1869. }
  1870. n_ph_list2 += 2;
  1871. if(Eof() && ((word_count == 0) || (option_endpause==0)))
  1872. {
  1873. clause_pause = 10;
  1874. }
  1875. MakePhonemeList(clause_pause, new_sentence);
  1876. if(embedded_count) // ???? is this needed
  1877. {
  1878. phoneme_list[n_phoneme_list-2].synthflags = SFLAG_EMBEDDED;
  1879. embedded_list[embedded_ix-1] |= 0x80;
  1880. }
  1881. prev_clause_pause = clause_pause;
  1882. GetTranslatedPhonemeString(phon_out,sizeof(phon_out));
  1883. *tone_out = tone;
  1884. new_sentence = 0;
  1885. if(terminator & CLAUSE_BIT_SENTENCE)
  1886. {
  1887. new_sentence = 1; // next clause is a new sentence
  1888. }
  1889. if(voice_change != NULL)
  1890. {
  1891. // return new voice name if an embedded voice change command terminated the clause
  1892. if(terminator & CLAUSE_BIT_VOICE)
  1893. *voice_change = &source[source_index];
  1894. else
  1895. *voice_change = NULL;
  1896. }
  1897. if(Eof() || (vp_input==NULL))
  1898. return(NULL);
  1899. if(option_multibyte == espeakCHARS_WCHAR)
  1900. return((void *)p_wchar_input);
  1901. else
  1902. return((void *)p_textinput);
  1903. } // end of TranslateClause
  1904. void InitText(int control)
  1905. {//=======================
  1906. count_sentences = 0;
  1907. count_words = 0;
  1908. end_character_position = 0;
  1909. skip_sentences = 0;
  1910. skip_marker[0] = 0;
  1911. skip_words = 0;
  1912. skip_characters = 0;
  1913. skipping_text = 0;
  1914. new_sentence = 1;
  1915. ungot_char = 0;
  1916. prev_clause_pause = 0;
  1917. option_sayas = 0;
  1918. option_sayas2 = 0;
  1919. option_emphasis = 0;
  1920. InitText2();
  1921. if((control & espeakKEEP_NAMEDATA) == 0)
  1922. {
  1923. InitNamedata();
  1924. }
  1925. }