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