dictionary.cpp 87 KB

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