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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173
  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 limit;
  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) && ((limit = langopts.param[LOPT_COMBINE_WORDS]) > 0))
  968. {
  969. char *p2;
  970. int ok = 1;
  971. int flags2;
  972. char ph_buf[N_WORD_PHONEMES];
  973. // LANG=cs,sk
  974. // combine a preposition with the following word
  975. p2 = word;
  976. while(*p2 != ' ') p2++;
  977. if(limit & 0x100)
  978. {
  979. // only if the second word has $alt attribute
  980. strcpy(ph_buf,word_phonemes);
  981. flags2 = translator->TranslateWord(p2+1, 0, wtab+1);
  982. if((flags2 & FLAG_ALT_TRANS) == 0)
  983. {
  984. ok = 0;
  985. strcpy(word_phonemes,ph_buf);
  986. }
  987. }
  988. if(ok)
  989. {
  990. *p2 = '-'; // replace next space by hyphen
  991. flags = translator->TranslateWord(word, next_pause, wtab); // translate the combined word
  992. if(CountSyllables(p) > (limit & 0xf))
  993. {
  994. // revert to separate words
  995. *p2 = ' ';
  996. flags = translator->TranslateWord(word, next_pause, wtab);
  997. }
  998. else
  999. {
  1000. flags |= FLAG_SKIPWORDS_1;
  1001. }
  1002. }
  1003. }
  1004. if(p[0] == phonSWITCH)
  1005. {
  1006. // this word uses a different language
  1007. new_language = (char *)(&p[1]);
  1008. if(new_language[0]==0)
  1009. new_language = "en";
  1010. if((switch_phonemes = SelectPhonemeTableName(new_language)) >= 0)
  1011. {
  1012. if((translator2 != NULL) && (strcmp(new_language,translator2_language) != 0))
  1013. {
  1014. // we already have an alternative translator, but not for the required language, delete it
  1015. delete translator2;
  1016. translator2 = NULL;
  1017. }
  1018. if(translator2 == NULL)
  1019. {
  1020. translator2 = SelectTranslator(new_language);
  1021. strcpy(translator2_language,new_language);
  1022. if(translator2->LoadDictionary(new_language,0) != 0)
  1023. {
  1024. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  1025. switch_phonemes = -1;
  1026. translator2_language[0] = 0;
  1027. }
  1028. }
  1029. }
  1030. if(switch_phonemes >= 0)
  1031. {
  1032. // re-translate the word using the new translator
  1033. flags = translator2->TranslateWord(word, next_pause, wtab);
  1034. strcpy((char *)p,translator2->word_phonemes);
  1035. if(p[0] == phonSWITCH)
  1036. {
  1037. // the second translator doesn't want to process this word
  1038. switch_phonemes = -1;
  1039. }
  1040. }
  1041. if(switch_phonemes < 0)
  1042. {
  1043. // language code is not recognised or 2nd translator won't translate it
  1044. p[0] = phonSCHWA; // just say something
  1045. p[1] = phonSCHWA;
  1046. p[2] = 0;
  1047. }
  1048. }
  1049. if(!(word_flags & FLAG_LAST_WORD) && !(word_flags & FLAG_HYPHEN))
  1050. {
  1051. if(flags & FLAG_PAUSE1)
  1052. {
  1053. if(pre_pause < 1)
  1054. pre_pause = 1;
  1055. }
  1056. if((flags & FLAG_PREPAUSE) && (prepause_timeout == 0))
  1057. {
  1058. // the word is marked in the dictionary list with $pause
  1059. if(pre_pause < 4) pre_pause = 4;
  1060. prepause_timeout = 3;
  1061. }
  1062. }
  1063. if((option_emphasis >= 3) && (pre_pause < 2))
  1064. pre_pause = 2;
  1065. }
  1066. plist2 = &ph_list2[n_ph_list2];
  1067. stress = 0;
  1068. next_stress = 0;
  1069. srcix = 0;
  1070. max_stress = -1;
  1071. found_dict_flag = 0;
  1072. if(flags & FLAG_FOUND)
  1073. found_dict_flag = SFLAG_DICTIONARY;
  1074. while((pre_pause > 0) && (n_ph_list2 < N_PHONEME_LIST-4))
  1075. {
  1076. // add pause phonemes here. Either because of punctuation (brackets or quotes) in the
  1077. // text, or because the word is marked in the dictionary lookup as a conjunction
  1078. if(pre_pause > 1)
  1079. {
  1080. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE);
  1081. pre_pause -= 2;
  1082. }
  1083. else
  1084. {
  1085. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_NOLINK);
  1086. pre_pause--;
  1087. }
  1088. end_stressed_vowel = 0; // forget about the previous word
  1089. prev_dict_flags = 0;
  1090. }
  1091. if((option_capitals==1) && (word_flags & FLAG_FIRST_UPPER))
  1092. {
  1093. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_SHORT);
  1094. SetPlist2(&ph_list2[n_ph_list2++],phonCAPITAL);
  1095. if((word_flags & FLAG_ALL_UPPER) && IsAlpha(word[1]))
  1096. {
  1097. // word > 1 letter and all capitals
  1098. SetPlist2(&ph_list2[n_ph_list2++],phonPAUSE_SHORT);
  1099. SetPlist2(&ph_list2[n_ph_list2++],phonCAPITAL);
  1100. }
  1101. }
  1102. if(switch_phonemes >= 0)
  1103. {
  1104. // this word uses a different phoneme table
  1105. SetPlist2(&ph_list2[n_ph_list2],phonSWITCH);
  1106. ph_list2[n_ph_list2++].tone_number = switch_phonemes; // temporary phoneme table number
  1107. }
  1108. // remove initial pause from a word if it follows a hyphen
  1109. if((word_flags & FLAG_HYPHEN) && (phoneme_tab[*p]->type == phPAUSE))
  1110. p++;
  1111. while(((ph_code = *p++) != 0) && (n_ph_list2 < N_PHONEME_LIST-4))
  1112. {
  1113. if(ph_code == 255)
  1114. continue; // unknown phoneme
  1115. // Add the phonemes to the first stage phoneme list (ph_list2)
  1116. ph = phoneme_tab[ph_code];
  1117. if(ph->type == phSTRESS)
  1118. {
  1119. // don't add stress phonemes codes to the list, but give their stress
  1120. // value to the next vowel phoneme
  1121. // std_length is used to hold stress number or (if >10) a tone number for a tone language
  1122. if(ph->spect == 0)
  1123. next_stress = ph->std_length;
  1124. else
  1125. {
  1126. // for tone languages, the tone number for a syllable follows the vowel
  1127. if(prev_vowel >= 0)
  1128. {
  1129. ph_list2[prev_vowel].tone_number = ph_code;
  1130. }
  1131. else
  1132. {
  1133. next_tone = ph_code; // no previous vowel, apply to the next vowel
  1134. }
  1135. }
  1136. }
  1137. else
  1138. if(ph_code == phonSYLLABIC)
  1139. {
  1140. // mark the previous phoneme as a syllabic consonant
  1141. ph_list2[n_ph_list2-1].synthflags |= SFLAG_SYLLABLE;
  1142. ph_list2[n_ph_list2-1].stress = next_stress;
  1143. }
  1144. else
  1145. if(ph_code == phonLENGTHEN)
  1146. {
  1147. ph_list2[n_ph_list2-1].synthflags |= SFLAG_LENGTHEN;
  1148. }
  1149. else
  1150. if(ph_code == phonEND_WORD)
  1151. {
  1152. // a || symbol in a phoneme string was used to indicate a word boundary
  1153. // Don't add this phoneme to the list, but make sure the next phoneme has
  1154. // a newword indication
  1155. srcix = source_ix+1;
  1156. }
  1157. else
  1158. if(ph_code == phonX1)
  1159. {
  1160. // a language specific action
  1161. if(langopts.param[LOPT_IT_DOUBLING])
  1162. {
  1163. flags |= FLAG_DOUBLING;
  1164. }
  1165. }
  1166. else
  1167. {
  1168. ph_list2[n_ph_list2].phcode = ph_code;
  1169. ph_list2[n_ph_list2].tone_number = 0;
  1170. ph_list2[n_ph_list2].synthflags = embedded_flag | found_dict_flag;
  1171. embedded_flag = 0;
  1172. ph_list2[n_ph_list2].sourceix = srcix;
  1173. srcix = 0;
  1174. if(ph->type == phVOWEL)
  1175. {
  1176. stress = next_stress;
  1177. next_stress = 0;
  1178. if((prev_vowel >= 0) && (n_ph_list2-1) != prev_vowel)
  1179. ph_list2[n_ph_list2-1].stress = stress; // set stress for previous consonant
  1180. ph_list2[n_ph_list2].synthflags |= SFLAG_SYLLABLE;
  1181. prev_vowel = n_ph_list2;
  1182. if(stress > max_stress)
  1183. {
  1184. max_stress = stress;
  1185. max_stress_ix = n_ph_list2;
  1186. }
  1187. if(next_tone != 0)
  1188. {
  1189. ph_list2[n_ph_list2].tone_number = next_tone;
  1190. next_tone=0;
  1191. }
  1192. }
  1193. else
  1194. {
  1195. if(first_phoneme && langopts.param[LOPT_IT_DOUBLING])
  1196. {
  1197. if((prev_dict_flags & FLAG_DOUBLING) || (end_stressed_vowel && (langopts.param[LOPT_IT_DOUBLING] == 2)))
  1198. {
  1199. // italian, double the initial consonant if the previous word ends with a
  1200. // stressed vowel, or is marked with a flag
  1201. ph_list2[n_ph_list2].synthflags |= SFLAG_LENGTHEN;
  1202. }
  1203. }
  1204. }
  1205. ph_list2[n_ph_list2].stress = stress;
  1206. n_ph_list2++;
  1207. first_phoneme = 0;
  1208. }
  1209. }
  1210. plist2->sourceix = source_ix; // ????
  1211. end_stressed_vowel = 0;
  1212. if((stress >= 4) && (phoneme_tab[ph_list2[n_ph_list2-1].phcode]->type == phVOWEL))
  1213. {
  1214. end_stressed_vowel = 1; // word ends with a stressed vowel
  1215. }
  1216. if(switch_phonemes >= 0)
  1217. {
  1218. // this word uses a different phoneme table, now switch back
  1219. SelectPhonemeTable(voice->phoneme_tab_ix);
  1220. SetPlist2(&ph_list2[n_ph_list2],phonSWITCH);
  1221. ph_list2[n_ph_list2++].tone_number = voice->phoneme_tab_ix; // original phoneme table number
  1222. }
  1223. if(pitch_raised > 0)
  1224. {
  1225. embedded_list[embedded_ix++] = EMBED_P+0x60+0x80 + (pitch_raised << 8); // lower pitch
  1226. SetPlist2(&ph_list2[n_ph_list2],phonPAUSE_SHORT);
  1227. ph_list2[n_ph_list2++].synthflags = SFLAG_EMBEDDED;
  1228. }
  1229. if(flags & FLAG_STRESS_END2)
  1230. {
  1231. // this's word's stress could be increased later
  1232. ph_list2[max_stress_ix].stress |= 0x80;
  1233. }
  1234. prev_dict_flags = flags;
  1235. return(flags);
  1236. } // end of TranslateWord2
  1237. static int EmbeddedCommand(unsigned int &source_index)
  1238. {//===================================================
  1239. // An embedded command to change the pitch, volume, etc.
  1240. // returns number of commands added to embedded_list
  1241. // pitch,speed,amplitude,expression,reverb,tone,voice,sayas
  1242. const char *commands = "PSARHTIVYMUBF";
  1243. int value = -1;
  1244. int sign = 0;
  1245. unsigned char c;
  1246. char *p;
  1247. int cmd;
  1248. c = source[source_index];
  1249. if(c == '+')
  1250. {
  1251. sign = 0x40;
  1252. source_index++;
  1253. }
  1254. else
  1255. if(c == '-')
  1256. {
  1257. sign = 0x60;
  1258. source_index++;
  1259. }
  1260. if(isdigit(source[source_index]))
  1261. {
  1262. value = atoi(&source[source_index]);
  1263. while(isdigit(source[source_index]))
  1264. source_index++;
  1265. }
  1266. c = source[source_index++];
  1267. if(embedded_ix >= (N_EMBEDDED_LIST - 2))
  1268. return(0); // list is full
  1269. if((p = strchr_w(commands,c)) == NULL)
  1270. return(0);
  1271. cmd = (p - commands)+1;
  1272. if(value == -1)
  1273. {
  1274. value = embedded_default[cmd];
  1275. sign = 0;
  1276. }
  1277. if(cmd == EMBED_Y)
  1278. {
  1279. option_sayas2 = value;
  1280. count_sayas_digits = 0;
  1281. }
  1282. embedded_list[embedded_ix++] = cmd + sign + (value << 8);
  1283. return(1);
  1284. } // end of EmbeddedCommand
  1285. int Translator::TranslateChar(char *ptr, int prev_in, int c, int next_in, int *insert)
  1286. {//===================================================================================
  1287. // To allow language specific examination and replacement of characters
  1288. const wchar_t *p;
  1289. unsigned int new_c;
  1290. if(c == 0) return(0);
  1291. if(langopts.replace_chars != NULL)
  1292. {
  1293. // there is a list of character codes to be substituted with alternative codes
  1294. if((p = wcschr(langopts.replace_chars,c)) != NULL)
  1295. {
  1296. new_c = langopts.replacement_chars[p - langopts.replace_chars];
  1297. if(new_c & 0xffe00000)
  1298. {
  1299. // there is a second character to be inserted
  1300. *insert = (new_c >> 16);
  1301. new_c &= 0xffff;
  1302. }
  1303. return(new_c);
  1304. }
  1305. }
  1306. return(c);
  1307. }
  1308. void *Translator::TranslateClause(FILE *f_text, const void *vp_input, int *tone_out, char **voice_change)
  1309. {//======================================================================================================
  1310. int ix;
  1311. int c;
  1312. int cc;
  1313. unsigned int source_index=0;
  1314. unsigned int prev_source_index;
  1315. int prev_in;
  1316. int prev_out=' ';
  1317. int prev_out2;
  1318. int prev_in2=0;
  1319. int next_in;
  1320. int char_inserted=0;
  1321. int clause_pause;
  1322. int pre_pause=0;
  1323. int pre_pause_add=0;
  1324. int word_mark = 0;
  1325. int all_upper_case=FLAG_ALL_UPPER;
  1326. int finished;
  1327. int single_quoted;
  1328. int phoneme_mode = 0;
  1329. int dict_flags; // returned from dictionary lookup
  1330. int word_flags; // set here
  1331. int embedded_count = 0;
  1332. char *word;
  1333. char *p;
  1334. int j, k;
  1335. int n_digits;
  1336. unsigned short charix[N_TR_SOURCE+1];
  1337. WORD_TAB words[N_CLAUSE_WORDS];
  1338. int word_count=0; // index into words
  1339. char sbuf[512];
  1340. int terminator;
  1341. int tone;
  1342. p_textinput = (char *)vp_input;
  1343. p_wchar_input = (wchar_t *)vp_input;
  1344. embedded_ix = 0;
  1345. embedded_read = 0;
  1346. if((clause_start_char = count_characters) < 0)
  1347. clause_start_char = 0;
  1348. clause_start_word = count_words + 1;
  1349. for(ix=0; ix<N_TR_SOURCE; ix++)
  1350. charix[ix] = 0;
  1351. terminator = translator->ReadClause(f_text,source,charix,N_TR_SOURCE);
  1352. charix[N_TR_SOURCE] = count_characters;
  1353. clause_pause = (terminator & 0xff) * 10; // mS
  1354. tone = (terminator & 0xf00) >> 8;
  1355. for(p=source; *p != 0; p++)
  1356. {
  1357. if(!isspace2(*p))
  1358. {
  1359. break;
  1360. }
  1361. }
  1362. if(*p == 0)
  1363. {
  1364. // No characters except spaces. This is not a sentence.
  1365. // Don't add this pause, just make up the previous pause to this value;
  1366. clause_pause -= max_clause_pause;
  1367. if(clause_pause < 0)
  1368. clause_pause = 0;
  1369. terminator &= ~CLAUSE_BIT_SENTENCE; // clear sentence bit
  1370. max_clause_pause += clause_pause;
  1371. }
  1372. else
  1373. {
  1374. max_clause_pause = clause_pause;
  1375. }
  1376. if(new_sentence)
  1377. {
  1378. count_sentences++;
  1379. if(skip_sentences > 0)
  1380. {
  1381. skip_sentences--;
  1382. if(skip_sentences == 0)
  1383. skipping_text = 0;
  1384. }
  1385. }
  1386. ph_list2[0].phcode = phonPAUSE_SHORT;
  1387. ph_list2[0].stress = 0;
  1388. ph_list2[0].tone_number = 0;
  1389. ph_list2[0].sourceix = 0;
  1390. n_ph_list2 = 1;
  1391. prev_last_stress = 0;
  1392. prepause_timeout = 0;
  1393. word_count = 0;
  1394. single_quoted = 0;
  1395. word_flags = 0;
  1396. expect_verb=0;
  1397. expect_past=0;
  1398. expect_verb_s=0;
  1399. end_stressed_vowel=0;
  1400. prev_dict_flags = 0;
  1401. sbuf[0] = 0;
  1402. sbuf[1] = ' ';
  1403. sbuf[2] = ' ';
  1404. ix = 3;
  1405. prev_in = ' ';
  1406. words[0].start = ix;
  1407. words[0].flags = 0;
  1408. finished = 0;
  1409. for(j=0; charix[j]==0; j++);
  1410. words[0].sourceix = charix[j];
  1411. for(k=j; charix[k]!=0; k++);
  1412. words[0].length = k-j;
  1413. while(!finished && (ix < (int)sizeof(sbuf))&& (n_ph_list2 < N_PHONEME_LIST-4))
  1414. {
  1415. prev_out2 = prev_out;
  1416. utf8_in(&prev_out,&sbuf[ix-1],1); // prev_out = sbuf[ix-1];
  1417. if(prev_in2 != 0)
  1418. {
  1419. prev_in = prev_in2;
  1420. prev_in2 = 0;
  1421. }
  1422. else
  1423. if(source_index > 0)
  1424. {
  1425. utf8_in(&prev_in,&source[source_index-1],1); // prev_in = source[source_index-1];
  1426. }
  1427. prev_source_index = source_index;
  1428. if(char_inserted)
  1429. {
  1430. c = char_inserted;
  1431. char_inserted = 0;
  1432. }
  1433. else
  1434. {
  1435. source_index += utf8_in(&cc,&source[source_index],0); // cc = source[source_index++];
  1436. c = cc;
  1437. }
  1438. utf8_in(&next_in,&source[source_index],0);
  1439. if((c == CTRL_EMBEDDED) || (c == ctrl_embedded))
  1440. {
  1441. // start of embedded command in the text
  1442. int srcix = source_index-1;
  1443. if(prev_in != ' ')
  1444. {
  1445. c = ' ';
  1446. prev_in2 = c;
  1447. source_index--;
  1448. }
  1449. else
  1450. {
  1451. embedded_count += EmbeddedCommand(source_index);
  1452. prev_in2 = prev_in;
  1453. // replace the embedded command by spaces
  1454. memset(&source[srcix],' ',source_index-srcix);
  1455. source_index = srcix;
  1456. continue;
  1457. }
  1458. }
  1459. if(phoneme_mode)
  1460. {
  1461. all_upper_case = FLAG_PHONEMES;
  1462. if((c == ']') && (next_in == ']'))
  1463. {
  1464. phoneme_mode = 0;
  1465. source_index++;
  1466. c = ' ';
  1467. }
  1468. }
  1469. else
  1470. if((option_sayas2 & 0xf0) == SAYAS_DIGITS)
  1471. {
  1472. if(iswdigit(c))
  1473. {
  1474. count_sayas_digits++;
  1475. if(count_sayas_digits > (option_sayas2 & 0xf))
  1476. {
  1477. // break after the specified number of digits
  1478. c = ' ';
  1479. source_index = prev_source_index; // unget
  1480. count_sayas_digits = 0;
  1481. }
  1482. }
  1483. else
  1484. {
  1485. count_sayas_digits = 0;
  1486. if(iswdigit(prev_out))
  1487. {
  1488. c = ' ';
  1489. source_index = prev_source_index; // unget
  1490. }
  1491. }
  1492. }
  1493. else
  1494. if((option_sayas2 & 0xf0) != 1)
  1495. {
  1496. if((c == '/') && (langopts.testing & 2) && isdigit(next_in) && IsAlpha(prev_out))
  1497. {
  1498. // TESTING, explicit indication of stressed syllable by /2 after the word
  1499. word_mark = next_in-'0';
  1500. source_index++;
  1501. c = ' ';
  1502. }
  1503. if((c == 0x92) || (c == 0xb4) || (c == 0x2019))
  1504. c = '\''; // 'microsoft' quote or sexed closing single quote - possibly used as apostrophe
  1505. if((c == '?') && IsAlpha(prev_out) && IsAlpha(next_in))
  1506. {
  1507. // ? between two letters may be a smart-quote replaced by ?
  1508. c = '\'';
  1509. }
  1510. c = TranslateChar(&source[source_index], prev_in,c, next_in, &char_inserted); // optional language specific function
  1511. if(char_inserted)
  1512. next_in = char_inserted;
  1513. if(!IsAlpha(c) && !iswspace(c) && (c != '\''))
  1514. {
  1515. if(IsAlpha(prev_out))
  1516. {
  1517. if(langopts.tone_numbers && isdigit(c) && !isdigit(next_in))
  1518. {
  1519. // allow a tone number as part of the word
  1520. }
  1521. else
  1522. {
  1523. c = ' '; // ensure we have an end-of-word terminator
  1524. source_index = prev_source_index;
  1525. }
  1526. }
  1527. }
  1528. if(iswdigit(prev_out))
  1529. {
  1530. if(!iswdigit(c) && (c != '.') && (c != ','))
  1531. {
  1532. c = ' '; // terminate digit string with a space
  1533. source_index = prev_source_index;
  1534. }
  1535. }
  1536. if((c == '[') && (next_in == '[') && option_phoneme_input)
  1537. {
  1538. phoneme_mode = FLAG_PHONEMES;
  1539. source_index++;
  1540. continue;
  1541. }
  1542. if(c == 0)
  1543. {
  1544. finished = 1;
  1545. c = ' ';
  1546. }
  1547. else
  1548. if(IsAlpha(c))
  1549. {
  1550. if(!IsAlpha(prev_out) || (langopts.ideographs && (c >= 0x3000)))
  1551. {
  1552. if((prev_out != ' ') && (prev_out != '\''))
  1553. {
  1554. // start of word, insert space if not one there already
  1555. c = ' ';
  1556. source_index = prev_source_index; // unget
  1557. }
  1558. else
  1559. {
  1560. if(iswupper(c))
  1561. word_flags |= FLAG_FIRST_UPPER;
  1562. if((prev_out == ' ') && iswdigit(sbuf[ix-2]) && !iswdigit(prev_in))
  1563. {
  1564. // word, following a number, but with a space between
  1565. // Add an extra space, to distinguish "2 a" from "2a"
  1566. sbuf[ix++] = ' ';
  1567. words[word_count].start++;
  1568. }
  1569. }
  1570. }
  1571. if(iswupper(c))
  1572. {
  1573. c = towlower(c);
  1574. if(iswlower(prev_in))
  1575. {
  1576. c = ' '; // lower case followed by upper case, treat as new word
  1577. prev_in2 = c;
  1578. source_index = prev_source_index; // unget
  1579. }
  1580. //#ifdef deleted
  1581. // changed to break after the last uppercase letter, not before. See below
  1582. else
  1583. if((c != ' ') && iswupper(prev_in) && iswlower(next_in) &&
  1584. (memcmp(&source[source_index],"s ",2) != 0)) // ENGLISH specific plural
  1585. {
  1586. c = ' '; // change from upper to lower case, start new word at the last uppercase
  1587. prev_in2 = c;
  1588. source_index = prev_source_index; // unget
  1589. }
  1590. //#endif
  1591. }
  1592. else
  1593. {
  1594. #ifdef deleted
  1595. if(iswupper(prev_in) && iswalpha(prev_out2) &&
  1596. (memcmp(&source[source_index-1],"s ",2) != 0)) // ENGLISH specific plural
  1597. {
  1598. // change to break after the last uppercase letter, not before.
  1599. c = ' '; // more than one upper case followed by lower case, treat as new word
  1600. prev_in2 = c;
  1601. source_index = prev_source_index; // unget
  1602. }
  1603. #endif
  1604. if((all_upper_case) && ((ix - words[word_count].start) > 1))
  1605. {
  1606. if((c == 's') && (next_in==' '))
  1607. {
  1608. c = ' ';
  1609. all_upper_case |= FLAG_HAS_PLURAL;
  1610. if(sbuf[ix-1] == '\'')
  1611. sbuf[ix-1] = ' ';
  1612. }
  1613. else
  1614. all_upper_case = 0; // current word contains lower case letters, not "'s"
  1615. }
  1616. else
  1617. all_upper_case = 0;
  1618. }
  1619. }
  1620. else
  1621. if(c=='-')
  1622. {
  1623. if(IsAlpha(prev_in) && IsAlpha(next_in))
  1624. {
  1625. // '-' between two letters is a hyphen, treat as a space
  1626. word_flags |= FLAG_HYPHEN;
  1627. words[word_count-1].flags |= FLAG_HYPHEN_AFTER;
  1628. c = ' ';
  1629. }
  1630. else
  1631. if((prev_in==' ') && (next_in==' '))
  1632. {
  1633. // ' - ' dash between two spaces, treat as pause
  1634. c = ' ';
  1635. pre_pause_add = 4;
  1636. }
  1637. else
  1638. if(next_in=='-')
  1639. {
  1640. // double hyphen, treat as pause
  1641. source_index++;
  1642. c = ' ';
  1643. pre_pause_add = 4;
  1644. }
  1645. else
  1646. if((prev_out == ' ') && IsAlpha(sbuf[ix-2]) && !IsAlpha(prev_in))
  1647. {
  1648. // insert extra space between a word + space + hyphen, to distinguish 'a -2' from 'a-2'
  1649. sbuf[ix++] = ' ';
  1650. words[word_count].start++;
  1651. }
  1652. }
  1653. else
  1654. if(c == '\'')
  1655. {
  1656. if(iswalnum(prev_in) && IsAlpha(next_in))
  1657. {
  1658. // between two letters, consider apostrophe as part of the word
  1659. single_quoted = 0;
  1660. }
  1661. else
  1662. if((wcschr(char_plus_apostrophe,prev_in) != 0) && (prev_out2 == ' '))
  1663. {
  1664. // consider single character plus apostrophe as a word
  1665. single_quoted = 0;
  1666. if(next_in == ' ')
  1667. {
  1668. source_index++; // skip following space
  1669. }
  1670. }
  1671. else
  1672. {
  1673. if((prev_out == 's') && (single_quoted==0))
  1674. {
  1675. // looks like apostrophe after an 's'
  1676. c = ' ';
  1677. }
  1678. else
  1679. {
  1680. if(iswspace(prev_out))
  1681. single_quoted = 1;
  1682. else
  1683. single_quoted = 0;
  1684. pre_pause_add = 4; // single quote
  1685. c = ' ';
  1686. }
  1687. }
  1688. }
  1689. else
  1690. if(IsBracket(c))
  1691. {
  1692. pre_pause_add = 4;
  1693. c = ' ';
  1694. }
  1695. else
  1696. if(lookupwchar(breaks,c) != 0)
  1697. {
  1698. c = ' '; // various characters to treat as space
  1699. }
  1700. else
  1701. if(iswdigit(c))
  1702. {
  1703. if(langopts.tone_numbers && IsAlpha(prev_out) && !isdigit(next_in))
  1704. {
  1705. }
  1706. else
  1707. if((prev_out != ' ') && !iswdigit(prev_out) && (prev_out != langopts.decimal_sep)) // TEST2
  1708. {
  1709. c = ' ';
  1710. source_index = prev_source_index;
  1711. }
  1712. else
  1713. if((prev_out == ' ') && IsAlpha(sbuf[ix-2]) && !IsAlpha(prev_in))
  1714. {
  1715. // insert extra space between a word and a number, to distinguish 'a 2' from 'a2'
  1716. sbuf[ix++] = ' ';
  1717. words[word_count].start++;
  1718. }
  1719. }
  1720. }
  1721. if(iswspace(c))
  1722. {
  1723. if(prev_out == ' ')
  1724. {
  1725. continue; // multiple spaces
  1726. }
  1727. // end of 'word'
  1728. sbuf[ix++] = ' ';
  1729. if((ix > words[word_count].start) && (word_count < N_CLAUSE_WORDS-1))
  1730. {
  1731. if(embedded_count > 0)
  1732. {
  1733. // there are embedded commands before this word
  1734. embedded_list[embedded_ix-1] |= 0x80; // terminate list of commands for this word
  1735. words[word_count].flags |= FLAG_EMBEDDED;
  1736. embedded_count = 0;
  1737. }
  1738. words[word_count].pre_pause = pre_pause;
  1739. words[word_count].flags |= (all_upper_case | word_flags);
  1740. words[word_count].wmark = word_mark;
  1741. if(pre_pause > 0)
  1742. {
  1743. // insert an extra space before the word, to prevent influence from previous word across the pause
  1744. for(j=ix; j>words[word_count].start; j--)
  1745. {
  1746. sbuf[j] = sbuf[j-1];
  1747. }
  1748. sbuf[j] = ' ';
  1749. words[word_count].start++;
  1750. ix++;
  1751. }
  1752. word_count++;
  1753. words[word_count].start = ix;
  1754. words[word_count].flags = 0;
  1755. for(j=source_index; charix[j] == 0; j++);
  1756. words[word_count].sourceix = charix[j];
  1757. for(k=j; charix[k]!=0; k++);
  1758. words[word_count].length = k-j;
  1759. word_flags = 0;
  1760. pre_pause = 0;
  1761. word_mark = 0;
  1762. all_upper_case = FLAG_ALL_UPPER;
  1763. }
  1764. }
  1765. else
  1766. {
  1767. ix += utf8_out(c,&sbuf[ix]); // sbuf[ix++] = c;
  1768. }
  1769. if(pre_pause_add > pre_pause)
  1770. pre_pause = pre_pause_add;
  1771. pre_pause_add = 0;
  1772. }
  1773. if((word_count==0) && (embedded_count > 0))
  1774. {
  1775. // add a null 'word' to carry the embedded command flag
  1776. embedded_list[embedded_ix-1] |= 0x80;
  1777. words[word_count].flags |= FLAG_EMBEDDED;
  1778. word_count = 1;
  1779. }
  1780. clause_end = &sbuf[ix-1];
  1781. sbuf[ix] = 0;
  1782. words[0].pre_pause = 0; // don't add extra pause at beginning of clause
  1783. words[word_count].pre_pause = 8;
  1784. if(word_count > 0)
  1785. words[word_count-1].flags |= FLAG_LAST_WORD;
  1786. for(ix=0; ix<word_count; ix++)
  1787. {
  1788. int j;
  1789. char *pn;
  1790. char *pw;
  1791. char number_buf[80];
  1792. // start speaking at a specified word position in the text?
  1793. count_words++;
  1794. if(skip_words > 0)
  1795. {
  1796. skip_words--;
  1797. if(skip_words == 0)
  1798. skipping_text = 0;
  1799. }
  1800. if(skipping_text)
  1801. continue;
  1802. word = pw = &sbuf[words[ix].start];
  1803. for(n_digits=0; iswdigit(word[n_digits]); n_digits++); // count consecutive digits
  1804. if((n_digits > 4) && (word[0] != '0'))
  1805. {
  1806. // word is entirely digits, insert commas and break into 3 digit "words"
  1807. number_buf[0] = ' ';
  1808. pn = &number_buf[1];
  1809. j = n_digits;
  1810. while(pn < &number_buf[sizeof(number_buf)-3])
  1811. {
  1812. if(!isdigit(c = *pw++) && (c != langopts.decimal_sep))
  1813. break;
  1814. *pn++ = c;
  1815. if((--j > 0) && (j % 3)==0)
  1816. {
  1817. if(langopts.thousands_sep != ' ')
  1818. *pn++ = langopts.thousands_sep;
  1819. *pn++ = ' ';
  1820. }
  1821. }
  1822. pn[0] = ' ';
  1823. pn[1] = 0;
  1824. word = pw;
  1825. for(pw = &number_buf[1]; pw < pn;)
  1826. {
  1827. TranslateWord2(pw, &words[ix], words[ix].pre_pause,0 );
  1828. while(*pw++ != ' ');
  1829. words[ix].pre_pause = 0;
  1830. words[ix].flags = 0;
  1831. }
  1832. }
  1833. else
  1834. {
  1835. dict_flags = TranslateWord2(word, &words[ix], words[ix].pre_pause, words[ix+1].pre_pause);
  1836. ix += ((dict_flags >> 5) & 7); // dictionary indicates skip next word(s)
  1837. if((dict_flags & FLAG_DOT) && (ix == word_count-1) && (terminator == CLAUSE_PERIOD))
  1838. {
  1839. // probably an abbreviation such as Mr. or B. rather than end of sentence
  1840. clause_pause = 10;
  1841. tone = 4;
  1842. }
  1843. }
  1844. }
  1845. for(ix=0; ix<2; ix++)
  1846. {
  1847. // terminate the clause with 2 PAUSE phonemes
  1848. ph_list2[n_ph_list2+ix].phcode = phonPAUSE;
  1849. ph_list2[n_ph_list2+ix].stress = 0;
  1850. ph_list2[n_ph_list2+ix].sourceix = 0;
  1851. ph_list2[n_ph_list2+ix].synthflags = 0;
  1852. }
  1853. n_ph_list2 += 2;
  1854. if(Eof() && ((word_count == 0) || (option_endpause==0)))
  1855. {
  1856. clause_pause = 10;
  1857. }
  1858. MakePhonemeList(clause_pause, new_sentence);
  1859. if(embedded_count) // ???? is this needed
  1860. {
  1861. phoneme_list[n_phoneme_list-2].synthflags = SFLAG_EMBEDDED;
  1862. embedded_list[embedded_ix-1] |= 0x80;
  1863. }
  1864. prev_clause_pause = clause_pause;
  1865. GetTranslatedPhonemeString(phon_out,sizeof(phon_out));
  1866. *tone_out = tone;
  1867. new_sentence = 0;
  1868. if(terminator & CLAUSE_BIT_SENTENCE)
  1869. {
  1870. new_sentence = 1; // next clause is a new sentence
  1871. }
  1872. if(voice_change != NULL)
  1873. {
  1874. // return new voice name if an embedded voice change command terminated the clause
  1875. if(terminator & CLAUSE_BIT_VOICE)
  1876. *voice_change = &source[source_index];
  1877. else
  1878. *voice_change = NULL;
  1879. }
  1880. if(Eof() || (vp_input==NULL))
  1881. return(NULL);
  1882. if(option_multibyte == espeakCHARS_WCHAR)
  1883. return((void *)p_wchar_input);
  1884. else
  1885. return((void *)p_textinput);
  1886. } // end of TranslateClause
  1887. void InitText(int control)
  1888. {//=======================
  1889. count_sentences = 0;
  1890. count_words = 0;
  1891. end_character_position = 0;
  1892. skip_sentences = 0;
  1893. skip_marker[0] = 0;
  1894. skip_words = 0;
  1895. skip_characters = 0;
  1896. skipping_text = 0;
  1897. new_sentence = 1;
  1898. ungot_char = 0;
  1899. prev_clause_pause = 0;
  1900. option_sayas = 0;
  1901. option_sayas2 = 0;
  1902. option_emphasis = 0;
  1903. InitText2();
  1904. if((control & espeakKEEP_NAMEDATA) == 0)
  1905. {
  1906. InitNamedata();
  1907. }
  1908. }