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

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