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

dictionary.c 84KB

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