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

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