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.c 84KB

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