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

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