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

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