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

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