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

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