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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186
  1. /***************************************************************************
  2. * Copyright (C) 2005 to 2007 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. #define LOG_TRANSLATE
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <wctype.h>
  26. #include <wchar.h>
  27. #include "speak_lib.h"
  28. #include "speech.h"
  29. #include "phoneme.h"
  30. #include "synthesize.h"
  31. #include "translate.h"
  32. int dictionary_skipwords;
  33. char dictionary_name[40];
  34. extern MNEM_TAB mnem_flags[];
  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
  40. static unsigned char remove_accent[] = {
  41. 'a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i', // 0c0
  42. 'd','n','o','o','o','o','o', 0, 'o','u','u','u','u','y','t','s', // 0d0
  43. 'a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i', // 0e0
  44. 'd','n','o','o','o','o','o', 0 ,'o','u','u','u','u','y','t','y', // 0f0
  45. 'a','a','a','a','a','a','c','c','c','c','c','c','c','c','d','d', // 100
  46. 'd','d','e','e','e','e','e','e','e','e','e','e','g','g','g','g', // 110
  47. 'g','g','g','g','h','h','h','h','i','i','i','i','i','i','i','i', // 120
  48. 'i','i','i','i','j','j','k','k','k','l','l','l','l','l','l','l', // 130
  49. 'l','l','l','n','n','n','n','n','n','n','n','n','o','o','o','o', // 140
  50. 'o','o','o','o','r','r','r','r','r','r','s','s','s','s','s','s', // 150
  51. 's','s','t','t','t','t','t','t','u','u','u','u','u','u','u','u', // 160
  52. 'u','u','u','u','w','w','y','y','y','z','z','z','z','z','z','s', // 170
  53. 'b','b','b','b', 0, 0, 'o','c','c','d','d','d','d','d','e','e', // 180
  54. 'e','f','f','g','g','h','i','i','k','k','l','l','m','n','n','o', // 190
  55. 'o','o','o','o','p','p','y', 0, 0, 's','s','t','t','t','t','u', // 1a0
  56. 'u','u','v','y','y','z','z','z','z','z','z','z', 0, 0, 0, 'w', // 1b0
  57. 't','t','t','k','d','d','d','l','l','l','n','n','n','a','a','i', // 1c0
  58. 'i','o','o','u','u','u','u','u','u','u','u','u','u','e','a','a', // 1d0
  59. 'a','a','a','a','g','g','g','g','k','k','o','o','o','o','z','z', // 1e0
  60. 'j','d','d','d','g','g','w','w','n','n','a','a','a','a','o','o', // 1f0
  61. 'a','a','a','a','e','e','e','e','i','i','i','i','o','o','o','o', // 200
  62. 'r','r','r','r','u','u','u','u','s','s','t','t','y','y','h','h', // 210
  63. 'n','d','o','o','z','z','a','a','e','e','o','o','o','o','o','o', // 220
  64. 'o','o','y','y','l','n','t','j','d','q','a','c','c','l','t','s', // 230
  65. 'z', 0 };
  66. void strncpy0(char *to,const char *from, int size)
  67. {//===============================================
  68. // strcpy with limit, ensures a zero terminator
  69. strncpy(to,from,size);
  70. to[size-1] = 0;
  71. }
  72. #ifdef ARCH_BIG
  73. int reverse_word_bytes(int word)
  74. {//=============================
  75. // reverse the order of bytes from little-endian to big-endian
  76. int ix;
  77. int word2 = 0;
  78. for(ix=24; ix>=0; ix -= 8)
  79. {
  80. word2 |= (word >> ix) & 0xff;
  81. word2 = word2 << 8;
  82. }
  83. return(word2);
  84. }
  85. #endif
  86. int LookupMnem(MNEM_TAB *table, char *string)
  87. {//==========================================
  88. while(table->mnem != NULL)
  89. {
  90. if(strcmp(string,table->mnem)==0)
  91. return(table->value);
  92. table++;
  93. }
  94. return(table->value);
  95. }
  96. const char *LookupMnem(MNEM_TAB *table, int value)
  97. {//===============================================
  98. while(table->mnem != NULL)
  99. {
  100. if(table->value == value)
  101. return(table->mnem);
  102. table++;
  103. }
  104. return("");
  105. }
  106. //=============================================================================================
  107. // Read pronunciation rules and pronunciation lookup dictionary
  108. //
  109. //=============================================================================================
  110. int Translator::LoadDictionary(const char *name, int no_error)
  111. {//===========================================================
  112. int hash;
  113. char *p;
  114. int *pw;
  115. int length;
  116. FILE *f;
  117. unsigned int size;
  118. char fname[sizeof(path_home)+20];
  119. strcpy(dictionary_name,name); // currently loaded dictionary name
  120. if(no_error) // don't load dictionary, just set the dictionary_name
  121. return(1);
  122. // Load a pronunciation data file into memory
  123. // bytes 0-3: offset to rules data
  124. // bytes 4-7: number of hash table entries
  125. sprintf(fname,"%s%c%s_dict",path_home,PATHSEP,name);
  126. size = GetFileLength(fname);
  127. f = fopen(fname,"rb");
  128. if((f == NULL) || (size == 0))
  129. {
  130. if(no_error == 0)
  131. {
  132. fprintf(stderr,"Can't read dictionary file: '%s'\n",fname);
  133. }
  134. return(1);
  135. }
  136. if(data_dictlist != NULL)
  137. Free(data_dictlist);
  138. data_dictlist = Alloc(size);
  139. fread(data_dictlist,size,1,f);
  140. fclose(f);
  141. pw = (int *)data_dictlist;
  142. length = pw[1];
  143. if(size <= (N_HASH_DICT + sizeof(int)*2))
  144. {
  145. fprintf(stderr,"Empty _dict file: '%s\n",fname);
  146. return(2);
  147. }
  148. if((pw[0] != N_HASH_DICT) || (length <= 0) || (length > 0x8000000))
  149. {
  150. fprintf(stderr,"Bad data: '%s'\n",fname);
  151. return(2);
  152. }
  153. data_dictrules = &data_dictlist[length];
  154. // set up indices into data_dictrules
  155. InitGroups();
  156. if(groups1[0] == NULL)
  157. {
  158. fprintf(stderr,"Error in %s_rules, no default rule group\n",name);
  159. }
  160. // set up hash table for data_dictlist
  161. p = &data_dictlist[8];
  162. for(hash=0; hash<N_HASH_DICT; hash++)
  163. {
  164. dict_hashtab[hash] = p;
  165. while((length = *p) != 0)
  166. {
  167. p += length;
  168. }
  169. p++; // skip over the zero which terminates the list for this hash value
  170. }
  171. return(0);
  172. } // end of LoadDictionary
  173. void Translator::InitGroups(void)
  174. {//==============================
  175. /* Called after dictionary 1 is loaded, to set up table of entry points for translation rule chains
  176. for single-letters and two-letter combinations
  177. */
  178. int ix;
  179. char *p;
  180. char *p_name;
  181. unsigned int *pw;
  182. unsigned char c, c2;
  183. int len;
  184. n_groups2 = 0;
  185. for(ix=0; ix<256; ix++)
  186. {
  187. groups1[ix]=NULL;
  188. groups2_count[ix]=0;
  189. groups2_start[ix]=255; // indicates "not set"
  190. }
  191. memset(letterGroups,0,sizeof(letterGroups));
  192. p = data_dictrules;
  193. while(*p != 0)
  194. {
  195. if(*p != RULE_GROUP_START)
  196. {
  197. fprintf(stderr,"Bad rules data in '%s_dict' at 0x%x\n",dictionary_name,(unsigned int)(p-data_dictrules));
  198. break;
  199. }
  200. p++;
  201. if(p[0] == RULE_REPLACEMENTS)
  202. {
  203. pw = (unsigned int *)(((long)p+4) & ~3); // advance to next word boundary
  204. langopts.replace_chars = pw;
  205. while(pw[0] != 0)
  206. {
  207. pw += 2; // find the end of the replacement list, each entry is 2 words.
  208. }
  209. p = (char *)(pw+1);
  210. #ifdef ARCH_BIG
  211. pw = (unsigned int *)langopts.replace_chars;
  212. while(*pw != 0)
  213. {
  214. *pw = reverse_word_bytes(*pw);
  215. pw++;
  216. }
  217. #endif
  218. continue;
  219. }
  220. if(p[0] == RULE_LETTERGP2)
  221. {
  222. ix = p[1] - 'A';
  223. p += 2;
  224. if((ix >= 0) && (ix < N_LETTER_GROUPS))
  225. {
  226. letterGroups[ix] = p;
  227. }
  228. }
  229. else
  230. {
  231. len = strlen(p);
  232. p_name = p;
  233. c = p_name[0];
  234. p += (len+1);
  235. if(len == 1)
  236. {
  237. groups1[c] = p;
  238. }
  239. else
  240. if(len == 0)
  241. {
  242. groups1[0] = p;
  243. }
  244. else
  245. {
  246. if(groups2_start[c] == 255)
  247. groups2_start[c] = n_groups2;
  248. groups2_count[c]++;
  249. groups2[n_groups2] = p;
  250. c2 = p_name[1];
  251. groups2_name[n_groups2++] = (c + (c2 << 8));
  252. }
  253. }
  254. // skip over all the rules in this group
  255. while(*p != RULE_GROUP_END)
  256. {
  257. p += (strlen(p) + 1);
  258. }
  259. p++;
  260. }
  261. } // end of InitGroups
  262. int HashDictionary(const char *string)
  263. //====================================
  264. /* Generate a hash code from the specified string
  265. This is used to access the dictionary_2 word-lookup dictionary
  266. */
  267. {
  268. int c;
  269. int chars=0;
  270. int hash=0;
  271. while((c = (*string++ & 0xff)) != 0)
  272. {
  273. hash = hash * 8 + c;
  274. hash = (hash & 0x3ff) ^ (hash >> 8); /* exclusive or */
  275. chars++;
  276. }
  277. return((hash+chars) & 0x3ff); // a 10 bit hash code
  278. } // end of HashDictionary
  279. //=============================================================================================
  280. // Translate between internal representation of phonemes and a mnemonic form for display
  281. //
  282. //=============================================================================================
  283. char *EncodePhonemes(char *p, char *outptr, unsigned char *bad_phoneme)
  284. /*********************************************************************/
  285. /* Translate a phoneme string from ascii mnemonics to internal phoneme numbers,
  286. from 'p' up to next blank .
  287. Returns advanced 'p'
  288. outptr contains encoded phonemes, unrecognised phonemes are encoded as 255
  289. bad_phoneme must point to char array of length 2 of more
  290. */
  291. {
  292. int ix;
  293. unsigned char c;
  294. int count; /* num. of matching characters */
  295. int max; /* highest num. of matching found so far */
  296. int max_ph; /* corresponding phoneme with highest matching */
  297. int consumed;
  298. unsigned int mnemonic_word;
  299. bad_phoneme[0] = 0;
  300. // skip initial blanks
  301. while(isspace(*p))
  302. {
  303. p++;
  304. }
  305. while(((c = *p) != 0) && !isspace(c))
  306. {
  307. consumed = 0;
  308. switch(c)
  309. {
  310. case '|':
  311. // used to separate phoneme mnemonics if needed, to prevent characters being treated
  312. // as a multi-letter mnemonic
  313. if((c = p[1]) == '|')
  314. {
  315. // treat double || as a word-break symbol, drop through
  316. // to the default case with c = '|'
  317. }
  318. else
  319. {
  320. p++;
  321. break;
  322. }
  323. default:
  324. // lookup the phoneme mnemonic, find the phoneme with the highest number of
  325. // matching characters
  326. max= -1;
  327. max_ph= 0;
  328. for(ix=1; ix<n_phoneme_tab; ix++)
  329. {
  330. if(phoneme_tab[ix] == NULL)
  331. continue;
  332. if(phoneme_tab[ix]->type == phINVALID)
  333. continue; // this phoneme is not defined for this language
  334. count = 0;
  335. mnemonic_word = phoneme_tab[ix]->mnemonic;
  336. while(((c = p[count]) > ' ') && (count < 4) &&
  337. (c == ((mnemonic_word >> (count*8)) & 0xff)))
  338. count++;
  339. if((count > max) &&
  340. ((count == 4) || (((mnemonic_word >> (count*8)) & 0xff)==0)))
  341. {
  342. max = count;
  343. max_ph = phoneme_tab[ix]->code;
  344. }
  345. }
  346. if(max_ph == 0)
  347. {
  348. max_ph = 255; /* not recognised */
  349. bad_phoneme[0] = *p;
  350. bad_phoneme[1] = 0;
  351. }
  352. if(max <= 0)
  353. max = 1;
  354. p += (consumed + max);
  355. *outptr++ = (char)(max_ph);
  356. if(max_ph == phonSWITCH)
  357. {
  358. // Switch Language: this phoneme is followed by a text string
  359. char *p_lang = outptr;
  360. while(!isspace(c = *p++) && (c != 0))
  361. *outptr++ = tolower(c);
  362. *outptr = 0;
  363. if(strcmp(p_lang,"en")==0)
  364. {
  365. *p_lang = 0; // don't need "en", it's assumed by default
  366. }
  367. return(p);
  368. }
  369. break;
  370. }
  371. }
  372. /* terminate the encoded string */
  373. *outptr = 0;
  374. return(p);
  375. } // end of EncodePhonemes
  376. void DecodePhonemes(const char *inptr, char *outptr)
  377. //==================================================
  378. // Translate from internal phoneme codes into phoneme mnemonics
  379. {
  380. unsigned char phcode;
  381. unsigned char c;
  382. unsigned int mnem;
  383. PHONEME_TAB *ph;
  384. static const char *stress_chars = "==,,'* ";
  385. while((phcode = *inptr++) > 0)
  386. {
  387. if(phcode == 255)
  388. continue; /* indicates unrecognised phoneme */
  389. if((ph = phoneme_tab[phcode]) == NULL)
  390. continue;
  391. if((ph->type == phSTRESS) && (ph->std_length <= 4) && (ph->spect == 0))
  392. {
  393. if(ph->std_length > 1)
  394. *outptr++ = stress_chars[ph->std_length];
  395. }
  396. else
  397. {
  398. mnem = ph->mnemonic;
  399. while((c = (mnem & 0xff)) != 0)
  400. {
  401. *outptr++ = c;
  402. mnem = mnem >> 8;
  403. }
  404. }
  405. }
  406. *outptr = 0; /* string terminator */
  407. } // end of DecodePhonemes
  408. void Translator::WriteMnemonic(int *ix, int mnem)
  409. {//==============================================
  410. unsigned char c;
  411. while((c = mnem & 0xff) != 0)
  412. {
  413. if((c == '/') && (option_phoneme_variants==0))
  414. break; // discard phoneme variant indicator
  415. phon_out[(*ix)++]= c;
  416. // phon_out[phon_out_ix++]= ipa1[c];
  417. mnem = mnem >> 8;
  418. }
  419. }
  420. void Translator::GetTranslatedPhonemeString(char *phon_out, int n_phon_out)
  421. {//========================================================================
  422. /* Can be called after a clause has been translated into phonemes, in order
  423. to display the clause in phoneme mnemonic form.
  424. */
  425. int ix;
  426. int phon_out_ix=0;
  427. int stress;
  428. PHONEME_LIST *plist;
  429. static const char *stress_chars = "==,,''";
  430. if(phon_out != NULL)
  431. {
  432. for(ix=1; ix<(n_phoneme_list-2) && (phon_out_ix < (n_phon_out - 6)); ix++)
  433. {
  434. plist = &phoneme_list[ix];
  435. if(plist->newword)
  436. phon_out[phon_out_ix++] = ' ';
  437. if(plist->synthflags & SFLAG_SYLLABLE)
  438. {
  439. if((stress = plist->tone) > 1)
  440. {
  441. if(stress > 5) stress = 5;
  442. phon_out[phon_out_ix++] = stress_chars[stress];
  443. }
  444. }
  445. WriteMnemonic(&phon_out_ix,plist->ph->mnemonic);
  446. if(plist->synthflags & SFLAG_LENGTHEN)
  447. {
  448. WriteMnemonic(&phon_out_ix,phoneme_tab[phonLENGTHEN]->mnemonic);
  449. }
  450. if((plist->synthflags & SFLAG_SYLLABLE) && (plist->type != phVOWEL))
  451. {
  452. // syllablic consonant
  453. WriteMnemonic(&phon_out_ix,phoneme_tab[phonSYLLABIC]->mnemonic);
  454. }
  455. if(plist->tone_ph > 0)
  456. {
  457. WriteMnemonic(&phon_out_ix,phoneme_tab[plist->tone_ph]->mnemonic);
  458. }
  459. }
  460. if(phon_out_ix >= n_phon_out)
  461. phon_out_ix = n_phon_out - 1;
  462. phon_out[phon_out_ix] = 0;
  463. }
  464. } // end of Translator::GetTranslatedPhonemeString
  465. //=============================================================================================
  466. // Is a word Unpronouncable - and so should be spoken as individual letters
  467. //
  468. //=============================================================================================
  469. #ifdef deleted
  470. // this is the initials_bitmap for english
  471. static unsigned char initials_bitmap[86] = {
  472. 0x00, 0x00, 0x00, 0x00, 0x22, 0x08, 0x00, 0x88, // 0
  473. 0x20, 0x24, 0x20, 0x80, 0x10, 0x00, 0x00, 0x00,
  474. 0x00, 0x28, 0x08, 0x00, 0x88, 0x22, 0x04, 0x00, // 16
  475. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  476. 0x00, 0x88, 0x22, 0x04, 0x00, 0x02, 0x00, 0x00, // 32
  477. 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  478. 0x00, 0x28, 0x8a, 0x03, 0x00, 0x00, 0x40, 0x00, // 48
  479. 0x02, 0x00, 0x41, 0xca, 0x9b, 0x06, 0x20, 0x80,
  480. 0x91, 0x00, 0x00, 0x00, 0x00, 0x20, 0x08, 0x00, // 64
  481. 0x08, 0x20, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  482. 0x00, 0x00, 0x22, 0x00, 0x01, 0x00, };
  483. #endif
  484. int Translator::Unpronouncable(char *word)
  485. {//=======================================
  486. /* Determines whether a word in 'unpronouncable', i.e. whether it should
  487. be spoken as individual letters.
  488. This function may be language specific. This is a generic version.
  489. */
  490. int c;
  491. int c1=0;
  492. int vowel_posn=9;
  493. int index;
  494. int count;
  495. int apostrophe=0;
  496. if(langopts.param[LOPT_UNPRONOUNCABLE] == 1)
  497. return(0);
  498. if((*word == ' ') || (*word == 0))
  499. return(0);
  500. index = 0;
  501. count = 0;
  502. for(;;)
  503. {
  504. index += utf8_in(&c,&word[index],0);
  505. if((c==0) || (c==' '))
  506. break;
  507. if(count==0)
  508. c1 = c;
  509. count++;
  510. if(IsVowel(c))
  511. {
  512. vowel_posn = count; // position of the first vowel
  513. break;
  514. }
  515. if(c == '\'')
  516. apostrophe = 1;
  517. else
  518. if(!iswalpha(c))
  519. return(0); // letter (not vowel) outside a-z range or apostrophe, abort test
  520. }
  521. if((vowel_posn < 9) && (langopts.param[LOPT_UNPRONOUNCABLE] == 2))
  522. return(0); // option means allow any word with a vowel
  523. if(c1 == langopts.param[LOPT_UNPRONOUNCABLE])
  524. vowel_posn--; // disregard this as the initial letter when counting
  525. if(vowel_posn > (langopts.max_initial_consonants+1))
  526. return(1); // no vowel, or no vowel in first four letters
  527. return(0);
  528. } /* end of Unpronounceable */
  529. int Translator::IsLetterGroup(char *word, int group, int pre)
  530. {//==========================================================
  531. // match the word against a list of utf-8 strings
  532. char *p;
  533. char *w;
  534. p = letterGroups[group];
  535. while(*p != RULE_GROUP_END)
  536. {
  537. w = word;
  538. while(*p == *w)
  539. {
  540. w++;
  541. p++;
  542. }
  543. if(*p == 0)
  544. return(w-word); // matched a complete string
  545. while(*p++ != 0); // skip to end of string
  546. }
  547. return(0);
  548. }
  549. int Translator::IsLetter(int letter, int group)
  550. {//============================================
  551. if(letter_groups[group] != NULL)
  552. {
  553. if(wcschr(letter_groups[group],letter))
  554. return(1);
  555. return(0);
  556. }
  557. if(group > 7)
  558. return(0);
  559. if(letter_bits_offset > 0)
  560. {
  561. letter -= letter_bits_offset;
  562. }
  563. else
  564. {
  565. if((letter >= 0xc0) && (letter <= 0x241))
  566. return(letter_bits[remove_accent[letter-0xc0]] & (1L << group));
  567. }
  568. if((letter >= 0) && (letter < 0x80))
  569. return(letter_bits[letter] & (1L << group));
  570. return(0);
  571. }
  572. int Translator::IsVowel(int letter)
  573. {//================================
  574. return(IsLetter(letter,0));
  575. }
  576. void SetLetterVowel(Translator *tr, int c)
  577. {//=======================================
  578. tr->letter_bits[c] = (tr->letter_bits[c] & 0x40) | 0x81; // keep value for group 6 (front vowels e,i,y)
  579. }
  580. void ResetLetterBits(Translator *tr, int groups)
  581. {//=============================================
  582. // Clear all the specified groups
  583. unsigned int ix;
  584. unsigned int mask;
  585. mask = ~groups;
  586. for(ix=0; ix<sizeof(tr->letter_bits); ix++)
  587. {
  588. tr->letter_bits[ix] &= mask;
  589. }
  590. }
  591. void SetLetterBits(Translator *tr, int group, const char *string)
  592. {//==============================================================
  593. int bits;
  594. unsigned char c;
  595. bits = (1L << group);
  596. while((c = *string++) != 0)
  597. tr->letter_bits[c] |= bits;
  598. }
  599. void SetLetterBitsRange(Translator *tr, int group, int first, int last)
  600. {//====================================================================
  601. int bits;
  602. int ix;
  603. bits = (1L << group);
  604. for(ix=first; ix<=last; ix++)
  605. {
  606. tr->letter_bits[ix] |= bits;
  607. }
  608. }
  609. //=============================================================================================
  610. // Determine the stress pattern of a word
  611. //
  612. //=============================================================================================
  613. static int GetVowelStress(Translator *tr, unsigned char *phonemes, unsigned char *vowel_stress, int &vowel_count, int &stressed_syllable, int control)
  614. {//====================================================================================================================================================
  615. // control = 1, set stress to 1 for forced unstressed vowels
  616. unsigned char phcode;
  617. PHONEME_TAB *ph;
  618. unsigned char *ph_out = phonemes;
  619. int count = 1;
  620. int max_stress = 0;
  621. int ix;
  622. int j;
  623. int stress = 0;
  624. int primary_posn = 0;
  625. vowel_stress[0] = 0;
  626. while((phcode = *phonemes++) != 0)
  627. {
  628. if((ph = phoneme_tab[phcode]) == NULL)
  629. continue;
  630. if((ph->type == phSTRESS) && (ph->spect == 0))
  631. {
  632. /* stress marker, use this for the following vowel */
  633. if(phcode == phonSTRESS_PREV)
  634. {
  635. /* primary stress on preceeding vowel */
  636. j = count - 1;
  637. while((j > 0) && (stressed_syllable == 0) && (vowel_stress[j] < 4))
  638. {
  639. if(vowel_stress[j] != 1)
  640. {
  641. // don't promote a phoneme which must be unstressed
  642. vowel_stress[j] = 4;
  643. if(max_stress < 4)
  644. {
  645. max_stress = 4;
  646. primary_posn = j;
  647. }
  648. /* reduce any preceding primary stress markers */
  649. for(ix=1; ix<j; ix++)
  650. {
  651. if(vowel_stress[ix] == 4)
  652. vowel_stress[ix] = 3;
  653. }
  654. break;
  655. }
  656. j--;
  657. }
  658. }
  659. else
  660. {
  661. if((ph->std_length < 4) || (stressed_syllable == 0))
  662. {
  663. stress = ph->std_length;
  664. if(stress > max_stress)
  665. max_stress = stress;
  666. }
  667. }
  668. continue;
  669. }
  670. if((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC))
  671. {
  672. vowel_stress[count] = (char)stress;
  673. if((stress >= 4) && (stress >= max_stress))
  674. {
  675. primary_posn = count;
  676. max_stress = stress;
  677. }
  678. if((stress == 0) && (control & 1) && (ph->phflags & phUNSTRESSED))
  679. vowel_stress[count] = 1; /* weak vowel, must be unstressed */
  680. count++;
  681. stress = 0;
  682. }
  683. else
  684. if(phcode == phonSYLLABIC)
  685. {
  686. // previous consonant phoneme is syllablic
  687. vowel_stress[count] = (char)stress;
  688. if((stress == 0) && (control & 1))
  689. vowel_stress[count++] = 1; // syllabic consonant, usually unstressed
  690. }
  691. *ph_out++ = phcode;
  692. }
  693. vowel_stress[count] = 0;
  694. *ph_out = 0;
  695. /* has the position of the primary stress been specified by $1, $2, etc? */
  696. if(stressed_syllable > 0)
  697. {
  698. if(stressed_syllable >= count)
  699. stressed_syllable = count-1; // the final syllable
  700. vowel_stress[stressed_syllable] = 4;
  701. max_stress = 4;
  702. primary_posn = stressed_syllable;
  703. }
  704. if(max_stress == 5)
  705. {
  706. // priority stress, replaces any other primary stress marker
  707. for(ix=1; ix<count; ix++)
  708. {
  709. if(vowel_stress[ix] == 4)
  710. {
  711. if(tr->langopts.stress_flags & 0x20000)
  712. vowel_stress[ix] = 0;
  713. else
  714. vowel_stress[ix] = 3;
  715. }
  716. if(vowel_stress[ix] == 5)
  717. {
  718. vowel_stress[ix] = 4;
  719. primary_posn = ix;
  720. }
  721. }
  722. max_stress = 4;
  723. }
  724. stressed_syllable = primary_posn;
  725. vowel_count = count;
  726. return(max_stress);
  727. } // end of GetVowelStress
  728. static char stress_phonemes[] = {phonSTRESS_U, phonSTRESS_D, phonSTRESS_2, phonSTRESS_3,
  729. phonSTRESS_P, phonSTRESS_P2, phonSTRESS_TONIC};
  730. void ChangeWordStress(Translator *tr, char *word, int new_stress)
  731. {//==============================================================
  732. int ix;
  733. unsigned char *p;
  734. int max_stress;
  735. int vowel_count; // num of vowels + 1
  736. int stressed_syllable=0; // position of stressed syllable
  737. unsigned char phonetic[N_WORD_PHONEMES];
  738. unsigned char vowel_stress[N_WORD_PHONEMES/2];
  739. strcpy((char *)phonetic,word);
  740. max_stress = GetVowelStress(tr, phonetic, vowel_stress, vowel_count, stressed_syllable, 0);
  741. if(new_stress >= 4)
  742. {
  743. // promote to primary stress
  744. for(ix=1; ix<vowel_count; ix++)
  745. {
  746. if(vowel_stress[ix] >= max_stress)
  747. {
  748. vowel_stress[ix] = new_stress;
  749. break;
  750. }
  751. }
  752. }
  753. else
  754. {
  755. // remove primary stress
  756. for(ix=1; ix<vowel_count; ix++)
  757. {
  758. if(vowel_stress[ix] > new_stress) // >= allows for diminished stress (=1)
  759. vowel_stress[ix] = new_stress;
  760. }
  761. }
  762. // write out phonemes
  763. ix = 1;
  764. p = phonetic;
  765. while(*p != 0)
  766. {
  767. if((phoneme_tab[*p]->type == phVOWEL) && !(phoneme_tab[*p]->phflags & phNONSYLLABIC))
  768. {
  769. if(vowel_stress[ix] != 0)
  770. *word++ = stress_phonemes[vowel_stress[ix]];
  771. ix++;
  772. }
  773. *word++ = *p++;
  774. }
  775. *word = 0;
  776. } // end of ChangeWordStress
  777. void Translator::SetWordStress(char *output, unsigned int dictionary_flags, int tonic, int prev_stress)
  778. {//===================================================================================================
  779. /* Guess stress pattern of word. This is language specific
  780. 'dictionary_flags' has bits 0-3 position of stressed vowel (if > 0)
  781. or unstressed (if == 7) or syllables 1 and 2 (if == 6)
  782. bits 8... dictionary flags
  783. If 'tonic' is set (>= 0), replace highest stress by this value.
  784. Parameter used for input and output
  785. */
  786. unsigned char phcode;
  787. unsigned char *p;
  788. PHONEME_TAB *ph;
  789. int stress;
  790. int max_stress;
  791. int vowel_count; // num of vowels + 1
  792. int ix;
  793. int v;
  794. int v_stress;
  795. int stressed_syllable; // position of stressed syllable
  796. int max_stress_posn;
  797. int unstressed_word = 0;
  798. char *max_output;
  799. int final_ph;
  800. int mnem;
  801. int post_tonic;
  802. int opt_length;
  803. int done;
  804. unsigned char vowel_stress[N_WORD_PHONEMES/2];
  805. char syllable_weight[N_WORD_PHONEMES/2];
  806. unsigned char phonetic[N_WORD_PHONEMES];
  807. static char consonant_types[16] = {0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0};
  808. /* stress numbers STRESS_BASE +
  809. 0 diminished, unstressed within a word
  810. 1 unstressed, weak
  811. 2
  812. 3 secondary stress
  813. 4 main stress */
  814. /* copy input string into internal buffer */
  815. for(ix=0; ix<N_WORD_PHONEMES; ix++)
  816. {
  817. phonetic[ix] = output[ix];
  818. // check for unknown phoneme codes
  819. if(phonetic[ix] >= n_phoneme_tab)
  820. phonetic[ix] = phonSCHWA;
  821. if(phonetic[ix] == 0)
  822. break;
  823. }
  824. if(ix == 0) return;
  825. final_ph = phonetic[ix-1];
  826. max_output = output + (N_WORD_PHONEMES-3); /* check for overrun */
  827. // any stress position marked in the xx_list dictionary ?
  828. stressed_syllable = dictionary_flags & 0x7;
  829. if(dictionary_flags & 0x8)
  830. {
  831. // this indicates a word without a primary stress
  832. stressed_syllable = dictionary_flags & 0x3;
  833. unstressed_word = 1;
  834. }
  835. max_stress = GetVowelStress(this, phonetic, vowel_stress, vowel_count, stressed_syllable, 1);
  836. // heavy or light syllables
  837. ix = 1;
  838. for(p = phonetic; *p != 0; p++)
  839. {
  840. if((phoneme_tab[p[0]]->type == phVOWEL) && !(phoneme_tab[p[0]]->phflags & phNONSYLLABIC))
  841. {
  842. int weight = 0;
  843. int lengthened = 0;
  844. if(phoneme_tab[p[1]]->code == phonLENGTHEN)
  845. lengthened = 1;
  846. if(lengthened || (phoneme_tab[p[0]]->phflags & phLONG))
  847. {
  848. // long vowel, increase syllable weight
  849. weight++;
  850. }
  851. if(lengthened) p++; // advance over phonLENGTHEN
  852. if(consonant_types[phoneme_tab[p[1]]->type] && ((phoneme_tab[p[2]]->type != phVOWEL) || (phoneme_tab[p[1]]->phflags & phLONG)))
  853. {
  854. // followed by two consonants, a long consonant, or consonant and end-of-word
  855. weight++;
  856. }
  857. syllable_weight[ix] = weight;
  858. ix++;
  859. }
  860. }
  861. switch(langopts.stress_rule)
  862. {
  863. case 2:
  864. // a language with stress on penultimate vowel
  865. if(stressed_syllable == 0)
  866. {
  867. /* no explicit stress - stress the penultimate vowel */
  868. max_stress = 4;
  869. if(vowel_count > 2)
  870. {
  871. stressed_syllable = vowel_count - 2;
  872. if(langopts.stress_flags & 0x300)
  873. {
  874. // LANG=Spanish, stress on last vowel if the word ends in a consonant other than 'n' or 's'
  875. if(phoneme_tab[final_ph]->type != phVOWEL)
  876. {
  877. if(langopts.stress_flags & 0x100)
  878. {
  879. stressed_syllable = vowel_count - 1;
  880. }
  881. else
  882. {
  883. mnem = phoneme_tab[final_ph]->mnemonic;
  884. if((mnem != 'n') && (mnem != 's'))
  885. {
  886. stressed_syllable = vowel_count - 1;
  887. }
  888. }
  889. }
  890. }
  891. if(vowel_stress[stressed_syllable] == 1)
  892. {
  893. // but this vowel is explicitly marked as unstressed
  894. if(stressed_syllable > 1)
  895. stressed_syllable--;
  896. else
  897. stressed_syllable++;
  898. }
  899. }
  900. else
  901. {
  902. stressed_syllable = 1;
  903. if(langopts.stress_flags & 0x1)
  904. max_stress = 3; // don't give full stress to monosyllables
  905. }
  906. // only set the stress if it's not already marked explicitly
  907. if(vowel_stress[stressed_syllable] == 0)
  908. {
  909. // don't stress if next and prev syllables are stressed
  910. if((vowel_stress[stressed_syllable-1] < 4) || (vowel_stress[stressed_syllable+1] < 4))
  911. vowel_stress[stressed_syllable] = max_stress;
  912. }
  913. }
  914. break;
  915. case 3:
  916. // stress on last vowel
  917. if(stressed_syllable == 0)
  918. {
  919. /* no explicit stress - stress the final vowel */
  920. stressed_syllable = vowel_count - 1;
  921. if(max_stress == 0)
  922. {
  923. vowel_stress[stressed_syllable] = 4;
  924. }
  925. max_stress = 4;
  926. }
  927. break;
  928. case 4: // stress on antipenultimate vowel
  929. if(stressed_syllable == 0)
  930. {
  931. stressed_syllable = vowel_count - 3;
  932. if(stressed_syllable < 1)
  933. stressed_syllable = 1;
  934. if(max_stress == 0)
  935. {
  936. vowel_stress[stressed_syllable] = 4;
  937. }
  938. max_stress = 4;
  939. }
  940. break;
  941. case 5:
  942. // LANG=Russian
  943. if(stressed_syllable == 0)
  944. {
  945. /* no explicit stress - guess the stress from the number of syllables */
  946. static char guess_ru[16] = {0,0,1,1,2,3,3,4,5,6,7,7,8,9,10,11};
  947. 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
  948. 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
  949. stressed_syllable = vowel_count - 3;
  950. if(vowel_count < 16)
  951. {
  952. if(phoneme_tab[final_ph]->type == phVOWEL)
  953. stressed_syllable = guess_ru_v[vowel_count];
  954. else
  955. if(phoneme_tab[final_ph]->type == phSTOP)
  956. stressed_syllable = guess_ru_t[vowel_count];
  957. else
  958. stressed_syllable = guess_ru[vowel_count];
  959. }
  960. vowel_stress[stressed_syllable] = 4;
  961. max_stress = 4;
  962. }
  963. break;
  964. case 6: // LANG=hi stress on the last heaviest syllable
  965. if(stressed_syllable == 0)
  966. {
  967. int wt;
  968. int max_weight = -1;
  969. int prev_stressed;
  970. // find the heaviest syllable, excluding the final syllable
  971. for(ix = 1; ix < (vowel_count-1); ix++)
  972. {
  973. if(vowel_stress[ix] == 0)
  974. {
  975. if((wt = syllable_weight[ix]) >= max_weight)
  976. {
  977. max_weight = wt;
  978. prev_stressed = stressed_syllable;
  979. stressed_syllable = ix;
  980. }
  981. }
  982. }
  983. if((syllable_weight[vowel_count-1] == 2) && (max_weight< 2))
  984. {
  985. // the only double=heavy syllable is the final syllable, so stress this
  986. stressed_syllable = vowel_count-1;
  987. }
  988. else
  989. if(max_weight <= 0)
  990. {
  991. // all syllables, exclusing the last, are light. Stress the first syllable
  992. stressed_syllable = 1;
  993. }
  994. vowel_stress[stressed_syllable] = 4;
  995. max_stress = 4;
  996. }
  997. break;
  998. case 7: // LANG=tr, the last syllable for any vowel markes explicitly as unstressed
  999. if(stressed_syllable == 0)
  1000. {
  1001. stressed_syllable = vowel_count - 1;
  1002. for(ix=2; ix < vowel_count; ix++)
  1003. {
  1004. if(vowel_stress[ix] == 1)
  1005. {
  1006. stressed_syllable = ix-1;
  1007. break;
  1008. }
  1009. }
  1010. vowel_stress[stressed_syllable] = 4;
  1011. max_stress = 4;
  1012. }
  1013. break;
  1014. case 9: // mark all as stressed
  1015. for(ix=1; ix<vowel_count; ix++)
  1016. {
  1017. if(vowel_stress[ix] == 0)
  1018. vowel_stress[ix] = 4;
  1019. }
  1020. break;
  1021. }
  1022. /* now guess the complete stress pattern */
  1023. if(max_stress < 4)
  1024. stress = 4; /* no primary stress marked, use for 1st syllable */
  1025. else
  1026. stress = 3;
  1027. if((langopts.stress_flags & 0x1000) && (vowel_count == 2))
  1028. {
  1029. // Two syllable word, if one syllable has primary stress, then give the other secondary stress
  1030. if(vowel_stress[1] == 4)
  1031. vowel_stress[2] = 3;
  1032. if(vowel_stress[2] == 4)
  1033. vowel_stress[1] = 3;
  1034. }
  1035. #if deleted
  1036. if((langopts.stress_flags & 0x2000) && (vowel_stress[1] == 0))
  1037. {
  1038. // If there is only one syllable before the primary stress, give it a secondary stress
  1039. if((vowel_count > 2) && (vowel_stress[2] >= 4))
  1040. {
  1041. vowel_stress[1] = 3;
  1042. }
  1043. }
  1044. #endif
  1045. done = 0;
  1046. for(v=1; v<vowel_count; v++)
  1047. {
  1048. if(vowel_stress[v] == 0)
  1049. {
  1050. if((langopts.stress_flags & 0x10) && (stress < 4) && (v == vowel_count-1))
  1051. {
  1052. // flag: don't give secondary stress to final vowel
  1053. }
  1054. else
  1055. if((langopts.stress_flags & 0x8000) && (done == 0))
  1056. {
  1057. vowel_stress[v] = (char)stress;
  1058. done =1;
  1059. stress = 3; /* use secondary stress for remaining syllables */
  1060. }
  1061. else
  1062. if((vowel_stress[v-1] <= 1) && (vowel_stress[v+1] <= 1))
  1063. {
  1064. /* trochaic: give stress to vowel surrounded by unstressed vowels */
  1065. if((stress == 3) && (langopts.stress_flags & 0x20))
  1066. continue; // don't use secondary stress
  1067. if((v > 1) && (langopts.stress_flags & 0x40) && (syllable_weight[v]==0) && (syllable_weight[v+1]>0))
  1068. {
  1069. // don't put secondary stress on a light syllable which is followed by a heavy syllable
  1070. continue;
  1071. }
  1072. // should start with secondary stress on the first syllable, or should it count back from
  1073. // the primary stress and put secondary stress on alternate syllables?
  1074. vowel_stress[v] = (char)stress;
  1075. done =1;
  1076. stress = 3; /* use secondary stress for remaining syllables */
  1077. }
  1078. }
  1079. }
  1080. if((unstressed_word) && (tonic < 0))
  1081. {
  1082. if(vowel_count <= 2)
  1083. tonic = langopts.unstressed_wd1; /* monosyllable - unstressed */
  1084. else
  1085. tonic = langopts.unstressed_wd2; /* more than one syllable, used secondary stress as the main stress */
  1086. }
  1087. max_stress = 0;
  1088. max_stress_posn = 0;
  1089. for(v=1; v<vowel_count; v++)
  1090. {
  1091. if(vowel_stress[v] >= max_stress)
  1092. {
  1093. max_stress = vowel_stress[v];
  1094. max_stress_posn = v;
  1095. }
  1096. }
  1097. if(tonic >= 0)
  1098. {
  1099. /* find position of highest stress, and replace it by 'tonic' */
  1100. /* don't disturb an explicitly set stress by 'unstress-at-end' flag */
  1101. if((tonic > max_stress) || (max_stress <= 4))
  1102. vowel_stress[max_stress_posn] = (char)tonic;
  1103. max_stress = tonic;
  1104. }
  1105. /* produce output phoneme string */
  1106. p = phonetic;
  1107. v = 1;
  1108. if((ph = phoneme_tab[*p]) != NULL)
  1109. {
  1110. if(ph->type == phSTRESS)
  1111. ph = phoneme_tab[p[1]];
  1112. #ifdef deleted
  1113. int gap = langopts.word_gap & 0x700;
  1114. if((gap) && (vowel_stress[1] >= 4) && (prev_stress >= 4))
  1115. {
  1116. /* two primary stresses together, insert a short pause */
  1117. *output++ = pause_phonemes[gap >> 8];
  1118. }
  1119. else
  1120. #endif
  1121. if((langopts.vowel_pause & 0x30) && (ph->type == phVOWEL))
  1122. {
  1123. // word starts with a vowel
  1124. if((langopts.vowel_pause & 0x20) && (vowel_stress[1] >= 4))
  1125. {
  1126. *output++ = phonPAUSE_NOLINK; // not to be replaced by link
  1127. }
  1128. else
  1129. {
  1130. *output++ = phonPAUSE_VSHORT; // break, but no pause
  1131. }
  1132. }
  1133. }
  1134. p = phonetic;
  1135. post_tonic = 0;
  1136. while(((phcode = *p++) != 0) && (output < max_output))
  1137. {
  1138. if((ph = phoneme_tab[phcode]) == NULL)
  1139. continue;
  1140. // if(ph->type == phSTRESS)
  1141. // continue;
  1142. if(ph->type == phPAUSE)
  1143. {
  1144. prev_last_stress = 0;
  1145. }
  1146. else
  1147. if(((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC)) || (*p == phonSYLLABIC))
  1148. {
  1149. // a vowel, or a consonant followed by a syllabic consonant marker
  1150. v_stress = vowel_stress[v];
  1151. prev_last_stress = v_stress;
  1152. if(vowel_stress[v-1] >= max_stress)
  1153. post_tonic = 1;
  1154. if(v_stress <= 1)
  1155. {
  1156. if((v > 1) && (max_stress >= 4) && (langopts.stress_flags & 4) && (v == (vowel_count-1)))
  1157. {
  1158. // option: mark unstressed final syllable as diminished
  1159. v_stress = 1;
  1160. }
  1161. else
  1162. if((langopts.stress_flags & 2) || (v == 1) || (v == (vowel_count-1)))
  1163. {
  1164. // first or last syllable, or option 'don't set diminished stress'
  1165. v_stress = 0;
  1166. }
  1167. else
  1168. if((v == (vowel_count-2)) && (vowel_stress[vowel_count-1] <= 1))
  1169. {
  1170. // penultimate syllable, followed by an unstressed final syllable
  1171. v_stress = 0;
  1172. }
  1173. else
  1174. {
  1175. // unstressed syllable within a word
  1176. if((vowel_stress[v-1] != 1) || ((langopts.stress_flags & 0x10000) == 0))
  1177. {
  1178. v_stress = 1; /* change from 0 (unstressed) to 1 (diminished stress) */
  1179. vowel_stress[v] = v_stress;
  1180. }
  1181. }
  1182. }
  1183. if(v_stress > 0)
  1184. *output++ = stress_phonemes[v_stress]; // mark stress of all vowels except 0 (unstressed)
  1185. if(vowel_stress[v] > max_stress)
  1186. {
  1187. max_stress = vowel_stress[v];
  1188. }
  1189. if((*p == phonLENGTHEN) && ((opt_length = langopts.param[LOPT_IT_LENGTHEN]) != 0))
  1190. {
  1191. // remove lengthen indicator from non-stressed syllables
  1192. int shorten=0;
  1193. if(opt_length & 0x10)
  1194. {
  1195. // only allow lengthen indicator on the highest stress syllable in the word
  1196. if(v != max_stress_posn)
  1197. shorten = 1;
  1198. }
  1199. else
  1200. if(v_stress < 4)
  1201. {
  1202. // only allow lengthen indicator if stress >= 4.
  1203. shorten = 1;
  1204. }
  1205. if(((opt_length & 0xf)==2) && (v != (vowel_count - 2)))
  1206. shorten = 1; // LANG=Italian, remove lengthen indicator from non-penultimate syllables
  1207. if(shorten)
  1208. p++;
  1209. }
  1210. v++;
  1211. }
  1212. if(phcode != 1)
  1213. *output++ = phcode;
  1214. }
  1215. *output++ = 0;
  1216. } /* end of SetWordStress */
  1217. //=============================================================================================
  1218. // Look up a word in the pronunciation rules
  1219. //
  1220. //=============================================================================================
  1221. #ifdef LOG_TRANSLATE
  1222. char *Translator::DecodeRule(const char *group, char *rule)
  1223. {//==================================================
  1224. /* Convert compiled match template to ascii */
  1225. unsigned char rb;
  1226. unsigned char c;
  1227. char *p;
  1228. int ix;
  1229. int match_type;
  1230. int finished=0;
  1231. int value;
  1232. int linenum=0;
  1233. int flags;
  1234. int suffix_char;
  1235. int condition_num=0;
  1236. char buf[60];
  1237. char buf_pre[60];
  1238. char suffix[20];
  1239. static char output[60];
  1240. static char symbols[] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',
  1241. '@','&','%','+','#','S','D','Z','A','L',' ',' ',' ',' ',' ','N','K','V',' ','T','X','?','W'};
  1242. static char symbols_lg[] = {'A','B','C','H','F','G','Y'};
  1243. match_type = 0;
  1244. buf_pre[0] = 0;
  1245. strcpy(buf,group);
  1246. p = &buf[strlen(buf)];
  1247. while(!finished)
  1248. {
  1249. rb = *rule++;
  1250. if(rb <= RULE_LINENUM)
  1251. {
  1252. switch(rb)
  1253. {
  1254. case 0:
  1255. case RULE_PHONEMES:
  1256. finished=1;
  1257. break;
  1258. case RULE_PRE:
  1259. match_type = RULE_PRE;
  1260. *p = 0;
  1261. p = buf_pre;
  1262. break;
  1263. case RULE_POST:
  1264. match_type = RULE_POST;
  1265. *p = 0;
  1266. strcat(buf," (");
  1267. p = &buf[strlen(buf)];
  1268. break;
  1269. case RULE_PH_COMMON:
  1270. break;
  1271. case RULE_CONDITION:
  1272. /* conditional rule, next byte gives condition number */
  1273. condition_num = *rule++;
  1274. break;
  1275. case RULE_LINENUM:
  1276. value = (rule[1] & 0xff) - 1;
  1277. linenum = (rule[0] & 0xff) - 1 + (value * 255);
  1278. rule+=2;
  1279. break;
  1280. }
  1281. continue;
  1282. }
  1283. if(rb == RULE_ENDING)
  1284. {
  1285. static const char *flag_chars = "ei vtfq t";
  1286. flags = ((rule[0] & 0x7f)<< 8) + (rule[1] & 0x7f);
  1287. suffix_char = 'S';
  1288. if(flags & (SUFX_P >> 8))
  1289. suffix_char = 'P';
  1290. sprintf(suffix,"%c%d",suffix_char,rule[2] & 0x7f);
  1291. rule += 3;
  1292. for(ix=0;ix<9;ix++)
  1293. {
  1294. if(flags & 1)
  1295. sprintf(&suffix[strlen(suffix)],"%c",flag_chars[ix]);
  1296. flags = (flags >> 1);
  1297. }
  1298. strcpy(p,suffix);
  1299. p += strlen(suffix);
  1300. c = ' ';
  1301. }
  1302. else
  1303. if(rb == RULE_LETTERGP)
  1304. {
  1305. c = symbols_lg[*rule++ - 'A'];
  1306. }
  1307. else
  1308. if(rb == RULE_LETTERGP2)
  1309. {
  1310. value = *rule++ - 'A';
  1311. p[0] = 'L';
  1312. p[1] = (value / 10) + '0';
  1313. c = (value % 10) + '0';
  1314. if(match_type == RULE_PRE)
  1315. {
  1316. p[0] = c;
  1317. c = 'L';
  1318. }
  1319. p+=2;
  1320. }
  1321. else
  1322. if(rb <= RULE_LAST_RULE)
  1323. c = symbols[rb];
  1324. else
  1325. if(rb == RULE_SPACE)
  1326. c = '_';
  1327. else
  1328. c = rb;
  1329. *p++ = c;
  1330. }
  1331. *p = 0;
  1332. p = output;
  1333. if(linenum > 0)
  1334. {
  1335. sprintf(p,"%5d:\t",linenum);
  1336. p += 7;
  1337. }
  1338. if(condition_num > 0)
  1339. {
  1340. sprintf(p,"?%d ",condition_num);
  1341. p = &p[strlen(p)];
  1342. }
  1343. if((ix = strlen(buf_pre)) > 0)
  1344. {
  1345. while(--ix >= 0)
  1346. *p++ = buf_pre[ix];
  1347. *p++ = ')';
  1348. *p++ = ' ';
  1349. }
  1350. *p = 0;
  1351. strcat(p,buf);
  1352. ix = strlen(output);
  1353. while(ix < 8)
  1354. output[ix++]=' ';
  1355. output[ix]=0;
  1356. return(output);
  1357. } /* end of decode_match */
  1358. #endif
  1359. void Translator::AppendPhonemes(char *string, int size, const char *ph)
  1360. {//====================================================================
  1361. /* Add new phoneme string "ph" to "string"
  1362. Keeps count of the number of vowel phonemes in the word, and whether these
  1363. can be stressed syllables. These values can be used in translation rules
  1364. */
  1365. const char *p;
  1366. unsigned char c;
  1367. int unstress_mark;
  1368. int length;
  1369. length = strlen(ph) + strlen(string);
  1370. if(length >= size)
  1371. {
  1372. return;
  1373. }
  1374. /* any stressable vowel ? */
  1375. unstress_mark = 0;
  1376. p = ph;
  1377. while((c = *p++) != 0)
  1378. {
  1379. if(c >= n_phoneme_tab) continue;
  1380. if(phoneme_tab[c]->type == phSTRESS)
  1381. {
  1382. if(phoneme_tab[c]->std_length < 4)
  1383. unstress_mark = 1;
  1384. }
  1385. else
  1386. {
  1387. if(phoneme_tab[c]->type == phVOWEL)
  1388. {
  1389. if(((phoneme_tab[c]->phflags & phUNSTRESSED) == 0) &&
  1390. (unstress_mark == 0))
  1391. {
  1392. word_stressed_count++;
  1393. }
  1394. unstress_mark = 0;
  1395. word_vowel_count++;
  1396. }
  1397. }
  1398. }
  1399. if(string != NULL)
  1400. strcat(string,ph);
  1401. } /* end of AppendPhonemes */
  1402. void Translator::MatchRule(char *word[], const char *group, char *rule, MatchRecord *match_out, int word_flags, int dict_flags)
  1403. {//============================================================================================================================
  1404. /* Checks a specified word against dictionary rules.
  1405. Returns with phoneme code string, or NULL if no match found.
  1406. word (indirect) points to current character group within the input word
  1407. This is advanced by this procedure as characters are consumed
  1408. group: the initial characters used to choose the rules group
  1409. rule: address of dictionary rule data for this character group
  1410. match_out: returns best points score
  1411. word_flags: indicates whether this is a retranslation after a suffix has been removed
  1412. */
  1413. unsigned char rb; // current instuction from rule
  1414. unsigned char letter; // current letter from input word, single byte
  1415. int letter_w; // current letter, wide character
  1416. int letter_xbytes; // number of extra bytes of multibyte character (num bytes - 1)
  1417. unsigned char last_letter;
  1418. char *pre_ptr;
  1419. char *post_ptr; /* pointer to first character after group */
  1420. char *rule_start; /* start of current match template */
  1421. char *p;
  1422. int match_type; /* left, right, or consume */
  1423. int failed;
  1424. int consumed; /* number of letters consumed from input */
  1425. int count; /* count through rules in the group */
  1426. int syllable_count;
  1427. int vowel;
  1428. int letter_group;
  1429. int distance_right;
  1430. int distance_left;
  1431. int lg_pts;
  1432. int n_bytes;
  1433. MatchRecord match;
  1434. static MatchRecord best;
  1435. int total_consumed; /* letters consumed for best match */
  1436. int group_length;
  1437. unsigned char condition_num;
  1438. char *common_phonemes; /* common to a group of entries */
  1439. if(rule == NULL)
  1440. {
  1441. match_out->points = 0;
  1442. (*word)++;
  1443. return;
  1444. }
  1445. total_consumed = 0;
  1446. count = 0;
  1447. common_phonemes = NULL;
  1448. match_type = 0;
  1449. best.points = 0;
  1450. best.phonemes = "";
  1451. best.end_type = 0;
  1452. best.del_fwd = NULL;
  1453. group_length = strlen(group);
  1454. /* search through dictionary rules */
  1455. while(rule[0] != RULE_GROUP_END)
  1456. {
  1457. match_type=0;
  1458. consumed = 0;
  1459. letter = 0;
  1460. distance_right= -6; /* used to reduce points for matches further away the current letter */
  1461. distance_left= -2;
  1462. count++;
  1463. match.points = 1;
  1464. match.end_type = 0;
  1465. match.del_fwd = NULL;
  1466. pre_ptr = *word;
  1467. post_ptr = *word + group_length;
  1468. /* work through next rule until end, or until no-match proved */
  1469. rule_start = rule;
  1470. failed = 0;
  1471. while(!failed)
  1472. {
  1473. rb = *rule++;
  1474. if(rb <= RULE_LINENUM)
  1475. {
  1476. switch(rb)
  1477. {
  1478. case 0: // no phoneme string for this rule, use previous common rule
  1479. if(common_phonemes != NULL)
  1480. {
  1481. match.phonemes = common_phonemes;
  1482. if(*match.phonemes == RULE_CONDITION)
  1483. match.phonemes += 2; // skip over condition number
  1484. while(((rb = *match.phonemes++) != 0) && (rb != RULE_PHONEMES));
  1485. }
  1486. else
  1487. {
  1488. match.phonemes = "";
  1489. }
  1490. rule--; // so we are still pointing at the 0
  1491. failed=2; // matched OK
  1492. break;
  1493. case RULE_PRE:
  1494. match_type = RULE_PRE;
  1495. break;
  1496. case RULE_POST:
  1497. match_type = RULE_POST;
  1498. break;
  1499. case RULE_PHONEMES:
  1500. match.phonemes = rule;
  1501. failed=2; // matched OK
  1502. break;
  1503. case RULE_PH_COMMON:
  1504. common_phonemes = rule;
  1505. break;
  1506. case RULE_CONDITION:
  1507. /* conditional rule, next byte gives condition number */
  1508. condition_num = *rule++;
  1509. if(condition_num >= 32)
  1510. {
  1511. // allow the rule only if the condition number is NOT set
  1512. if((dict_condition & (1L << (condition_num-32))) != 0)
  1513. failed = 1;
  1514. }
  1515. else
  1516. {
  1517. // allow the rule only if the condition number is set
  1518. if((dict_condition & (1L << condition_num)) == 0)
  1519. failed = 1;
  1520. }
  1521. if(!failed)
  1522. match.points++; // add one point for a matched conditional rule
  1523. break;
  1524. case RULE_LINENUM:
  1525. rule+=2;
  1526. break;
  1527. }
  1528. continue;
  1529. }
  1530. switch(match_type)
  1531. {
  1532. case 0:
  1533. /* match and consume this letter */
  1534. last_letter = letter;
  1535. letter = *post_ptr++;
  1536. if((letter == rb) || ((letter==(unsigned char)REPLACED_E) && (rb=='e')))
  1537. {
  1538. match.points += 21;
  1539. consumed++;
  1540. }
  1541. else
  1542. failed = 1;
  1543. break;
  1544. case RULE_POST:
  1545. /* continue moving fowards */
  1546. distance_right += 6;
  1547. if(distance_right > 18)
  1548. distance_right = 19;
  1549. last_letter = letter;
  1550. letter_xbytes = utf8_in(&letter_w,post_ptr,0)-1;
  1551. letter = *post_ptr++;
  1552. switch(rb)
  1553. {
  1554. case RULE_LETTERGP:
  1555. letter_group = *rule++ - 'A';
  1556. if(IsLetter(letter_w,letter_group))
  1557. {
  1558. lg_pts = 20;
  1559. if(letter_group==2)
  1560. lg_pts = 19; // fewer points for C, general consonant
  1561. match.points += (lg_pts-distance_right);
  1562. post_ptr += letter_xbytes;
  1563. }
  1564. else
  1565. failed = 1;
  1566. break;
  1567. case RULE_LETTERGP2: // match against a list of utf-8 strings
  1568. letter_group = *rule++ - 'A';
  1569. if((n_bytes = IsLetterGroup(post_ptr-1,letter_group,0)) >0)
  1570. {
  1571. match.points += (20-distance_right);
  1572. post_ptr += (n_bytes-1);
  1573. }
  1574. else
  1575. failed =1;
  1576. break;
  1577. case RULE_NOTVOWEL:
  1578. if(!IsLetter(letter_w,0))
  1579. {
  1580. match.points += (20-distance_right);
  1581. post_ptr += letter_xbytes;
  1582. }
  1583. else
  1584. failed = 1;
  1585. break;
  1586. case RULE_DIGIT:
  1587. if(IsDigit(letter_w))
  1588. {
  1589. match.points += (20-distance_right);
  1590. post_ptr += letter_xbytes;
  1591. }
  1592. else
  1593. if(langopts.tone_numbers)
  1594. {
  1595. // also match if there is no digit
  1596. match.points += (20-distance_right);
  1597. post_ptr--;
  1598. }
  1599. else
  1600. failed = 1;
  1601. break;
  1602. case RULE_NONALPHA:
  1603. if(!iswalpha(letter_w))
  1604. {
  1605. match.points += (21-distance_right);
  1606. post_ptr += letter_xbytes;
  1607. }
  1608. else
  1609. failed = 1;
  1610. break;
  1611. case RULE_DOUBLE:
  1612. if(letter == last_letter)
  1613. match.points += (21-distance_right);
  1614. else
  1615. failed = 1;
  1616. break;
  1617. case RULE_ALT1:
  1618. if(dict_flags & FLAG_ALT_TRANS)
  1619. match.points++;
  1620. else
  1621. failed = 1;
  1622. break;
  1623. case '-':
  1624. if((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN_AFTER)))
  1625. {
  1626. match.points += (22-distance_right); // one point more than match against space
  1627. }
  1628. else
  1629. failed = 1;
  1630. break;
  1631. case RULE_SYLLABLE:
  1632. {
  1633. /* more than specified number of vowel letters to the right */
  1634. char *p = post_ptr + letter_xbytes;
  1635. syllable_count = 1;
  1636. while(*rule == RULE_SYLLABLE)
  1637. {
  1638. rule++;
  1639. syllable_count+=1; /* number of syllables to match */
  1640. }
  1641. vowel = 0;
  1642. while(letter_w != RULE_SPACE)
  1643. {
  1644. if((vowel==0) && IsLetter(letter_w,LETTERGP_VOWEL2))
  1645. {
  1646. // this is counting vowels which are separated by non-vowels
  1647. syllable_count--;
  1648. }
  1649. vowel = IsLetter(letter_w,LETTERGP_VOWEL2);
  1650. p += utf8_in(&letter_w,p,0);
  1651. }
  1652. if(syllable_count <= 0)
  1653. match.points+= (19-distance_right);
  1654. else
  1655. failed = 1;
  1656. }
  1657. break;
  1658. case RULE_NOVOWELS:
  1659. {
  1660. char *p = post_ptr + letter_xbytes;
  1661. while(letter_w != RULE_SPACE)
  1662. {
  1663. if(IsLetter(letter_w,LETTERGP_VOWEL2))
  1664. {
  1665. failed = 1;
  1666. break;
  1667. }
  1668. p += utf8_in(&letter_w,p,0);
  1669. }
  1670. if(!failed)
  1671. match.points += (19-distance_right);
  1672. }
  1673. break;
  1674. case RULE_INC_SCORE:
  1675. match.points += 20; // force an increase in points
  1676. break;
  1677. case RULE_DEL_FWD:
  1678. // find the next 'e' in the word and replace by ''
  1679. for(p = *word + group_length; *p != ' '; p++)
  1680. {
  1681. if(*p == 'e')
  1682. {
  1683. match.del_fwd = p;
  1684. break;
  1685. }
  1686. }
  1687. break;
  1688. case RULE_ENDING:
  1689. // next 3 bytes are a (non-zero) ending type. 2 bytes of flags + suffix length
  1690. match.end_type = (rule[0] << 16) + ((rule[1] & 0x7f) << 8) + (rule[2] & 0x7f);
  1691. rule += 3;
  1692. break;
  1693. case RULE_NO_SUFFIX:
  1694. if(word_flags & FLAG_SUFFIX_REMOVED)
  1695. failed = 1; // a suffix has been removed
  1696. else
  1697. match.points++;
  1698. break;
  1699. default:
  1700. if(letter == rb)
  1701. {
  1702. if(letter == RULE_SPACE)
  1703. match.points += (21-distance_right);
  1704. else
  1705. match.points += (21-distance_right);
  1706. }
  1707. else
  1708. failed = 1;
  1709. break;
  1710. }
  1711. break;
  1712. case RULE_PRE:
  1713. /* match backwards from start of current group */
  1714. distance_left += 2;
  1715. if(distance_left > 18)
  1716. distance_left = 19;
  1717. last_letter = *pre_ptr;
  1718. pre_ptr--;
  1719. letter_xbytes = utf8_in(&letter_w,pre_ptr,1)-1;
  1720. letter = *pre_ptr;
  1721. switch(rb)
  1722. {
  1723. case RULE_LETTERGP:
  1724. letter_group = *rule++ - 'A';
  1725. if(IsLetter(letter_w,letter_group))
  1726. {
  1727. lg_pts = 20;
  1728. if(letter_group==2)
  1729. lg_pts = 19; // fewer points for C, general consonant
  1730. match.points += (lg_pts-distance_left);
  1731. pre_ptr -= letter_xbytes;
  1732. }
  1733. else
  1734. failed = 1;
  1735. break;
  1736. case RULE_LETTERGP2: // match against a list of utf-8 strings
  1737. letter_group = *rule++ - 'A';
  1738. if((n_bytes = IsLetterGroup(pre_ptr-letter_xbytes,letter_group,1)) >0)
  1739. {
  1740. match.points += (20-distance_right);
  1741. pre_ptr -= (n_bytes-1);
  1742. }
  1743. else
  1744. failed =1;
  1745. break;
  1746. case RULE_NOTVOWEL:
  1747. if(!IsLetter(letter_w,0))
  1748. {
  1749. match.points += (20-distance_left);
  1750. pre_ptr -= letter_xbytes;
  1751. }
  1752. else
  1753. failed = 1;
  1754. break;
  1755. case RULE_DOUBLE:
  1756. if(letter == last_letter)
  1757. match.points += (21-distance_left);
  1758. else
  1759. failed = 1;
  1760. break;
  1761. case RULE_DIGIT:
  1762. if(IsDigit(letter_w))
  1763. {
  1764. match.points += (21-distance_left);
  1765. pre_ptr -= letter_xbytes;
  1766. }
  1767. else
  1768. failed = 1;
  1769. break;
  1770. case RULE_NONALPHA:
  1771. if(!iswalpha(letter_w))
  1772. {
  1773. match.points += (21-distance_right);
  1774. pre_ptr -= letter_xbytes;
  1775. }
  1776. else
  1777. failed = 1;
  1778. break;
  1779. case RULE_SYLLABLE:
  1780. /* more than specified number of vowels to the left */
  1781. syllable_count = 1;
  1782. while(*rule == RULE_SYLLABLE)
  1783. {
  1784. rule++;
  1785. syllable_count++; /* number of syllables to match */
  1786. }
  1787. if(syllable_count <= word_vowel_count)
  1788. match.points+= (19-distance_left);
  1789. else
  1790. failed = 1;
  1791. break;
  1792. case RULE_STRESSED:
  1793. if(word_stressed_count > 0)
  1794. match.points += 19;
  1795. else
  1796. failed = 1;
  1797. break;
  1798. case RULE_NOVOWELS:
  1799. {
  1800. char *p = pre_ptr - letter_xbytes - 1;
  1801. while(letter_w != RULE_SPACE)
  1802. {
  1803. if(IsLetter(letter_w,LETTERGP_VOWEL2))
  1804. {
  1805. failed = 1;
  1806. break;
  1807. }
  1808. p -= utf8_in(&letter_w,p,1);
  1809. }
  1810. if(!failed)
  1811. match.points += 3;
  1812. }
  1813. break;
  1814. case RULE_IFVERB:
  1815. if(expect_verb)
  1816. match.points += 1;
  1817. else
  1818. failed = 1;
  1819. break;
  1820. case RULE_CAPITAL:
  1821. if(word_flags & FLAG_FIRST_UPPER)
  1822. match.points += 1;
  1823. else
  1824. failed = 1;
  1825. break;
  1826. case '.':
  1827. // dot in pre- section, match on any dot before this point in the word
  1828. for(p=pre_ptr; *p != ' '; p--)
  1829. {
  1830. if(*p == '.')
  1831. {
  1832. match.points +=50;
  1833. break;
  1834. }
  1835. }
  1836. if(*p == ' ')
  1837. failed = 1;
  1838. break;
  1839. case '-':
  1840. if((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN)))
  1841. {
  1842. match.points += (22-distance_right); // one point more than match against space
  1843. }
  1844. else
  1845. failed = 1;
  1846. break;
  1847. default:
  1848. if(letter == rb)
  1849. {
  1850. if(letter == RULE_SPACE)
  1851. match.points += 4;
  1852. else
  1853. match.points += (21-distance_left);
  1854. }
  1855. else
  1856. failed = 1;
  1857. break;
  1858. }
  1859. break;
  1860. }
  1861. }
  1862. if(failed == 2)
  1863. {
  1864. /* matched OK, is this better than the last best match ? */
  1865. if(match.points >= best.points)
  1866. {
  1867. memcpy(&best,&match,sizeof(match));
  1868. total_consumed = consumed;
  1869. }
  1870. #ifdef LOG_TRANSLATE
  1871. if((option_phonemes == 2) && (match.points > 0) && ((word_flags & FLAG_NO_TRACE) == 0))
  1872. {
  1873. // show each rule that matches, and it's points score
  1874. int pts;
  1875. char decoded_phonemes[80];
  1876. // note: 'count' contains the rule number, if we want to include it
  1877. pts = match.points;
  1878. if(group_length > 1)
  1879. pts += 35; // to account for an extra letter matching
  1880. DecodePhonemes(match.phonemes,decoded_phonemes);
  1881. fprintf(f_trans,"%3d\t%s [%s]\n",pts,DecodeRule(group,rule_start),decoded_phonemes);
  1882. }
  1883. #endif
  1884. }
  1885. /* skip phoneme string to reach start of next template */
  1886. while(*rule++ != 0);
  1887. }
  1888. #ifdef LOG_TRANSLATE
  1889. if((option_phonemes == 2) && ((word_flags & FLAG_NO_TRACE)==0))
  1890. {
  1891. if(group_length <= 1)
  1892. fprintf(f_trans,"\n");
  1893. }
  1894. #endif
  1895. /* advance input data pointer */
  1896. total_consumed += group_length;
  1897. if(total_consumed == 0)
  1898. total_consumed = 1; /* always advance over 1st letter */
  1899. *word += total_consumed;
  1900. if(best.points == 0)
  1901. best.phonemes = "";
  1902. memcpy(match_out,&best,sizeof(MatchRecord));
  1903. } /* end of MatchRule */
  1904. int Translator::TranslateRules(char *p_start, char *phonemes, int ph_size, char *end_phonemes, int word_flags, int dict_flags)
  1905. {//===========================================================================================================================
  1906. /* Translate a word bounded by space characters
  1907. Append the result to 'phonemes' and any standard prefix/suffix in 'end_phonemes' */
  1908. unsigned char c, c2;
  1909. unsigned int c12;
  1910. int wc=0;
  1911. int wc_prev;
  1912. int wc_bytes;
  1913. char *p2; /* copy of p for use in double letter chain match */
  1914. int found;
  1915. int g; /* group chain number */
  1916. int g1; /* first group for this letter */
  1917. int n;
  1918. int letter;
  1919. int any_alpha=0;
  1920. int ix;
  1921. int digit_count=0;
  1922. char *p;
  1923. MatchRecord match1;
  1924. MatchRecord match2;
  1925. char ph_buf[40];
  1926. char word_copy[N_WORD_BYTES];
  1927. static const char str_pause[2] = {phonPAUSE_NOLINK,0};
  1928. char group_name[4];
  1929. if(data_dictrules == NULL)
  1930. return(0);
  1931. for(ix=0; ix<(N_WORD_BYTES-1);)
  1932. {
  1933. c = p_start[ix];
  1934. word_copy[ix++] = c;
  1935. }
  1936. word_copy[ix] = 0;
  1937. #ifdef LOG_TRANSLATE
  1938. if((option_phonemes == 2) && ((word_flags & FLAG_NO_TRACE)==0))
  1939. {
  1940. char wordbuf[120];
  1941. int ix;
  1942. for(ix=0; ((c = p_start[ix]) != ' ') && (c != 0); ix++)
  1943. {
  1944. wordbuf[ix] = c;
  1945. }
  1946. wordbuf[ix] = 0;
  1947. fprintf(f_trans,"Translate '%s'\n",wordbuf);
  1948. }
  1949. #endif
  1950. p = p_start;
  1951. word_vowel_count = 0;
  1952. word_stressed_count = 0;
  1953. if(end_phonemes != NULL)
  1954. end_phonemes[0] = 0;
  1955. while(((c = *p) != ' ') && (c != 0))
  1956. {
  1957. if(IsAlpha(wc))
  1958. any_alpha = wc;
  1959. wc_prev = wc;
  1960. wc_bytes = utf8_in(&wc,p,0);
  1961. n = groups2_count[c];
  1962. if(IsDigit(wc) && ((langopts.tone_numbers == 0) || !any_alpha))
  1963. {
  1964. // lookup the number in *_list not *_rules
  1965. char string[8];
  1966. char buf[40];
  1967. string[0] = '_';
  1968. memcpy(&string[1],p,wc_bytes);
  1969. string[1+wc_bytes] = 0;
  1970. Lookup(string,buf);
  1971. if(++digit_count >= 2)
  1972. {
  1973. strcat(buf,str_pause);
  1974. digit_count=0;
  1975. }
  1976. AppendPhonemes(phonemes,ph_size,buf);
  1977. p += wc_bytes;
  1978. continue;
  1979. // MatchRule(&p, "", groups1[(unsigned char)'9'],&match1,word_flags);
  1980. // if(match1.points == 0)
  1981. // p++; // not found, move on past this digit
  1982. }
  1983. else
  1984. {
  1985. digit_count = 0;
  1986. found = 0;
  1987. if(n > 0)
  1988. {
  1989. /* there are some 2 byte chains for this initial letter */
  1990. c2 = p[1];
  1991. c12 = c + (c2 << 8); /* 2 characters */
  1992. g1 = groups2_start[c];
  1993. for(g=g1; g < (g1+n); g++)
  1994. {
  1995. if(groups2_name[g] == c12)
  1996. {
  1997. found = 1;
  1998. group_name[0] = c;
  1999. group_name[1] = c2;
  2000. group_name[2] = 0;
  2001. p2 = p;
  2002. MatchRule(&p2, group_name, groups2[g], &match2, word_flags, dict_flags);
  2003. if(match2.points > 0)
  2004. match2.points += 35; /* to acount for 2 letters matching */
  2005. /* now see whether single letter chain gives a better match ? */
  2006. group_name[1] = 0;
  2007. MatchRule(&p, group_name, groups1[c], &match1, word_flags, dict_flags);
  2008. if(match2.points >= match1.points)
  2009. {
  2010. // use match from the 2-letter group
  2011. memcpy(&match1,&match2,sizeof(MatchRecord));
  2012. p = p2;
  2013. }
  2014. }
  2015. }
  2016. }
  2017. if(!found)
  2018. {
  2019. /* alphabetic, single letter chain */
  2020. group_name[0] = c;
  2021. group_name[1] = 0;
  2022. if(groups1[c] != NULL)
  2023. MatchRule(&p, group_name, groups1[c], &match1, word_flags, dict_flags);
  2024. else
  2025. {
  2026. // no group for this letter, use default group
  2027. MatchRule(&p, "", groups1[0], &match1, word_flags, dict_flags);
  2028. if(match1.points == 0)
  2029. {
  2030. // no match, try removing the accent and re-translating the word
  2031. n = utf8_in(&letter,p-1,0)-1;
  2032. if((letter >= 0xc0) && (letter <= 0x241))
  2033. {
  2034. // within range of the remove_accent table
  2035. p2 = p-1;
  2036. p[-1] = remove_accent[letter-0xc0];
  2037. while((p[0] = p[n]) != ' ') p++;
  2038. while(n-- > 0) *p++ = ' '; // replacement character must be no longer than original
  2039. if(langopts.param[LOPT_DIERESES] && (lookupwchar(diereses_list,letter) > 0))
  2040. {
  2041. // vowel with dieresis, replace and continue from this point
  2042. p = p2;
  2043. continue;
  2044. }
  2045. phonemes[0] = 0; // delete any phonemes which have been produced so far
  2046. p = p_start;
  2047. word_vowel_count = 0;
  2048. word_stressed_count = 0;
  2049. continue; // start again at the beginning of the word
  2050. }
  2051. else
  2052. if((letter >= 0x3200) && (letter < 0xa700) && (end_phonemes != NULL))
  2053. {
  2054. // ideograms
  2055. // outside the range of the accent table, speak the unknown symbol sound
  2056. Lookup("_??",ph_buf);
  2057. match1.phonemes = ph_buf;
  2058. match1.points = 1;
  2059. p += (wc_bytes-1);
  2060. }
  2061. }
  2062. }
  2063. }
  2064. }
  2065. if(match1.phonemes == NULL)
  2066. match1.phonemes = "";
  2067. if(match1.points > 0)
  2068. {
  2069. if((match1.phonemes[0] == phonSWITCH) && ((word_flags & FLAG_DONT_SWITCH_TRANSLATOR)==0))
  2070. {
  2071. // an instruction to switch language, return immediately so we can re-translate
  2072. strcpy(phonemes,match1.phonemes);
  2073. return(0);
  2074. }
  2075. if((match1.end_type != 0) && (end_phonemes != NULL))
  2076. {
  2077. /* a standard ending has been found, re-translate the word without it */
  2078. if((match1.end_type & SUFX_P) && (word_flags & FLAG_NO_PREFIX))
  2079. {
  2080. // ignore the match on a prefix
  2081. }
  2082. else
  2083. {
  2084. if((match1.end_type & SUFX_P) && ((match1.end_type & 0x7f) == 0))
  2085. {
  2086. // no prefix length specified
  2087. match1.end_type |= p - p_start;
  2088. }
  2089. strcpy(end_phonemes,match1.phonemes);
  2090. memcpy(p_start,word_copy,strlen(word_copy));
  2091. return(match1.end_type);
  2092. }
  2093. }
  2094. if(match1.del_fwd != NULL)
  2095. *match1.del_fwd = REPLACED_E;
  2096. AppendPhonemes(phonemes,ph_size,match1.phonemes);
  2097. }
  2098. }
  2099. // any language specific changes ?
  2100. ApplySpecialAttribute(phonemes,dict_flags);
  2101. memcpy(p_start,word_copy,strlen(word_copy));
  2102. return(0);
  2103. } /* end of TranslateRules */
  2104. void Translator::ApplySpecialAttribute(char *phonemes, int dict_flags)
  2105. {//===================================================================
  2106. // Amend the translated phonemes according to an attribute which is specific for the language.
  2107. int len;
  2108. int ix;
  2109. char *p_end;
  2110. int phoneme_1;
  2111. if((dict_flags & (FLAG_ALT_TRANS | FLAG_ALT2_TRANS)) == 0)
  2112. return;
  2113. len = strlen(phonemes);
  2114. p_end = &phonemes[len-1];
  2115. switch(translator_name)
  2116. {
  2117. case L('d','e'):
  2118. if(p_end[0] == LookupPh("i:"))
  2119. {
  2120. // words ends in ['i:], change to [=I@]
  2121. p_end[-1] = phonSTRESS_PREV;
  2122. p_end[0] = LookupPh("I");
  2123. p_end[1] = phonSCHWA;
  2124. p_end[2] = 0;
  2125. }
  2126. break;
  2127. case L('p','t'):
  2128. phoneme_1 = LookupPh("o");
  2129. for(ix=0; ix<(len-1); ix++)
  2130. {
  2131. if(phonemes[ix] == phoneme_1)
  2132. {
  2133. phonemes[ix] = LookupPh("O");
  2134. break;
  2135. }
  2136. }
  2137. break;
  2138. case L('r','o'):
  2139. if(p_end[0] == LookupPh("j"))
  2140. {
  2141. // word end in [j], change to ['i]
  2142. p_end[0] = phonSTRESS_P;
  2143. p_end[1] = LookupPh("i");
  2144. p_end[2] = 0;
  2145. }
  2146. break;
  2147. }
  2148. } // end of ApplySpecialAttribute
  2149. //=============================================================================================
  2150. // Look up a word in the pronunciation dictionary list
  2151. // - exceptions which override the usual pronunciation rules, or which give a word
  2152. // special properties, such as pronounce as unstressed
  2153. //=============================================================================================
  2154. // common letter pairs, encode these as a single byte
  2155. static const short pairs_ru[] = {
  2156. 0x010c, // ла 21052 0x23
  2157. 0x010e, // на 18400
  2158. 0x0113, // та 14254
  2159. 0x0301, // ав 31083
  2160. 0x030f, // ов 13420
  2161. 0x060e, // не 21798
  2162. 0x0611, // ре 19458
  2163. 0x0903, // ви 16226
  2164. 0x0b01, // ак 14456
  2165. 0x0b0f, // ок 17836
  2166. 0x0c01, // ал 13324
  2167. 0x0c09, // ил 16877
  2168. 0x0e01, // ан 15359
  2169. 0x0e06, // ен 13543 0x30
  2170. 0x0e09, // ин 17168
  2171. 0x0e0e, // нн 15973
  2172. 0x0e0f, // он 22373
  2173. 0x0e1c, // ын 15052
  2174. 0x0f03, // во 24947
  2175. 0x0f11, // ро 13552
  2176. 0x0f12, // со 16368
  2177. 0x100f, // оп 19054
  2178. 0x1011, // рп 17067
  2179. 0x1101, // ар 23967
  2180. 0x1106, // ер 18795
  2181. 0x1109, // ир 13797
  2182. 0x110f, // ор 21737
  2183. 0x1213, // тс 25076
  2184. 0x1220, // яс 14310
  2185. 0x7fff};
  2186. //0x040f ог 12976
  2187. //0x1306 ет 12826
  2188. //0x0f0d мо 12688
  2189. int TransposeAlphabet(char *text, int offset, int min, int max)
  2190. {//============================================================
  2191. // transpose cyrillic alphabet (for example) into ascii (single byte) character codes
  2192. // return: number of bytes, bit 6: 1=used compression
  2193. int c;
  2194. int c2;
  2195. int ix;
  2196. char *p = text;
  2197. char *p2 = text;
  2198. int all_alpha=1;
  2199. int bits;
  2200. int acc;
  2201. do {
  2202. p += utf8_in(&c,p,0);
  2203. if((c >= min) && (c <= max))
  2204. {
  2205. *p2++ = c - offset;
  2206. }
  2207. else
  2208. if(c != 0)
  2209. {
  2210. p2 += utf8_out(c,p2);
  2211. all_alpha=0;
  2212. }
  2213. } while (c != 0);
  2214. *p2 = 0;
  2215. if(all_alpha)
  2216. {
  2217. // compress to 6 bits per character
  2218. acc=0;
  2219. bits=0;
  2220. p = text;
  2221. p2 = text;
  2222. while((c = *p++) != 0)
  2223. {
  2224. c2 = c + (*p << 8);
  2225. for(ix=0; c2 >= pairs_ru[ix]; ix++)
  2226. {
  2227. if(c2 == pairs_ru[ix])
  2228. {
  2229. // found an encoding for a 2-character pair
  2230. c = ix + 0x23; // 2-character codes start at 0x23
  2231. p++;
  2232. break;
  2233. }
  2234. }
  2235. acc = (acc << 6) + (c & 0x3f);
  2236. bits += 6;
  2237. if(bits >= 8)
  2238. {
  2239. bits -= 8;
  2240. *p2++ = (acc >> bits);
  2241. }
  2242. }
  2243. if(bits > 0)
  2244. {
  2245. *p2++ = (acc << (8-bits));
  2246. }
  2247. *p2 = 0;
  2248. return((p2 - text) | 0x40); // bit 6 indicates compressed characters
  2249. }
  2250. return(p2 - text);
  2251. } // end of TransposeAlphabet
  2252. char *print_dflags(unsigned int *flags)
  2253. {//====================================
  2254. static char buf[20];
  2255. sprintf(buf,"%s 0x%x/%x",LookupMnem(mnem_flags,(flags[0] & 0xf)+0x40), flags[0], flags[1]);
  2256. return(buf);
  2257. }
  2258. const char *Translator::LookupDict2(const char *word, const char *word2, char *phonetic, unsigned int *flags, int end_flags, WORD_TAB *wtab)
  2259. //==========================================================================================================================================
  2260. /* Find an entry in the word_dict file for a specified word.
  2261. Returns NULL if no match, else returns 'word_end'
  2262. word zero terminated word to match
  2263. word2 pointer to next word(s) in the input text (terminated by space)
  2264. flags: returns dictionary flags which are associated with a matched word
  2265. end_flags: indicates whether this is a retranslation after removing a suffix
  2266. */
  2267. {
  2268. char *p;
  2269. char *next;
  2270. int hash;
  2271. int phoneme_len;
  2272. int wlen;
  2273. unsigned char flag;
  2274. unsigned int dictionary_flags;
  2275. unsigned int dictionary_flags2;
  2276. int condition_failed=0;
  2277. int n_chars;
  2278. int no_phonemes;
  2279. int skipwords;
  2280. int ix;
  2281. const char *word_end;
  2282. const char *word1;
  2283. char word_buf[N_WORD_BYTES];
  2284. word1 = word;
  2285. if(transpose_offset > 0)
  2286. {
  2287. strcpy(word_buf,word);
  2288. wlen = TransposeAlphabet(word_buf, transpose_offset, transpose_min, transpose_max);
  2289. word = word_buf;
  2290. }
  2291. else
  2292. {
  2293. wlen = strlen(word);
  2294. }
  2295. hash = HashDictionary(word);
  2296. p = dict_hashtab[hash];
  2297. if(p == NULL)
  2298. {
  2299. if(flags != NULL)
  2300. *flags = 0;
  2301. return(0);
  2302. }
  2303. // Find the first entry in the list for this hash value which matches.
  2304. // This corresponds to the last matching entry in the *_list file.
  2305. while(*p != 0)
  2306. {
  2307. next = p + p[0];
  2308. if(((p[1] & 0x7f) != wlen) || (memcmp(word,&p[2],wlen & 0x3f) != 0))
  2309. {
  2310. // bit 6 of wlen indicates whether the word has been compressed; so we need to match on this also.
  2311. p = next;
  2312. continue;
  2313. }
  2314. /* found matching entry. Decode the phonetic string */
  2315. word_end = word2;
  2316. dictionary_flags = 0;
  2317. dictionary_flags2 = 0;
  2318. no_phonemes = p[1] & 0x80;
  2319. p += ((p[1] & 0x3f) + 2);
  2320. if(no_phonemes)
  2321. {
  2322. phonetic[0] = 0;
  2323. phoneme_len = 0;
  2324. }
  2325. else
  2326. {
  2327. strcpy(phonetic,p);
  2328. phoneme_len = strlen(p);
  2329. p += (phoneme_len + 1);
  2330. }
  2331. while(p < next)
  2332. {
  2333. // examine the flags which follow the phoneme string
  2334. flag = *p++;
  2335. if(flag >= 100)
  2336. {
  2337. // conditional rule
  2338. if(flag >= 132)
  2339. {
  2340. // fail if this condition is set
  2341. if((dict_condition & (1 << (flag-132))) != 0)
  2342. condition_failed = 1;
  2343. }
  2344. else
  2345. {
  2346. // allow only if this condition is set
  2347. if((dict_condition & (1 << (flag-100))) == 0)
  2348. condition_failed = 1;
  2349. }
  2350. }
  2351. else
  2352. if(flag > 80)
  2353. {
  2354. // flags 81 to 90 match more than one word
  2355. // This comes after the other flags
  2356. n_chars = next - p;
  2357. skipwords = flag - 80;
  2358. // don't use the contraction if any of the words are emphasized
  2359. for(ix=0; ix <= skipwords; ix++)
  2360. {
  2361. if((wtab != NULL) && (wtab[ix].flags & FLAG_EMPHASIZED))
  2362. {
  2363. condition_failed = 1;
  2364. }
  2365. }
  2366. if(memcmp(word2,p,n_chars) != 0)
  2367. condition_failed = 1;
  2368. if(condition_failed)
  2369. {
  2370. p = next;
  2371. break;
  2372. }
  2373. dictionary_flags |= FLAG_SKIPWORDS;
  2374. dictionary_skipwords = skipwords;
  2375. p = next;
  2376. word_end = word2 + n_chars;
  2377. }
  2378. else
  2379. if(flag > 64)
  2380. {
  2381. // stressed syllable information, put in bits 0-3
  2382. dictionary_flags = (dictionary_flags & ~0xf) | (flag & 0xf);
  2383. if((flag & 0xc) == 0xc)
  2384. dictionary_flags |= FLAG_STRESS_END;
  2385. }
  2386. else
  2387. if(flag >= 32)
  2388. {
  2389. dictionary_flags2 |= (1L << (flag-32));
  2390. }
  2391. else
  2392. {
  2393. dictionary_flags |= (1L << flag);
  2394. }
  2395. }
  2396. if(condition_failed)
  2397. {
  2398. condition_failed=0;
  2399. continue;
  2400. }
  2401. if((end_flags & FLAG_SUFX)==0)
  2402. {
  2403. // no suffix has been removed
  2404. if(dictionary_flags & FLAG_STEM)
  2405. continue; // this word must have a suffix
  2406. }
  2407. if((end_flags & SUFX_P) && (dictionary_flags & (FLAG_ONLY | FLAG_ONLY_S)))
  2408. continue; // $only or $onlys, don't match if a prefix has been removed
  2409. if(end_flags & FLAG_SUFX)
  2410. {
  2411. // a suffix was removed from the word
  2412. if(dictionary_flags & FLAG_ONLY)
  2413. continue; // no match if any suffix
  2414. if((dictionary_flags & FLAG_ONLY_S) && ((end_flags & FLAG_SUFX_S)==0))
  2415. {
  2416. // only a 's' suffix allowed, but the suffix wasn't 's'
  2417. continue;
  2418. }
  2419. }
  2420. if(dictionary_flags2 & FLAG_CAPITAL)
  2421. {
  2422. if(!(word_flags & FLAG_FIRST_UPPER))
  2423. {
  2424. continue;
  2425. }
  2426. }
  2427. if(dictionary_flags2 & FLAG_ALLCAPS)
  2428. {
  2429. if(!(word_flags & FLAG_ALL_UPPER))
  2430. {
  2431. continue;
  2432. }
  2433. }
  2434. if((dictionary_flags & FLAG_ATEND) && (word_end < clause_end))
  2435. {
  2436. // only use this pronunciation if it's the last word of the clause
  2437. continue;
  2438. }
  2439. if(dictionary_flags2 & FLAG_VERB)
  2440. {
  2441. // this is a verb-form pronunciation
  2442. if(expect_verb || (expect_verb_s && (end_flags & FLAG_SUFX_S)))
  2443. {
  2444. // OK, we are expecting a verb
  2445. }
  2446. else
  2447. {
  2448. /* don't use the 'verb' pronunciation unless we are
  2449. expecting a verb */
  2450. continue;
  2451. }
  2452. }
  2453. if(dictionary_flags2 & FLAG_PAST)
  2454. {
  2455. if(!expect_past)
  2456. {
  2457. /* don't use the 'past' pronunciation unless we are
  2458. expecting past tense */
  2459. continue;
  2460. }
  2461. }
  2462. if(dictionary_flags2 & FLAG_NOUN)
  2463. {
  2464. if(!expect_noun)
  2465. {
  2466. /* don't use the 'noun' pronunciation unless we are
  2467. expecting a noun */
  2468. continue;
  2469. }
  2470. }
  2471. if(flags != NULL)
  2472. {
  2473. flags[0] = dictionary_flags | FLAG_FOUND_ATTRIBUTES;
  2474. flags[1] = dictionary_flags2;
  2475. }
  2476. if(phoneme_len == 0)
  2477. {
  2478. if(option_phonemes == 2)
  2479. {
  2480. fprintf(f_trans,"Flags: %s %s\n",word1,print_dflags(flags));
  2481. }
  2482. return(0); // no phoneme translation found here, only flags. So use rules
  2483. }
  2484. if(flags != NULL)
  2485. flags[0] |= FLAG_FOUND; // this flag indicates word was found in dictionary
  2486. if(option_phonemes == 2)
  2487. {
  2488. unsigned int flags1 = 0;
  2489. char ph_decoded[N_WORD_PHONEMES];
  2490. int textmode;
  2491. DecodePhonemes(phonetic,ph_decoded);
  2492. if(flags != NULL)
  2493. flags1 = flags[0];
  2494. if((dictionary_flags & FLAG_TEXTMODE) == 0)
  2495. textmode = 0;
  2496. else
  2497. textmode = 1;
  2498. if(textmode == translator->langopts.textmode)
  2499. {
  2500. // only show this line if the word translates to phonemes, not replacement text
  2501. fprintf(f_trans,"Found: %s [%s] %s\n",word1,ph_decoded,print_dflags(flags));
  2502. }
  2503. }
  2504. return(word_end);
  2505. }
  2506. return(0);
  2507. } // end of LookupDict2
  2508. int Translator::LookupDictList(char **wordptr, char *ph_out, unsigned int *flags, int end_flags, WORD_TAB *wtab)
  2509. //==============================================================================================================
  2510. /* Lookup a specified word in the word dictionary.
  2511. Returns phonetic data in 'phonetic' and bits in 'flags'
  2512. end_flags: indicates if a suffix has been removed
  2513. */
  2514. {
  2515. int length;
  2516. const char *found;
  2517. const char *word1;
  2518. const char *word2;
  2519. unsigned char c;
  2520. int nbytes;
  2521. int len;
  2522. char word[N_WORD_BYTES];
  2523. static char word_replacement[N_WORD_BYTES];
  2524. length = 0;
  2525. word2 = word1 = *wordptr;
  2526. while((word2[nbytes = utf8_nbytes(word2)]==' ') && (word2[nbytes+1]=='.'))
  2527. {
  2528. // look for an abbreviation of the form a.b.c
  2529. // try removing the spaces between the dots and looking for a match
  2530. memcpy(&word[length],word2,nbytes);
  2531. length += nbytes;
  2532. word[length++] = '.';
  2533. word2 += nbytes+3;
  2534. }
  2535. if(length > 0)
  2536. {
  2537. // found an abbreviation containing dots
  2538. nbytes = utf8_nbytes(word2);
  2539. memcpy(&word[length],word2,nbytes);
  2540. word[length+nbytes] = 0;
  2541. found = LookupDict2(word, word2, ph_out, flags, end_flags, wtab);
  2542. if(found)
  2543. {
  2544. // set the skip words flag
  2545. flags[0] |= FLAG_SKIPWORDS;
  2546. dictionary_skipwords = length;
  2547. return(1);
  2548. }
  2549. }
  2550. for(length=0; length<N_WORD_BYTES; length++)
  2551. {
  2552. if(((c = *word1++)==0) || (c == ' '))
  2553. break;
  2554. word[length] = c;
  2555. }
  2556. word[length] = 0;
  2557. found = LookupDict2(word, word1, ph_out, flags, end_flags, wtab);
  2558. if(found == 0)
  2559. {
  2560. ph_out[0] = 0;
  2561. // try modifications to find a recognised word
  2562. if((end_flags & FLAG_SUFX_E_ADDED) && (word[length-1] == 'e'))
  2563. {
  2564. // try removing an 'e' which has been added by RemoveEnding
  2565. word[length-1] = 0;
  2566. found = LookupDict2(word, word1, ph_out, flags, end_flags, wtab);
  2567. }
  2568. else
  2569. if((end_flags & SUFX_D) && (word[length-1] == word[length-2]))
  2570. {
  2571. // try removing a double letter
  2572. word[length-1] = 0;
  2573. found = LookupDict2(word, word1, ph_out, flags, end_flags, wtab);
  2574. }
  2575. }
  2576. if(found)
  2577. {
  2578. // if textmode is the default, then words which have phonemes are marked.
  2579. if(langopts.textmode)
  2580. *flags ^= FLAG_TEXTMODE;
  2581. if(*flags & FLAG_TEXTMODE)
  2582. {
  2583. // the word translates to replacement text, not to phonemes
  2584. if(end_flags & FLAG_ALLOW_TEXTMODE)
  2585. {
  2586. // only use replacement text if this is the original word, not if a prefix or suffix has been removed
  2587. word_replacement[0] = 0;
  2588. word_replacement[1] = ' ';
  2589. strcpy(&word_replacement[2],ph_out); // replacement word, preceded by zerochar and space
  2590. word1 = *wordptr;
  2591. *wordptr = &word_replacement[2];
  2592. if(option_phonemes == 2)
  2593. {
  2594. len = found - word1;
  2595. memcpy(word,word1,len); // include multiple matching words
  2596. word[len] = 0;
  2597. fprintf(f_trans,"Replace: %s %s\n",word,*wordptr);
  2598. }
  2599. }
  2600. ph_out[0] = 0;
  2601. return(0);
  2602. }
  2603. return(1);
  2604. }
  2605. ph_out[0] = 0;
  2606. return(0);
  2607. } // end of LookupDictList
  2608. int Translator::Lookup(const char *word, char *ph_out)
  2609. {//===================================================
  2610. unsigned int flags[2];
  2611. char *word1 = (char *)word;
  2612. return(LookupDictList(&word1, ph_out, flags, 0, NULL));
  2613. }
  2614. int Translator::RemoveEnding(char *word, int end_type, char *word_copy)
  2615. {//====================================================================
  2616. /* Removes a standard suffix from a word, once it has been indicated by the dictionary rules.
  2617. end_type: bits 0-6 number of letters
  2618. bits 8-14 suffix flags
  2619. word_copy: make a copy of the original word
  2620. This routine is language specific. In English it deals with reversing y->i and e-dropping
  2621. that were done when the suffix was added to the original word.
  2622. */
  2623. int i;
  2624. char *word_end;
  2625. int len_ending;
  2626. int end_flags;
  2627. const char *p;
  2628. int len;
  2629. static char ending[12];
  2630. // these lists are language specific, but are only relevent if the 'e' suffix flag is used
  2631. static const char *add_e_exceptions[] = {
  2632. "ion", NULL };
  2633. static const char *add_e_additions[] = {
  2634. "c", "rs", "ir", "ur", "ath", "ns", "lu", NULL };
  2635. for(word_end = word; *word_end != ' '; word_end++)
  2636. {
  2637. /* replace discarded 'e's */
  2638. if(*word_end == REPLACED_E)
  2639. *word_end = 'e';
  2640. }
  2641. i = word_end - word;
  2642. memcpy(word_copy,word,i);
  2643. word_copy[i] = 0;
  2644. // look for multibyte characters to increase the number of bytes to remove
  2645. for(len_ending = i = (end_type & 0xf); i>0 ;i--) // num.of characters of the suffix
  2646. {
  2647. word_end--;
  2648. while((*word_end & 0xc0) == 0x80)
  2649. {
  2650. word_end--; // for multibyte characters
  2651. len_ending++;
  2652. }
  2653. }
  2654. // remove bytes from the end of the word and replace them by spaces
  2655. for(i=0; i<len_ending; i++)
  2656. {
  2657. ending[i] = word_end[i];
  2658. word_end[i] = ' ';
  2659. }
  2660. ending[i] = 0;
  2661. word_end--; /* now pointing at last character of stem */
  2662. end_flags = (end_type & 0xfff0) | FLAG_SUFX;
  2663. /* add an 'e' to the stem if appropriate,
  2664. if stem ends in vowel+consonant
  2665. or stem ends in 'c' (add 'e' to soften it) */
  2666. if(end_type & SUFX_I)
  2667. {
  2668. if(word_end[0] == 'i')
  2669. word_end[0] = 'y';
  2670. }
  2671. if(end_type & SUFX_E)
  2672. {
  2673. // add 'e' to end of stem
  2674. if(IsLetter(word_end[-1],LETTERGP_VOWEL2) && IsLetter(word_end[0],1))
  2675. {
  2676. // vowel(incl.'y') + hard.consonant
  2677. for(i=0; (p = add_e_exceptions[i]) != NULL; i++)
  2678. {
  2679. len = strlen(p);
  2680. if(memcmp(p,&word_end[1-len],len)==0)
  2681. {
  2682. break;
  2683. }
  2684. }
  2685. if(p == NULL)
  2686. end_flags |= FLAG_SUFX_E_ADDED; // no exception found
  2687. }
  2688. else
  2689. {
  2690. for(i=0; (p = add_e_additions[i]) != NULL; i++)
  2691. {
  2692. len = strlen(p);
  2693. if(memcmp(p,&word_end[1-len],len)==0)
  2694. {
  2695. end_flags |= FLAG_SUFX_E_ADDED;
  2696. break;
  2697. }
  2698. }
  2699. }
  2700. if(end_flags & FLAG_SUFX_E_ADDED)
  2701. {
  2702. word_end[1] = 'e';
  2703. #ifdef LOG_TRANSLATE
  2704. if(option_phonemes == 2)
  2705. {
  2706. fprintf(f_trans,"add e\n");
  2707. }
  2708. #endif
  2709. }
  2710. }
  2711. if((end_type & SUFX_V) && (expect_verb==0))
  2712. expect_verb = 1; // this suffix indicates the verb pronunciation
  2713. if((strcmp(ending,"s")==0) || (strcmp(ending,"es")==0))
  2714. end_flags |= FLAG_SUFX_S;
  2715. if(strcmp(ending,"'s")==0)
  2716. end_flags &= ~FLAG_SUFX; // don't consider 's as an added suffix
  2717. return(end_flags);
  2718. } /* end of RemoveEnding */