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

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