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

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