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

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