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

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