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

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