eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dictionary.cpp 82KB

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