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

dictionary.c 84KB

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