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

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