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

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