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

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