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

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