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

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