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

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