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

dictionary.cpp 88KB

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