eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dictionary.cpp 86KB

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