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

dictionary.c 87KB

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