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

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