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.

dictionary.cpp 90KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * Copyright (C) 2013-2015 Reece H. Dunn *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write see: *
  18. * <http://www.gnu.org/licenses/>. *
  19. ***************************************************************************/
  20. #include "StdAfx.h"
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <wctype.h>
  26. #include <wchar.h>
  27. #include "speak_lib.h"
  28. #include "speech.h"
  29. #include "phoneme.h"
  30. #include "synthesize.h"
  31. #include "translate.h"
  32. int dictionary_skipwords;
  33. char dictionary_name[40];
  34. extern void print_dictionary_flags(unsigned int *flags, char *buf, int buf_len);
  35. extern char *DecodeRule(const char *group_chars, int group_length, char *rule, int control);
  36. // accented characters which indicate (in some languages) the start of a separate syllable
  37. //static const unsigned short diereses_list[7] = {L'ä',L'ë',L'ï',L'ö',L'ü',L'ÿ',0};
  38. static const unsigned short diereses_list[7] = {0xe4,0xeb,0xef,0xf6,0xfc,0xff,0};
  39. // convert characters to an approximate 7 bit ascii equivalent
  40. // used for checking for vowels (up to 0x259=schwa)
  41. #define N_REMOVE_ACCENT 0x25e
  42. static unsigned char remove_accent[N_REMOVE_ACCENT] = {
  43. 'a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i', // 0c0
  44. 'd','n','o','o','o','o','o', 0, 'o','u','u','u','u','y','t','s', // 0d0
  45. 'a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i', // 0e0
  46. 'd','n','o','o','o','o','o', 0 ,'o','u','u','u','u','y','t','y', // 0f0
  47. 'a','a','a','a','a','a','c','c','c','c','c','c','c','c','d','d', // 100
  48. 'd','d','e','e','e','e','e','e','e','e','e','e','g','g','g','g', // 110
  49. 'g','g','g','g','h','h','h','h','i','i','i','i','i','i','i','i', // 120
  50. 'i','i','i','i','j','j','k','k','k','l','l','l','l','l','l','l', // 130
  51. 'l','l','l','n','n','n','n','n','n','n','n','n','o','o','o','o', // 140
  52. 'o','o','o','o','r','r','r','r','r','r','s','s','s','s','s','s', // 150
  53. 's','s','t','t','t','t','t','t','u','u','u','u','u','u','u','u', // 160
  54. 'u','u','u','u','w','w','y','y','y','z','z','z','z','z','z','s', // 170
  55. 'b','b','b','b', 0, 0, 'o','c','c','d','d','d','d','d','e','e', // 180
  56. 'e','f','f','g','g','h','i','i','k','k','l','l','m','n','n','o', // 190
  57. 'o','o','o','o','p','p','y', 0, 0, 's','s','t','t','t','t','u', // 1a0
  58. 'u','u','v','y','y','z','z','z','z','z','z','z', 0, 0, 0, 'w', // 1b0
  59. 't','t','t','k','d','d','d','l','l','l','n','n','n','a','a','i', // 1c0
  60. 'i','o','o','u','u','u','u','u','u','u','u','u','u','e','a','a', // 1d0
  61. 'a','a','a','a','g','g','g','g','k','k','o','o','o','o','z','z', // 1e0
  62. 'j','d','d','d','g','g','w','w','n','n','a','a','a','a','o','o', // 1f0
  63. 'a','a','a','a','e','e','e','e','i','i','i','i','o','o','o','o', // 200
  64. 'r','r','r','r','u','u','u','u','s','s','t','t','y','y','h','h', // 210
  65. 'n','d','o','o','z','z','a','a','e','e','o','o','o','o','o','o', // 220
  66. 'o','o','y','y','l','n','t','j','d','q','a','c','c','l','t','s', // 230
  67. 'z', 0, 0, 'b','u','v','e','e','j','j','q','q','r','r','y','y', // 240
  68. 'a','a','a','b','o','c','d','d','e','e','e','e','e','e'
  69. };
  70. void strncpy0(char *to,const char *from, int size)
  71. {//===================================================
  72. // strcpy with limit, ensures a zero terminator
  73. strncpy(to,from,size);
  74. to[size-1] = 0;
  75. }
  76. int Reverse4Bytes(int word)
  77. {//==========================
  78. // reverse the order of bytes from little-endian to big-endian
  79. #ifdef ARCH_BIG
  80. int ix;
  81. int word2 = 0;
  82. for(ix=0; ix<=24; ix+=8)
  83. {
  84. word2 = word2 << 8;
  85. word2 |= (word >> ix) & 0xff;
  86. }
  87. return(word2);
  88. #else
  89. return(word);
  90. #endif
  91. }
  92. int LookupMnem(MNEM_TAB *table, const char *string)
  93. {//==================================================
  94. while(table->mnem != NULL)
  95. {
  96. if(strcmp(string,table->mnem)==0)
  97. return(table->value);
  98. table++;
  99. }
  100. return(table->value);
  101. }
  102. //=============================================================================================
  103. // Read pronunciation rules and pronunciation lookup dictionary
  104. //
  105. //=============================================================================================
  106. static void InitGroups(Translator *tr)
  107. {//===================================
  108. // Called after dictionary 1 is loaded, to set up table of entry points for translation rule chains
  109. // for single-letters and two-letter combinations
  110. int ix;
  111. char *p;
  112. char *p_name;
  113. unsigned int *pw;
  114. unsigned char c, c2;
  115. int len;
  116. tr->n_groups2 = 0;
  117. for(ix=0; ix<256; ix++)
  118. {
  119. tr->groups1[ix]=NULL;
  120. tr->groups2_count[ix]=0;
  121. tr->groups2_start[ix]=255; // indicates "not set"
  122. }
  123. memset(tr->letterGroups,0,sizeof(tr->letterGroups));
  124. memset(tr->groups3,0,sizeof(tr->groups3));
  125. p = tr->data_dictrules;
  126. while(*p != 0)
  127. {
  128. if(*p != RULE_GROUP_START)
  129. {
  130. fprintf(stderr,"Bad rules data in '%s_dict' at 0x%x\n",dictionary_name,(unsigned int)(p - tr->data_dictrules));
  131. break;
  132. }
  133. p++;
  134. if(p[0] == RULE_REPLACEMENTS)
  135. {
  136. pw = (unsigned int *)(((long64)p+4) & ~3); // advance to next word boundary
  137. tr->langopts.replace_chars = pw;
  138. while(pw[0] != 0)
  139. {
  140. pw += 2; // find the end of the replacement list, each entry is 2 words.
  141. }
  142. p = (char *)(pw+1);
  143. #ifdef ARCH_BIG
  144. pw = (unsigned int *)(tr->langopts.replace_chars);
  145. while(*pw != 0)
  146. {
  147. *pw = Reverse4Bytes(*pw);
  148. pw++;
  149. *pw = Reverse4Bytes(*pw);
  150. pw++;
  151. }
  152. #endif
  153. continue;
  154. }
  155. if(p[0] == RULE_LETTERGP2)
  156. {
  157. ix = p[1] - 'A';
  158. p += 2;
  159. if((ix >= 0) && (ix < N_LETTER_GROUPS))
  160. {
  161. tr->letterGroups[ix] = p;
  162. }
  163. }
  164. else
  165. {
  166. len = strlen(p);
  167. p_name = p;
  168. c = p_name[0];
  169. c2 = p_name[1];
  170. p += (len+1);
  171. if(len == 1)
  172. {
  173. tr->groups1[c] = p;
  174. }
  175. else if(len == 0)
  176. {
  177. tr->groups1[0] = p;
  178. }
  179. else if(c == 1)
  180. {
  181. // index by offset from letter base
  182. tr->groups3[c2 - 1] = p;
  183. }
  184. else
  185. {
  186. if(tr->groups2_start[c] == 255)
  187. tr->groups2_start[c] = tr->n_groups2;
  188. tr->groups2_count[c]++;
  189. tr->groups2[tr->n_groups2] = p;
  190. tr->groups2_name[tr->n_groups2++] = (c + (c2 << 8));
  191. }
  192. }
  193. // skip over all the rules in this group
  194. while(*p != RULE_GROUP_END)
  195. {
  196. p += (strlen(p) + 1);
  197. }
  198. p++;
  199. }
  200. } // end of InitGroups
  201. int LoadDictionary(Translator *tr, const char *name, int no_error)
  202. {//===============================================================
  203. int hash;
  204. char *p;
  205. int *pw;
  206. int length;
  207. FILE *f;
  208. unsigned int size;
  209. char fname[sizeof(path_home)+20];
  210. strcpy(dictionary_name,name); // currently loaded dictionary name
  211. strcpy(tr->dictionary_name, name);
  212. // Load a pronunciation data file into memory
  213. // bytes 0-3: offset to rules data
  214. // bytes 4-7: number of hash table entries
  215. sprintf(fname,"%s%c%s_dict",path_home,PATHSEP,name);
  216. size = GetFileLength(fname);
  217. if(tr->data_dictlist != NULL)
  218. {
  219. Free(tr->data_dictlist);
  220. tr->data_dictlist = NULL;
  221. }
  222. f = fopen(fname,"rb");
  223. if((f == NULL) || (size <= 0))
  224. {
  225. if(no_error == 0)
  226. {
  227. fprintf(stderr,"Can't read dictionary file: '%s'\n",fname);
  228. }
  229. if (f != NULL)
  230. fclose(f);
  231. return(1);
  232. }
  233. tr->data_dictlist = Alloc(size);
  234. size = fread(tr->data_dictlist,1,size,f);
  235. fclose(f);
  236. pw = (int *)(tr->data_dictlist);
  237. length = Reverse4Bytes(pw[1]);
  238. if(size <= (N_HASH_DICT + sizeof(int)*2))
  239. {
  240. fprintf(stderr,"Empty _dict file: '%s\n",fname);
  241. return(2);
  242. }
  243. if((Reverse4Bytes(pw[0]) != N_HASH_DICT) ||
  244. (length <= 0) || (length > 0x8000000))
  245. {
  246. fprintf(stderr,"Bad data: '%s' (%x length=%x)\n",fname,Reverse4Bytes(pw[0]),length);
  247. return(2);
  248. }
  249. tr->data_dictrules = &(tr->data_dictlist[length]);
  250. // set up indices into data_dictrules
  251. InitGroups(tr);
  252. // set up hash table for data_dictlist
  253. p = &(tr->data_dictlist[8]);
  254. for(hash=0; hash<N_HASH_DICT; hash++)
  255. {
  256. tr->dict_hashtab[hash] = p;
  257. while((length = *p) != 0)
  258. {
  259. p += length;
  260. }
  261. p++; // skip over the zero which terminates the list for this hash value
  262. }
  263. if((tr->dict_min_size > 0) && (size < (unsigned int)tr->dict_min_size))
  264. {
  265. fprintf(stderr, "Full dictionary is not installed for '%s'\n", name);
  266. }
  267. return(0);
  268. } // end of LoadDictionary
  269. int HashDictionary(const char *string)
  270. //====================================
  271. /* Generate a hash code from the specified string
  272. This is used to access the dictionary_2 word-lookup dictionary
  273. */
  274. {
  275. int c;
  276. int chars=0;
  277. int hash=0;
  278. while((c = (*string++ & 0xff)) != 0)
  279. {
  280. hash = hash * 8 + c;
  281. hash = (hash & 0x3ff) ^ (hash >> 8); /* exclusive or */
  282. chars++;
  283. }
  284. return((hash+chars) & 0x3ff); // a 10 bit hash code
  285. } // end of HashDictionary
  286. //=============================================================================================
  287. // Translate between internal representation of phonemes and a mnemonic form for display
  288. //
  289. //=============================================================================================
  290. const char *EncodePhonemes(const char *p, char *outptr, int *bad_phoneme)
  291. /******************************************************************/
  292. /* Translate a phoneme string from ascii mnemonics to internal phoneme numbers,
  293. from 'p' up to next blank .
  294. Returns advanced 'p'
  295. outptr contains encoded phonemes, unrecognized phoneme stops the encoding
  296. bad_phoneme must point to char array of length 2 of more
  297. */
  298. {
  299. int ix;
  300. unsigned char c;
  301. int count; /* num. of matching characters */
  302. int max; /* highest num. of matching found so far */
  303. int max_ph; /* corresponding phoneme with highest matching */
  304. int consumed;
  305. unsigned int mnemonic_word;
  306. if(bad_phoneme != NULL)
  307. *bad_phoneme = 0;
  308. // skip initial blanks
  309. while(isspace(*p))
  310. {
  311. p++;
  312. }
  313. while(((c = *p) != 0) && !isspace(c))
  314. {
  315. consumed = 0;
  316. switch(c)
  317. {
  318. case '|':
  319. // used to separate phoneme mnemonics if needed, to prevent characters being treated
  320. // as a multi-letter mnemonic
  321. if((c = p[1]) == '|')
  322. {
  323. // treat double || as a word-break symbol, drop through
  324. // to the default case with c = '|'
  325. }
  326. else
  327. {
  328. p++;
  329. break;
  330. }
  331. default:
  332. // lookup the phoneme mnemonic, find the phoneme with the highest number of
  333. // matching characters
  334. max= -1;
  335. max_ph= 0;
  336. for(ix=1; ix<n_phoneme_tab; ix++)
  337. {
  338. if(phoneme_tab[ix] == NULL)
  339. continue;
  340. if(phoneme_tab[ix]->type == phINVALID)
  341. continue; // this phoneme is not defined for this language
  342. count = 0;
  343. mnemonic_word = phoneme_tab[ix]->mnemonic;
  344. while(((c = p[count]) > ' ') && (count < 4) &&
  345. (c == ((mnemonic_word >> (count*8)) & 0xff)))
  346. count++;
  347. if((count > max) &&
  348. ((count == 4) || (((mnemonic_word >> (count*8)) & 0xff)==0)))
  349. {
  350. max = count;
  351. max_ph = phoneme_tab[ix]->code;
  352. }
  353. }
  354. if(max_ph == 0)
  355. {
  356. // not recognised, report and ignore
  357. if(bad_phoneme != NULL)
  358. {
  359. utf8_in(bad_phoneme, p);
  360. }
  361. *outptr++ = 0;
  362. return(p+1);
  363. }
  364. if(max <= 0)
  365. max = 1;
  366. p += (consumed + max);
  367. *outptr++ = (char)(max_ph);
  368. if(max_ph == phonSWITCH)
  369. {
  370. // Switch Language: this phoneme is followed by a text string
  371. char *p_lang = outptr;
  372. while(!isspace(c = *p) && (c != 0))
  373. {
  374. p++;
  375. *outptr++ = tolower(c);
  376. }
  377. *outptr = 0;
  378. if(c == 0)
  379. {
  380. if(strcmp(p_lang,"en")==0)
  381. {
  382. *p_lang = 0; // don't need "en", it's assumed by default
  383. return(p);
  384. }
  385. }
  386. else
  387. {
  388. *outptr++ = '|'; // more phonemes follow, terminate language string with separator
  389. }
  390. }
  391. break;
  392. }
  393. }
  394. /* terminate the encoded string */
  395. *outptr = 0;
  396. return(p);
  397. } // end of EncodePhonemes
  398. void DecodePhonemes(const char *inptr, char *outptr)
  399. {//==================================================
  400. // Translate from internal phoneme codes into phoneme mnemonics
  401. unsigned char phcode;
  402. unsigned char c;
  403. unsigned int mnem;
  404. PHONEME_TAB *ph;
  405. static const char *stress_chars = "==,,'* ";
  406. sprintf(outptr,"* ");
  407. while((phcode = *inptr++) > 0)
  408. {
  409. if(phcode == 255)
  410. continue; /* indicates unrecognised phoneme */
  411. if((ph = phoneme_tab[phcode]) == NULL)
  412. continue;
  413. if((ph->type == phSTRESS) && (ph->std_length <= 4) && (ph->program == 0))
  414. {
  415. if(ph->std_length > 1)
  416. *outptr++ = stress_chars[ph->std_length];
  417. }
  418. else
  419. {
  420. mnem = ph->mnemonic;
  421. while((c = (mnem & 0xff)) != 0)
  422. {
  423. *outptr++ = c;
  424. mnem = mnem >> 8;
  425. }
  426. if(phcode == phonSWITCH)
  427. {
  428. while(isalpha(*inptr))
  429. {
  430. *outptr++ = *inptr++;
  431. }
  432. }
  433. }
  434. }
  435. *outptr = 0; /* string terminator */
  436. } // end of DecodePhonemes
  437. // using Kirschenbaum to IPA translation, ascii 0x20 to 0x7f
  438. unsigned short ipa1[96] = {
  439. 0x20,0x21,0x22,0x2b0,0x24,0x25,0x0e6,0x2c8,0x28,0x29,0x27e,0x2b,0x2cc,0x2d,0x2e,0x2f,
  440. 0x252,0x31,0x32,0x25c,0x34,0x35,0x36,0x37,0x275,0x39,0x2d0,0x2b2,0x3c,0x3d,0x3e,0x294,
  441. 0x259,0x251,0x3b2,0xe7,0xf0,0x25b,0x46,0x262,0x127,0x26a,0x25f,0x4b,0x26b,0x271,0x14b,0x254,
  442. 0x3a6,0x263,0x280,0x283,0x3b8,0x28a,0x28c,0x153,0x3c7,0xf8,0x292,0x32a,0x5c,0x5d,0x5e,0x5f,
  443. 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x261,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
  444. 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x303,0x7f
  445. };
  446. #define N_PHON_OUT 500 // realloc increment
  447. static char *phon_out_buf = NULL; // passes the result of GetTranslatedPhonemeString()
  448. static unsigned int phon_out_size = 0;
  449. char *WritePhMnemonic(char *phon_out, PHONEME_TAB *ph, PHONEME_LIST *plist, int use_ipa, int *flags)
  450. {//===================================================================================================
  451. int c;
  452. int mnem;
  453. int len;
  454. int first;
  455. int ix = 0;
  456. char *p;
  457. PHONEME_DATA phdata;
  458. if(ph->code == phonEND_WORD)
  459. {
  460. // ignore
  461. phon_out[0] = 0;
  462. return(phon_out);
  463. }
  464. if(ph->code == phonSWITCH)
  465. {
  466. // the tone_ph field contains a phoneme table number
  467. p = phoneme_tab_list[plist->tone_ph].name;
  468. sprintf(phon_out, "(%s)", p);
  469. return(phon_out + strlen(phon_out));
  470. }
  471. if(use_ipa)
  472. {
  473. // has an ipa name been defined for this phoneme ?
  474. phdata.ipa_string[0] = 0;
  475. if(plist == NULL)
  476. {
  477. InterpretPhoneme2(ph->code, &phdata);
  478. }
  479. else
  480. {
  481. InterpretPhoneme(NULL, 0, plist, &phdata, NULL);
  482. }
  483. p = phdata.ipa_string;
  484. if(*p == 0x20)
  485. {
  486. // indicates no name for this phoneme
  487. *phon_out = 0;
  488. return(phon_out);
  489. }
  490. if((*p != 0) && ((*p & 0xff) < 0x20))
  491. {
  492. // name starts with a flags byte
  493. if(flags != NULL)
  494. *flags = *p;
  495. p++;
  496. }
  497. len = strlen(p);
  498. if(len > 0)
  499. {
  500. strcpy(phon_out, p);
  501. phon_out += len;
  502. *phon_out = 0;
  503. return(phon_out);
  504. }
  505. }
  506. first = 1;
  507. for(mnem = ph->mnemonic; (c = mnem & 0xff) != 0; mnem = mnem >> 8)
  508. {
  509. if((c == '/') && (option_phoneme_variants==0))
  510. break; // discard phoneme variant indicator
  511. if(use_ipa)
  512. {
  513. // convert from ascii to ipa
  514. if(first && (c == '_'))
  515. break; // don't show pause phonemes
  516. if((c == '#') && (ph->type == phVOWEL))
  517. break; // # is subscript-h, but only for consonants
  518. // ignore digits after the first character
  519. if(!first && IsDigit09(c))
  520. continue;
  521. if((c >= 0x20) && (c < 128))
  522. c = ipa1[c-0x20];
  523. ix += utf8_out(c, &phon_out[ix]);
  524. }
  525. else
  526. {
  527. phon_out[ix++]= c;
  528. }
  529. first = 0;
  530. }
  531. phon_out = &phon_out[ix];
  532. *phon_out = 0;
  533. return(phon_out);
  534. } // end of WritePhMnemonic
  535. const char *GetTranslatedPhonemeString(int phoneme_mode)
  536. {//=======================================================
  537. /* Called after a clause has been translated into phonemes, in order
  538. to display the clause in phoneme mnemonic form.
  539. phoneme_mode
  540. bit 1: use IPA phoneme names
  541. bit 7: use tie between letters in multi-character phoneme names
  542. bits 8-23 tie or separator character
  543. */
  544. int ix;
  545. unsigned int len;
  546. int phon_out_ix=0;
  547. int stress;
  548. int c;
  549. char *p;
  550. char *buf;
  551. int count;
  552. int flags;
  553. int use_ipa;
  554. int use_tie;
  555. int separate_phonemes;
  556. char phon_buf[30];
  557. char phon_buf2[30];
  558. PHONEME_LIST *plist;
  559. static const char *stress_chars = "==,,''";
  560. if(phon_out_buf == NULL)
  561. {
  562. phon_out_size = N_PHON_OUT;
  563. if((phon_out_buf = (char *)realloc(phon_out_buf, phon_out_size)) == NULL)
  564. {
  565. phon_out_size = 0;
  566. return("");
  567. }
  568. }
  569. use_ipa = phoneme_mode & espeakPHONEMES_IPA;
  570. if(phoneme_mode & espeakPHONEMES_TIE)
  571. {
  572. use_tie = phoneme_mode >> 8;
  573. separate_phonemes = 0;
  574. }
  575. else
  576. {
  577. separate_phonemes = phoneme_mode >> 8;
  578. use_tie = 0;
  579. }
  580. for(ix=1; ix<(n_phoneme_list-2); ix++)
  581. {
  582. buf = phon_buf;
  583. plist = &phoneme_list[ix];
  584. WritePhMnemonic(phon_buf2, plist->ph, plist, use_ipa, &flags);
  585. if(plist->newword)
  586. *buf++ = ' ';
  587. if((!plist->newword) || (separate_phonemes == ' '))
  588. {
  589. if((separate_phonemes != 0) && (ix > 1))
  590. {
  591. utf8_in(&c, phon_buf2);
  592. if((c < 0x2b0) || (c > 0x36f)) // not if the phoneme starts with a superscript letter
  593. {
  594. buf += utf8_out(separate_phonemes, buf);
  595. }
  596. }
  597. }
  598. if(plist->synthflags & SFLAG_SYLLABLE)
  599. {
  600. if((stress = plist->stresslevel) > 1)
  601. {
  602. c = 0;
  603. if(stress > 5) stress = 5;
  604. if(use_ipa)
  605. {
  606. c = 0x2cc; // ipa, secondary stress
  607. if(stress > 3)
  608. c = 0x02c8; // ipa, primary stress
  609. }
  610. else
  611. {
  612. c = stress_chars[stress];
  613. }
  614. if(c != 0)
  615. {
  616. buf += utf8_out(c, buf);
  617. // if(separate_phonemes)
  618. // *buf++ = separate_phonemes;
  619. }
  620. }
  621. }
  622. flags = 0;
  623. count = 0;
  624. for(p=phon_buf2; *p != 0;)
  625. {
  626. p += utf8_in(&c, p);
  627. if(use_tie != 0)
  628. {
  629. // look for non-inital alphabetic character, but not diacritic, superscript etc.
  630. if((count>0) && !(flags & (1 << (count-1))) && ((c < 0x2b0) || (c > 0x36f)) && iswalpha2(c))
  631. {
  632. buf += utf8_out(use_tie, buf);
  633. }
  634. }
  635. buf += utf8_out(c, buf);
  636. count++;
  637. }
  638. if(plist->ph->code != phonSWITCH)
  639. {
  640. if(plist->synthflags & SFLAG_LENGTHEN)
  641. {
  642. buf = WritePhMnemonic(buf, phoneme_tab[phonLENGTHEN], NULL, use_ipa, NULL);
  643. }
  644. if((plist->synthflags & SFLAG_SYLLABLE) && (plist->type != phVOWEL))
  645. {
  646. // syllablic consonant
  647. buf = WritePhMnemonic(buf, phoneme_tab[phonSYLLABIC], NULL, use_ipa, NULL);
  648. }
  649. if(plist->tone_ph > 0)
  650. {
  651. buf = WritePhMnemonic(buf, phoneme_tab[plist->tone_ph], NULL, use_ipa, NULL);
  652. }
  653. }
  654. len = buf - phon_buf;
  655. if((phon_out_ix + len) >= phon_out_size)
  656. {
  657. // enlarge the phoneme buffer
  658. phon_out_size = phon_out_ix + len + N_PHON_OUT;
  659. if((phon_out_buf = (char *)realloc(phon_out_buf, phon_out_size)) == NULL)
  660. {
  661. phon_out_size = 0;
  662. return("");
  663. }
  664. }
  665. phon_buf[len] = 0;
  666. strcpy(&phon_out_buf[phon_out_ix], phon_buf);
  667. phon_out_ix += len;
  668. }
  669. if (!phon_out_buf)
  670. return("");
  671. phon_out_buf[phon_out_ix] = 0;
  672. return(phon_out_buf);
  673. } // end of GetTranslatedPhonemeString
  674. //=============================================================================================
  675. // Is a word Unpronouncable - and so should be spoken as individual letters
  676. //
  677. //=============================================================================================
  678. static int IsLetterGroup(Translator *tr, char *word, int group, int pre)
  679. {//=====================================================================
  680. // match the word against a list of utf-8 strings
  681. char *p;
  682. char *w;
  683. int len=0;
  684. p = tr->letterGroups[group];
  685. if(p == NULL)
  686. return(0);
  687. while(*p != RULE_GROUP_END)
  688. {
  689. if(pre)
  690. {
  691. len = strlen(p);
  692. w = word - len + 1;
  693. }
  694. else
  695. {
  696. w = word;
  697. }
  698. while((*p == *w) && (*w != 0))
  699. {
  700. w++;
  701. p++;
  702. }
  703. if(*p == 0)
  704. {
  705. if(pre)
  706. return(len);
  707. return(w-word); // matched a complete string
  708. }
  709. while(*p++ != 0); // skip to end of string
  710. }
  711. return(0);
  712. }
  713. static int IsLetter(Translator *tr, int letter, int group)
  714. {//=======================================================
  715. int letter2;
  716. if(tr->letter_groups[group] != NULL)
  717. {
  718. if(wcschr(tr->letter_groups[group],letter))
  719. return(1);
  720. return(0);
  721. }
  722. if(group > 7)
  723. return(0);
  724. if(tr->letter_bits_offset > 0)
  725. {
  726. if(((letter2 = (letter - tr->letter_bits_offset)) > 0) && (letter2 < 0x100))
  727. letter = letter2;
  728. else
  729. return(0);
  730. }
  731. else
  732. {
  733. if((letter >= 0xc0) && (letter < N_REMOVE_ACCENT))
  734. return(tr->letter_bits[remove_accent[letter-0xc0]] & (1L << group));
  735. }
  736. if((letter >= 0) && (letter < 0x100))
  737. return(tr->letter_bits[letter] & (1L << group));
  738. return(0);
  739. }
  740. int IsVowel(Translator *tr, int letter)
  741. {//====================================
  742. return(IsLetter(tr, letter, LETTERGP_VOWEL2));
  743. }
  744. static int Unpronouncable2(Translator *tr, char *word)
  745. {//===================================================
  746. int c;
  747. int end_flags;
  748. char ph_buf[N_WORD_PHONEMES];
  749. ph_buf[0] = 0;
  750. c = word[-1];
  751. word[-1] = ' '; // ensure there is a space before the "word"
  752. end_flags = TranslateRules(tr, word, ph_buf, sizeof(ph_buf), NULL, FLAG_UNPRON_TEST, NULL);
  753. word[-1] = c;
  754. if((end_flags == 0) || (end_flags & SUFX_UNPRON))
  755. return(1);
  756. return(0);
  757. }
  758. int Unpronouncable(Translator *tr, char *word, int posn)
  759. {//=====================================================
  760. /* Determines whether a word in 'unpronouncable', i.e. whether it should
  761. be spoken as individual letters.
  762. This function may be language specific. This is a generic version.
  763. */
  764. int c;
  765. int c1=0;
  766. int vowel_posn=9;
  767. int index;
  768. int count;
  769. ALPHABET *alphabet;
  770. utf8_in(&c,word);
  771. if((tr->letter_bits_offset > 0) && (c < 0x241))
  772. {
  773. // Latin characters for a language with a non-latin alphabet
  774. return(0); // so we can re-translate the word as English
  775. }
  776. if(((alphabet = AlphabetFromChar(c)) != NULL) && (alphabet->offset != tr->letter_bits_offset))
  777. {
  778. // Character is not in our alphabet
  779. return(0);
  780. }
  781. if(tr->langopts.param[LOPT_UNPRONOUNCABLE] == 1)
  782. return(0);
  783. if(((c = *word) == ' ') || (c == 0) || (c == '\''))
  784. return(0);
  785. index = 0;
  786. count = 0;
  787. for(;;)
  788. {
  789. index += utf8_in(&c,&word[index]);
  790. if((c==0) || (c==' '))
  791. break;
  792. if((c=='\'') && ((count > 1) || (posn > 0)))
  793. break; // "tv'" but not "l'"
  794. if(count==0)
  795. c1 = c;
  796. if((c == '\'') && (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 3))
  797. {
  798. // don't count apostrophe
  799. }
  800. else
  801. count++;
  802. if(IsVowel(tr, c))
  803. {
  804. vowel_posn = count; // position of the first vowel
  805. break;
  806. }
  807. if((c != '\'') && !iswalpha2(c))
  808. return(0);
  809. }
  810. if((vowel_posn > 2) && (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 2))
  811. {
  812. // Lookup unpronounable rules in *_rules
  813. return(Unpronouncable2(tr, word));
  814. }
  815. if(c1 == tr->langopts.param[LOPT_UNPRONOUNCABLE])
  816. vowel_posn--; // disregard this as the initial letter when counting
  817. if(vowel_posn > (tr->langopts.max_initial_consonants+1))
  818. return(1); // no vowel, or no vowel in first few letters
  819. return(0);
  820. } /* end of Unpronounceable */
  821. //=============================================================================================
  822. // Determine the stress pattern of a word
  823. //
  824. //=============================================================================================
  825. static int GetVowelStress(Translator *tr, unsigned char *phonemes, signed char *vowel_stress, int *vowel_count, int *stressed_syllable, int control)
  826. {//=================================================================================================================================================
  827. // control = 1, set stress to 1 for forced unstressed vowels
  828. unsigned char phcode;
  829. PHONEME_TAB *ph;
  830. unsigned char *ph_out = phonemes;
  831. int count = 1;
  832. int max_stress = -1;
  833. int ix;
  834. int j;
  835. int stress = -1;
  836. int primary_posn = 0;
  837. vowel_stress[0] = 1;
  838. while(((phcode = *phonemes++) != 0) && (count < (N_WORD_PHONEMES/2)-1))
  839. {
  840. if((ph = phoneme_tab[phcode]) == NULL)
  841. continue;
  842. if((ph->type == phSTRESS) && (ph->program == 0))
  843. {
  844. /* stress marker, use this for the following vowel */
  845. if(phcode == phonSTRESS_PREV)
  846. {
  847. /* primary stress on preceeding vowel */
  848. j = count - 1;
  849. while((j > 0) && (*stressed_syllable == 0) && (vowel_stress[j] < 4))
  850. {
  851. if((vowel_stress[j] != 0) && (vowel_stress[j] != 1))
  852. {
  853. // don't promote a phoneme which must be unstressed
  854. vowel_stress[j] = 4;
  855. if(max_stress < 4)
  856. {
  857. max_stress = 4;
  858. primary_posn = j;
  859. }
  860. /* reduce any preceding primary stress markers */
  861. for(ix=1; ix<j; ix++)
  862. {
  863. if(vowel_stress[ix] == 4)
  864. vowel_stress[ix] = 3;
  865. }
  866. break;
  867. }
  868. j--;
  869. }
  870. }
  871. else
  872. {
  873. if((ph->std_length < 4) || (*stressed_syllable == 0))
  874. {
  875. stress = ph->std_length;
  876. if(stress > max_stress)
  877. max_stress = stress;
  878. }
  879. }
  880. continue;
  881. }
  882. if((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC))
  883. {
  884. vowel_stress[count] = (char)stress;
  885. if((stress >= 4) && (stress >= max_stress))
  886. {
  887. primary_posn = count;
  888. max_stress = stress;
  889. }
  890. if((stress < 0) && (control & 1) && (ph->phflags & phUNSTRESSED))
  891. vowel_stress[count] = 1; /* weak vowel, must be unstressed */
  892. count++;
  893. stress = -1;
  894. }
  895. else if(phcode == phonSYLLABIC)
  896. {
  897. // previous consonant phoneme is syllablic
  898. vowel_stress[count] = (char)stress;
  899. if((stress == 0) && (control & 1))
  900. vowel_stress[count++] = 1; // syllabic consonant, usually unstressed
  901. }
  902. *ph_out++ = phcode;
  903. }
  904. vowel_stress[count] = 1;
  905. *ph_out = 0;
  906. /* has the position of the primary stress been specified by $1, $2, etc? */
  907. if(*stressed_syllable > 0)
  908. {
  909. if(*stressed_syllable >= count)
  910. *stressed_syllable = count-1; // the final syllable
  911. vowel_stress[*stressed_syllable] = 4;
  912. max_stress = 4;
  913. primary_posn = *stressed_syllable;
  914. }
  915. if(max_stress == 5)
  916. {
  917. // priority stress, replaces any other primary stress marker
  918. for(ix=1; ix<count; ix++)
  919. {
  920. if(vowel_stress[ix] == 4)
  921. {
  922. if(tr->langopts.stress_flags & S_PRIORITY_STRESS)
  923. vowel_stress[ix] = 1;
  924. else
  925. vowel_stress[ix] = 3;
  926. }
  927. if(vowel_stress[ix] == 5)
  928. {
  929. vowel_stress[ix] = 4;
  930. primary_posn = ix;
  931. }
  932. }
  933. max_stress = 4;
  934. }
  935. *stressed_syllable = primary_posn;
  936. *vowel_count = count;
  937. return(max_stress);
  938. } // end of GetVowelStress
  939. static char stress_phonemes[] = {phonSTRESS_D, phonSTRESS_U, phonSTRESS_2, phonSTRESS_3,
  940. phonSTRESS_P, phonSTRESS_P2, phonSTRESS_TONIC
  941. };
  942. void ChangeWordStress(Translator *tr, char *word, int new_stress)
  943. {//==============================================================
  944. int ix;
  945. unsigned char *p;
  946. int max_stress;
  947. int vowel_count; // num of vowels + 1
  948. int stressed_syllable=0; // position of stressed syllable
  949. unsigned char phonetic[N_WORD_PHONEMES];
  950. signed char vowel_stress[N_WORD_PHONEMES/2];
  951. strcpy((char *)phonetic,word);
  952. max_stress = GetVowelStress(tr, phonetic, vowel_stress, &vowel_count, &stressed_syllable, 0);
  953. if(new_stress >= 4)
  954. {
  955. // promote to primary stress
  956. for(ix=1; ix<vowel_count; ix++)
  957. {
  958. if(vowel_stress[ix] >= max_stress)
  959. {
  960. vowel_stress[ix] = new_stress;
  961. break;
  962. }
  963. }
  964. }
  965. else
  966. {
  967. // remove primary stress
  968. for(ix=1; ix<vowel_count; ix++)
  969. {
  970. if(vowel_stress[ix] > new_stress) // >= allows for diminished stress (=1)
  971. vowel_stress[ix] = new_stress;
  972. }
  973. }
  974. // write out phonemes
  975. ix = 1;
  976. p = phonetic;
  977. while(*p != 0)
  978. {
  979. if((phoneme_tab[*p]->type == phVOWEL) && !(phoneme_tab[*p]->phflags & phNONSYLLABIC))
  980. {
  981. if((vowel_stress[ix] == 0) || (vowel_stress[ix] > 1))
  982. *word++ = stress_phonemes[(unsigned char)vowel_stress[ix]];
  983. ix++;
  984. }
  985. *word++ = *p++;
  986. }
  987. *word = 0;
  988. } // end of ChangeWordStress
  989. void SetWordStress(Translator *tr, char *output, unsigned int *dictionary_flags, int tonic, int control)
  990. {//=====================================================================================================
  991. /* Guess stress pattern of word. This is language specific
  992. 'output' is used for input and output
  993. 'dictionary_flags' has bits 0-3 position of stressed vowel (if > 0)
  994. or unstressed (if == 7) or syllables 1 and 2 (if == 6)
  995. bits 8... dictionary flags
  996. If 'tonic' is set (>= 0), replace highest stress by this value.
  997. control: bit 0 This is an individual symbol, not a word
  998. bit 1 Suffix phonemes are still to be added
  999. */
  1000. unsigned char phcode;
  1001. unsigned char *p;
  1002. PHONEME_TAB *ph;
  1003. int stress;
  1004. int max_stress;
  1005. int max_stress_input; // any stress specified in the input?
  1006. int vowel_count; // num of vowels + 1
  1007. int ix;
  1008. int v;
  1009. int v_stress;
  1010. int stressed_syllable; // position of stressed syllable
  1011. int max_stress_posn;
  1012. int unstressed_word = 0;
  1013. char *max_output;
  1014. int final_ph;
  1015. int final_ph2;
  1016. int mnem;
  1017. int post_tonic; // currently not used
  1018. int opt_length;
  1019. int done;
  1020. int stressflags;
  1021. int dflags = 0;
  1022. int first_primary;
  1023. int long_vowel;
  1024. signed char vowel_stress[N_WORD_PHONEMES/2];
  1025. char syllable_weight[N_WORD_PHONEMES/2];
  1026. char vowel_length[N_WORD_PHONEMES/2];
  1027. unsigned char phonetic[N_WORD_PHONEMES];
  1028. static char consonant_types[16] = {0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0};
  1029. /* stress numbers STRESS_BASE +
  1030. 0 diminished, unstressed within a word
  1031. 1 unstressed, weak
  1032. 2
  1033. 3 secondary stress
  1034. 4 main stress */
  1035. stressflags = tr->langopts.stress_flags;
  1036. if(dictionary_flags != NULL)
  1037. dflags = dictionary_flags[0];
  1038. /* copy input string into internal buffer */
  1039. for(ix=0; ix<N_WORD_PHONEMES; ix++)
  1040. {
  1041. phonetic[ix] = output[ix];
  1042. // check for unknown phoneme codes
  1043. if(phonetic[ix] >= n_phoneme_tab)
  1044. phonetic[ix] = phonSCHWA;
  1045. if(phonetic[ix] == 0)
  1046. break;
  1047. }
  1048. if(ix == 0) return;
  1049. final_ph = phonetic[ix-1];
  1050. final_ph2 = phonetic[ix-2];
  1051. max_output = output + (N_WORD_PHONEMES-3); /* check for overrun */
  1052. // any stress position marked in the xx_list dictionary ?
  1053. stressed_syllable = dflags & 0x7;
  1054. if(dflags & 0x8)
  1055. {
  1056. // this indicates a word without a primary stress
  1057. stressed_syllable = dflags & 0x3;
  1058. unstressed_word = 1;
  1059. }
  1060. max_stress = max_stress_input = GetVowelStress(tr, phonetic, vowel_stress, &vowel_count, &stressed_syllable, 1);
  1061. if((max_stress < 0) && dictionary_flags)
  1062. {
  1063. max_stress = 0;
  1064. }
  1065. // heavy or light syllables
  1066. ix = 1;
  1067. for(p = phonetic; *p != 0; p++)
  1068. {
  1069. if((phoneme_tab[p[0]]->type == phVOWEL) && !(phoneme_tab[p[0]]->phflags & phNONSYLLABIC))
  1070. {
  1071. int weight = 0;
  1072. int lengthened = 0;
  1073. if(phoneme_tab[p[1]]->code == phonLENGTHEN)
  1074. lengthened = 1;
  1075. if(lengthened || (phoneme_tab[p[0]]->phflags & phLONG))
  1076. {
  1077. // long vowel, increase syllable weight
  1078. weight++;
  1079. }
  1080. vowel_length[ix] = weight;
  1081. if(lengthened) p++; // advance over phonLENGTHEN
  1082. if(consonant_types[phoneme_tab[p[1]]->type] && ((phoneme_tab[p[2]]->type != phVOWEL) || (phoneme_tab[p[1]]->phflags & phLONG)))
  1083. {
  1084. // followed by two consonants, a long consonant, or consonant and end-of-word
  1085. weight++;
  1086. }
  1087. syllable_weight[ix] = weight;
  1088. ix++;
  1089. }
  1090. }
  1091. switch(tr->langopts.stress_rule)
  1092. {
  1093. case 8:
  1094. // stress on first syllable, unless it is a light syllable followed by a heavy syllable
  1095. if((syllable_weight[1] > 0) || (syllable_weight[2] == 0))
  1096. break;
  1097. // else drop through to case 1
  1098. case 1:
  1099. // stress on second syllable
  1100. if((stressed_syllable == 0) && (vowel_count > 2))
  1101. {
  1102. stressed_syllable = 2;
  1103. if(max_stress == 0)
  1104. {
  1105. vowel_stress[stressed_syllable] = 4;
  1106. }
  1107. max_stress = 4;
  1108. }
  1109. break;
  1110. case 10: // penultimate, but final if only 1 or 2 syllables
  1111. if(stressed_syllable == 0)
  1112. {
  1113. if(vowel_count < 4)
  1114. {
  1115. vowel_stress[vowel_count - 1] = 4;
  1116. max_stress = 4;
  1117. break;
  1118. }
  1119. }
  1120. // drop through to next case
  1121. case 2:
  1122. // a language with stress on penultimate vowel
  1123. if(stressed_syllable == 0)
  1124. {
  1125. /* no explicit stress - stress the penultimate vowel */
  1126. max_stress = 4;
  1127. if(vowel_count > 2)
  1128. {
  1129. stressed_syllable = vowel_count - 2;
  1130. if(stressflags & S_FINAL_SPANISH)
  1131. {
  1132. // LANG=Spanish, stress on last vowel if the word ends in a consonant other than 'n' or 's'
  1133. if(phoneme_tab[final_ph]->type != phVOWEL)
  1134. {
  1135. mnem = phoneme_tab[final_ph]->mnemonic;
  1136. if(tr->translator_name == L('a','n'))
  1137. {
  1138. if(((mnem != 's') && (mnem !='n')) || phoneme_tab[final_ph2]->type != phVOWEL)
  1139. stressed_syllable = vowel_count - 1; // stress on last syllable
  1140. }
  1141. else
  1142. if(tr->translator_name == L('i','a'))
  1143. {
  1144. if((mnem != 's') || phoneme_tab[final_ph2]->type != phVOWEL)
  1145. stressed_syllable = vowel_count - 1; // stress on last syllable
  1146. }
  1147. else
  1148. {
  1149. if((mnem == 's') && (phoneme_tab[final_ph2]->type == phNASAL))
  1150. {
  1151. // -ns stress remains on penultimate syllable
  1152. }
  1153. else if(((phoneme_tab[final_ph]->type != phNASAL) && (mnem != 's')) || (phoneme_tab[final_ph2]->type != phVOWEL))
  1154. {
  1155. stressed_syllable = vowel_count - 1;
  1156. }
  1157. }
  1158. }
  1159. }
  1160. if(stressflags & S_FINAL_LONG)
  1161. {
  1162. // stress on last syllable if it has a long vowel, but previous syllable has a short vowel
  1163. if(vowel_length[vowel_count - 1] > vowel_length[vowel_count - 2])
  1164. {
  1165. stressed_syllable = vowel_count - 1;
  1166. }
  1167. }
  1168. if((vowel_stress[stressed_syllable] == 0) || (vowel_stress[stressed_syllable] == 1))
  1169. {
  1170. // but this vowel is explicitly marked as unstressed
  1171. if(stressed_syllable > 1)
  1172. stressed_syllable--;
  1173. else
  1174. stressed_syllable++;
  1175. }
  1176. }
  1177. else
  1178. {
  1179. stressed_syllable = 1;
  1180. }
  1181. // only set the stress if it's not already marked explicitly
  1182. if(vowel_stress[stressed_syllable] < 0)
  1183. {
  1184. // don't stress if next and prev syllables are stressed
  1185. if((vowel_stress[stressed_syllable-1] < 4) || (vowel_stress[stressed_syllable+1] < 4))
  1186. vowel_stress[stressed_syllable] = max_stress;
  1187. }
  1188. }
  1189. break;
  1190. case 3:
  1191. // stress on last vowel
  1192. if(stressed_syllable == 0)
  1193. {
  1194. /* no explicit stress - stress the final vowel */
  1195. stressed_syllable = vowel_count - 1;
  1196. while(stressed_syllable > 0)
  1197. {
  1198. // find the last vowel which is not unstressed
  1199. if(vowel_stress[stressed_syllable] < 0)
  1200. {
  1201. vowel_stress[stressed_syllable] = 4;
  1202. break;
  1203. }
  1204. else
  1205. stressed_syllable--;
  1206. }
  1207. max_stress = 4;
  1208. }
  1209. break;
  1210. case 4: // stress on antipenultimate vowel
  1211. if(stressed_syllable == 0)
  1212. {
  1213. stressed_syllable = vowel_count - 3;
  1214. if(stressed_syllable < 1)
  1215. stressed_syllable = 1;
  1216. if(max_stress == 0)
  1217. {
  1218. vowel_stress[stressed_syllable] = 4;
  1219. }
  1220. max_stress = 4;
  1221. }
  1222. break;
  1223. case 5:
  1224. // LANG=Russian
  1225. if(stressed_syllable == 0)
  1226. {
  1227. /* no explicit stress - guess the stress from the number of syllables */
  1228. static char guess_ru[16] = {0,0,1,1,2,3,3,4,5,6,7,7,8,9,10,11};
  1229. static char guess_ru_v[16] = {0,0,1,1,2,2,3,3,4,5,6,7,7,8,9,10}; // for final phoneme is a vowel
  1230. static char guess_ru_t[16] = {0,0,1,2,3,3,3,4,5,6,7,7,7,8,9,10}; // for final phoneme is an unvoiced stop
  1231. stressed_syllable = vowel_count - 3;
  1232. if(vowel_count < 16)
  1233. {
  1234. if(phoneme_tab[final_ph]->type == phVOWEL)
  1235. stressed_syllable = guess_ru_v[vowel_count];
  1236. else if(phoneme_tab[final_ph]->type == phSTOP)
  1237. stressed_syllable = guess_ru_t[vowel_count];
  1238. else
  1239. stressed_syllable = guess_ru[vowel_count];
  1240. }
  1241. vowel_stress[stressed_syllable] = 4;
  1242. max_stress = 4;
  1243. }
  1244. break;
  1245. case 6: // LANG=hi stress on the last heaviest syllable
  1246. if(stressed_syllable == 0)
  1247. {
  1248. int wt;
  1249. int max_weight = -1;
  1250. // int prev_stressed;
  1251. // find the heaviest syllable, excluding the final syllable
  1252. for(ix = 1; ix < (vowel_count-1); ix++)
  1253. {
  1254. if(vowel_stress[ix] < 0)
  1255. {
  1256. if((wt = syllable_weight[ix]) >= max_weight)
  1257. {
  1258. max_weight = wt;
  1259. // prev_stressed = stressed_syllable;
  1260. stressed_syllable = ix;
  1261. }
  1262. }
  1263. }
  1264. if((syllable_weight[vowel_count-1] == 2) && (max_weight< 2))
  1265. {
  1266. // the only double=heavy syllable is the final syllable, so stress this
  1267. stressed_syllable = vowel_count-1;
  1268. }
  1269. else if(max_weight <= 0)
  1270. {
  1271. // all syllables, exclusing the last, are light. Stress the first syllable
  1272. stressed_syllable = 1;
  1273. }
  1274. vowel_stress[stressed_syllable] = 4;
  1275. max_stress = 4;
  1276. }
  1277. break;
  1278. case 7: // LANG=tr, the last syllable for any vowel marked explicitly as unstressed
  1279. if(stressed_syllable == 0)
  1280. {
  1281. stressed_syllable = vowel_count - 1;
  1282. for(ix=1; ix < vowel_count; ix++)
  1283. {
  1284. if(vowel_stress[ix] == 1)
  1285. {
  1286. stressed_syllable = ix-1;
  1287. break;
  1288. }
  1289. }
  1290. vowel_stress[stressed_syllable] = 4;
  1291. max_stress = 4;
  1292. }
  1293. break;
  1294. case 9: // mark all as stressed
  1295. for(ix=1; ix<vowel_count; ix++)
  1296. {
  1297. if(vowel_stress[ix] < 0)
  1298. vowel_stress[ix] = 4;
  1299. }
  1300. break;
  1301. case 12: // LANG=kl (Greenlandic)
  1302. long_vowel = 0;
  1303. for(ix=1; ix < vowel_count; ix++)
  1304. {
  1305. if(vowel_stress[ix] == 4)
  1306. vowel_stress[ix] = 3; // change marked stress (consonant clusters) to secondary (except the last)
  1307. if(vowel_length[ix] > 0)
  1308. {
  1309. long_vowel = ix;
  1310. vowel_stress[ix] = 3; // give secondary stress to all long vowels
  1311. }
  1312. }
  1313. // 'stressed_syllable' gives the last marked stress
  1314. if(stressed_syllable == 0)
  1315. {
  1316. // no marked stress, choose the last long vowel
  1317. if(long_vowel > 0)
  1318. stressed_syllable = long_vowel;
  1319. else
  1320. {
  1321. // no long vowels or consonant clusters
  1322. if(vowel_count > 5)
  1323. stressed_syllable = vowel_count - 3; // more than 4 syllables
  1324. else
  1325. stressed_syllable = vowel_count - 1;
  1326. }
  1327. }
  1328. vowel_stress[stressed_syllable] = 4;
  1329. max_stress = 4;
  1330. break;
  1331. case 13: // LANG=ml, 1st unless 1st vowel is short and 2nd is long
  1332. if(stressed_syllable == 0)
  1333. {
  1334. stressed_syllable = 1;
  1335. if((vowel_length[1] == 0) && (vowel_count > 2) && (vowel_length[2] > 0))
  1336. stressed_syllable = 2;
  1337. vowel_stress[stressed_syllable] = 4;
  1338. max_stress = 4;
  1339. }
  1340. break;
  1341. }
  1342. if((stressflags & S_FINAL_VOWEL_UNSTRESSED) && ((control & 2) == 0) && (vowel_count > 2) && (max_stress_input < 3) && (vowel_stress[vowel_count - 1] == 4))
  1343. {
  1344. // Don't allow stress on a word-final vowel
  1345. // Only do this if there is no suffix phonemes to be added, and if a stress position was not given explicitly
  1346. if(phoneme_tab[final_ph]->type == phVOWEL)
  1347. {
  1348. vowel_stress[vowel_count - 1] = 1;
  1349. vowel_stress[vowel_count - 2] = 4;
  1350. }
  1351. }
  1352. /* now guess the complete stress pattern */
  1353. if(max_stress < 4)
  1354. stress = 4; /* no primary stress marked, use for 1st syllable */
  1355. else
  1356. stress = 3;
  1357. if(unstressed_word == 0)
  1358. {
  1359. if((stressflags & S_2_SYL_2) && (vowel_count == 3))
  1360. {
  1361. // Two syllable word, if one syllable has primary stress, then give the other secondary stress
  1362. if(vowel_stress[1] == 4)
  1363. vowel_stress[2] = 3;
  1364. if(vowel_stress[2] == 4)
  1365. vowel_stress[1] = 3;
  1366. }
  1367. if((stressflags & S_INITIAL_2) && (vowel_stress[1] < 0))
  1368. {
  1369. // If there is only one syllable before the primary stress, give it a secondary stress
  1370. if((vowel_count > 3) && (vowel_stress[2] >= 4))
  1371. {
  1372. vowel_stress[1] = 3;
  1373. }
  1374. }
  1375. }
  1376. done = 0;
  1377. first_primary = 0;
  1378. for(v=1; v<vowel_count; v++)
  1379. {
  1380. if(vowel_stress[v] < 0)
  1381. {
  1382. if((stressflags & S_FINAL_NO_2) && (stress < 4) && (v == vowel_count-1))
  1383. {
  1384. // flag: don't give secondary stress to final vowel
  1385. }
  1386. else if((stressflags & 0x8000) && (done == 0))
  1387. {
  1388. vowel_stress[v] = (char)stress;
  1389. done =1;
  1390. stress = 3; /* use secondary stress for remaining syllables */
  1391. }
  1392. else if((vowel_stress[v-1] <= 1) && ((vowel_stress[v+1] <= 1) || ((stress == 4) && (vowel_stress[v+1] <= 2))))
  1393. {
  1394. /* trochaic: give stress to vowel surrounded by unstressed vowels */
  1395. if((stress == 3) && (stressflags & S_NO_AUTO_2))
  1396. continue; // don't use secondary stress
  1397. if((v > 1) && (stressflags & S_2_TO_HEAVY) && (syllable_weight[v]==0) && (syllable_weight[v+1]>0))
  1398. {
  1399. // don't put secondary stress on a light syllable which is followed by a heavy syllable
  1400. continue;
  1401. }
  1402. // should start with secondary stress on the first syllable, or should it count back from
  1403. // the primary stress and put secondary stress on alternate syllables?
  1404. vowel_stress[v] = (char)stress;
  1405. done =1;
  1406. stress = 3; /* use secondary stress for remaining syllables */
  1407. }
  1408. }
  1409. if(vowel_stress[v] >= 4)
  1410. {
  1411. if(first_primary == 0)
  1412. first_primary = v;
  1413. else if(stressflags & S_FIRST_PRIMARY)
  1414. {
  1415. // reduce primary stresses after the first to secondary
  1416. vowel_stress[v] = 3;
  1417. }
  1418. }
  1419. }
  1420. if((unstressed_word) && (tonic < 0))
  1421. {
  1422. if(vowel_count <= 2)
  1423. tonic = tr->langopts.unstressed_wd1; /* monosyllable - unstressed */
  1424. else
  1425. tonic = tr->langopts.unstressed_wd2; /* more than one syllable, used secondary stress as the main stress */
  1426. }
  1427. max_stress = 0;
  1428. max_stress_posn = 0;
  1429. for(v=1; v<vowel_count; v++)
  1430. {
  1431. if(vowel_stress[v] >= max_stress)
  1432. {
  1433. max_stress = vowel_stress[v];
  1434. max_stress_posn = v;
  1435. }
  1436. }
  1437. if(tonic >= 0)
  1438. {
  1439. /* find position of highest stress, and replace it by 'tonic' */
  1440. /* don't disturb an explicitly set stress by 'unstress-at-end' flag */
  1441. if((tonic > max_stress) || (max_stress <= 4))
  1442. vowel_stress[max_stress_posn] = (char)tonic;
  1443. max_stress = tonic;
  1444. }
  1445. /* produce output phoneme string */
  1446. p = phonetic;
  1447. v = 1;
  1448. if(!(control & 1) && ((ph = phoneme_tab[*p]) != NULL))
  1449. {
  1450. while((ph->type == phSTRESS) || (*p == phonEND_WORD))
  1451. {
  1452. p++;
  1453. ph = phoneme_tab[p[0]];
  1454. }
  1455. #ifdef deleted
  1456. int gap = tr->langopts.word_gap & 0x700;
  1457. if((gap) && (vowel_stress[1] >= 4) && (prev_stress >= 4))
  1458. {
  1459. /* two primary stresses together, insert a short pause */
  1460. *output++ = pause_phonemes[gap >> 8];
  1461. }
  1462. else
  1463. #endif
  1464. if((tr->langopts.vowel_pause & 0x30) && (ph->type == phVOWEL))
  1465. {
  1466. // word starts with a vowel
  1467. if((tr->langopts.vowel_pause & 0x20) && (vowel_stress[1] >= 4))
  1468. {
  1469. *output++ = phonPAUSE_NOLINK; // not to be replaced by link
  1470. }
  1471. else
  1472. {
  1473. *output++ = phonPAUSE_VSHORT; // break, but no pause
  1474. }
  1475. }
  1476. }
  1477. p = phonetic;
  1478. post_tonic = 0;
  1479. while(((phcode = *p++) != 0) && (output < max_output))
  1480. {
  1481. if((ph = phoneme_tab[phcode]) == NULL)
  1482. continue;
  1483. // if(ph->type == phSTRESS)
  1484. // continue;
  1485. if(ph->type == phPAUSE)
  1486. {
  1487. tr->prev_last_stress = 0;
  1488. }
  1489. else if(((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC)) || (*p == phonSYLLABIC))
  1490. {
  1491. // a vowel, or a consonant followed by a syllabic consonant marker
  1492. v_stress = vowel_stress[v];
  1493. tr->prev_last_stress = v_stress;
  1494. if(vowel_stress[v-1] >= max_stress)
  1495. post_tonic = 1;
  1496. if(v_stress <= 1)
  1497. {
  1498. if((v > 1) && (max_stress >= 2) && (stressflags & S_FINAL_DIM) && (v == (vowel_count-1)))
  1499. {
  1500. // option: mark unstressed final syllable as diminished
  1501. v_stress = 0;
  1502. }
  1503. else if((stressflags & S_NO_DIM) || (v == 1) || (v == (vowel_count-1)))
  1504. {
  1505. // first or last syllable, or option 'don't set diminished stress'
  1506. v_stress = 1;
  1507. }
  1508. else if((v == (vowel_count-2)) && (vowel_stress[vowel_count-1] <= 1))
  1509. {
  1510. // penultimate syllable, followed by an unstressed final syllable
  1511. v_stress = 1;
  1512. }
  1513. else
  1514. {
  1515. // unstressed syllable within a word
  1516. if((vowel_stress[v-1] < 0) || ((stressflags & S_MID_DIM) == 0))
  1517. {
  1518. v_stress = 0; /* change to 0 (diminished stress) */
  1519. vowel_stress[v] = v_stress;
  1520. }
  1521. }
  1522. }
  1523. if((v_stress == 0) || (v_stress > 1))
  1524. *output++ = stress_phonemes[v_stress]; // mark stress of all vowels except 1 (unstressed)
  1525. if(vowel_stress[v] > max_stress)
  1526. {
  1527. max_stress = vowel_stress[v];
  1528. }
  1529. if((*p == phonLENGTHEN) && ((opt_length = tr->langopts.param[LOPT_IT_LENGTHEN]) & 1))
  1530. {
  1531. // remove lengthen indicator from non-stressed syllables
  1532. int shorten=0;
  1533. if(opt_length & 0x10)
  1534. {
  1535. // only allow lengthen indicator on the highest stress syllable in the word
  1536. if(v != max_stress_posn)
  1537. shorten = 1;
  1538. }
  1539. else if(v_stress < 4)
  1540. {
  1541. // only allow lengthen indicator if stress >= 4.
  1542. shorten = 1;
  1543. }
  1544. if(shorten)
  1545. p++;
  1546. }
  1547. if((v_stress >= 4) && (tr->langopts.param[LOPT_IT_LENGTHEN] == 2))
  1548. {
  1549. // LANG=Italian, lengthen penultimate stressed vowels, unless followed by 2 consonants
  1550. if((v == (vowel_count - 2)) && (syllable_weight[v] == 0))
  1551. {
  1552. *output++ = phcode;
  1553. phcode = phonLENGTHEN;
  1554. }
  1555. }
  1556. v++;
  1557. }
  1558. if(phcode != 1)
  1559. *output++ = phcode;
  1560. }
  1561. *output++ = 0;
  1562. return;
  1563. } /* end of SetWordStress */
  1564. //=============================================================================================
  1565. // Look up a word in the pronunciation rules
  1566. //
  1567. //=============================================================================================
  1568. void AppendPhonemes(Translator *tr, char *string, int size, const char *ph)
  1569. {//========================================================================
  1570. /* Add new phoneme string "ph" to "string"
  1571. Keeps count of the number of vowel phonemes in the word, and whether these
  1572. can be stressed syllables. These values can be used in translation rules
  1573. */
  1574. const char *p;
  1575. unsigned char c;
  1576. int unstress_mark;
  1577. int length;
  1578. length = strlen(ph) + strlen(string);
  1579. if(length >= size)
  1580. {
  1581. return;
  1582. }
  1583. /* any stressable vowel ? */
  1584. unstress_mark = 0;
  1585. p = ph;
  1586. while((c = *p++) != 0)
  1587. {
  1588. if(c >= n_phoneme_tab) continue;
  1589. if(phoneme_tab[c]->type == phSTRESS)
  1590. {
  1591. if(phoneme_tab[c]->std_length < 4)
  1592. unstress_mark = 1;
  1593. }
  1594. else
  1595. {
  1596. if(phoneme_tab[c]->type == phVOWEL)
  1597. {
  1598. if(((phoneme_tab[c]->phflags & phUNSTRESSED) == 0) &&
  1599. (unstress_mark == 0))
  1600. {
  1601. tr->word_stressed_count++;
  1602. }
  1603. unstress_mark = 0;
  1604. tr->word_vowel_count++;
  1605. }
  1606. }
  1607. }
  1608. if(string != NULL)
  1609. strcat(string,ph);
  1610. } /* end of AppendPhonemes */
  1611. static void MatchRule(Translator *tr, char *word[], char *word_start, int group_length, char *rule, MatchRecord *match_out, int word_flags, int dict_flags)
  1612. {//========================================================================================================================================================
  1613. /* Checks a specified word against dictionary rules.
  1614. Returns with phoneme code string, or NULL if no match found.
  1615. word (indirect) points to current character group within the input word
  1616. This is advanced by this procedure as characters are consumed
  1617. group: the initial characters used to choose the rules group
  1618. rule: address of dictionary rule data for this character group
  1619. match_out: returns best points score
  1620. word_flags: indicates whether this is a retranslation after a suffix has been removed
  1621. */
  1622. unsigned char rb; // current instuction from rule
  1623. unsigned char letter; // current letter from input word, single byte
  1624. int letter_w; // current letter, wide character
  1625. int letter_xbytes; // number of extra bytes of multibyte character (num bytes - 1)
  1626. unsigned char last_letter;
  1627. char *pre_ptr;
  1628. char *post_ptr; /* pointer to first character after group */
  1629. char *rule_start; /* start of current match template */
  1630. char *p;
  1631. int ix;
  1632. int match_type; /* left, right, or consume */
  1633. int failed;
  1634. int unpron_ignore;
  1635. int consumed; /* number of letters consumed from input */
  1636. int syllable_count;
  1637. int vowel;
  1638. int letter_group;
  1639. int distance_right;
  1640. int distance_left;
  1641. int lg_pts;
  1642. int n_bytes;
  1643. int add_points;
  1644. int command;
  1645. int check_atstart;
  1646. unsigned int *flags;
  1647. MatchRecord match;
  1648. static MatchRecord best;
  1649. int total_consumed; /* letters consumed for best match */
  1650. unsigned char condition_num;
  1651. char *common_phonemes; /* common to a group of entries */
  1652. char *group_chars;
  1653. char word_buf[N_WORD_BYTES];
  1654. group_chars = *word;
  1655. if(rule == NULL)
  1656. {
  1657. match_out->points = 0;
  1658. (*word)++;
  1659. return;
  1660. }
  1661. total_consumed = 0;
  1662. common_phonemes = NULL;
  1663. match_type = 0;
  1664. best.points = 0;
  1665. best.phonemes = "";
  1666. best.end_type = 0;
  1667. best.del_fwd = NULL;
  1668. /* search through dictionary rules */
  1669. while(rule[0] != RULE_GROUP_END)
  1670. {
  1671. unpron_ignore = word_flags & FLAG_UNPRON_TEST;
  1672. match_type=0;
  1673. consumed = 0;
  1674. letter = 0;
  1675. distance_right= -6; /* used to reduce points for matches further away the current letter */
  1676. distance_left= -2;
  1677. check_atstart = 0;
  1678. match.points = 1;
  1679. match.end_type = 0;
  1680. match.del_fwd = NULL;
  1681. pre_ptr = *word;
  1682. post_ptr = *word + group_length;
  1683. /* work through next rule until end, or until no-match proved */
  1684. rule_start = rule;
  1685. failed = 0;
  1686. while(!failed)
  1687. {
  1688. rb = *rule++;
  1689. if(rb <= RULE_LINENUM)
  1690. {
  1691. switch(rb)
  1692. {
  1693. case 0: // no phoneme string for this rule, use previous common rule
  1694. if(common_phonemes != NULL)
  1695. {
  1696. match.phonemes = common_phonemes;
  1697. while(((rb = *match.phonemes++) != 0) && (rb != RULE_PHONEMES))
  1698. {
  1699. if(rb == RULE_CONDITION)
  1700. match.phonemes++; // skip over condition number
  1701. if(rb == RULE_LINENUM)
  1702. match.phonemes += 2; // skip over line number
  1703. }
  1704. }
  1705. else
  1706. {
  1707. match.phonemes = "";
  1708. }
  1709. rule--; // so we are still pointing at the 0
  1710. failed=2; // matched OK
  1711. break;
  1712. case RULE_PRE_ATSTART: // pre rule with implied 'start of word'
  1713. check_atstart = 1;
  1714. unpron_ignore = 0;
  1715. match_type = RULE_PRE;
  1716. break;
  1717. case RULE_PRE:
  1718. match_type = RULE_PRE;
  1719. if(word_flags & FLAG_UNPRON_TEST)
  1720. {
  1721. // checking the start of the word for unpronouncable character sequences, only
  1722. // consider rules which explicitly match the start of a word
  1723. // Note: Those rules now use RULE_PRE_ATSTART
  1724. failed = 1;
  1725. }
  1726. break;
  1727. case RULE_POST:
  1728. match_type = RULE_POST;
  1729. break;
  1730. case RULE_PHONEMES:
  1731. match.phonemes = rule;
  1732. failed=2; // matched OK
  1733. break;
  1734. case RULE_PH_COMMON:
  1735. common_phonemes = rule;
  1736. break;
  1737. case RULE_CONDITION:
  1738. /* conditional rule, next byte gives condition number */
  1739. condition_num = *rule++;
  1740. if(condition_num >= 32)
  1741. {
  1742. // allow the rule only if the condition number is NOT set
  1743. if((tr->dict_condition & (1L << (condition_num-32))) != 0)
  1744. failed = 1;
  1745. }
  1746. else
  1747. {
  1748. // allow the rule only if the condition number is set
  1749. if((tr->dict_condition & (1L << condition_num)) == 0)
  1750. failed = 1;
  1751. }
  1752. if(!failed)
  1753. match.points++; // add one point for a matched conditional rule
  1754. break;
  1755. case RULE_LINENUM:
  1756. rule+=2;
  1757. break;
  1758. }
  1759. continue;
  1760. }
  1761. add_points = 0;
  1762. switch(match_type)
  1763. {
  1764. case 0:
  1765. /* match and consume this letter */
  1766. last_letter = letter;
  1767. letter = *post_ptr++;
  1768. if((letter == rb) || ((letter==(unsigned char)REPLACED_E) && (rb=='e')))
  1769. {
  1770. if((letter & 0xc0) != 0x80)
  1771. add_points = 21; // don't add point for non-initial UTF-8 bytes
  1772. consumed++;
  1773. }
  1774. else
  1775. failed = 1;
  1776. break;
  1777. case RULE_POST:
  1778. /* continue moving fowards */
  1779. distance_right += 6;
  1780. if(distance_right > 18)
  1781. distance_right = 19;
  1782. last_letter = letter;
  1783. letter_xbytes = utf8_in(&letter_w,post_ptr)-1;
  1784. letter = *post_ptr++;
  1785. switch(rb)
  1786. {
  1787. case RULE_LETTERGP:
  1788. letter_group = *rule++ - 'A';
  1789. if(IsLetter(tr, letter_w, letter_group))
  1790. {
  1791. lg_pts = 20;
  1792. if(letter_group==2)
  1793. lg_pts = 19; // fewer points for C, general consonant
  1794. add_points = (lg_pts-distance_right);
  1795. post_ptr += letter_xbytes;
  1796. }
  1797. else
  1798. failed = 1;
  1799. break;
  1800. case RULE_LETTERGP2: // match against a list of utf-8 strings
  1801. letter_group = *rule++ - 'A';
  1802. if((n_bytes = IsLetterGroup(tr, post_ptr-1,letter_group,0)) >0)
  1803. {
  1804. add_points = (20-distance_right);
  1805. post_ptr += (n_bytes-1);
  1806. }
  1807. else
  1808. failed =1;
  1809. break;
  1810. case RULE_NOTVOWEL:
  1811. if(IsLetter(tr, letter_w, 0) || ((letter_w == ' ') && (word_flags & FLAG_SUFFIX_VOWEL)))
  1812. {
  1813. failed = 1;
  1814. }
  1815. else
  1816. {
  1817. add_points = (20-distance_right);
  1818. post_ptr += letter_xbytes;
  1819. }
  1820. break;
  1821. case RULE_DIGIT:
  1822. if(IsDigit(letter_w))
  1823. {
  1824. add_points = (20-distance_right);
  1825. post_ptr += letter_xbytes;
  1826. }
  1827. else if(tr->langopts.tone_numbers)
  1828. {
  1829. // also match if there is no digit
  1830. add_points = (20-distance_right);
  1831. post_ptr--;
  1832. }
  1833. else
  1834. failed = 1;
  1835. break;
  1836. case RULE_NONALPHA:
  1837. if(!iswalpha2(letter_w))
  1838. {
  1839. add_points = (21-distance_right);
  1840. post_ptr += letter_xbytes;
  1841. }
  1842. else
  1843. failed = 1;
  1844. break;
  1845. case RULE_DOUBLE:
  1846. if(letter == last_letter)
  1847. add_points = (21-distance_right);
  1848. else
  1849. failed = 1;
  1850. break;
  1851. case RULE_DOLLAR:
  1852. command = *rule++;
  1853. if(command == DOLLAR_UNPR)
  1854. {
  1855. match.end_type = SUFX_UNPRON; // $unpron
  1856. }
  1857. else if(command == DOLLAR_NOPREFIX) // $noprefix
  1858. {
  1859. if(word_flags & FLAG_PREFIX_REMOVED)
  1860. failed = 1; // a prefix has been removed
  1861. else
  1862. add_points = 1;
  1863. }
  1864. else if((command & 0xf0) == 0x10)
  1865. {
  1866. // $w_alt
  1867. if(dict_flags & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
  1868. add_points = 23;
  1869. else
  1870. failed = 1;
  1871. }
  1872. else if(((command & 0xf0) == 0x20) || (command == DOLLAR_LIST))
  1873. {
  1874. // $list or $p_alt
  1875. // make a copy of the word up to the post-match characters
  1876. ix = *word - word_start + consumed + group_length + 1;
  1877. memcpy(word_buf, word_start-1, ix);
  1878. word_buf[ix] = ' ';
  1879. word_buf[ix+1] = 0;
  1880. LookupFlags(tr, &word_buf[1], &flags);
  1881. if((command == DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY))
  1882. add_points = 23;
  1883. else
  1884. if(flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
  1885. add_points = 23;
  1886. else
  1887. failed = 1;
  1888. }
  1889. break;
  1890. case '-':
  1891. if((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN_AFTER)))
  1892. {
  1893. add_points = (22-distance_right); // one point more than match against space
  1894. }
  1895. else
  1896. failed = 1;
  1897. break;
  1898. case RULE_SYLLABLE:
  1899. {
  1900. /* more than specified number of vowel letters to the right */
  1901. char *p = post_ptr + letter_xbytes;
  1902. int vowel_count=0;
  1903. syllable_count = 1;
  1904. while(*rule == RULE_SYLLABLE)
  1905. {
  1906. rule++;
  1907. syllable_count+=1; /* number of syllables to match */
  1908. }
  1909. vowel = 0;
  1910. while(letter_w != RULE_SPACE)
  1911. {
  1912. if((vowel==0) && IsLetter(tr, letter_w,LETTERGP_VOWEL2))
  1913. {
  1914. // this is counting vowels which are separated by non-vowel letters
  1915. vowel_count++;
  1916. }
  1917. vowel = IsLetter(tr, letter_w,LETTERGP_VOWEL2);
  1918. p += utf8_in(&letter_w,p);
  1919. }
  1920. if(syllable_count <= vowel_count)
  1921. add_points = (18+syllable_count-distance_right);
  1922. else
  1923. failed = 1;
  1924. }
  1925. break;
  1926. case RULE_NOVOWELS:
  1927. {
  1928. char *p = post_ptr + letter_xbytes;
  1929. while(letter_w != RULE_SPACE)
  1930. {
  1931. if(IsLetter(tr, letter_w,LETTERGP_VOWEL2))
  1932. {
  1933. failed = 1;
  1934. break;
  1935. }
  1936. p += utf8_in(&letter_w,p);
  1937. }
  1938. if(!failed)
  1939. add_points = (19-distance_right);
  1940. }
  1941. break;
  1942. case RULE_SKIPCHARS:
  1943. {
  1944. // Used for lang=Tamil, used to match on the next word after an unknown word ending
  1945. // only look until the end of the word (including the end-of-word marker)
  1946. // Jx means 'skip characters until x', where 'x' may be '_' for 'end of word'
  1947. char *p = post_ptr + letter_xbytes;
  1948. char *p2 = p;
  1949. int rule_w; // skip characters until this
  1950. utf8_in(&rule_w,rule);
  1951. while((letter_w != rule_w) && (letter_w != RULE_SPACE))
  1952. {
  1953. p2 = p;
  1954. p += utf8_in(&letter_w,p);
  1955. }
  1956. if(letter_w == rule_w)
  1957. {
  1958. post_ptr = p2;
  1959. }
  1960. }
  1961. break;
  1962. case RULE_INC_SCORE:
  1963. add_points = 20; // force an increase in points
  1964. break;
  1965. case RULE_DEL_FWD:
  1966. // find the next 'e' in the word and replace by 'E'
  1967. for(p = *word + group_length; p < post_ptr; p++)
  1968. {
  1969. if(*p == 'e')
  1970. {
  1971. match.del_fwd = p;
  1972. break;
  1973. }
  1974. }
  1975. break;
  1976. case RULE_ENDING:
  1977. {
  1978. int end_type;
  1979. // next 3 bytes are a (non-zero) ending type. 2 bytes of flags + suffix length
  1980. end_type = (rule[0] << 16) + ((rule[1] & 0x7f) << 8) + (rule[2] & 0x7f);
  1981. if((tr->word_vowel_count == 0) && !(end_type & SUFX_P) && (tr->langopts.param[LOPT_SUFFIX] & 1))
  1982. failed = 1; // don't match a suffix rule if there are no previous syllables (needed for lang=tr).
  1983. else
  1984. {
  1985. match.end_type = end_type;
  1986. rule += 3;
  1987. }
  1988. }
  1989. break;
  1990. case RULE_NO_SUFFIX:
  1991. if(word_flags & FLAG_SUFFIX_REMOVED)
  1992. failed = 1; // a suffix has been removed
  1993. else
  1994. add_points = 1;
  1995. break;
  1996. default:
  1997. if(letter == rb)
  1998. {
  1999. if((letter & 0xc0) != 0x80)
  2000. {
  2001. // not for non-initial UTF-8 bytes
  2002. add_points = (21-distance_right);
  2003. }
  2004. }
  2005. else
  2006. failed = 1;
  2007. break;
  2008. }
  2009. break;
  2010. case RULE_PRE:
  2011. /* match backwards from start of current group */
  2012. distance_left += 2;
  2013. if(distance_left > 18)
  2014. distance_left = 19;
  2015. last_letter = *pre_ptr;
  2016. pre_ptr--;
  2017. letter_xbytes = utf8_in2(&letter_w,pre_ptr,1)-1;
  2018. letter = *pre_ptr;
  2019. switch(rb)
  2020. {
  2021. case RULE_LETTERGP:
  2022. letter_group = *rule++ - 'A';
  2023. if(IsLetter(tr, letter_w,letter_group))
  2024. {
  2025. lg_pts = 20;
  2026. if(letter_group==2)
  2027. lg_pts = 19; // fewer points for C, general consonant
  2028. add_points = (lg_pts-distance_left);
  2029. pre_ptr -= letter_xbytes;
  2030. }
  2031. else
  2032. failed = 1;
  2033. break;
  2034. case RULE_LETTERGP2: // match against a list of utf-8 strings
  2035. letter_group = *rule++ - 'A';
  2036. if((n_bytes = IsLetterGroup(tr, pre_ptr,letter_group,1)) >0)
  2037. {
  2038. add_points = (20-distance_right);
  2039. pre_ptr -= (n_bytes-1);
  2040. }
  2041. else
  2042. failed =1;
  2043. break;
  2044. case RULE_NOTVOWEL:
  2045. if(!IsLetter(tr, letter_w,0))
  2046. {
  2047. add_points = (20-distance_left);
  2048. pre_ptr -= letter_xbytes;
  2049. }
  2050. else
  2051. failed = 1;
  2052. break;
  2053. case RULE_DOUBLE:
  2054. if(letter == last_letter)
  2055. add_points = (21-distance_left);
  2056. else
  2057. failed = 1;
  2058. break;
  2059. case RULE_DIGIT:
  2060. if(IsDigit(letter_w))
  2061. {
  2062. add_points = (21-distance_left);
  2063. pre_ptr -= letter_xbytes;
  2064. }
  2065. else
  2066. failed = 1;
  2067. break;
  2068. case RULE_NONALPHA:
  2069. if(!iswalpha2(letter_w))
  2070. {
  2071. add_points = (21-distance_right);
  2072. pre_ptr -= letter_xbytes;
  2073. }
  2074. else
  2075. failed = 1;
  2076. break;
  2077. case RULE_DOLLAR:
  2078. command = *rule++;
  2079. if((command==DOLLAR_LIST) || ((command & 0xf0) == 0x20))
  2080. {
  2081. // $list or $p_alt
  2082. // make a copy of the word up to the current character
  2083. ix = *word - word_start + 1;
  2084. memcpy(word_buf, word_start-1, ix);
  2085. word_buf[ix] = ' ';
  2086. word_buf[ix+1] = 0;
  2087. LookupFlags(tr, &word_buf[1], &flags);
  2088. if((command==DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY))
  2089. add_points = 23;
  2090. else
  2091. if(flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
  2092. add_points = 23;
  2093. else
  2094. failed = 1;
  2095. }
  2096. break;
  2097. case RULE_SYLLABLE:
  2098. /* more than specified number of vowels to the left */
  2099. syllable_count = 1;
  2100. while(*rule == RULE_SYLLABLE)
  2101. {
  2102. rule++;
  2103. syllable_count++; /* number of syllables to match */
  2104. }
  2105. if(syllable_count <= tr->word_vowel_count)
  2106. add_points = (18+syllable_count-distance_left);
  2107. else
  2108. failed = 1;
  2109. break;
  2110. case RULE_STRESSED:
  2111. if(tr->word_stressed_count > 0)
  2112. add_points = 19;
  2113. else
  2114. failed = 1;
  2115. break;
  2116. case RULE_NOVOWELS:
  2117. {
  2118. char *p = pre_ptr - letter_xbytes - 1;
  2119. while(letter_w != RULE_SPACE)
  2120. {
  2121. if(IsLetter(tr, letter_w,LETTERGP_VOWEL2))
  2122. {
  2123. failed = 1;
  2124. break;
  2125. }
  2126. p -= utf8_in2(&letter_w,p,1);
  2127. }
  2128. if(!failed)
  2129. add_points = 3;
  2130. }
  2131. break;
  2132. case RULE_IFVERB:
  2133. if(tr->expect_verb)
  2134. add_points = 1;
  2135. else
  2136. failed = 1;
  2137. break;
  2138. case RULE_CAPITAL:
  2139. if(word_flags & FLAG_FIRST_UPPER)
  2140. add_points = 1;
  2141. else
  2142. failed = 1;
  2143. break;
  2144. case '.':
  2145. // dot in pre- section, match on any dot before this point in the word
  2146. for(p=pre_ptr; *p != ' '; p--)
  2147. {
  2148. if(*p == '.')
  2149. {
  2150. add_points = 50;
  2151. break;
  2152. }
  2153. }
  2154. if(*p == ' ')
  2155. failed = 1;
  2156. break;
  2157. case '-':
  2158. if((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN)))
  2159. {
  2160. add_points = (22-distance_right); // one point more than match against space
  2161. }
  2162. else
  2163. failed = 1;
  2164. break;
  2165. default:
  2166. if(letter == rb)
  2167. {
  2168. if(letter == RULE_SPACE)
  2169. add_points = 4;
  2170. else
  2171. {
  2172. if((letter & 0xc0) != 0x80)
  2173. {
  2174. // not for non-initial UTF-8 bytes
  2175. add_points = (21-distance_left);
  2176. }
  2177. }
  2178. }
  2179. else
  2180. failed = 1;
  2181. break;
  2182. }
  2183. break;
  2184. }
  2185. if(failed == 0)
  2186. match.points += add_points;
  2187. }
  2188. if((failed == 2) && (unpron_ignore == 0))
  2189. {
  2190. // do we also need to check for 'start of word' ?
  2191. if((check_atstart==0) || (pre_ptr[-1] == ' '))
  2192. {
  2193. if(check_atstart)
  2194. match.points += 4;
  2195. /* matched OK, is this better than the last best match ? */
  2196. if(match.points >= best.points)
  2197. {
  2198. memcpy(&best,&match,sizeof(match));
  2199. total_consumed = consumed;
  2200. }
  2201. if((option_phonemes & espeakPHONEMES_TRACE) && (match.points > 0) && ((word_flags & FLAG_NO_TRACE) == 0))
  2202. {
  2203. // show each rule that matches, and it's points score
  2204. int pts;
  2205. char decoded_phonemes[80];
  2206. pts = match.points;
  2207. if(group_length > 1)
  2208. pts += 35; // to account for an extra letter matching
  2209. DecodePhonemes(match.phonemes,decoded_phonemes);
  2210. fprintf(f_trans,"%3d\t%s [%s]\n",pts,DecodeRule(group_chars, group_length, rule_start, word_flags), decoded_phonemes);
  2211. }
  2212. }
  2213. }
  2214. /* skip phoneme string to reach start of next template */
  2215. while(*rule++ != 0);
  2216. }
  2217. /* advance input data pointer */
  2218. total_consumed += group_length;
  2219. if(total_consumed == 0)
  2220. total_consumed = 1; /* always advance over 1st letter */
  2221. *word += total_consumed;
  2222. if(best.points == 0)
  2223. best.phonemes = "";
  2224. memcpy(match_out,&best,sizeof(MatchRecord));
  2225. } /* end of MatchRule */
  2226. int TranslateRules(Translator *tr, char *p_start, char *phonemes, int ph_size, char *end_phonemes, int word_flags, unsigned int *dict_flags)
  2227. {//=====================================================================================================================================
  2228. /* Translate a word bounded by space characters
  2229. Append the result to 'phonemes' and any standard prefix/suffix in 'end_phonemes' */
  2230. unsigned char c, c2;
  2231. unsigned int c12;
  2232. int wc=0;
  2233. int wc_bytes;
  2234. char *p2; /* copy of p for use in double letter chain match */
  2235. int found;
  2236. int g; /* group chain number */
  2237. int g1; /* first group for this letter */
  2238. int n;
  2239. int letter;
  2240. int any_alpha=0;
  2241. int ix;
  2242. unsigned int digit_count=0;
  2243. char *p;
  2244. ALPHABET *alphabet;
  2245. int dict_flags0=0;
  2246. MatchRecord match1;
  2247. MatchRecord match2;
  2248. char ph_buf[40];
  2249. char word_copy[N_WORD_BYTES];
  2250. static const char str_pause[2] = {phonPAUSE_NOLINK,0};
  2251. if(tr->data_dictrules == NULL)
  2252. return(0);
  2253. if(dict_flags != NULL)
  2254. dict_flags0 = dict_flags[0];
  2255. for(ix=0; ix<(N_WORD_BYTES-1);)
  2256. {
  2257. c = p_start[ix];
  2258. word_copy[ix++] = c;
  2259. if(c == 0)
  2260. break;
  2261. }
  2262. word_copy[ix] = 0;
  2263. if((option_phonemes & espeakPHONEMES_TRACE) && ((word_flags & FLAG_NO_TRACE)==0))
  2264. {
  2265. char wordbuf[120];
  2266. unsigned int ix;
  2267. for(ix=0; ((c = p_start[ix]) != ' ') && (c != 0) && (ix < (sizeof(wordbuf)-1)); ix++)
  2268. {
  2269. wordbuf[ix] = c;
  2270. }
  2271. wordbuf[ix] = 0;
  2272. if(word_flags & FLAG_UNPRON_TEST)
  2273. fprintf(f_trans,"Unpronouncable? '%s'\n",wordbuf);
  2274. else
  2275. fprintf(f_trans,"Translate '%s'\n",wordbuf);
  2276. }
  2277. p = p_start;
  2278. tr->word_vowel_count = 0;
  2279. tr->word_stressed_count = 0;
  2280. if(end_phonemes != NULL)
  2281. end_phonemes[0] = 0;
  2282. while(((c = *p) != ' ') && (c != 0))
  2283. {
  2284. wc_bytes = utf8_in(&wc,p);
  2285. if(IsAlpha(wc))
  2286. any_alpha++;
  2287. n = tr->groups2_count[c];
  2288. if(IsDigit(wc) && ((tr->langopts.tone_numbers == 0) || !any_alpha))
  2289. {
  2290. // lookup the number in *_list not *_rules
  2291. char string[8];
  2292. char buf[40];
  2293. string[0] = '_';
  2294. memcpy(&string[1],p,wc_bytes);
  2295. string[1+wc_bytes] = 0;
  2296. Lookup(tr, string,buf);
  2297. if(++digit_count >= 2)
  2298. {
  2299. strcat(buf,str_pause);
  2300. digit_count=0;
  2301. }
  2302. AppendPhonemes(tr,phonemes,ph_size,buf);
  2303. p += wc_bytes;
  2304. continue;
  2305. }
  2306. else
  2307. {
  2308. digit_count = 0;
  2309. found = 0;
  2310. if(((ix = wc - tr->letter_bits_offset) >= 0) && (ix < 128))
  2311. {
  2312. if(tr->groups3[ix] != NULL)
  2313. {
  2314. MatchRule(tr, &p, p_start, wc_bytes, tr->groups3[ix], &match1, word_flags, dict_flags0);
  2315. found = 1;
  2316. }
  2317. }
  2318. if(!found && (n > 0))
  2319. {
  2320. /* there are some 2 byte chains for this initial letter */
  2321. c2 = p[1];
  2322. c12 = c + (c2 << 8); /* 2 characters */
  2323. g1 = tr->groups2_start[c];
  2324. for(g=g1; g < (g1+n); g++)
  2325. {
  2326. if(tr->groups2_name[g] == c12)
  2327. {
  2328. found = 1;
  2329. p2 = p;
  2330. MatchRule(tr, &p2, p_start, 2, tr->groups2[g], &match2, word_flags, dict_flags0);
  2331. if(match2.points > 0)
  2332. match2.points += 35; /* to acount for 2 letters matching */
  2333. /* now see whether single letter chain gives a better match ? */
  2334. MatchRule(tr, &p, p_start, 1, tr->groups1[c], &match1, word_flags, dict_flags0);
  2335. if(match2.points >= match1.points)
  2336. {
  2337. // use match from the 2-letter group
  2338. memcpy(&match1,&match2,sizeof(MatchRecord));
  2339. p = p2;
  2340. }
  2341. }
  2342. }
  2343. }
  2344. if(!found)
  2345. {
  2346. /* alphabetic, single letter chain */
  2347. if(tr->groups1[c] != NULL)
  2348. MatchRule(tr, &p, p_start, 1, tr->groups1[c], &match1, word_flags, dict_flags0);
  2349. else
  2350. {
  2351. // no group for this letter, use default group
  2352. MatchRule(tr, &p, p_start, 0, tr->groups1[0], &match1, word_flags, dict_flags0);
  2353. if((match1.points == 0) && ((option_sayas & 0x10) == 0))
  2354. {
  2355. n = utf8_in(&letter,p-1)-1;
  2356. if(tr->letter_bits_offset > 0)
  2357. {
  2358. // not a Latin alphabet, switch to the default Latin alphabet language
  2359. if((letter <= 0x241) && iswalpha2(letter))
  2360. {
  2361. sprintf(phonemes,"%c%s",phonSWITCH,tr->langopts.ascii_language);
  2362. return(0);
  2363. }
  2364. }
  2365. #ifdef deleted
  2366. // can't switch to a tone language, because the tone-phoneme numbers are not valid for the original language
  2367. if((letter >= 0x4e00) && (letter < 0xa000) && (tr->langopts.ideographs != 1))
  2368. {
  2369. // Chinese ideogram
  2370. sprintf(phonemes,"%czh",phonSWITCH);
  2371. return(0);
  2372. }
  2373. #endif
  2374. // is it a bracket ?
  2375. if(letter == 0xe000+'(')
  2376. {
  2377. if(pre_pause < tr->langopts.param2[LOPT_BRACKET_PAUSE])
  2378. pre_pause = tr->langopts.param2[LOPT_BRACKET_PAUSE]; // a bracket, aleady spoken by AnnouncePunctuation()
  2379. }
  2380. if(IsBracket(letter))
  2381. {
  2382. if(pre_pause < tr->langopts.param[LOPT_BRACKET_PAUSE])
  2383. pre_pause = tr->langopts.param[LOPT_BRACKET_PAUSE];
  2384. }
  2385. // no match, try removing the accent and re-translating the word
  2386. if((letter >= 0xc0) && (letter < N_REMOVE_ACCENT) && ((ix = remove_accent[letter-0xc0]) != 0))
  2387. {
  2388. // within range of the remove_accent table
  2389. if((p[-2] != ' ') || (p[n] != ' '))
  2390. {
  2391. // not the only letter in the word
  2392. p2 = p-1;
  2393. p[-1] = ix;
  2394. while((p[0] = p[n]) != ' ') p++;
  2395. while(n-- > 0) *p++ = ' '; // replacement character must be no longer than original
  2396. if(tr->langopts.param[LOPT_DIERESES] && (lookupwchar(diereses_list,letter) > 0))
  2397. {
  2398. // vowel with dieresis, replace and continue from this point
  2399. p = p2;
  2400. continue;
  2401. }
  2402. phonemes[0] = 0; // delete any phonemes which have been produced so far
  2403. p = p_start;
  2404. tr->word_vowel_count = 0;
  2405. tr->word_stressed_count = 0;
  2406. continue; // start again at the beginning of the word
  2407. }
  2408. }
  2409. if(((alphabet = AlphabetFromChar(letter)) != NULL) && (alphabet->offset != tr->letter_bits_offset))
  2410. {
  2411. if(tr->langopts.alt_alphabet == alphabet->offset)
  2412. {
  2413. sprintf(phonemes,"%c%s",phonSWITCH, WordToString2(tr->langopts.alt_alphabet_lang));
  2414. return(0);
  2415. }
  2416. if(alphabet->flags & AL_WORDS)
  2417. {
  2418. // switch to the nominated language for this alphabet
  2419. sprintf(phonemes,"%c%s",phonSWITCH, WordToString2(alphabet->language));
  2420. return(0);
  2421. }
  2422. }
  2423. }
  2424. }
  2425. if(match1.points == 0)
  2426. {
  2427. if((wc >= 0x300) && (wc <= 0x36f))
  2428. {
  2429. // combining accent inside a word, ignore
  2430. }
  2431. else if(IsAlpha(wc))
  2432. {
  2433. if((any_alpha > 1) || (p[wc_bytes-1] > ' '))
  2434. {
  2435. // an unrecognised character in a word, abort and then spell the word
  2436. phonemes[0] = 0;
  2437. if(dict_flags != NULL)
  2438. dict_flags[0] |= FLAG_SPELLWORD;
  2439. break;
  2440. }
  2441. }
  2442. else
  2443. {
  2444. LookupLetter(tr, wc, -1, ph_buf, 0);
  2445. if(ph_buf[0])
  2446. {
  2447. match1.phonemes = ph_buf;
  2448. match1.points = 1;
  2449. }
  2450. }
  2451. p += (wc_bytes-1);
  2452. }
  2453. else
  2454. {
  2455. tr->phonemes_repeat_count = 0;
  2456. }
  2457. }
  2458. }
  2459. if(match1.phonemes == NULL)
  2460. match1.phonemes = "";
  2461. if(match1.points > 0)
  2462. {
  2463. if(word_flags & FLAG_UNPRON_TEST)
  2464. return(match1.end_type | 1);
  2465. #ifdef deleted
  2466. // ?? allow $unpr while translating rules, not just on initial FLAG_UNPRON_TEST
  2467. if((match1.end_type & SUFX_UNPRON) && !(word_flags & FLAG_SUFFIX_REMOVED))
  2468. return(match1.end_type);
  2469. #endif
  2470. if((match1.phonemes[0] == phonSWITCH) && ((word_flags & FLAG_DONT_SWITCH_TRANSLATOR)==0))
  2471. {
  2472. // an instruction to switch language, return immediately so we can re-translate
  2473. strcpy(phonemes,match1.phonemes);
  2474. return(0);
  2475. }
  2476. if((option_phonemes & espeakPHONEMES_TRACE) && ((word_flags & FLAG_NO_TRACE)==0))
  2477. {
  2478. fprintf(f_trans,"\n");
  2479. }
  2480. match1.end_type &= ~SUFX_UNPRON;
  2481. if((match1.end_type != 0) && (end_phonemes != NULL))
  2482. {
  2483. /* a standard ending has been found, re-translate the word without it */
  2484. if((match1.end_type & SUFX_P) && (word_flags & FLAG_NO_PREFIX))
  2485. {
  2486. // ignore the match on a prefix
  2487. }
  2488. else
  2489. {
  2490. if((match1.end_type & SUFX_P) && ((match1.end_type & 0x7f) == 0))
  2491. {
  2492. // no prefix length specified
  2493. match1.end_type |= p - p_start;
  2494. }
  2495. strcpy(end_phonemes,match1.phonemes);
  2496. memcpy(p_start,word_copy,strlen(word_copy));
  2497. return(match1.end_type);
  2498. }
  2499. }
  2500. if(match1.del_fwd != NULL)
  2501. *match1.del_fwd = REPLACED_E;
  2502. AppendPhonemes(tr,phonemes,ph_size,match1.phonemes);
  2503. }
  2504. }
  2505. memcpy(p_start,word_copy,strlen(word_copy));
  2506. return(0);
  2507. } /* end of TranslateRules */
  2508. void ApplySpecialAttribute2(Translator *tr, char *phonemes, int dict_flags)
  2509. {//========================================================================
  2510. // apply after the translation is complete
  2511. int ix;
  2512. int len;
  2513. char *p;
  2514. len = strlen(phonemes);
  2515. if(tr->langopts.param[LOPT_ALT] & 2)
  2516. {
  2517. for(ix=0; ix<(len-1); ix++)
  2518. {
  2519. if(phonemes[ix] == phonSTRESS_P)
  2520. {
  2521. p = &phonemes[ix+1];
  2522. if((dict_flags & FLAG_ALT2_TRANS) != 0)
  2523. {
  2524. if(*p == PhonemeCode('E'))
  2525. *p = PhonemeCode('e');
  2526. if(*p == PhonemeCode('O'))
  2527. *p = PhonemeCode('o');
  2528. }
  2529. else
  2530. {
  2531. if(*p == PhonemeCode('e'))
  2532. *p = PhonemeCode('E');
  2533. if(*p == PhonemeCode('o'))
  2534. *p = PhonemeCode('O');
  2535. }
  2536. break;
  2537. }
  2538. }
  2539. }
  2540. } // end of ApplySpecialAttribute2
  2541. //=============================================================================================
  2542. // Look up a word in the pronunciation dictionary list
  2543. // - exceptions which override the usual pronunciation rules, or which give a word
  2544. // special properties, such as pronounce as unstressed
  2545. //=============================================================================================
  2546. int TransposeAlphabet(Translator *tr, char *text)
  2547. {//==============================================
  2548. // transpose cyrillic alphabet (for example) into ascii (single byte) character codes
  2549. // return: number of bytes, bit 6: 1=used compression
  2550. int c;
  2551. int c2;
  2552. int ix;
  2553. int offset;
  2554. int min;
  2555. int max;
  2556. const char *map;
  2557. char *p = text;
  2558. char *p2;
  2559. int all_alpha=1;
  2560. int bits;
  2561. int acc;
  2562. int pairs_start;
  2563. const short *pairs_list;
  2564. int bufix;
  2565. char buf[N_WORD_BYTES+1];
  2566. offset = tr->transpose_min - 1;
  2567. min = tr->transpose_min;
  2568. max = tr->transpose_max;
  2569. map = tr->transpose_map;
  2570. pairs_start = max - min + 2;
  2571. bufix = 0;
  2572. do {
  2573. p += utf8_in(&c,p);
  2574. if(c != 0)
  2575. {
  2576. if((c >= min) && (c <= max))
  2577. {
  2578. if(map == NULL)
  2579. {
  2580. buf[bufix++] = c - offset;
  2581. }
  2582. else
  2583. {
  2584. // get the code from the transpose map
  2585. if(map[c - min] > 0)
  2586. {
  2587. buf[bufix++] = map[c - min];
  2588. }
  2589. else
  2590. {
  2591. all_alpha=0;
  2592. break;
  2593. }
  2594. }
  2595. }
  2596. else
  2597. {
  2598. all_alpha=0;
  2599. break;
  2600. }
  2601. }
  2602. } while ((c != 0) && (bufix < N_WORD_BYTES));
  2603. buf[bufix] = 0;
  2604. if(all_alpha)
  2605. {
  2606. // compress to 6 bits per character
  2607. acc=0;
  2608. bits=0;
  2609. p = buf;
  2610. p2 = buf;
  2611. while((c = *p++) != 0)
  2612. {
  2613. if((pairs_list = tr->frequent_pairs) != NULL)
  2614. {
  2615. c2 = c + (*p << 8);
  2616. for(ix=0; c2 >= pairs_list[ix]; ix++)
  2617. {
  2618. if(c2 == pairs_list[ix])
  2619. {
  2620. // found an encoding for a 2-character pair
  2621. c = ix + pairs_start; // 2-character codes start after the single letter codes
  2622. p++;
  2623. break;
  2624. }
  2625. }
  2626. }
  2627. acc = (acc << 6) + (c & 0x3f);
  2628. bits += 6;
  2629. if(bits >= 8)
  2630. {
  2631. bits -= 8;
  2632. *p2++ = (acc >> bits);
  2633. }
  2634. }
  2635. if(bits > 0)
  2636. {
  2637. *p2++ = (acc << (8-bits));
  2638. }
  2639. *p2 = 0;
  2640. ix = p2 - buf;
  2641. memcpy(text, buf, ix);
  2642. return(ix | 0x40); // bit 6 indicates compressed characters
  2643. }
  2644. else
  2645. {
  2646. return(strlen(text));
  2647. }
  2648. } // end of TransposeAlphabet
  2649. static const char *LookupDict2(Translator *tr, const char *word, const char *word2,
  2650. char *phonetic, unsigned int *flags, int end_flags, WORD_TAB *wtab)
  2651. //=====================================================================================
  2652. /* Find an entry in the word_dict file for a specified word.
  2653. Returns NULL if no match, else returns 'word_end'
  2654. word zero terminated word to match
  2655. word2 pointer to next word(s) in the input text (terminated by space)
  2656. flags: returns dictionary flags which are associated with a matched word
  2657. end_flags: indicates whether this is a retranslation after removing a suffix
  2658. */
  2659. {
  2660. char *p;
  2661. char *next;
  2662. int hash;
  2663. int phoneme_len;
  2664. int wlen;
  2665. unsigned char flag;
  2666. unsigned int dictionary_flags;
  2667. unsigned int dictionary_flags2;
  2668. int condition_failed=0;
  2669. int n_chars;
  2670. int no_phonemes;
  2671. int skipwords;
  2672. int ix;
  2673. int c;
  2674. const char *word_end;
  2675. const char *word1;
  2676. int wflags = 0;
  2677. int lookup_symbol;
  2678. char word_buf[N_WORD_BYTES+1];
  2679. char dict_flags_buf[80];
  2680. if(wtab != NULL)
  2681. {
  2682. wflags = wtab->flags;
  2683. }
  2684. lookup_symbol = flags[1] & FLAG_LOOKUP_SYMBOL;
  2685. word1 = word;
  2686. if(tr->transpose_min > 0)
  2687. {
  2688. strncpy0(word_buf,word, N_WORD_BYTES);
  2689. wlen = TransposeAlphabet(tr, word_buf); // bit 6 indicates compressed characters
  2690. word = word_buf;
  2691. }
  2692. else
  2693. {
  2694. wlen = strlen(word);
  2695. }
  2696. hash = HashDictionary(word);
  2697. p = tr->dict_hashtab[hash];
  2698. if(p == NULL)
  2699. {
  2700. if(flags != NULL)
  2701. *flags = 0;
  2702. return(0);
  2703. }
  2704. // Find the first entry in the list for this hash value which matches.
  2705. // This corresponds to the last matching entry in the *_list file.
  2706. while(*p != 0)
  2707. {
  2708. next = p + p[0];
  2709. if(((p[1] & 0x7f) != wlen) || (memcmp(word,&p[2],wlen & 0x3f) != 0))
  2710. {
  2711. // bit 6 of wlen indicates whether the word has been compressed; so we need to match on this also.
  2712. p = next;
  2713. continue;
  2714. }
  2715. /* found matching entry. Decode the phonetic string */
  2716. word_end = word2;
  2717. dictionary_flags = 0;
  2718. dictionary_flags2 = 0;
  2719. no_phonemes = p[1] & 0x80;
  2720. p += ((p[1] & 0x3f) + 2);
  2721. if(no_phonemes)
  2722. {
  2723. phonetic[0] = 0;
  2724. phoneme_len = 0;
  2725. }
  2726. else
  2727. {
  2728. strcpy(phonetic,p);
  2729. phoneme_len = strlen(p);
  2730. p += (phoneme_len + 1);
  2731. }
  2732. while(p < next)
  2733. {
  2734. // examine the flags which follow the phoneme string
  2735. flag = *p++;
  2736. if(flag >= 100)
  2737. {
  2738. // conditional rule
  2739. if(flag >= 132)
  2740. {
  2741. // fail if this condition is set
  2742. if((tr->dict_condition & (1 << (flag-132))) != 0)
  2743. condition_failed = 1;
  2744. }
  2745. else
  2746. {
  2747. // allow only if this condition is set
  2748. if((tr->dict_condition & (1 << (flag-100))) == 0)
  2749. condition_failed = 1;
  2750. }
  2751. }
  2752. else if(flag > 80)
  2753. {
  2754. // flags 81 to 90 match more than one word
  2755. // This comes after the other flags
  2756. n_chars = next - p;
  2757. skipwords = flag - 80;
  2758. // don't use the contraction if any of the words are emphasized
  2759. // or has an embedded command, such as MARK
  2760. if(wtab != NULL)
  2761. {
  2762. for(ix=0; ix <= skipwords; ix++)
  2763. {
  2764. if(wtab[ix].flags & FLAG_EMPHASIZED2)
  2765. // if(((wflags2 = wtab[ix].flags) & FLAG_EMPHASIZED2) || ((ix > 0) && (wflags2 & FLAG_EMBEDDED)))
  2766. {
  2767. condition_failed = 1;
  2768. }
  2769. }
  2770. }
  2771. if(memcmp(word2,p,n_chars) != 0)
  2772. condition_failed = 1;
  2773. if(condition_failed)
  2774. {
  2775. p = next;
  2776. break;
  2777. }
  2778. dictionary_flags |= FLAG_SKIPWORDS;
  2779. dictionary_skipwords = skipwords;
  2780. p = next;
  2781. word_end = word2 + n_chars;
  2782. }
  2783. else if(flag > 64)
  2784. {
  2785. // stressed syllable information, put in bits 0-3
  2786. dictionary_flags = (dictionary_flags & ~0xf) | (flag & 0xf);
  2787. if((flag & 0xc) == 0xc)
  2788. dictionary_flags |= FLAG_STRESS_END;
  2789. }
  2790. else if(flag >= 32)
  2791. {
  2792. dictionary_flags2 |= (1L << (flag-32));
  2793. }
  2794. else
  2795. {
  2796. dictionary_flags |= (1L << flag);
  2797. }
  2798. }
  2799. if(condition_failed)
  2800. {
  2801. condition_failed=0;
  2802. continue;
  2803. }
  2804. if((end_flags & FLAG_SUFX)==0)
  2805. {
  2806. // no suffix has been removed
  2807. if(dictionary_flags2 & FLAG_STEM)
  2808. continue; // this word must have a suffix
  2809. }
  2810. if((end_flags & SUFX_P) && (dictionary_flags2 & (FLAG_ONLY | FLAG_ONLY_S)))
  2811. continue; // $only or $onlys, don't match if a prefix has been removed
  2812. if(end_flags & FLAG_SUFX)
  2813. {
  2814. // a suffix was removed from the word
  2815. if(dictionary_flags2 & FLAG_ONLY)
  2816. continue; // no match if any suffix
  2817. if((dictionary_flags2 & FLAG_ONLY_S) && ((end_flags & FLAG_SUFX_S)==0))
  2818. {
  2819. // only a 's' suffix allowed, but the suffix wasn't 's'
  2820. continue;
  2821. }
  2822. }
  2823. if(dictionary_flags2 & FLAG_HYPHENATED)
  2824. {
  2825. if(!(wflags & FLAG_HYPHEN_AFTER))
  2826. {
  2827. continue;
  2828. }
  2829. }
  2830. if(dictionary_flags2 & FLAG_CAPITAL)
  2831. {
  2832. if(!(wflags & FLAG_FIRST_UPPER))
  2833. {
  2834. continue;
  2835. }
  2836. }
  2837. if(dictionary_flags2 & FLAG_ALLCAPS)
  2838. {
  2839. if(!(wflags & FLAG_ALL_UPPER))
  2840. {
  2841. continue;
  2842. }
  2843. }
  2844. if(dictionary_flags & FLAG_NEEDS_DOT)
  2845. {
  2846. if(!(wflags & FLAG_HAS_DOT))
  2847. continue;
  2848. }
  2849. if((dictionary_flags2 & FLAG_ATEND) && (word_end < translator->clause_end) && (lookup_symbol==0))
  2850. {
  2851. // only use this pronunciation if it's the last word of the clause, or called from Lookup()
  2852. continue;
  2853. }
  2854. if((dictionary_flags2 & FLAG_ATSTART) && !(wtab->flags & FLAG_FIRST_WORD))
  2855. {
  2856. // only use this pronunciation if it's the first word of a clause
  2857. continue;
  2858. }
  2859. if((dictionary_flags2 & FLAG_SENTENCE) && !(translator->clause_terminator & CLAUSE_BIT_SENTENCE))
  2860. {
  2861. // only if this clause is a sentence , i.e. terminator is {. ? !} not {, : :}
  2862. continue;
  2863. }
  2864. if(dictionary_flags2 & FLAG_VERB)
  2865. {
  2866. // this is a verb-form pronunciation
  2867. if(tr->expect_verb || (tr->expect_verb_s && (end_flags & FLAG_SUFX_S)))
  2868. {
  2869. // OK, we are expecting a verb
  2870. if((tr->translator_name == L('e','n')) && (tr->prev_dict_flags[0] & FLAG_ALT7_TRANS) && (end_flags & FLAG_SUFX_S))
  2871. {
  2872. // lang=en, don't use verb form after 'to' if the word has 's' suffix
  2873. continue;
  2874. }
  2875. }
  2876. else
  2877. {
  2878. /* don't use the 'verb' pronunciation unless we are
  2879. expecting a verb */
  2880. continue;
  2881. }
  2882. }
  2883. if(dictionary_flags2 & FLAG_PAST)
  2884. {
  2885. if(!tr->expect_past)
  2886. {
  2887. /* don't use the 'past' pronunciation unless we are
  2888. expecting past tense */
  2889. continue;
  2890. }
  2891. }
  2892. if(dictionary_flags2 & FLAG_NOUN)
  2893. {
  2894. if((!tr->expect_noun) || (end_flags & SUFX_V))
  2895. {
  2896. /* don't use the 'noun' pronunciation unless we are
  2897. expecting a noun */
  2898. continue;
  2899. }
  2900. }
  2901. if(dictionary_flags2 & FLAG_NATIVE)
  2902. {
  2903. if(tr != translator)
  2904. continue; // don't use if we've switched translators
  2905. }
  2906. if(dictionary_flags & FLAG_ALT2_TRANS)
  2907. {
  2908. // language specific
  2909. if((tr->translator_name == L('h','u')) && !(tr->prev_dict_flags[0] & FLAG_ALT_TRANS))
  2910. continue;
  2911. }
  2912. if(flags != NULL)
  2913. {
  2914. flags[0] = dictionary_flags | FLAG_FOUND_ATTRIBUTES;
  2915. flags[1] = dictionary_flags2;
  2916. }
  2917. if(phoneme_len == 0)
  2918. {
  2919. if(option_phonemes & espeakPHONEMES_TRACE)
  2920. {
  2921. print_dictionary_flags(flags, dict_flags_buf, sizeof(dict_flags_buf));
  2922. fprintf(f_trans,"Flags: %s %s\n", word1, dict_flags_buf);
  2923. }
  2924. return(0); // no phoneme translation found here, only flags. So use rules
  2925. }
  2926. if(flags != NULL)
  2927. flags[0] |= FLAG_FOUND; // this flag indicates word was found in dictionary
  2928. if(option_phonemes & espeakPHONEMES_TRACE)
  2929. {
  2930. char ph_decoded[N_WORD_PHONEMES];
  2931. int textmode;
  2932. DecodePhonemes(phonetic,ph_decoded);
  2933. if((dictionary_flags & FLAG_TEXTMODE) == 0)
  2934. textmode = 0;
  2935. else
  2936. textmode = 1;
  2937. if(textmode == translator->langopts.textmode)
  2938. {
  2939. // only show this line if the word translates to phonemes, not replacement text
  2940. if((dictionary_flags & FLAG_SKIPWORDS) && (wtab != NULL))
  2941. {
  2942. // matched more than one word
  2943. // (check for wtab prevents showing RULE_SPELLING byte when speaking individual letters)
  2944. memcpy(word_buf,word2,word_end-word2);
  2945. word_buf[word_end-word2-1] = 0;
  2946. fprintf(f_trans,"Found: '%s %s\n",word1,word_buf);
  2947. }
  2948. else
  2949. {
  2950. fprintf(f_trans,"Found: '%s",word1);
  2951. }
  2952. print_dictionary_flags(flags, dict_flags_buf, sizeof(dict_flags_buf));
  2953. fprintf(f_trans,"' [%s] %s\n", ph_decoded,dict_flags_buf);
  2954. }
  2955. }
  2956. ix = utf8_in(&c, word);
  2957. if((word[ix] == 0) && !IsAlpha(c))
  2958. {
  2959. flags[0] |= FLAG_MAX3;
  2960. }
  2961. return(word_end);
  2962. }
  2963. return(0);
  2964. } // end of LookupDict2
  2965. int LookupDictList(Translator *tr, char **wordptr, char *ph_out, unsigned int *flags, int end_flags, WORD_TAB *wtab)
  2966. //==================================================================================================================
  2967. /* Lookup a specified word in the word dictionary.
  2968. Returns phonetic data in 'phonetic' and bits in 'flags'
  2969. end_flags: indicates if a suffix has been removed
  2970. */
  2971. {
  2972. int length;
  2973. const char *found;
  2974. const char *word1;
  2975. const char *word2;
  2976. unsigned char c;
  2977. int nbytes;
  2978. int len;
  2979. char word[N_WORD_BYTES];
  2980. static char word_replacement[N_WORD_BYTES];
  2981. length = 0;
  2982. word2 = word1 = *wordptr;
  2983. while((word2[nbytes = utf8_nbytes(word2)]==' ') && (word2[nbytes+1]=='.'))
  2984. {
  2985. // look for an abbreviation of the form a.b.c
  2986. // try removing the spaces between the dots and looking for a match
  2987. memcpy(&word[length],word2,nbytes);
  2988. length += nbytes;
  2989. word[length++] = '.';
  2990. word2 += nbytes+3;
  2991. }
  2992. if(length > 0)
  2993. {
  2994. // found an abbreviation containing dots
  2995. nbytes = 0;
  2996. while(((c = word2[nbytes]) != 0) && (c != ' '))
  2997. {
  2998. nbytes++;
  2999. }
  3000. memcpy(&word[length],word2,nbytes);
  3001. word[length+nbytes] = 0;
  3002. found = LookupDict2(tr, word, word2, ph_out, flags, end_flags, wtab);
  3003. if(found)
  3004. {
  3005. // set the skip words flag
  3006. flags[0] |= FLAG_SKIPWORDS;
  3007. dictionary_skipwords = length;
  3008. return(1);
  3009. }
  3010. }
  3011. for(length=0; length<(N_WORD_BYTES-1); length++)
  3012. {
  3013. if(((c = *word1++)==0) || (c == ' '))
  3014. break;
  3015. if((c=='.') && (length > 0) && (IsDigit09(word[length-1])))
  3016. break; // needed for lang=hu, eg. "december 2.-ig"
  3017. word[length] = c;
  3018. }
  3019. word[length] = 0;
  3020. found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);
  3021. if(flags[0] & FLAG_MAX3)
  3022. {
  3023. if(strcmp(ph_out, tr->phonemes_repeat) == 0)
  3024. {
  3025. tr->phonemes_repeat_count++;
  3026. if(tr->phonemes_repeat_count > 3)
  3027. {
  3028. ph_out[0] = 0;
  3029. }
  3030. }
  3031. else
  3032. {
  3033. strncpy0(tr->phonemes_repeat, ph_out, sizeof(tr->phonemes_repeat));
  3034. tr->phonemes_repeat_count = 1;
  3035. }
  3036. }
  3037. else
  3038. {
  3039. tr->phonemes_repeat_count = 0;
  3040. }
  3041. if((found == 0) && (flags[1] & FLAG_ACCENT))
  3042. {
  3043. int letter;
  3044. word2 = word;
  3045. if(*word2 == '_') word2++;
  3046. len = utf8_in(&letter, word2);
  3047. LookupAccentedLetter(tr,letter, ph_out);
  3048. found = word2 + len;
  3049. }
  3050. if(found == 0)
  3051. {
  3052. ph_out[0] = 0;
  3053. // try modifications to find a recognised word
  3054. if((end_flags & FLAG_SUFX_E_ADDED) && (word[length-1] == 'e'))
  3055. {
  3056. // try removing an 'e' which has been added by RemoveEnding
  3057. word[length-1] = 0;
  3058. found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);
  3059. }
  3060. else if((end_flags & SUFX_D) && (word[length-1] == word[length-2]))
  3061. {
  3062. // try removing a double letter
  3063. word[length-1] = 0;
  3064. found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);
  3065. }
  3066. }
  3067. if(found)
  3068. {
  3069. // if textmode is the default, then words which have phonemes are marked.
  3070. if(tr->langopts.textmode)
  3071. *flags ^= FLAG_TEXTMODE;
  3072. if(*flags & FLAG_TEXTMODE)
  3073. {
  3074. // the word translates to replacement text, not to phonemes
  3075. if(end_flags & FLAG_ALLOW_TEXTMODE)
  3076. {
  3077. // only use replacement text if this is the original word, not if a prefix or suffix has been removed
  3078. word_replacement[0] = 0;
  3079. word_replacement[1] = ' ';
  3080. sprintf(&word_replacement[2],"%s ",ph_out); // replacement word, preceded by zerochar and space
  3081. word1 = *wordptr;
  3082. *wordptr = &word_replacement[2];
  3083. if(option_phonemes & espeakPHONEMES_TRACE)
  3084. {
  3085. len = found - word1;
  3086. memcpy(word,word1,len); // include multiple matching words
  3087. word[len] = 0;
  3088. fprintf(f_trans,"Replace: %s %s\n",word,*wordptr);
  3089. }
  3090. }
  3091. else
  3092. {
  3093. // flags[0] &= ~FLAG_SKIPWORDS; // check lang=hu január 21.-ig (error: suffix repeated ??)
  3094. }
  3095. ph_out[0] = 0;
  3096. return(0);
  3097. }
  3098. return(1);
  3099. }
  3100. ph_out[0] = 0;
  3101. return(0);
  3102. } // end of LookupDictList
  3103. extern char word_phonemes[N_WORD_PHONEMES]; // a word translated into phoneme codes
  3104. int Lookup(Translator *tr, const char *word, char *ph_out)
  3105. {//=========================================================
  3106. // Look up in *_list, returns dictionary flags[0] and phonemes
  3107. int flags0;
  3108. unsigned int flags[2];
  3109. int say_as;
  3110. char *word1 = (char *)word;
  3111. char text[80];
  3112. flags[0] = 0;
  3113. flags[1] = FLAG_LOOKUP_SYMBOL;
  3114. if((flags0 = LookupDictList(tr, &word1, ph_out, flags, FLAG_ALLOW_TEXTMODE, NULL)) != 0)
  3115. {
  3116. flags0 = flags[0];
  3117. }
  3118. if(flags[0] & FLAG_TEXTMODE)
  3119. {
  3120. say_as = option_sayas;
  3121. option_sayas = 0; // don't speak replacement word as letter names
  3122. text[0] = 0;
  3123. strncpy0(&text[1], word1, sizeof(text));
  3124. flags0 = TranslateWord(tr, &text[1], 0, NULL, NULL);
  3125. strcpy(ph_out, word_phonemes);
  3126. option_sayas = say_as;
  3127. }
  3128. return(flags0);
  3129. }
  3130. int LookupFlags(Translator *tr, const char *word, unsigned int **flags_out)
  3131. {//===========================================================================
  3132. char buf[100];
  3133. static unsigned int flags[2];
  3134. char *word1 = (char *)word;
  3135. flags[0] = flags[1] = 0;
  3136. LookupDictList(tr, &word1, buf, flags, 0, NULL);
  3137. *flags_out = flags;
  3138. return(flags[0]);
  3139. }
  3140. int RemoveEnding(Translator *tr, char *word, int end_type, char *word_copy)
  3141. {//========================================================================
  3142. /* Removes a standard suffix from a word, once it has been indicated by the dictionary rules.
  3143. end_type: bits 0-6 number of letters
  3144. bits 8-14 suffix flags
  3145. word_copy: make a copy of the original word
  3146. This routine is language specific. In English it deals with reversing y->i and e-dropping
  3147. that were done when the suffix was added to the original word.
  3148. */
  3149. int i;
  3150. char *word_end;
  3151. int len_ending;
  3152. int end_flags;
  3153. const char *p;
  3154. int len;
  3155. char ending[50];
  3156. // these lists are language specific, but are only relevent if the 'e' suffix flag is used
  3157. static const char *add_e_exceptions[] = {
  3158. "ion", NULL
  3159. };
  3160. static const char *add_e_additions[] = {
  3161. // "c", "rs", "ir", "ur", "ath", "ns", "lu", NULL };
  3162. "c", "rs", "ir", "ur", "ath", "ns", "u", NULL
  3163. };
  3164. for(word_end = word; *word_end != ' '; word_end++)
  3165. {
  3166. /* replace discarded 'e's */
  3167. if(*word_end == REPLACED_E)
  3168. *word_end = 'e';
  3169. }
  3170. i = word_end - word;
  3171. if(word_copy != NULL)
  3172. {
  3173. memcpy(word_copy,word,i);
  3174. word_copy[i] = 0;
  3175. }
  3176. // look for multibyte characters to increase the number of bytes to remove
  3177. for(len_ending = i = (end_type & 0x3f); i>0 ; i--) // num.of characters of the suffix
  3178. {
  3179. word_end--;
  3180. while((*word_end & 0xc0) == 0x80)
  3181. {
  3182. word_end--; // for multibyte characters
  3183. len_ending++;
  3184. }
  3185. }
  3186. // remove bytes from the end of the word and replace them by spaces
  3187. for(i=0; (i<len_ending) && (i < (int)sizeof(ending)-1); i++)
  3188. {
  3189. ending[i] = word_end[i];
  3190. word_end[i] = ' ';
  3191. }
  3192. ending[i] = 0;
  3193. word_end--; /* now pointing at last character of stem */
  3194. end_flags = (end_type & 0xfff0) | FLAG_SUFX;
  3195. /* add an 'e' to the stem if appropriate,
  3196. if stem ends in vowel+consonant
  3197. or stem ends in 'c' (add 'e' to soften it) */
  3198. if(end_type & SUFX_I)
  3199. {
  3200. if(word_end[0] == 'i')
  3201. word_end[0] = 'y';
  3202. }
  3203. if(end_type & SUFX_E)
  3204. {
  3205. if(tr->translator_name == L('n','l'))
  3206. {
  3207. if(((word_end[0] & 0x80) == 0) && ((word_end[-1] & 0x80) == 0) && IsVowel(tr, word_end[-1]) && IsLetter(tr, word_end[0], LETTERGP_C) && !IsVowel(tr, word_end[-2]))
  3208. {
  3209. //double the vowel before the (ascii) final consonant
  3210. word_end[1] = word_end[0];
  3211. word_end[0] = word_end[-1];
  3212. word_end[2] = ' ';
  3213. }
  3214. }
  3215. else if(tr->translator_name == L('e','n'))
  3216. {
  3217. // add 'e' to end of stem
  3218. if(IsLetter(tr, word_end[-1],LETTERGP_VOWEL2) && IsLetter(tr, word_end[0],1))
  3219. {
  3220. // vowel(incl.'y') + hard.consonant
  3221. for(i=0; (p = add_e_exceptions[i]) != NULL; i++)
  3222. {
  3223. len = strlen(p);
  3224. if(memcmp(p,&word_end[1-len],len)==0)
  3225. {
  3226. break;
  3227. }
  3228. }
  3229. if(p == NULL)
  3230. end_flags |= FLAG_SUFX_E_ADDED; // no exception found
  3231. }
  3232. else
  3233. {
  3234. for(i=0; (p = add_e_additions[i]) != NULL; i++)
  3235. {
  3236. len = strlen(p);
  3237. if(memcmp(p,&word_end[1-len],len)==0)
  3238. {
  3239. end_flags |= FLAG_SUFX_E_ADDED;
  3240. break;
  3241. }
  3242. }
  3243. }
  3244. }
  3245. else if(tr->langopts.suffix_add_e != 0)
  3246. {
  3247. end_flags |= FLAG_SUFX_E_ADDED;
  3248. }
  3249. if(end_flags & FLAG_SUFX_E_ADDED)
  3250. {
  3251. utf8_out(tr->langopts.suffix_add_e, &word_end[1]);
  3252. if(option_phonemes & espeakPHONEMES_TRACE)
  3253. {
  3254. fprintf(f_trans,"add e\n");
  3255. }
  3256. }
  3257. }
  3258. if((end_type & SUFX_V) && (tr->expect_verb==0))
  3259. tr->expect_verb = 1; // this suffix indicates the verb pronunciation
  3260. if((strcmp(ending,"s")==0) || (strcmp(ending,"es")==0))
  3261. end_flags |= FLAG_SUFX_S;
  3262. // if(strcmp(ending,"'s")==0)
  3263. if(ending[0] == '\'')
  3264. end_flags &= ~FLAG_SUFX; // don't consider 's as an added suffix
  3265. return(end_flags);
  3266. } /* end of RemoveEnding */