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

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