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

dictionary.cpp 88KB

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