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

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