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

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