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

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