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

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