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

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