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

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