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

translate.cpp 73KB

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