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

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