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

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