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.c 90KB

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