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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111
  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 dictionary: '%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 != 0)
  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)
  614. {//======================================================================================================================================
  615. unsigned char phcode;
  616. PHONEME_TAB *ph;
  617. unsigned char *ph_out = phonemes;
  618. int count = 1;
  619. int max_stress = 0;
  620. int ix;
  621. int j;
  622. int stress = 0;
  623. int primary_posn = 0;
  624. vowel_stress[0] = 0;
  625. while((phcode = *phonemes++) != 0)
  626. {
  627. if((ph = phoneme_tab[phcode]) == NULL)
  628. continue;
  629. if((ph->type == phSTRESS) && (ph->spect == 0))
  630. {
  631. /* stress marker, use this for the following vowel */
  632. if(phcode == phonSTRESS_PREV)
  633. {
  634. /* primary stress on preceeding vowel */
  635. j = count - 1;
  636. while((j > 0) && (stressed_syllable == 0) && (vowel_stress[j] < 4))
  637. {
  638. if(vowel_stress[j] != 1)
  639. {
  640. // don't promote a phoneme which must be unstressed
  641. vowel_stress[j] = 4;
  642. if(max_stress < 4)
  643. {
  644. max_stress = 4;
  645. primary_posn = j;
  646. }
  647. /* reduce any preceding primary stress markers */
  648. for(ix=1; ix<j; ix++)
  649. {
  650. if(vowel_stress[ix] == 4)
  651. vowel_stress[ix] = 3;
  652. }
  653. break;
  654. }
  655. j--;
  656. }
  657. }
  658. else
  659. {
  660. if((ph->std_length < 4) || (stressed_syllable == 0))
  661. {
  662. stress = ph->std_length;
  663. if(stress > max_stress)
  664. max_stress = stress;
  665. }
  666. }
  667. continue;
  668. }
  669. if((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC))
  670. {
  671. vowel_stress[count] = (char)stress;
  672. if((stress >= 4) && (stress >= max_stress))
  673. {
  674. primary_posn = count;
  675. max_stress = stress;
  676. }
  677. if((stress == 0) && (ph->phflags & phUNSTRESSED))
  678. vowel_stress[count] = 1; /* weak vowel, must be unstressed */
  679. count++;
  680. stress = 0;
  681. }
  682. else
  683. if(phcode == phonSYLLABIC)
  684. {
  685. // previous consonant phoneme is syllablic
  686. vowel_stress[count] = (char)stress;
  687. if(stress == 0)
  688. vowel_stress[count++] = 1; // syllabic consonant, usually unstressed
  689. }
  690. *ph_out++ = phcode;
  691. }
  692. vowel_stress[count] = 0;
  693. *ph_out = 0;
  694. /* has the position of the primary stress been specified by $1, $2, etc? */
  695. if(stressed_syllable > 0)
  696. {
  697. if(stressed_syllable >= count)
  698. stressed_syllable = count-1; // the final syllable
  699. vowel_stress[stressed_syllable] = 4;
  700. max_stress = 4;
  701. primary_posn = stressed_syllable;
  702. }
  703. if(max_stress == 5)
  704. {
  705. // priority stress, replaces any other primary stress marker
  706. for(ix=1; ix<count; ix++)
  707. {
  708. if(vowel_stress[ix] == 4)
  709. {
  710. if(tr->langopts.stress_flags & 0x20000)
  711. vowel_stress[ix] = 0;
  712. else
  713. vowel_stress[ix] = 3;
  714. }
  715. if(vowel_stress[ix] == 5)
  716. {
  717. vowel_stress[ix] = 4;
  718. primary_posn = ix;
  719. }
  720. }
  721. max_stress = 4;
  722. }
  723. stressed_syllable = primary_posn;
  724. vowel_count = count;
  725. return(max_stress);
  726. } // end of GetVowelStress
  727. static char stress_phonemes[] = {phonSTRESS_U, phonSTRESS_D, phonSTRESS_2, phonSTRESS_3,
  728. phonSTRESS_P, phonSTRESS_TONIC, phonSTRESS_TONIC};
  729. void ChangeWordStress(Translator *tr, char *word, int new_stress)
  730. {//==============================================================
  731. int ix;
  732. unsigned char *p;
  733. int max_stress;
  734. int vowel_count; // num of vowels + 1
  735. int stressed_syllable=0; // position of stressed syllable
  736. unsigned char phonetic[N_WORD_PHONEMES];
  737. unsigned char vowel_stress[N_WORD_PHONEMES/2];
  738. strcpy((char *)phonetic,word);
  739. max_stress = GetVowelStress(tr, phonetic,vowel_stress,vowel_count,stressed_syllable);
  740. if(new_stress >= 4)
  741. {
  742. // promote to primary stress
  743. for(ix=1; ix<vowel_count; ix++)
  744. {
  745. if(vowel_stress[ix] == max_stress)
  746. {
  747. vowel_stress[ix] = new_stress;
  748. break;
  749. }
  750. }
  751. }
  752. else
  753. {
  754. // remove primary stress
  755. for(ix=1; ix<vowel_count; ix++)
  756. {
  757. if(vowel_stress[ix] > new_stress)
  758. vowel_stress[ix] = new_stress;
  759. }
  760. }
  761. // write out phonemes
  762. ix = 1;
  763. p = phonetic;
  764. while(*p != 0)
  765. {
  766. if((phoneme_tab[*p]->type == phVOWEL) && !(phoneme_tab[*p]->phflags & phNONSYLLABIC))
  767. {
  768. if(vowel_stress[ix] != 0)
  769. *word++ = stress_phonemes[vowel_stress[ix]];
  770. ix++;
  771. }
  772. *word++ = *p++;
  773. }
  774. *word = 0;
  775. } // end of ChangeWordStress
  776. void Translator::SetWordStress(char *output, unsigned int dictionary_flags, int tonic, int prev_stress)
  777. {//===================================================================================================
  778. /* Guess stress pattern of word. This is language specific
  779. 'dictionary_flags' has bits 0-3 position of stressed vowel (if > 0)
  780. or unstressed (if == 7) or syllables 1 and 2 (if == 6)
  781. bits 8... dictionary flags
  782. If 'tonic' is set (>= 0), replace highest stress by this value.
  783. Parameter used for input and output
  784. */
  785. unsigned char phcode;
  786. unsigned char *p;
  787. PHONEME_TAB *ph;
  788. int stress;
  789. int max_stress;
  790. int vowel_count; // num of vowels + 1
  791. int ix;
  792. int v;
  793. int v_stress;
  794. int stressed_syllable; // position of stressed syllable
  795. int max_stress_posn;
  796. int unstressed_word = 0;
  797. char *max_output;
  798. int final_ph;
  799. int mnem;
  800. int post_tonic;
  801. int opt_length;
  802. int done;
  803. unsigned char vowel_stress[N_WORD_PHONEMES/2];
  804. char syllable_type[N_WORD_PHONEMES/2];
  805. unsigned char phonetic[N_WORD_PHONEMES];
  806. static char consonant_types[16] = {0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0};
  807. /* stress numbers STRESS_BASE +
  808. 0 diminished, unstressed within a word
  809. 1 unstressed, weak
  810. 2
  811. 3 secondary stress
  812. 4 main stress */
  813. /* copy input string into internal buffer */
  814. for(ix=0; ix<N_WORD_PHONEMES; ix++)
  815. {
  816. phonetic[ix] = output[ix];
  817. // check for unknown phoneme codes
  818. if(phonetic[ix] >= n_phoneme_tab)
  819. phonetic[ix] = phonSCHWA;
  820. if(phonetic[ix] == 0)
  821. break;
  822. }
  823. if(ix == 0) return;
  824. final_ph = phonetic[ix-1];
  825. max_output = output + (N_WORD_PHONEMES-3); /* check for overrun */
  826. // any stress position marked in the xx_list dictionary ?
  827. stressed_syllable = dictionary_flags & 0x7;
  828. if(dictionary_flags & 0x8)
  829. {
  830. // this indicates a word without a primary stress
  831. stressed_syllable = dictionary_flags & 0x3;
  832. unstressed_word = 1;
  833. }
  834. max_stress = GetVowelStress(this,phonetic,vowel_stress,vowel_count,stressed_syllable);
  835. // heavy or light syllables
  836. ix = 1;
  837. for(p = phonetic; *p != 0; p++)
  838. {
  839. if((phoneme_tab[p[0]]->type == phVOWEL) && !(phoneme_tab[p[0]]->phflags & phNONSYLLABIC))
  840. {
  841. int weight = 0;
  842. int lengthened = 0;
  843. if(phoneme_tab[p[1]]->code == phonLENGTHEN)
  844. lengthened = 1;
  845. if(lengthened || (phoneme_tab[p[0]]->phflags & phLONG))
  846. {
  847. // long vowel, increase syllable weight
  848. weight++;
  849. }
  850. if(lengthened) p++; // advance over phonLENGTHEN
  851. if(consonant_types[phoneme_tab[p[1]]->type] && ((phoneme_tab[p[2]]->type != phVOWEL) || (phoneme_tab[p[1]]->phflags & phLONG)))
  852. {
  853. // followed by two consonants, a long consonant, or consonant and end-of-word
  854. weight++;
  855. }
  856. syllable_type[ix] = weight;
  857. ix++;
  858. }
  859. }
  860. switch(langopts.stress_rule)
  861. {
  862. case 2:
  863. // a language with stress on penultimate vowel
  864. if(stressed_syllable == 0)
  865. {
  866. /* no explicit stress - stress the penultimate vowel */
  867. max_stress = 4;
  868. if(vowel_count > 2)
  869. {
  870. stressed_syllable = vowel_count - 2;
  871. if(langopts.stress_flags & 0x300)
  872. {
  873. // LANG=Spanish, stress on last vowel if the word ends in a consonant other than 'n' or 's'
  874. if(phoneme_tab[final_ph]->type != phVOWEL)
  875. {
  876. if(langopts.stress_flags & 0x100)
  877. {
  878. stressed_syllable = vowel_count - 1;
  879. }
  880. else
  881. {
  882. mnem = phoneme_tab[final_ph]->mnemonic;
  883. if((mnem != 'n') && (mnem != 's'))
  884. {
  885. stressed_syllable = vowel_count - 1;
  886. }
  887. }
  888. }
  889. }
  890. if(vowel_stress[stressed_syllable] == 1)
  891. {
  892. // but this vowel is explicitly marked as unstressed
  893. if(stressed_syllable > 1)
  894. stressed_syllable--;
  895. else
  896. stressed_syllable++;
  897. }
  898. }
  899. else
  900. {
  901. stressed_syllable = 1;
  902. if(langopts.stress_flags & 0x1)
  903. max_stress = 3; // don't give full stress to monosyllables
  904. }
  905. // only set the stress if it's not already marked explicitly
  906. if(vowel_stress[stressed_syllable] == 0)
  907. {
  908. // don't stress if next and prev syllables are stressed
  909. if((vowel_stress[stressed_syllable-1] < 4) || (vowel_stress[stressed_syllable+1] < 4))
  910. vowel_stress[stressed_syllable] = max_stress;
  911. }
  912. }
  913. break;
  914. case 3:
  915. // stress on last vowel
  916. if(stressed_syllable == 0)
  917. {
  918. /* no explicit stress - stress the final vowel */
  919. stressed_syllable = vowel_count - 1;
  920. if(max_stress == 0)
  921. {
  922. vowel_stress[stressed_syllable] = 4;
  923. }
  924. max_stress = 4;
  925. }
  926. break;
  927. case 4: // stress on antipenultimate vowel
  928. if(stressed_syllable == 0)
  929. {
  930. stressed_syllable = vowel_count - 3;
  931. if(stressed_syllable < 1)
  932. stressed_syllable = 1;
  933. if(max_stress == 0)
  934. {
  935. vowel_stress[stressed_syllable] = 4;
  936. }
  937. max_stress = 4;
  938. }
  939. break;
  940. case 5:
  941. // LANG=Russian
  942. if(stressed_syllable == 0)
  943. {
  944. /* no explicit stress - guess the stress from the number of syllables */
  945. static char guess_ru[16] = {0,0,1,1,2,3,3,4,5,6,7,7,8,9,10,11};
  946. 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
  947. 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
  948. stressed_syllable = vowel_count - 3;
  949. if(vowel_count < 16)
  950. {
  951. if(phoneme_tab[final_ph]->type == phVOWEL)
  952. stressed_syllable = guess_ru_v[vowel_count];
  953. else
  954. if(phoneme_tab[final_ph]->type == phSTOP)
  955. stressed_syllable = guess_ru_t[vowel_count];
  956. else
  957. stressed_syllable = guess_ru[vowel_count];
  958. }
  959. vowel_stress[stressed_syllable] = 4;
  960. max_stress = 4;
  961. }
  962. break;
  963. case 6: // LANG=hi stress on the last heaviest syllable
  964. if(stressed_syllable == 0)
  965. {
  966. int wt;
  967. int max_weight = -1;
  968. int min_weight = 10;
  969. int prev_stressed;
  970. for(ix = 1; ix < vowel_count; ix++)
  971. {
  972. if(vowel_stress[ix] == 0)
  973. {
  974. if((wt = syllable_type[ix]) < min_weight)
  975. min_weight = wt;
  976. if(wt >= max_weight)
  977. {
  978. max_weight = wt;
  979. prev_stressed = stressed_syllable;
  980. stressed_syllable = ix;
  981. }
  982. }
  983. }
  984. if(max_weight > min_weight)
  985. {
  986. // different weights, don't stress the final syllable
  987. if((stressed_syllable == vowel_count-1) && (syllable_type[vowel_count-2] > 0))
  988. {
  989. stressed_syllable = vowel_count-2;
  990. }
  991. }
  992. vowel_stress[stressed_syllable] = 4;
  993. max_stress = 4;
  994. }
  995. break;
  996. case 9: // mark all as stressed
  997. for(ix=1; ix<vowel_count; ix++)
  998. {
  999. if(vowel_stress[ix] == 0)
  1000. vowel_stress[ix] = 4;
  1001. }
  1002. break;
  1003. }
  1004. /* now guess the complete stress pattern */
  1005. if(max_stress < 4)
  1006. stress = 4; /* no primary stress marked, use for 1st syllable */
  1007. else
  1008. stress = 3;
  1009. if((langopts.stress_flags & 0x1000) && (vowel_count == 2))
  1010. {
  1011. // Two syllable word, if one syllable has primary stress, then give the other secondary stress
  1012. if(vowel_stress[1] == 4)
  1013. vowel_stress[2] = 3;
  1014. if(vowel_stress[2] == 4)
  1015. vowel_stress[1] = 3;
  1016. }
  1017. #if deleted
  1018. if((langopts.stress_flags & 0x2000) && (vowel_stress[1] == 0))
  1019. {
  1020. // If there is only one syllable before the primary stress, give it a secondary stress
  1021. if((vowel_count > 2) && (vowel_stress[2] >= 4))
  1022. {
  1023. vowel_stress[1] = 3;
  1024. }
  1025. }
  1026. #endif
  1027. done = 0;
  1028. for(v=1; v<vowel_count; v++)
  1029. {
  1030. if(vowel_stress[v] == 0)
  1031. {
  1032. if((langopts.stress_flags & 0x10) && (stress < 4) && (v == vowel_count-1))
  1033. {
  1034. // flag: don't give secondary stress to final vowel
  1035. }
  1036. else
  1037. if((langopts.stress_flags & 0x8000) && (done == 0))
  1038. {
  1039. vowel_stress[v] = (char)stress;
  1040. done =1;
  1041. stress = 3; /* use secondary stress for remaining syllables */
  1042. }
  1043. else
  1044. if((vowel_stress[v-1] <= 1) && (vowel_stress[v+1] <= 1))
  1045. {
  1046. /* trochaic: give stress to vowel surrounded by unstressed vowels */
  1047. if((stress == 3) && (langopts.stress_flags & 0x20))
  1048. continue; // don't use secondary stress
  1049. if((v > 1) && (langopts.stress_flags & 0x40) && (syllable_type[v]==0) && (syllable_type[v+1]>0))
  1050. {
  1051. // don't put secondary stress on a light syllable which is followed by a heavy syllable
  1052. continue;
  1053. }
  1054. // should start with secondary stress on the first syllable, or should it count back from
  1055. // the primary stress and put secondary stress on alternate syllables?
  1056. vowel_stress[v] = (char)stress;
  1057. done =1;
  1058. stress = 3; /* use secondary stress for remaining syllables */
  1059. }
  1060. }
  1061. }
  1062. if((unstressed_word) && (tonic < 0))
  1063. {
  1064. if(vowel_count <= 2)
  1065. tonic = langopts.unstressed_wd1; /* monosyllable - unstressed */
  1066. else
  1067. tonic = langopts.unstressed_wd2; /* more than one syllable, used secondary stress as the main stress */
  1068. }
  1069. max_stress = 0;
  1070. max_stress_posn = 0;
  1071. for(v=1; v<vowel_count; v++)
  1072. {
  1073. if(vowel_stress[v] >= max_stress)
  1074. {
  1075. max_stress = vowel_stress[v];
  1076. max_stress_posn = v;
  1077. }
  1078. }
  1079. if(tonic >= 0)
  1080. {
  1081. /* find position of highest stress, and replace it by 'tonic' */
  1082. /* don't disturb an explicitly set stress by 'unstress-at-end' flag */
  1083. if((tonic > max_stress) || (max_stress <= 4))
  1084. vowel_stress[max_stress_posn] = (char)tonic;
  1085. max_stress = tonic;
  1086. }
  1087. /* produce output phoneme string */
  1088. p = phonetic;
  1089. v = 1;
  1090. if((ph = phoneme_tab[*p]) != NULL)
  1091. {
  1092. if(ph->type == phSTRESS)
  1093. ph = phoneme_tab[p[1]];
  1094. #ifdef deleted
  1095. int gap = langopts.word_gap & 0x700;
  1096. if((gap) && (vowel_stress[1] >= 4) && (prev_stress >= 4))
  1097. {
  1098. /* two primary stresses together, insert a short pause */
  1099. *output++ = pause_phonemes[gap >> 8];
  1100. }
  1101. else
  1102. #endif
  1103. if((langopts.vowel_pause & 0x30) && (ph->type == phVOWEL))
  1104. {
  1105. // word starts with a vowel
  1106. if((langopts.vowel_pause & 0x20) && (vowel_stress[1] >= 4))
  1107. {
  1108. *output++ = phonPAUSE_NOLINK; // not to be replaced by link
  1109. }
  1110. else
  1111. {
  1112. *output++ = phonPAUSE_VSHORT; // break, but no pause
  1113. }
  1114. }
  1115. }
  1116. p = phonetic;
  1117. post_tonic = 0;
  1118. while(((phcode = *p++) != 0) && (output < max_output))
  1119. {
  1120. if((ph = phoneme_tab[phcode]) == NULL)
  1121. continue;
  1122. // if(ph->type == phSTRESS)
  1123. // continue;
  1124. if(ph->type == phPAUSE)
  1125. {
  1126. prev_last_stress = 0;
  1127. }
  1128. else
  1129. if(((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC)) || (*p == phonSYLLABIC))
  1130. {
  1131. // a vowel, or a consonant followed by a syllabic consonant marker
  1132. v_stress = vowel_stress[v];
  1133. prev_last_stress = v_stress;
  1134. if(vowel_stress[v-1] >= max_stress)
  1135. post_tonic = 1;
  1136. if(v_stress <= 1)
  1137. {
  1138. if((v > 1) && (max_stress >= 4) && (langopts.stress_flags & 4) && (v == (vowel_count-1)))
  1139. {
  1140. // option: mark unstressed final syllable as diminished
  1141. v_stress = 1;
  1142. }
  1143. else
  1144. if((langopts.stress_flags & 2) || (v == 1) || (v == (vowel_count-1)))
  1145. {
  1146. // first or last syllable, or option 'don't set diminished stress'
  1147. v_stress = 0;
  1148. }
  1149. else
  1150. if((v == (vowel_count-2)) && (vowel_stress[vowel_count-1] <= 1))
  1151. {
  1152. // penultimate syllable, followed by an unstressed final syllable
  1153. v_stress = 0;
  1154. }
  1155. else
  1156. {
  1157. // unstressed syllable within a word
  1158. if((vowel_stress[v-1] != 1) || ((langopts.stress_flags & 0x10000) == 0))
  1159. {
  1160. v_stress = 1; /* change from 0 (unstressed) to 1 (diminished stress) */
  1161. vowel_stress[v] = v_stress;
  1162. }
  1163. }
  1164. }
  1165. if(v_stress > 0)
  1166. *output++ = stress_phonemes[v_stress]; // mark stress of all vowels except 0 (unstressed)
  1167. if(vowel_stress[v] > max_stress)
  1168. {
  1169. max_stress = vowel_stress[v];
  1170. }
  1171. if((*p == phonLENGTHEN) && ((opt_length = langopts.param[LOPT_IT_LENGTHEN]) != 0))
  1172. {
  1173. // remove lengthen indicator from non-stressed syllables
  1174. int shorten=0;
  1175. if(opt_length & 0x10)
  1176. {
  1177. // only allow lengthen indicator on the highest stress syllable in the word
  1178. if(v != max_stress_posn)
  1179. shorten = 1;
  1180. }
  1181. else
  1182. if(v_stress < 4)
  1183. {
  1184. // only allow lengthen indicator if stress >= 4.
  1185. shorten = 1;
  1186. }
  1187. if(((opt_length & 0xf)==2) && (v != (vowel_count - 2)))
  1188. shorten = 1; // LANG=Italian, remove lengthen indicator from non-penultimate syllables
  1189. if(shorten)
  1190. p++;
  1191. }
  1192. v++;
  1193. }
  1194. if(phcode != 1)
  1195. *output++ = phcode;
  1196. }
  1197. *output++ = 0;
  1198. } /* end of SetWordStress */
  1199. //=============================================================================================
  1200. // Look up a word in the pronunciation rules
  1201. //
  1202. //=============================================================================================
  1203. #ifdef LOG_TRANSLATE
  1204. char *Translator::DecodeRule(const char *group, char *rule)
  1205. {//==================================================
  1206. /* Convert compiled match template to ascii */
  1207. unsigned char rb;
  1208. unsigned char c;
  1209. char *p;
  1210. int ix;
  1211. int match_type;
  1212. int finished=0;
  1213. int value;
  1214. int flags;
  1215. int suffix_char;
  1216. int condition_num=0;
  1217. char buf[60];
  1218. char buf_pre[60];
  1219. char suffix[20];
  1220. static char output[60];
  1221. static char symbols[] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',
  1222. '@','&','%','+','#','S','D','Z','A','L',' ',' ',' ',' ',' ','N','K','V',' ','T','X','?','W'};
  1223. static char symbols_lg[] = {'A','B','C','H','F','G','Y'};
  1224. match_type = 0;
  1225. buf_pre[0] = 0;
  1226. strcpy(buf,group);
  1227. p = &buf[strlen(buf)];
  1228. while(!finished)
  1229. {
  1230. rb = *rule++;
  1231. if(rb <= 5)
  1232. {
  1233. switch(rb)
  1234. {
  1235. case 0:
  1236. case RULE_PHONEMES:
  1237. finished=1;
  1238. break;
  1239. case RULE_PRE:
  1240. match_type = RULE_PRE;
  1241. *p = 0;
  1242. p = buf_pre;
  1243. break;
  1244. case RULE_POST:
  1245. match_type = RULE_POST;
  1246. *p = 0;
  1247. strcat(buf," (");
  1248. p = &buf[strlen(buf)];
  1249. break;
  1250. case RULE_PH_COMMON:
  1251. break;
  1252. case RULE_CONDITION:
  1253. /* conditional rule, next byte gives condition number */
  1254. condition_num = *rule++;
  1255. break;
  1256. }
  1257. continue;
  1258. }
  1259. if(rb == RULE_ENDING)
  1260. {
  1261. static const char *flag_chars = "ei vtfq t";
  1262. flags = ((rule[0] & 0x7f)<< 8) + (rule[1] & 0x7f);
  1263. suffix_char = 'S';
  1264. if(flags & (SUFX_P >> 8))
  1265. suffix_char = 'P';
  1266. sprintf(suffix,"%c%d",suffix_char,rule[2] & 0x7f);
  1267. rule += 3;
  1268. for(ix=0;ix<9;ix++)
  1269. {
  1270. if(flags & 1)
  1271. sprintf(&suffix[strlen(suffix)],"%c",flag_chars[ix]);
  1272. flags = (flags >> 1);
  1273. }
  1274. strcpy(p,suffix);
  1275. p += strlen(suffix);
  1276. c = ' ';
  1277. }
  1278. else
  1279. if(rb == RULE_LETTERGP)
  1280. {
  1281. c = symbols_lg[*rule++ - 'A'];
  1282. }
  1283. else
  1284. if(rb == RULE_LETTERGP2)
  1285. {
  1286. value = *rule++ - 'A';
  1287. p[0] = 'L';
  1288. p[1] = (value / 10) + '0';
  1289. c = (value % 10) + '0';
  1290. if(match_type == RULE_PRE)
  1291. {
  1292. p[0] = c;
  1293. c = 'L';
  1294. }
  1295. p+=2;
  1296. }
  1297. else
  1298. if(rb <= RULE_LAST_RULE)
  1299. c = symbols[rb];
  1300. else
  1301. if(rb == RULE_SPACE)
  1302. c = '_';
  1303. else
  1304. c = rb;
  1305. *p++ = c;
  1306. }
  1307. *p = 0;
  1308. p = output;
  1309. if(condition_num > 0)
  1310. {
  1311. sprintf(output,"?%d ",condition_num);
  1312. p = &output[strlen(output)];
  1313. }
  1314. if((ix = strlen(buf_pre)) > 0)
  1315. {
  1316. while(--ix >= 0)
  1317. *p++ = buf_pre[ix];
  1318. *p++ = ')';
  1319. *p++ = ' ';
  1320. }
  1321. *p = 0;
  1322. strcat(p,buf);
  1323. ix = strlen(output);
  1324. while(ix < 8)
  1325. output[ix++]=' ';
  1326. output[ix]=0;
  1327. return(output);
  1328. } /* end of decode_match */
  1329. #endif
  1330. void Translator::AppendPhonemes(char *string, int size, const char *ph)
  1331. {//====================================================================
  1332. /* Add new phoneme string "ph" to "string"
  1333. Keeps count of the number of vowel phonemes in the word, and whether these
  1334. can be stressed syllables. These values can be used in translation rules
  1335. */
  1336. const char *p;
  1337. unsigned char c;
  1338. int unstress_mark;
  1339. int length;
  1340. length = strlen(ph) + strlen(string);
  1341. if(length >= size)
  1342. {
  1343. return;
  1344. }
  1345. /* any stressable vowel ? */
  1346. unstress_mark = 0;
  1347. p = ph;
  1348. while((c = *p++) != 0)
  1349. {
  1350. if(c >= n_phoneme_tab) continue;
  1351. if(phoneme_tab[c]->type == phSTRESS)
  1352. {
  1353. if(phoneme_tab[c]->std_length < 4)
  1354. unstress_mark = 1;
  1355. }
  1356. else
  1357. {
  1358. if(phoneme_tab[c]->type == phVOWEL)
  1359. {
  1360. if(((phoneme_tab[c]->phflags & phUNSTRESSED) == 0) &&
  1361. (unstress_mark == 0))
  1362. {
  1363. word_stressed_count++;
  1364. }
  1365. unstress_mark = 0;
  1366. word_vowel_count++;
  1367. }
  1368. }
  1369. }
  1370. if(string != NULL)
  1371. strcat(string,ph);
  1372. } /* end of AppendPhonemes */
  1373. void Translator::MatchRule(char *word[], const char *group, char *rule, MatchRecord *match_out, int word_flags, int dict_flags)
  1374. {//============================================================================================================================
  1375. /* Checks a specified word against dictionary rules.
  1376. Returns with phoneme code string, or NULL if no match found.
  1377. word (indirect) points to current character group within the input word
  1378. This is advanced by this procedure as characters are consumed
  1379. group: the initial characters used to choose the rules group
  1380. rule: address of dictionary rule data for this character group
  1381. match_out: returns best points score
  1382. word_flags: indicates whether this is a retranslation after a suffix has been removed
  1383. */
  1384. unsigned char rb; // current instuction from rule
  1385. unsigned char letter; // current letter from input word, single byte
  1386. int letter_w; // current letter, wide character
  1387. int letter_xbytes; // number of extra bytes of multibyte character (num bytes - 1)
  1388. unsigned char last_letter;
  1389. char *pre_ptr;
  1390. char *post_ptr; /* pointer to first character after group */
  1391. char *rule_start; /* start of current match template */
  1392. char *p;
  1393. int match_type; /* left, right, or consume */
  1394. int failed;
  1395. int consumed; /* number of letters consumed from input */
  1396. int count; /* count through rules in the group */
  1397. int syllable_count;
  1398. int vowel;
  1399. int letter_group;
  1400. int distance_right;
  1401. int distance_left;
  1402. int lg_pts;
  1403. int n_bytes;
  1404. MatchRecord match;
  1405. static MatchRecord best;
  1406. int total_consumed; /* letters consumed for best match */
  1407. int group_length;
  1408. unsigned char condition_num;
  1409. char *common_phonemes; /* common to a group of entries */
  1410. if(rule == NULL)
  1411. {
  1412. match_out->points = 0;
  1413. (*word)++;
  1414. return;
  1415. }
  1416. total_consumed = 0;
  1417. count = 0;
  1418. common_phonemes = NULL;
  1419. match_type = 0;
  1420. best.points = 0;
  1421. best.phonemes = "";
  1422. best.end_type = 0;
  1423. best.del_fwd = NULL;
  1424. group_length = strlen(group);
  1425. /* search through dictionary rules */
  1426. while(rule[0] != RULE_GROUP_END)
  1427. {
  1428. match_type=0;
  1429. consumed = 0;
  1430. letter = 0;
  1431. distance_right= -6; /* used to reduce points for matches further away the current letter */
  1432. distance_left= -2;
  1433. count++;
  1434. match.points = 1;
  1435. match.end_type = 0;
  1436. match.del_fwd = NULL;
  1437. pre_ptr = *word;
  1438. post_ptr = *word + group_length;
  1439. /* work through next rule until end, or until no-match proved */
  1440. rule_start = rule;
  1441. failed = 0;
  1442. while(!failed)
  1443. {
  1444. rb = *rule++;
  1445. if(rb <= 5)
  1446. {
  1447. switch(rb)
  1448. {
  1449. case 0: // no phoneme string for this rule, use previous common rule
  1450. if(common_phonemes != NULL)
  1451. {
  1452. match.phonemes = common_phonemes;
  1453. if(*match.phonemes == RULE_CONDITION)
  1454. match.phonemes += 2; // skip over condition number
  1455. while(((rb = *match.phonemes++) != 0) && (rb != RULE_PHONEMES));
  1456. }
  1457. else
  1458. {
  1459. match.phonemes = "";
  1460. }
  1461. rule--; // so we are still pointing at the 0
  1462. failed=2; // matched OK
  1463. break;
  1464. case RULE_PRE:
  1465. match_type = RULE_PRE;
  1466. break;
  1467. case RULE_POST:
  1468. match_type = RULE_POST;
  1469. break;
  1470. case RULE_PHONEMES:
  1471. match.phonemes = rule;
  1472. failed=2; // matched OK
  1473. break;
  1474. case RULE_PH_COMMON:
  1475. common_phonemes = rule;
  1476. break;
  1477. case RULE_CONDITION:
  1478. /* conditional rule, next byte gives condition number */
  1479. condition_num = *rule++;
  1480. if(condition_num >= 32)
  1481. {
  1482. // allow the rule only if the condition number is NOT set
  1483. if((dict_condition & (1L << (condition_num-32))) != 0)
  1484. failed = 1;
  1485. }
  1486. else
  1487. {
  1488. // allow the rule only if the condition number is set
  1489. if((dict_condition & (1L << condition_num)) == 0)
  1490. failed = 1;
  1491. }
  1492. if(!failed)
  1493. match.points++; // add one point for a matched conditional rule
  1494. break;
  1495. }
  1496. continue;
  1497. }
  1498. switch(match_type)
  1499. {
  1500. case 0:
  1501. /* match and consume this letter */
  1502. last_letter = letter;
  1503. letter = *post_ptr++;
  1504. if((letter == rb) || ((letter==(unsigned char)REPLACED_E) && (rb=='e')))
  1505. {
  1506. match.points += 21;
  1507. consumed++;
  1508. }
  1509. else
  1510. failed = 1;
  1511. break;
  1512. case RULE_POST:
  1513. /* continue moving fowards */
  1514. distance_right += 6;
  1515. if(distance_right > 18)
  1516. distance_right = 19;
  1517. last_letter = letter;
  1518. letter_xbytes = utf8_in(&letter_w,post_ptr,0)-1;
  1519. letter = *post_ptr++;
  1520. switch(rb)
  1521. {
  1522. case RULE_LETTERGP:
  1523. letter_group = *rule++ - 'A';
  1524. if(IsLetter(letter_w,letter_group))
  1525. {
  1526. lg_pts = 20;
  1527. if(letter_group==2)
  1528. lg_pts = 19; // fewer points for C, general consonant
  1529. match.points += (lg_pts-distance_right);
  1530. post_ptr += letter_xbytes;
  1531. }
  1532. else
  1533. failed = 1;
  1534. break;
  1535. case RULE_LETTERGP2: // match against a list of utf-8 strings
  1536. letter_group = *rule++ - 'A';
  1537. if((n_bytes = IsLetterGroup(post_ptr-1,letter_group,0)) >0)
  1538. {
  1539. match.points += (20-distance_right);
  1540. post_ptr += (n_bytes-1);
  1541. }
  1542. else
  1543. failed =1;
  1544. break;
  1545. case RULE_NOTVOWEL:
  1546. if(!IsLetter(letter_w,0))
  1547. {
  1548. match.points += (20-distance_right);
  1549. post_ptr += letter_xbytes;
  1550. }
  1551. else
  1552. failed = 1;
  1553. break;
  1554. case RULE_DIGIT:
  1555. if(IsDigit(letter_w))
  1556. {
  1557. match.points += (21-distance_right);
  1558. post_ptr += letter_xbytes;
  1559. }
  1560. else
  1561. if(langopts.tone_numbers)
  1562. {
  1563. // also match if there is no digit
  1564. match.points += (21-distance_right);
  1565. post_ptr--;
  1566. }
  1567. else
  1568. failed = 1;
  1569. break;
  1570. case RULE_NONALPHA:
  1571. if(!iswalpha(letter_w))
  1572. {
  1573. match.points += (21-distance_right);
  1574. post_ptr += letter_xbytes;
  1575. }
  1576. else
  1577. failed = 1;
  1578. break;
  1579. case RULE_DOUBLE:
  1580. if(letter == last_letter)
  1581. match.points += (21-distance_right);
  1582. else
  1583. failed = 1;
  1584. break;
  1585. case RULE_ALT1:
  1586. if(dict_flags & FLAG_ALT_TRANS)
  1587. match.points++;
  1588. else
  1589. failed = 1;
  1590. break;
  1591. case '-':
  1592. if((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN_AFTER)))
  1593. {
  1594. match.points += (22-distance_right); // one point more than match against space
  1595. }
  1596. else
  1597. failed = 1;
  1598. break;
  1599. case RULE_SYLLABLE:
  1600. {
  1601. /* more than specified number of vowel letters to the right */
  1602. char *p = post_ptr + letter_xbytes;
  1603. syllable_count = 1;
  1604. while(*rule == RULE_SYLLABLE)
  1605. {
  1606. rule++;
  1607. syllable_count+=1; /* number of syllables to match */
  1608. }
  1609. vowel = 0;
  1610. while(letter_w != RULE_SPACE)
  1611. {
  1612. if((vowel==0) && IsLetter(letter_w,LETTERGP_VOWEL2))
  1613. {
  1614. // this is counting vowels which are separated by non-vowels
  1615. syllable_count--;
  1616. }
  1617. vowel = IsLetter(letter_w,LETTERGP_VOWEL2);
  1618. p += utf8_in(&letter_w,p,0);
  1619. }
  1620. if(syllable_count <= 0)
  1621. match.points+= (19-distance_right);
  1622. else
  1623. failed = 1;
  1624. }
  1625. break;
  1626. case RULE_NOVOWELS:
  1627. {
  1628. char *p = post_ptr + letter_xbytes;
  1629. while(letter_w != RULE_SPACE)
  1630. {
  1631. if(IsLetter(letter_w,LETTERGP_VOWEL2))
  1632. {
  1633. failed = 1;
  1634. break;
  1635. }
  1636. p += utf8_in(&letter_w,p,0);
  1637. }
  1638. if(!failed)
  1639. match.points += (19-distance_right);
  1640. }
  1641. break;
  1642. case RULE_INC_SCORE:
  1643. match.points += 20; // force an increase in points
  1644. break;
  1645. case RULE_DEL_FWD:
  1646. // find the next 'e' in the word and replace by ''
  1647. for(p = *word + group_length; *p != ' '; p++)
  1648. {
  1649. if(*p == 'e')
  1650. {
  1651. match.del_fwd = p;
  1652. break;
  1653. }
  1654. }
  1655. break;
  1656. case RULE_ENDING:
  1657. // next 3 bytes are a (non-zero) ending type. 2 bytes of flags + suffix length
  1658. match.end_type = (rule[0] << 16) + ((rule[1] & 0x7f) << 8) + (rule[2] & 0x7f);
  1659. rule += 3;
  1660. break;
  1661. case RULE_NO_SUFFIX:
  1662. if(word_flags & FLAG_SUFFIX_REMOVED)
  1663. failed = 1; // a suffix has been removed
  1664. else
  1665. match.points++;
  1666. break;
  1667. default:
  1668. if(letter == rb)
  1669. {
  1670. if(letter == RULE_SPACE)
  1671. match.points += (21-distance_right);
  1672. else
  1673. match.points += (21-distance_right);
  1674. }
  1675. else
  1676. failed = 1;
  1677. break;
  1678. }
  1679. break;
  1680. case RULE_PRE:
  1681. /* match backwards from start of current group */
  1682. distance_left += 2;
  1683. if(distance_left > 18)
  1684. distance_left = 19;
  1685. last_letter = *pre_ptr;
  1686. pre_ptr--;
  1687. letter_xbytes = utf8_in(&letter_w,pre_ptr,1)-1;
  1688. letter = *pre_ptr;
  1689. switch(rb)
  1690. {
  1691. case RULE_LETTERGP:
  1692. letter_group = *rule++ - 'A';
  1693. if(IsLetter(letter_w,letter_group))
  1694. {
  1695. lg_pts = 20;
  1696. if(letter_group==2)
  1697. lg_pts = 19; // fewer points for C, general consonant
  1698. match.points += (lg_pts-distance_left);
  1699. pre_ptr -= letter_xbytes;
  1700. }
  1701. else
  1702. failed = 1;
  1703. break;
  1704. case RULE_LETTERGP2: // match against a list of utf-8 strings
  1705. letter_group = *rule++ - 'A';
  1706. if((n_bytes = IsLetterGroup(pre_ptr-letter_xbytes,letter_group,1)) >0)
  1707. {
  1708. match.points += (20-distance_right);
  1709. pre_ptr -= (n_bytes-1);
  1710. }
  1711. else
  1712. failed =1;
  1713. break;
  1714. case RULE_NOTVOWEL:
  1715. if(!IsLetter(letter_w,0))
  1716. {
  1717. match.points += (20-distance_left);
  1718. pre_ptr -= letter_xbytes;
  1719. }
  1720. else
  1721. failed = 1;
  1722. break;
  1723. case RULE_DOUBLE:
  1724. if(letter == last_letter)
  1725. match.points += (21-distance_left);
  1726. else
  1727. failed = 1;
  1728. break;
  1729. case RULE_DIGIT:
  1730. if(IsDigit(letter_w))
  1731. {
  1732. match.points += (21-distance_left);
  1733. pre_ptr -= letter_xbytes;
  1734. }
  1735. else
  1736. failed = 1;
  1737. break;
  1738. case RULE_NONALPHA:
  1739. if(!iswalpha(letter_w))
  1740. {
  1741. match.points += (21-distance_right);
  1742. pre_ptr -= letter_xbytes;
  1743. }
  1744. else
  1745. failed = 1;
  1746. break;
  1747. case RULE_SYLLABLE:
  1748. /* more than specified number of vowels to the left */
  1749. syllable_count = 1;
  1750. while(*rule == RULE_SYLLABLE)
  1751. {
  1752. rule++;
  1753. syllable_count++; /* number of syllables to match */
  1754. }
  1755. if(syllable_count <= word_vowel_count)
  1756. match.points+= (19-distance_left);
  1757. else
  1758. failed = 1;
  1759. break;
  1760. case RULE_STRESSED:
  1761. if(word_stressed_count > 0)
  1762. match.points += 19;
  1763. else
  1764. failed = 1;
  1765. break;
  1766. case RULE_NOVOWELS:
  1767. {
  1768. char *p = pre_ptr - letter_xbytes - 1;
  1769. while(letter_w != RULE_SPACE)
  1770. {
  1771. if(IsLetter(letter_w,LETTERGP_VOWEL2))
  1772. {
  1773. failed = 1;
  1774. break;
  1775. }
  1776. p -= utf8_in(&letter_w,p,1);
  1777. }
  1778. if(!failed)
  1779. match.points += 3;
  1780. }
  1781. break;
  1782. case RULE_IFVERB:
  1783. if(expect_verb)
  1784. match.points += 1;
  1785. else
  1786. failed = 1;
  1787. break;
  1788. case RULE_CAPITAL:
  1789. if(word_flags & FLAG_FIRST_UPPER)
  1790. match.points += 1;
  1791. else
  1792. failed = 1;
  1793. break;
  1794. case '.':
  1795. // dot in pre- section, match on any dot before this point in the word
  1796. for(p=pre_ptr; *p != ' '; p--)
  1797. {
  1798. if(*p == '.')
  1799. {
  1800. match.points +=50;
  1801. break;
  1802. }
  1803. }
  1804. if(*p == ' ')
  1805. failed = 1;
  1806. break;
  1807. case '-':
  1808. if((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN)))
  1809. {
  1810. match.points += (22-distance_right); // one point more than match against space
  1811. }
  1812. else
  1813. failed = 1;
  1814. break;
  1815. default:
  1816. if(letter == rb)
  1817. {
  1818. if(letter == RULE_SPACE)
  1819. match.points += 4;
  1820. else
  1821. match.points += (21-distance_left);
  1822. }
  1823. else
  1824. failed = 1;
  1825. break;
  1826. }
  1827. break;
  1828. }
  1829. }
  1830. if(failed == 2)
  1831. {
  1832. /* matched OK, is this better than the last best match ? */
  1833. if(match.points >= best.points)
  1834. {
  1835. memcpy(&best,&match,sizeof(match));
  1836. total_consumed = consumed;
  1837. }
  1838. #ifdef LOG_TRANSLATE
  1839. if((option_phonemes == 2) && (match.points > 0) && ((word_flags & FLAG_NO_TRACE) == 0))
  1840. {
  1841. // show each rule that matches, and it's points score
  1842. int pts;
  1843. char decoded_phonemes[80];
  1844. // note: 'count' contains the rule number, if we want to include it
  1845. pts = match.points;
  1846. if(group_length > 1)
  1847. pts += 35; // to account for an extra letter matching
  1848. DecodePhonemes(match.phonemes,decoded_phonemes);
  1849. fprintf(f_trans,"%3d %s [%s]\n",pts,DecodeRule(group,rule_start),decoded_phonemes);
  1850. }
  1851. #endif
  1852. }
  1853. /* skip phoneme string to reach start of next template */
  1854. while(*rule++ != 0);
  1855. }
  1856. #ifdef LOG_TRANSLATE
  1857. if((option_phonemes == 2) && ((word_flags & FLAG_NO_TRACE)==0))
  1858. {
  1859. if(group_length <= 1)
  1860. fprintf(f_trans,"\n");
  1861. }
  1862. #endif
  1863. /* advance input data pointer */
  1864. total_consumed += group_length;
  1865. if(total_consumed == 0)
  1866. total_consumed = 1; /* always advance over 1st letter */
  1867. *word += total_consumed;
  1868. if(best.points == 0)
  1869. best.phonemes = "";
  1870. memcpy(match_out,&best,sizeof(MatchRecord));
  1871. } /* end of MatchRule */
  1872. int Translator::TranslateRules(char *p_start, char *phonemes, int ph_size, char *end_phonemes, int word_flags, int dict_flags)
  1873. {//===========================================================================================================================
  1874. /* Translate a word bounded by space characters
  1875. Append the result to 'phonemes' and any standard prefix/suffix in 'end_phonemes' */
  1876. unsigned char c, c2;
  1877. unsigned int c12;
  1878. int wc=0;
  1879. int wc_prev;
  1880. int wc_bytes;
  1881. char *p2; /* copy of p for use in double letter chain match */
  1882. int found;
  1883. int g; /* group chain number */
  1884. int g1; /* first group for this letter */
  1885. int n;
  1886. int letter;
  1887. int any_alpha=0;
  1888. int ix;
  1889. int digit_count=0;
  1890. char *p;
  1891. MatchRecord match1;
  1892. MatchRecord match2;
  1893. char ph_buf[40];
  1894. char word_copy[N_WORD_BYTES];
  1895. static const char str_pause[2] = {phonPAUSE_NOLINK,0};
  1896. char group_name[4];
  1897. if(data_dictrules == NULL)
  1898. return(0);
  1899. for(ix=0; ix<(N_WORD_BYTES-1);)
  1900. {
  1901. c = p_start[ix];
  1902. word_copy[ix++] = c;
  1903. }
  1904. word_copy[ix] = 0;
  1905. #ifdef LOG_TRANSLATE
  1906. if((option_phonemes == 2) && ((word_flags & FLAG_NO_TRACE)==0))
  1907. {
  1908. char wordbuf[120];
  1909. int ix;
  1910. for(ix=0; ((c = p_start[ix]) != ' ') && (c != 0); ix++)
  1911. {
  1912. wordbuf[ix] = c;
  1913. }
  1914. wordbuf[ix] = 0;
  1915. fprintf(f_trans,"Translate '%s'\n",wordbuf);
  1916. }
  1917. #endif
  1918. p = p_start;
  1919. word_vowel_count = 0;
  1920. word_stressed_count = 0;
  1921. if(end_phonemes != NULL)
  1922. end_phonemes[0] = 0;
  1923. while(((c = *p) != ' ') && (c != 0))
  1924. {
  1925. if(IsAlpha(wc))
  1926. any_alpha = wc;
  1927. wc_prev = wc;
  1928. wc_bytes = utf8_in(&wc,p,0);
  1929. n = groups2_count[c];
  1930. if(IsDigit(wc) && ((langopts.tone_numbers == 0) || !any_alpha))
  1931. {
  1932. // lookup the number in *_list not *_rules
  1933. char string[8];
  1934. char buf[40];
  1935. string[0] = '_';
  1936. memcpy(&string[1],p,wc_bytes);
  1937. string[1+wc_bytes] = 0;
  1938. Lookup(string,buf);
  1939. if(++digit_count >= 2)
  1940. {
  1941. strcat(buf,str_pause);
  1942. digit_count=0;
  1943. }
  1944. AppendPhonemes(phonemes,ph_size,buf);
  1945. p += wc_bytes;
  1946. continue;
  1947. // MatchRule(&p, "", groups1[(unsigned char)'9'],&match1,word_flags);
  1948. // if(match1.points == 0)
  1949. // p++; // not found, move on past this digit
  1950. }
  1951. else
  1952. {
  1953. digit_count = 0;
  1954. found = 0;
  1955. if(n > 0)
  1956. {
  1957. /* there are some 2 byte chains for this initial letter */
  1958. c2 = p[1];
  1959. c12 = c + (c2 << 8); /* 2 characters */
  1960. g1 = groups2_start[c];
  1961. for(g=g1; g < (g1+n); g++)
  1962. {
  1963. if(groups2_name[g] == c12)
  1964. {
  1965. found = 1;
  1966. group_name[0] = c;
  1967. group_name[1] = c2;
  1968. group_name[2] = 0;
  1969. p2 = p;
  1970. MatchRule(&p2, group_name, groups2[g], &match2, word_flags, dict_flags);
  1971. if(match2.points > 0)
  1972. match2.points += 35; /* to acount for 2 letters matching */
  1973. /* now see whether single letter chain gives a better match ? */
  1974. group_name[1] = 0;
  1975. MatchRule(&p, group_name, groups1[c], &match1, word_flags, dict_flags);
  1976. if(match2.points >= match1.points)
  1977. {
  1978. // use match from the 2-letter group
  1979. memcpy(&match1,&match2,sizeof(MatchRecord));
  1980. p = p2;
  1981. }
  1982. }
  1983. }
  1984. }
  1985. if(!found)
  1986. {
  1987. /* alphabetic, single letter chain */
  1988. group_name[0] = c;
  1989. group_name[1] = 0;
  1990. if(groups1[c] != NULL)
  1991. MatchRule(&p, group_name, groups1[c], &match1, word_flags, dict_flags);
  1992. else
  1993. {
  1994. // no group for this letter, use default group
  1995. MatchRule(&p, "", groups1[0], &match1, word_flags, dict_flags);
  1996. if(match1.points == 0)
  1997. {
  1998. // no match, try removing the accent and re-translating the word
  1999. n = utf8_in(&letter,p-1,0)-1;
  2000. if((letter >= 0xc0) && (letter <= 0x241))
  2001. {
  2002. // within range of the remove_accent table
  2003. p2 = p-1;
  2004. p[-1] = remove_accent[letter-0xc0];
  2005. while((p[0] = p[n]) != ' ') p++;
  2006. while(n-- > 0) *p++ = ' '; // replacement character must be no longer than original
  2007. if(langopts.param[LOPT_DIERESES] && (lookupwchar(diereses_list,letter) > 0))
  2008. {
  2009. // vowel with dieresis, replace and continue from this point
  2010. p = p2;
  2011. continue;
  2012. }
  2013. phonemes[0] = 0; // delete any phonemes which have been produced so far
  2014. p = p_start;
  2015. word_vowel_count = 0;
  2016. word_stressed_count = 0;
  2017. continue; // start again at the beginning of the word
  2018. }
  2019. else
  2020. if((letter >= 0x3200) && (letter < 0xa700))
  2021. {
  2022. // ideograms
  2023. // outside the range of the accent table, speak the unknown symbol sound
  2024. Lookup("_??",ph_buf);
  2025. match1.phonemes = ph_buf;
  2026. match1.points = 1;
  2027. p += (wc_bytes-1);
  2028. }
  2029. }
  2030. }
  2031. }
  2032. }
  2033. if(match1.phonemes == NULL)
  2034. match1.phonemes = "";
  2035. if(match1.points > 0)
  2036. {
  2037. if((match1.phonemes[0] == phonSWITCH) && ((word_flags & FLAG_DONT_SWITCH_TRANSLATOR)==0))
  2038. {
  2039. // an instruction to switch language, return immediately so we can re-translate
  2040. strcpy(phonemes,match1.phonemes);
  2041. return(0);
  2042. }
  2043. if((match1.end_type != 0) && (end_phonemes != NULL))
  2044. {
  2045. /* a standard ending has been found, re-translate the word without it */
  2046. if((match1.end_type & SUFX_P) && (word_flags & FLAG_NO_PREFIX))
  2047. {
  2048. // ignore the match on a prefix
  2049. }
  2050. else
  2051. {
  2052. if((match1.end_type & SUFX_P) && ((match1.end_type & 0x7f) == 0))
  2053. {
  2054. // no prefix length specified
  2055. match1.end_type |= p - p_start;
  2056. }
  2057. strcpy(end_phonemes,match1.phonemes);
  2058. memcpy(p_start,word_copy,strlen(word_copy));
  2059. return(match1.end_type);
  2060. }
  2061. }
  2062. if(match1.del_fwd != NULL)
  2063. *match1.del_fwd = REPLACED_E;
  2064. AppendPhonemes(phonemes,ph_size,match1.phonemes);
  2065. }
  2066. }
  2067. // any language specific changes ?
  2068. ApplySpecialAttribute(phonemes,dict_flags);
  2069. memcpy(p_start,word_copy,strlen(word_copy));
  2070. return(0);
  2071. } /* end of TranslateRules */
  2072. void Translator::ApplySpecialAttribute(char *phonemes, int dict_flags)
  2073. {//===================================================================
  2074. // Amend the translated phonemes according to an attribute which is specific for the language.
  2075. int len;
  2076. int ix;
  2077. char *p_end;
  2078. int phoneme_1;
  2079. if((dict_flags & (FLAG_ALT_TRANS | FLAG_ALT2_TRANS)) == 0)
  2080. return;
  2081. len = strlen(phonemes);
  2082. p_end = &phonemes[len-1];
  2083. switch(translator_name)
  2084. {
  2085. case L('d','e'):
  2086. if(p_end[0] == LookupPh("i:"))
  2087. {
  2088. // words ends in ['i:], change to [=I@]
  2089. p_end[-1] = phonSTRESS_PREV;
  2090. p_end[0] = LookupPh("I");
  2091. p_end[1] = phonSCHWA;
  2092. p_end[2] = 0;
  2093. }
  2094. break;
  2095. case L('p','t'):
  2096. phoneme_1 = LookupPh("o");
  2097. for(ix=0; ix<(len-1); ix++)
  2098. {
  2099. if(phonemes[ix] == phoneme_1)
  2100. {
  2101. phonemes[ix] = LookupPh("O");
  2102. break;
  2103. }
  2104. }
  2105. break;
  2106. case L('r','o'):
  2107. if(p_end[0] == LookupPh("j"))
  2108. {
  2109. // word end in [j], change to ['i]
  2110. p_end[0] = phonSTRESS_P;
  2111. p_end[1] = LookupPh("i");
  2112. p_end[2] = 0;
  2113. }
  2114. break;
  2115. }
  2116. } // end of ApplySpecialAttribute
  2117. //=============================================================================================
  2118. // Look up a word in the pronunciation dictionary list
  2119. // - exceptions which override the usual pronunciation rules, or which give a word
  2120. // special properties, such as pronounce as unstressed
  2121. //=============================================================================================
  2122. // common letter pairs, encode these as a single byte
  2123. static const short pairs_ru[] = {
  2124. 0x010c, // ла 21052 0x23
  2125. 0x010e, // на 18400
  2126. 0x0113, // та 14254
  2127. 0x0301, // ав 31083
  2128. 0x030f, // ов 13420
  2129. 0x060e, // не 21798
  2130. 0x0611, // ре 19458
  2131. 0x0903, // ви 16226
  2132. 0x0b01, // ак 14456
  2133. 0x0b0f, // ок 17836
  2134. 0x0c01, // ал 13324
  2135. 0x0c09, // ил 16877
  2136. 0x0e01, // ан 15359
  2137. 0x0e06, // ен 13543 0x30
  2138. 0x0e09, // ин 17168
  2139. 0x0e0e, // нн 15973
  2140. 0x0e0f, // он 22373
  2141. 0x0e1c, // ын 15052
  2142. 0x0f03, // во 24947
  2143. 0x0f11, // ро 13552
  2144. 0x0f12, // со 16368
  2145. 0x100f, // оп 19054
  2146. 0x1011, // рп 17067
  2147. 0x1101, // ар 23967
  2148. 0x1106, // ер 18795
  2149. 0x1109, // ир 13797
  2150. 0x110f, // ор 21737
  2151. 0x1213, // тс 25076
  2152. 0x1220, // яс 14310
  2153. 0x7fff};
  2154. //0x040f ог 12976
  2155. //0x1306 ет 12826
  2156. //0x0f0d мо 12688
  2157. int TransposeAlphabet(char *text, int offset, int min, int max)
  2158. {//============================================================
  2159. // transpose cyrillic alphabet (for example) into ascii (single byte) character codes
  2160. // return: number of bytes, bit 6: 1=used compression
  2161. int c;
  2162. int c2;
  2163. int ix;
  2164. char *p = text;
  2165. char *p2 = text;
  2166. int all_alpha=1;
  2167. int bits;
  2168. int acc;
  2169. do {
  2170. p += utf8_in(&c,p,0);
  2171. if((c >= min) && (c <= max))
  2172. {
  2173. *p2++ = c - offset;
  2174. }
  2175. else
  2176. if(c != 0)
  2177. {
  2178. p2 += utf8_out(c,p2);
  2179. all_alpha=0;
  2180. }
  2181. } while (c != 0);
  2182. *p2 = 0;
  2183. if(all_alpha)
  2184. {
  2185. // compress to 6 bits per character
  2186. acc=0;
  2187. bits=0;
  2188. p = text;
  2189. p2 = text;
  2190. while((c = *p++) != 0)
  2191. {
  2192. c2 = c + (*p << 8);
  2193. for(ix=0; c2 >= pairs_ru[ix]; ix++)
  2194. {
  2195. if(c2 == pairs_ru[ix])
  2196. {
  2197. // found an encoding for a 2-character pair
  2198. c = ix + 0x23; // 2-character codes start at 0x23
  2199. p++;
  2200. break;
  2201. }
  2202. }
  2203. acc = (acc << 6) + (c & 0x3f);
  2204. bits += 6;
  2205. if(bits >= 8)
  2206. {
  2207. bits -= 8;
  2208. *p2++ = (acc >> bits);
  2209. }
  2210. }
  2211. if(bits > 0)
  2212. {
  2213. *p2++ = (acc << (8-bits));
  2214. }
  2215. *p2 = 0;
  2216. return((p2 - text) | 0x40); // bit 6 indicates compressed characters
  2217. }
  2218. return(p2 - text);
  2219. } // end of TransposeAlphabet
  2220. char *print_dflags(int flags)
  2221. {//==========================
  2222. static char buf[20];
  2223. sprintf(buf,"%s 0x%x",LookupMnem(mnem_flags,(flags & 0xf)+0x40), flags);
  2224. return(buf);
  2225. }
  2226. const char *Translator::LookupDict2(const char *word, const char *word2, char *phonetic, unsigned int *flags, int end_flags)
  2227. //====================================================================================================================
  2228. /* Find an entry in the word_dict file for a specified word.
  2229. Returns NULL if no match, else returns 'word_end'
  2230. word zero terminated word to match
  2231. word2 pointer to next word(s) in the input text (terminated by space)
  2232. flags: returns dictionary flags which are associated with a matched word
  2233. end_flags: indicates whether this is a retranslation after removing a suffix
  2234. */
  2235. {
  2236. char *p;
  2237. char *next;
  2238. int hash;
  2239. int phoneme_len;
  2240. int wlen;
  2241. unsigned char flag;
  2242. unsigned int dictionary_flags;
  2243. int condition_failed=0;
  2244. int n_chars;
  2245. int no_phonemes;
  2246. const char *word_end;
  2247. const char *word1;
  2248. char word_buf[N_WORD_BYTES];
  2249. word1 = word;
  2250. if(transpose_offset > 0)
  2251. {
  2252. strcpy(word_buf,word);
  2253. wlen = TransposeAlphabet(word_buf, transpose_offset, transpose_min, transpose_max);
  2254. word = word_buf;
  2255. }
  2256. else
  2257. {
  2258. wlen = strlen(word);
  2259. }
  2260. hash = HashDictionary(word);
  2261. p = dict_hashtab[hash];
  2262. if(p == NULL)
  2263. {
  2264. if(flags != NULL)
  2265. *flags = 0;
  2266. return(0);
  2267. }
  2268. // Find the first entry in the list for this hash value which matches.
  2269. // This corresponds to the last matching entry in the *_list file.
  2270. while(*p != 0)
  2271. {
  2272. next = p + p[0];
  2273. if(((p[1] & 0x7f) != wlen) || (memcmp(word,&p[2],wlen & 0x3f) != 0))
  2274. {
  2275. // bit 6 of wlen indicates whether the word has been compressed; so we need to match on this also.
  2276. p = next;
  2277. continue;
  2278. }
  2279. /* found matching entry. Decode the phonetic string */
  2280. word_end = word2;
  2281. dictionary_flags = 0;
  2282. no_phonemes = p[1] & 0x80;
  2283. p += ((p[1] & 0x3f) + 2);
  2284. if(no_phonemes)
  2285. {
  2286. phonetic[0] = 0;
  2287. phoneme_len = 0;
  2288. }
  2289. else
  2290. {
  2291. strcpy(phonetic,p);
  2292. phoneme_len = strlen(p);
  2293. p += (phoneme_len + 1);
  2294. }
  2295. while(p < next)
  2296. {
  2297. // examine the flags which follow the phoneme string
  2298. flag = *p++;
  2299. if(flag >= 100)
  2300. {
  2301. // conditional rule
  2302. if(flag >= 132)
  2303. {
  2304. // fail if this condition is set
  2305. if((dict_condition & (1 << (flag-132))) != 0)
  2306. condition_failed = 1;
  2307. }
  2308. else
  2309. {
  2310. // allow only if this condition is set
  2311. if((dict_condition & (1 << (flag-100))) == 0)
  2312. condition_failed = 1;
  2313. }
  2314. }
  2315. else
  2316. if(flag > 64)
  2317. {
  2318. // stressed syllable information, put in bits 0-3
  2319. dictionary_flags = (dictionary_flags & ~0xf) | (flag & 0xf);
  2320. if((flag & 0xc) == 0xc)
  2321. dictionary_flags |= FLAG_STRESS_END;
  2322. }
  2323. else
  2324. if(flag > 40)
  2325. {
  2326. // flags 41 to 50 match more than one word
  2327. // This comes after the other flags
  2328. n_chars = next - p;
  2329. if(memcmp(word2,p,n_chars)==0)
  2330. {
  2331. dictionary_flags |= FLAG_SKIPWORDS;
  2332. dictionary_skipwords = (flag - 40);
  2333. p = next;
  2334. word_end = word2 + n_chars;
  2335. }
  2336. else
  2337. {
  2338. p = next;
  2339. condition_failed = 1;
  2340. break;
  2341. }
  2342. }
  2343. else
  2344. {
  2345. dictionary_flags |= (1L << flag);
  2346. }
  2347. }
  2348. if(condition_failed)
  2349. {
  2350. condition_failed=0;
  2351. continue;
  2352. }
  2353. if((end_flags & FLAG_SUFX)==0)
  2354. {
  2355. // no suffix has been removed
  2356. if(dictionary_flags & FLAG_STEM)
  2357. continue; // this word must have a suffix
  2358. }
  2359. if((end_flags & SUFX_P) && (dictionary_flags & (FLAG_ONLY | FLAG_ONLY_S)))
  2360. continue; // $only or $onlys, don't match if a prefix has been removed
  2361. if(end_flags & FLAG_SUFX)
  2362. {
  2363. // a suffix was removed from the word
  2364. if(dictionary_flags & FLAG_ONLY)
  2365. continue; // no match if any suffix
  2366. if((dictionary_flags & FLAG_ONLY_S) && ((end_flags & FLAG_SUFX_S)==0))
  2367. {
  2368. // only a 's' suffix allowed, but the suffix wasn't 's'
  2369. continue;
  2370. }
  2371. }
  2372. if(dictionary_flags & FLAG_CAPITAL)
  2373. {
  2374. if(!(word_flags & FLAG_FIRST_UPPER))
  2375. {
  2376. continue;
  2377. }
  2378. }
  2379. if((dictionary_flags & FLAG_ATEND) && (word_end < clause_end))
  2380. {
  2381. // only use this pronunciation if it's the last word of the clause
  2382. continue;
  2383. }
  2384. if(dictionary_flags & FLAG_VERB)
  2385. {
  2386. // this is a verb-form pronunciation
  2387. if(expect_verb || (expect_verb_s && (end_flags & FLAG_SUFX_S)))
  2388. {
  2389. // OK, we are expecting a verb
  2390. }
  2391. else
  2392. {
  2393. /* don't use the 'verb' pronunciation unless we are
  2394. expecting a verb */
  2395. continue;
  2396. }
  2397. }
  2398. if(dictionary_flags & FLAG_PAST)
  2399. {
  2400. if(!expect_past)
  2401. {
  2402. /* don't use the 'past' pronunciation unless we are
  2403. expecting past tense */
  2404. continue;
  2405. }
  2406. }
  2407. if(flags != NULL)
  2408. *flags = dictionary_flags;
  2409. if(phoneme_len == 0)
  2410. {
  2411. if(option_phonemes == 2)
  2412. {
  2413. fprintf(f_trans,"Flags: %s %s\n",word1,print_dflags(*flags));
  2414. }
  2415. return(0); // no phoneme translation found here, only flags. So use rules
  2416. }
  2417. if(flags != NULL)
  2418. *flags |= FLAG_FOUND; // this flag indicates word was found in dictionary
  2419. if(option_phonemes == 2)
  2420. {
  2421. unsigned int flags1 = 0;
  2422. char ph_decoded[N_WORD_PHONEMES];
  2423. int textmode;
  2424. DecodePhonemes(phonetic,ph_decoded);
  2425. if(flags != NULL)
  2426. flags1 = *flags;
  2427. if((dictionary_flags & FLAG_TEXTMODE) == 0)
  2428. textmode = 0;
  2429. else
  2430. textmode = 1;
  2431. if(textmode == translator->langopts.textmode)
  2432. {
  2433. // only show this line if the word translates to phonemes, not replacement text
  2434. fprintf(f_trans,"Found: %s [%s] %s\n",word1,ph_decoded,print_dflags(flags1));
  2435. }
  2436. }
  2437. return(word_end);
  2438. }
  2439. return(0);
  2440. } // end of LookupDict2
  2441. int Translator::LookupDictList(char **wordptr, char *ph_out, unsigned int *flags, int end_flags)
  2442. //==============================================================================================
  2443. /* Lookup a specified word in the word dictionary.
  2444. Returns phonetic data in 'phonetic' and bits in 'flags'
  2445. end_flags: indicates if a suffix has been removed
  2446. */
  2447. {
  2448. int length;
  2449. const char *found;
  2450. const char *word1;
  2451. const char *word2;
  2452. unsigned char c;
  2453. int nbytes;
  2454. int c2;
  2455. int len;
  2456. char word[N_WORD_BYTES];
  2457. static char word_replacement[N_WORD_BYTES];
  2458. length = 0;
  2459. word2 = word1 = *wordptr;
  2460. while((word2[nbytes = utf8_in(&c2,word2,0)]==' ') && (word2[nbytes+1]=='.'))
  2461. {
  2462. // look for an abbreviation of the form a.b.c
  2463. // try removing the spaces between the dots and looking for a match
  2464. memcpy(&word[length],word2,nbytes);
  2465. length += nbytes;
  2466. word[length++] = '.';
  2467. word2 += nbytes+3;
  2468. }
  2469. if(length > 0)
  2470. {
  2471. // found an abbreviation containing dots
  2472. nbytes = utf8_in(&c2,word2,0);
  2473. memcpy(&word[length],word2,nbytes);
  2474. word[length+nbytes] = 0;
  2475. found = LookupDict2(word,word2,ph_out,flags,end_flags);
  2476. if(found)
  2477. {
  2478. *flags = *flags & ~(7 << 5) | (length << 5);
  2479. return(1);
  2480. }
  2481. }
  2482. for(length=0; length<N_WORD_BYTES; length++)
  2483. {
  2484. if(((c = *word1++)==0) || (c == ' '))
  2485. break;
  2486. word[length] = c;
  2487. }
  2488. word[length] = 0;
  2489. found = LookupDict2(word,word1,ph_out,flags,end_flags);
  2490. if(found == 0)
  2491. {
  2492. ph_out[0] = 0;
  2493. // try modifications to find a recognised word
  2494. if((end_flags & FLAG_SUFX_E_ADDED) && (word[length-1] == 'e'))
  2495. {
  2496. // try removing an 'e' which has been added by RemoveEnding
  2497. word[length-1] = 0;
  2498. found = LookupDict2(word,word1,ph_out,flags,end_flags);
  2499. }
  2500. else
  2501. if((end_flags & SUFX_D) && (word[length-1] == word[length-2]))
  2502. {
  2503. // try removing a double letter
  2504. word[length-1] = 0;
  2505. found = LookupDict2(word,word1,ph_out,flags,end_flags);
  2506. }
  2507. }
  2508. if(found)
  2509. {
  2510. // if textmode is the default, then words which have phonemes are marked.
  2511. if(langopts.textmode)
  2512. *flags ^= FLAG_TEXTMODE;
  2513. if(*flags & FLAG_TEXTMODE)
  2514. {
  2515. // the word translates to replacement text, not to phonemes
  2516. if(end_flags & FLAG_ALLOW_TEXTMODE)
  2517. {
  2518. // only use replacement text if this is the original word, not if a prefix or suffix has been removed
  2519. word_replacement[0] = 0;
  2520. word_replacement[1] = ' ';
  2521. strcpy(&word_replacement[2],ph_out); // replacement word, preceded by zerochar and space
  2522. word1 = *wordptr;
  2523. *wordptr = &word_replacement[2];
  2524. if(option_phonemes == 2)
  2525. {
  2526. len = found - word1;
  2527. memcpy(word,word1,len); // include multiple matching words
  2528. fprintf(f_trans,"Replace: %s %s\n",word,*wordptr);
  2529. }
  2530. }
  2531. ph_out[0] = 0;
  2532. return(0);
  2533. }
  2534. return(1);
  2535. }
  2536. ph_out[0] = 0;
  2537. return(0);
  2538. } // end of LookupDictList
  2539. int Translator::Lookup(const char *word, char *ph_out)
  2540. {//===================================================
  2541. unsigned int flags;
  2542. char *word1 = (char *)word;
  2543. return(LookupDictList(&word1,ph_out,&flags,0));
  2544. }
  2545. int Translator::RemoveEnding(char *word, int end_type, char *word_copy)
  2546. {//====================================================================
  2547. /* Removes a standard suffix from a word, once it has been indicated by the dictionary rules.
  2548. end_type: bits 0-6 number of letters
  2549. bits 8-14 suffix flags
  2550. word_copy: make a copy of the original word
  2551. This routine is language specific. In English it deals with reversing y->i and e-dropping
  2552. that were done when the suffix was added to the original word.
  2553. */
  2554. int i;
  2555. char *word_end;
  2556. int len_ending;
  2557. int end_flags;
  2558. const char *p;
  2559. int len;
  2560. static char ending[12];
  2561. // these lists are language specific, but are only relevent if the 'e' suffix flag is used
  2562. static const char *add_e_exceptions[] = {
  2563. "ion", NULL };
  2564. static const char *add_e_additions[] = {
  2565. "c", "rs", "ir", "ur", "ath", "ns", "lu", NULL };
  2566. for(word_end = word; *word_end != ' '; word_end++)
  2567. {
  2568. /* replace discarded 'e's */
  2569. if(*word_end == REPLACED_E)
  2570. *word_end = 'e';
  2571. }
  2572. i = word_end - word;
  2573. memcpy(word_copy,word,i);
  2574. word_copy[i] = 0;
  2575. // look for multibyte characters to increase the number of bytes to remove
  2576. for(len_ending = i = (end_type & 0xf); i>0 ;i--) // num.of characters of the suffix
  2577. {
  2578. word_end--;
  2579. while((*word_end & 0xc0) == 0x80)
  2580. {
  2581. word_end--; // for multibyte characters
  2582. len_ending++;
  2583. }
  2584. }
  2585. // remove bytes from the end of the word and replace them by spaces
  2586. for(i=0; i<len_ending; i++)
  2587. {
  2588. ending[i] = word_end[i];
  2589. word_end[i] = ' ';
  2590. }
  2591. ending[i] = 0;
  2592. word_end--; /* now pointing at last character of stem */
  2593. end_flags = (end_type & 0xfff0) | FLAG_SUFX;
  2594. /* add an 'e' to the stem if appropriate,
  2595. if stem ends in vowel+consonant
  2596. or stem ends in 'c' (add 'e' to soften it) */
  2597. if(end_type & SUFX_I)
  2598. {
  2599. if(word_end[0] == 'i')
  2600. word_end[0] = 'y';
  2601. }
  2602. if(end_type & SUFX_E)
  2603. {
  2604. // add 'e' to end of stem
  2605. if(IsLetter(word_end[-1],LETTERGP_VOWEL2) && IsLetter(word_end[0],1))
  2606. {
  2607. // vowel(incl.'y') + hard.consonant
  2608. for(i=0; (p = add_e_exceptions[i]) != NULL; i++)
  2609. {
  2610. len = strlen(p);
  2611. if(memcmp(p,&word_end[1-len],len)==0)
  2612. {
  2613. break;
  2614. }
  2615. }
  2616. if(p == NULL)
  2617. end_flags |= FLAG_SUFX_E_ADDED; // no exception found
  2618. }
  2619. else
  2620. {
  2621. for(i=0; (p = add_e_additions[i]) != NULL; i++)
  2622. {
  2623. len = strlen(p);
  2624. if(memcmp(p,&word_end[1-len],len)==0)
  2625. {
  2626. end_flags |= FLAG_SUFX_E_ADDED;
  2627. break;
  2628. }
  2629. }
  2630. }
  2631. if(end_flags & FLAG_SUFX_E_ADDED)
  2632. {
  2633. word_end[1] = 'e';
  2634. #ifdef LOG_TRANSLATE
  2635. if(option_phonemes == 2)
  2636. {
  2637. fprintf(f_trans,"add e\n");
  2638. }
  2639. #endif
  2640. }
  2641. }
  2642. if((end_type & SUFX_V) && (expect_verb==0))
  2643. expect_verb = 1; // this suffix indicates the verb pronunciation
  2644. if((strcmp(ending,"s")==0) || (strcmp(ending,"es")==0))
  2645. end_flags |= FLAG_SUFX_S;
  2646. if(strcmp(ending,"'s")==0)
  2647. end_flags &= ~FLAG_SUFX; // don't consider 's as an added suffix
  2648. return(end_flags);
  2649. } /* end of RemoveEnding */