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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118
  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 LetterGroupNo(char *rule)
  566. {
  567. /*
  568. * Returns number of letter group
  569. */
  570. int groupNo = *rule;
  571. groupNo = groupNo - 'A'; // substracting 'A' makes letter_group equal to number in .Lxx definition
  572. if (groupNo < 0) // fix sign if necessary
  573. groupNo += 256;
  574. return groupNo;
  575. }
  576. static int IsLetterGroup(Translator *tr, char *word, int group, int pre)
  577. {
  578. /* Match the word against a list of utf-8 strings.
  579. * returns length of matching letter group or -1
  580. *
  581. * How this works:
  582. *
  583. * +-+
  584. * |c|<-(tr->letterGroups[group])
  585. * |0|
  586. * *p->|c|<-len+ +-+
  587. * |s|<----+ |a|<-(Actual word to be tested)
  588. * |0| *word-> |t|<-*w=word-len+1 (for pre-rule)
  589. * |~| |a|<-*w=word (for post-rule)
  590. * |7| |s|
  591. * +-+ +-+
  592. *
  593. * 7=RULE_GROUP_END
  594. * 0=null terminator
  595. * pre==1 — pre-rule
  596. * pre==0 — post-rule
  597. */
  598. char *p; // group counter
  599. char *w; // word counter
  600. int len = 0;
  601. p = tr->letterGroups[group];
  602. if (p == NULL)
  603. return 0;
  604. while (*p != RULE_GROUP_END) {
  605. if (pre) {
  606. len = strlen(p);
  607. w = word - len + 1;
  608. } else
  609. w = word;
  610. // If '~' (no character) is allowed in group, return 0.
  611. if (*p == '~')
  612. return 0;
  613. // Check current group
  614. while ((*p == *w) && (*w != 0)) {
  615. w++;
  616. p++;
  617. }
  618. if (*p == 0) { // Matched the current group.
  619. if (pre)
  620. return len;
  621. return w - word;
  622. }
  623. // No match, so skip the rest of this group.
  624. while (*p++ != 0)
  625. ;
  626. }
  627. // Not found
  628. return -1;
  629. }
  630. static int IsLetter(Translator *tr, int letter, int group)
  631. {
  632. int letter2;
  633. if (tr->letter_groups[group] != NULL) {
  634. if (wcschr(tr->letter_groups[group], letter))
  635. return 1;
  636. return 0;
  637. }
  638. if (group > 7)
  639. return 0;
  640. if (tr->letter_bits_offset > 0) {
  641. if (((letter2 = (letter - tr->letter_bits_offset)) > 0) && (letter2 < 0x100))
  642. letter = letter2;
  643. else
  644. return 0;
  645. } else if ((letter >= 0xc0) && (letter < N_REMOVE_ACCENT))
  646. return tr->letter_bits[remove_accent[letter-0xc0]] & (1L << group);
  647. if ((letter >= 0) && (letter < 0x100))
  648. return tr->letter_bits[letter] & (1L << group);
  649. return 0;
  650. }
  651. int IsVowel(Translator *tr, int letter)
  652. {
  653. return IsLetter(tr, letter, LETTERGP_VOWEL2);
  654. }
  655. static int Unpronouncable2(Translator *tr, char *word)
  656. {
  657. int c;
  658. int end_flags;
  659. char ph_buf[N_WORD_PHONEMES];
  660. ph_buf[0] = 0;
  661. c = word[-1];
  662. word[-1] = ' '; // ensure there is a space before the "word"
  663. end_flags = TranslateRules(tr, word, ph_buf, sizeof(ph_buf), NULL, FLAG_UNPRON_TEST, NULL);
  664. word[-1] = c;
  665. if ((end_flags == 0) || (end_flags & SUFX_UNPRON))
  666. return 1;
  667. return 0;
  668. }
  669. int Unpronouncable(Translator *tr, char *word, int posn)
  670. {
  671. /* Determines whether a word in 'unpronouncable', i.e. whether it should
  672. be spoken as individual letters.
  673. This function may be language specific. This is a generic version.
  674. */
  675. int c;
  676. int c1 = 0;
  677. int vowel_posn = 9;
  678. int index;
  679. int count;
  680. ALPHABET *alphabet;
  681. utf8_in(&c, word);
  682. if ((tr->letter_bits_offset > 0) && (c < 0x241)) {
  683. // Latin characters for a language with a non-latin alphabet
  684. return 0; // so we can re-translate the word as English
  685. }
  686. if (((alphabet = AlphabetFromChar(c)) != NULL) && (alphabet->offset != tr->letter_bits_offset)) {
  687. // Character is not in our alphabet
  688. return 0;
  689. }
  690. if (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 1)
  691. return 0;
  692. if (((c = *word) == ' ') || (c == 0) || (c == '\''))
  693. return 0;
  694. index = 0;
  695. count = 0;
  696. for (;;) {
  697. index += utf8_in(&c, &word[index]);
  698. if ((c == 0) || (c == ' '))
  699. break;
  700. if ((c == '\'') && ((count > 1) || (posn > 0)))
  701. break; // "tv'" but not "l'"
  702. if (count == 0)
  703. c1 = c;
  704. if ((c == '\'') && (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 3)) {
  705. // don't count apostrophe
  706. } else
  707. count++;
  708. if (IsVowel(tr, c)) {
  709. vowel_posn = count; // position of the first vowel
  710. break;
  711. }
  712. if ((c != '\'') && !iswalpha2(c))
  713. return 0;
  714. }
  715. if ((vowel_posn > 2) && (tr->langopts.param[LOPT_UNPRONOUNCABLE] == 2)) {
  716. // Lookup unpronounable rules in *_rules
  717. return Unpronouncable2(tr, word);
  718. }
  719. if (c1 == tr->langopts.param[LOPT_UNPRONOUNCABLE])
  720. vowel_posn--; // disregard this as the initial letter when counting
  721. if (vowel_posn > (tr->langopts.max_initial_consonants+1))
  722. return 1; // no vowel, or no vowel in first few letters
  723. return 0;
  724. }
  725. static int GetVowelStress(Translator *tr, unsigned char *phonemes, signed char *vowel_stress, int *vowel_count, int *stressed_syllable, int control)
  726. {
  727. // control = 1, set stress to 1 for forced unstressed vowels
  728. unsigned char phcode;
  729. PHONEME_TAB *ph;
  730. unsigned char *ph_out = phonemes;
  731. int count = 1;
  732. int max_stress = -1;
  733. int ix;
  734. int j;
  735. int stress = -1;
  736. int primary_posn = 0;
  737. vowel_stress[0] = 1;
  738. while (((phcode = *phonemes++) != 0) && (count < (N_WORD_PHONEMES/2)-1)) {
  739. if ((ph = phoneme_tab[phcode]) == NULL)
  740. continue;
  741. if ((ph->type == phSTRESS) && (ph->program == 0)) {
  742. // stress marker, use this for the following vowel
  743. if (phcode == phonSTRESS_PREV) {
  744. // primary stress on preceeding vowel
  745. j = count - 1;
  746. while ((j > 0) && (*stressed_syllable == 0) && (vowel_stress[j] < 4)) {
  747. if ((vowel_stress[j] != 0) && (vowel_stress[j] != 1)) {
  748. // don't promote a phoneme which must be unstressed
  749. vowel_stress[j] = 4;
  750. if (max_stress < 4) {
  751. max_stress = 4;
  752. primary_posn = j;
  753. }
  754. /* reduce any preceding primary stress markers */
  755. for (ix = 1; ix < j; ix++) {
  756. if (vowel_stress[ix] == 4)
  757. vowel_stress[ix] = 3;
  758. }
  759. break;
  760. }
  761. j--;
  762. }
  763. } else {
  764. if ((ph->std_length < 4) || (*stressed_syllable == 0)) {
  765. stress = ph->std_length;
  766. if (stress > max_stress)
  767. max_stress = stress;
  768. }
  769. }
  770. continue;
  771. }
  772. if ((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC)) {
  773. vowel_stress[count] = (char)stress;
  774. if ((stress >= 4) && (stress >= max_stress)) {
  775. primary_posn = count;
  776. max_stress = stress;
  777. }
  778. if ((stress < 0) && (control & 1) && (ph->phflags & phUNSTRESSED))
  779. vowel_stress[count] = 1; // weak vowel, must be unstressed
  780. count++;
  781. stress = -1;
  782. } else if (phcode == phonSYLLABIC) {
  783. // previous consonant phoneme is syllablic
  784. vowel_stress[count] = (char)stress;
  785. if ((stress == 0) && (control & 1))
  786. vowel_stress[count++] = 1; // syllabic consonant, usually unstressed
  787. }
  788. *ph_out++ = phcode;
  789. }
  790. vowel_stress[count] = 1;
  791. *ph_out = 0;
  792. // has the position of the primary stress been specified by $1, $2, etc?
  793. if (*stressed_syllable > 0) {
  794. if (*stressed_syllable >= count)
  795. *stressed_syllable = count-1; // the final syllable
  796. vowel_stress[*stressed_syllable] = 4;
  797. max_stress = 4;
  798. primary_posn = *stressed_syllable;
  799. }
  800. if (max_stress == 5) {
  801. // priority stress, replaces any other primary stress marker
  802. for (ix = 1; ix < count; ix++) {
  803. if (vowel_stress[ix] == 4) {
  804. if (tr->langopts.stress_flags & S_PRIORITY_STRESS)
  805. vowel_stress[ix] = 1;
  806. else
  807. vowel_stress[ix] = 3;
  808. }
  809. if (vowel_stress[ix] == 5) {
  810. vowel_stress[ix] = 4;
  811. primary_posn = ix;
  812. }
  813. }
  814. max_stress = 4;
  815. }
  816. *stressed_syllable = primary_posn;
  817. *vowel_count = count;
  818. return max_stress;
  819. }
  820. static char stress_phonemes[] = {
  821. phonSTRESS_D, phonSTRESS_U, phonSTRESS_2, phonSTRESS_3,
  822. phonSTRESS_P, phonSTRESS_P2, phonSTRESS_TONIC
  823. };
  824. void ChangeWordStress(Translator *tr, char *word, int new_stress)
  825. {
  826. int ix;
  827. unsigned char *p;
  828. int max_stress;
  829. int vowel_count; // num of vowels + 1
  830. int stressed_syllable = 0; // position of stressed syllable
  831. unsigned char phonetic[N_WORD_PHONEMES];
  832. signed char vowel_stress[N_WORD_PHONEMES/2];
  833. strcpy((char *)phonetic, word);
  834. max_stress = GetVowelStress(tr, phonetic, vowel_stress, &vowel_count, &stressed_syllable, 0);
  835. if (new_stress >= 4) {
  836. // promote to primary stress
  837. for (ix = 1; ix < vowel_count; ix++) {
  838. if (vowel_stress[ix] >= max_stress) {
  839. vowel_stress[ix] = new_stress;
  840. break;
  841. }
  842. }
  843. } else {
  844. // remove primary stress
  845. for (ix = 1; ix < vowel_count; ix++) {
  846. if (vowel_stress[ix] > new_stress) // >= allows for diminished stress (=1)
  847. vowel_stress[ix] = new_stress;
  848. }
  849. }
  850. // write out phonemes
  851. ix = 1;
  852. p = phonetic;
  853. while (*p != 0) {
  854. if ((phoneme_tab[*p]->type == phVOWEL) && !(phoneme_tab[*p]->phflags & phNONSYLLABIC)) {
  855. if ((vowel_stress[ix] == 0) || (vowel_stress[ix] > 1))
  856. *word++ = stress_phonemes[(unsigned char)vowel_stress[ix]];
  857. ix++;
  858. }
  859. *word++ = *p++;
  860. }
  861. *word = 0;
  862. }
  863. void SetWordStress(Translator *tr, char *output, unsigned int *dictionary_flags, int tonic, int control)
  864. {
  865. /* Guess stress pattern of word. This is language specific
  866. 'output' is used for input and output
  867. 'dictionary_flags' has bits 0-3 position of stressed vowel (if > 0)
  868. or unstressed (if == 7) or syllables 1 and 2 (if == 6)
  869. bits 8... dictionary flags
  870. If 'tonic' is set (>= 0), replace highest stress by this value.
  871. control: bit 0 This is an individual symbol, not a word
  872. bit 1 Suffix phonemes are still to be added
  873. */
  874. unsigned char phcode;
  875. unsigned char *p;
  876. PHONEME_TAB *ph;
  877. int stress;
  878. int max_stress;
  879. int max_stress_input; // any stress specified in the input?
  880. int vowel_count; // num of vowels + 1
  881. int ix;
  882. int v;
  883. int v_stress;
  884. int stressed_syllable; // position of stressed syllable
  885. int max_stress_posn;
  886. int unstressed_word = 0;
  887. char *max_output;
  888. int final_ph;
  889. int final_ph2;
  890. int mnem;
  891. int opt_length;
  892. int done;
  893. int stressflags;
  894. int dflags = 0;
  895. int first_primary;
  896. int long_vowel;
  897. signed char vowel_stress[N_WORD_PHONEMES/2];
  898. char syllable_weight[N_WORD_PHONEMES/2];
  899. char vowel_length[N_WORD_PHONEMES/2];
  900. unsigned char phonetic[N_WORD_PHONEMES];
  901. static char consonant_types[16] = { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
  902. /* stress numbers STRESS_BASE +
  903. 0 diminished, unstressed within a word
  904. 1 unstressed, weak
  905. 2
  906. 3 secondary stress
  907. 4 main stress */
  908. stressflags = tr->langopts.stress_flags;
  909. if (dictionary_flags != NULL)
  910. dflags = dictionary_flags[0];
  911. // copy input string into internal buffer
  912. for (ix = 0; ix < N_WORD_PHONEMES; ix++) {
  913. phonetic[ix] = output[ix];
  914. // check for unknown phoneme codes
  915. if (phonetic[ix] >= n_phoneme_tab)
  916. phonetic[ix] = phonSCHWA;
  917. if (phonetic[ix] == 0)
  918. break;
  919. }
  920. if (ix == 0) return;
  921. final_ph = phonetic[ix-1];
  922. final_ph2 = phonetic[ix-2];
  923. max_output = output + (N_WORD_PHONEMES-3); // check for overrun
  924. // any stress position marked in the xx_list dictionary ?
  925. stressed_syllable = dflags & 0x7;
  926. if (dflags & 0x8) {
  927. // this indicates a word without a primary stress
  928. stressed_syllable = dflags & 0x3;
  929. unstressed_word = 1;
  930. }
  931. max_stress = max_stress_input = GetVowelStress(tr, phonetic, vowel_stress, &vowel_count, &stressed_syllable, 1);
  932. if ((max_stress < 0) && dictionary_flags)
  933. max_stress = 0;
  934. // heavy or light syllables
  935. ix = 1;
  936. for (p = phonetic; *p != 0; p++) {
  937. if ((phoneme_tab[p[0]]->type == phVOWEL) && !(phoneme_tab[p[0]]->phflags & phNONSYLLABIC)) {
  938. int weight = 0;
  939. int lengthened = 0;
  940. if (phoneme_tab[p[1]]->code == phonLENGTHEN)
  941. lengthened = 1;
  942. if (lengthened || (phoneme_tab[p[0]]->phflags & phLONG)) {
  943. // long vowel, increase syllable weight
  944. weight++;
  945. }
  946. vowel_length[ix] = weight;
  947. if (lengthened) p++; // advance over phonLENGTHEN
  948. if (consonant_types[phoneme_tab[p[1]]->type] && ((phoneme_tab[p[2]]->type != phVOWEL) || (phoneme_tab[p[1]]->phflags & phLONG))) {
  949. // followed by two consonants, a long consonant, or consonant and end-of-word
  950. weight++;
  951. }
  952. syllable_weight[ix] = weight;
  953. ix++;
  954. }
  955. }
  956. switch (tr->langopts.stress_rule)
  957. {
  958. case 8:
  959. // stress on first syllable, unless it is a light syllable followed by a heavy syllable
  960. if ((syllable_weight[1] > 0) || (syllable_weight[2] == 0))
  961. break;
  962. // fallthrough:
  963. case 1:
  964. // stress on second syllable
  965. if ((stressed_syllable == 0) && (vowel_count > 2)) {
  966. stressed_syllable = 2;
  967. if (max_stress == 0)
  968. vowel_stress[stressed_syllable] = 4;
  969. max_stress = 4;
  970. }
  971. break;
  972. case 10: // penultimate, but final if only 1 or 2 syllables
  973. if (stressed_syllable == 0) {
  974. if (vowel_count < 4) {
  975. vowel_stress[vowel_count - 1] = 4;
  976. max_stress = 4;
  977. break;
  978. }
  979. }
  980. // fallthrough:
  981. case 2:
  982. // a language with stress on penultimate vowel
  983. if (stressed_syllable == 0) {
  984. // no explicit stress - stress the penultimate vowel
  985. max_stress = 4;
  986. if (vowel_count > 2) {
  987. stressed_syllable = vowel_count - 2;
  988. if (stressflags & S_FINAL_SPANISH) {
  989. // LANG=Spanish, stress on last vowel if the word ends in a consonant other than 'n' or 's'
  990. if (phoneme_tab[final_ph]->type != phVOWEL) {
  991. mnem = phoneme_tab[final_ph]->mnemonic;
  992. if (tr->translator_name == L('a', 'n')) {
  993. if (((mnem != 's') && (mnem != 'n')) || phoneme_tab[final_ph2]->type != phVOWEL)
  994. stressed_syllable = vowel_count - 1; // stress on last syllable
  995. } else if (tr->translator_name == L('i', 'a')) {
  996. if ((mnem != 's') || phoneme_tab[final_ph2]->type != phVOWEL)
  997. stressed_syllable = vowel_count - 1; // stress on last syllable
  998. } else {
  999. if ((mnem == 's') && (phoneme_tab[final_ph2]->type == phNASAL)) {
  1000. // -ns stress remains on penultimate syllable
  1001. } else if (((phoneme_tab[final_ph]->type != phNASAL) && (mnem != 's')) || (phoneme_tab[final_ph2]->type != phVOWEL))
  1002. stressed_syllable = vowel_count - 1;
  1003. }
  1004. }
  1005. }
  1006. if (stressflags & S_FINAL_LONG) {
  1007. // stress on last syllable if it has a long vowel, but previous syllable has a short vowel
  1008. if (vowel_length[vowel_count - 1] > vowel_length[vowel_count - 2])
  1009. stressed_syllable = vowel_count - 1;
  1010. }
  1011. if ((vowel_stress[stressed_syllable] == 0) || (vowel_stress[stressed_syllable] == 1)) {
  1012. // but this vowel is explicitly marked as unstressed
  1013. if (stressed_syllable > 1)
  1014. stressed_syllable--;
  1015. else
  1016. stressed_syllable++;
  1017. }
  1018. } else
  1019. stressed_syllable = 1;
  1020. // only set the stress if it's not already marked explicitly
  1021. if (vowel_stress[stressed_syllable] < 0) {
  1022. // don't stress if next and prev syllables are stressed
  1023. if ((vowel_stress[stressed_syllable-1] < 4) || (vowel_stress[stressed_syllable+1] < 4))
  1024. vowel_stress[stressed_syllable] = max_stress;
  1025. }
  1026. }
  1027. break;
  1028. case 3:
  1029. // stress on last vowel
  1030. if (stressed_syllable == 0) {
  1031. // no explicit stress - stress the final vowel
  1032. stressed_syllable = vowel_count - 1;
  1033. while (stressed_syllable > 0) {
  1034. // find the last vowel which is not unstressed
  1035. if (vowel_stress[stressed_syllable] < 0) {
  1036. vowel_stress[stressed_syllable] = 4;
  1037. break;
  1038. } else
  1039. stressed_syllable--;
  1040. }
  1041. max_stress = 4;
  1042. }
  1043. break;
  1044. case 4: // stress on antipenultimate vowel
  1045. if (stressed_syllable == 0) {
  1046. stressed_syllable = vowel_count - 3;
  1047. if (stressed_syllable < 1)
  1048. stressed_syllable = 1;
  1049. if (max_stress == 0)
  1050. vowel_stress[stressed_syllable] = 4;
  1051. max_stress = 4;
  1052. }
  1053. break;
  1054. case 5:
  1055. // LANG=Russian
  1056. if (stressed_syllable == 0) {
  1057. // no explicit stress - guess the stress from the number of syllables
  1058. static char guess_ru[16] = { 0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 7, 7, 8, 9, 10, 11 };
  1059. 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
  1060. 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
  1061. stressed_syllable = vowel_count - 3;
  1062. if (vowel_count < 16) {
  1063. if (phoneme_tab[final_ph]->type == phVOWEL)
  1064. stressed_syllable = guess_ru_v[vowel_count];
  1065. else if (phoneme_tab[final_ph]->type == phSTOP)
  1066. stressed_syllable = guess_ru_t[vowel_count];
  1067. else
  1068. stressed_syllable = guess_ru[vowel_count];
  1069. }
  1070. vowel_stress[stressed_syllable] = 4;
  1071. max_stress = 4;
  1072. }
  1073. break;
  1074. case 6: // LANG=hi stress on the last heaviest syllable
  1075. if (stressed_syllable == 0) {
  1076. int wt;
  1077. int max_weight = -1;
  1078. // find the heaviest syllable, excluding the final syllable
  1079. for (ix = 1; ix < (vowel_count-1); ix++) {
  1080. if (vowel_stress[ix] < 0) {
  1081. if ((wt = syllable_weight[ix]) >= max_weight) {
  1082. max_weight = wt;
  1083. stressed_syllable = ix;
  1084. }
  1085. }
  1086. }
  1087. if ((syllable_weight[vowel_count-1] == 2) && (max_weight < 2)) {
  1088. // the only double=heavy syllable is the final syllable, so stress this
  1089. stressed_syllable = vowel_count-1;
  1090. } else if (max_weight <= 0) {
  1091. // all syllables, exclusing the last, are light. Stress the first syllable
  1092. stressed_syllable = 1;
  1093. }
  1094. vowel_stress[stressed_syllable] = 4;
  1095. max_stress = 4;
  1096. }
  1097. break;
  1098. case 7: // LANG=tr, the last syllable for any vowel marked explicitly as unstressed
  1099. if (stressed_syllable == 0) {
  1100. stressed_syllable = vowel_count - 1;
  1101. for (ix = 1; ix < vowel_count; ix++) {
  1102. if (vowel_stress[ix] == 1) {
  1103. stressed_syllable = ix-1;
  1104. break;
  1105. }
  1106. }
  1107. vowel_stress[stressed_syllable] = 4;
  1108. max_stress = 4;
  1109. }
  1110. break;
  1111. case 9: // mark all as stressed
  1112. for (ix = 1; ix < vowel_count; ix++) {
  1113. if (vowel_stress[ix] < 0)
  1114. vowel_stress[ix] = 4;
  1115. }
  1116. break;
  1117. case 12: // LANG=kl (Greenlandic)
  1118. long_vowel = 0;
  1119. for (ix = 1; ix < vowel_count; ix++) {
  1120. if (vowel_stress[ix] == 4)
  1121. vowel_stress[ix] = 3; // change marked stress (consonant clusters) to secondary (except the last)
  1122. if (vowel_length[ix] > 0) {
  1123. long_vowel = ix;
  1124. vowel_stress[ix] = 3; // give secondary stress to all long vowels
  1125. }
  1126. }
  1127. // 'stressed_syllable' gives the last marked stress
  1128. if (stressed_syllable == 0) {
  1129. // no marked stress, choose the last long vowel
  1130. if (long_vowel > 0)
  1131. stressed_syllable = long_vowel;
  1132. else {
  1133. // no long vowels or consonant clusters
  1134. if (vowel_count > 5)
  1135. stressed_syllable = vowel_count - 3; // more than 4 syllables
  1136. else
  1137. stressed_syllable = vowel_count - 1;
  1138. }
  1139. }
  1140. vowel_stress[stressed_syllable] = 4;
  1141. max_stress = 4;
  1142. break;
  1143. case 13: // LANG=ml, 1st unless 1st vowel is short and 2nd is long
  1144. if (stressed_syllable == 0) {
  1145. stressed_syllable = 1;
  1146. if ((vowel_length[1] == 0) && (vowel_count > 2) && (vowel_length[2] > 0))
  1147. stressed_syllable = 2;
  1148. vowel_stress[stressed_syllable] = 4;
  1149. max_stress = 4;
  1150. }
  1151. break;
  1152. }
  1153. if ((stressflags & S_FINAL_VOWEL_UNSTRESSED) && ((control & 2) == 0) && (vowel_count > 2) && (max_stress_input < 3) && (vowel_stress[vowel_count - 1] == 4)) {
  1154. // Don't allow stress on a word-final vowel
  1155. // Only do this if there is no suffix phonemes to be added, and if a stress position was not given explicitly
  1156. if (phoneme_tab[final_ph]->type == phVOWEL) {
  1157. vowel_stress[vowel_count - 1] = 1;
  1158. vowel_stress[vowel_count - 2] = 4;
  1159. }
  1160. }
  1161. // now guess the complete stress pattern
  1162. if (max_stress < 4)
  1163. stress = 4; // no primary stress marked, use for 1st syllable
  1164. else
  1165. stress = 3;
  1166. if (unstressed_word == 0) {
  1167. if ((stressflags & S_2_SYL_2) && (vowel_count == 3)) {
  1168. // Two syllable word, if one syllable has primary stress, then give the other secondary stress
  1169. if (vowel_stress[1] == 4)
  1170. vowel_stress[2] = 3;
  1171. if (vowel_stress[2] == 4)
  1172. vowel_stress[1] = 3;
  1173. }
  1174. if ((stressflags & S_INITIAL_2) && (vowel_stress[1] < 0)) {
  1175. // If there is only one syllable before the primary stress, give it a secondary stress
  1176. if ((vowel_count > 3) && (vowel_stress[2] >= 4))
  1177. vowel_stress[1] = 3;
  1178. }
  1179. }
  1180. done = 0;
  1181. first_primary = 0;
  1182. for (v = 1; v < vowel_count; v++) {
  1183. if (vowel_stress[v] < 0) {
  1184. if ((stressflags & S_FINAL_NO_2) && (stress < 4) && (v == vowel_count-1)) {
  1185. // flag: don't give secondary stress to final vowel
  1186. } else if ((stressflags & 0x8000) && (done == 0)) {
  1187. vowel_stress[v] = (char)stress;
  1188. done = 1;
  1189. stress = 3; // use secondary stress for remaining syllables
  1190. } else if ((vowel_stress[v-1] <= 1) && ((vowel_stress[v+1] <= 1) || ((stress == 4) && (vowel_stress[v+1] <= 2)))) {
  1191. // trochaic: give stress to vowel surrounded by unstressed vowels
  1192. if ((stress == 3) && (stressflags & S_NO_AUTO_2))
  1193. continue; // don't use secondary stress
  1194. if ((v > 1) && (stressflags & S_2_TO_HEAVY) && (syllable_weight[v] == 0) && (syllable_weight[v+1] > 0)) {
  1195. // don't put secondary stress on a light syllable which is followed by a heavy syllable
  1196. continue;
  1197. }
  1198. // should start with secondary stress on the first syllable, or should it count back from
  1199. // the primary stress and put secondary stress on alternate syllables?
  1200. vowel_stress[v] = (char)stress;
  1201. done = 1;
  1202. stress = 3; // use secondary stress for remaining syllables
  1203. }
  1204. }
  1205. if (vowel_stress[v] >= 4) {
  1206. if (first_primary == 0)
  1207. first_primary = v;
  1208. else if (stressflags & S_FIRST_PRIMARY) {
  1209. // reduce primary stresses after the first to secondary
  1210. vowel_stress[v] = 3;
  1211. }
  1212. }
  1213. }
  1214. if ((unstressed_word) && (tonic < 0)) {
  1215. if (vowel_count <= 2)
  1216. tonic = tr->langopts.unstressed_wd1; // monosyllable - unstressed
  1217. else
  1218. tonic = tr->langopts.unstressed_wd2; // more than one syllable, used secondary stress as the main stress
  1219. }
  1220. max_stress = 0;
  1221. max_stress_posn = 0;
  1222. for (v = 1; v < vowel_count; v++) {
  1223. if (vowel_stress[v] >= max_stress) {
  1224. max_stress = vowel_stress[v];
  1225. max_stress_posn = v;
  1226. }
  1227. }
  1228. if (tonic >= 0) {
  1229. // find position of highest stress, and replace it by 'tonic'
  1230. // don't disturb an explicitly set stress by 'unstress-at-end' flag
  1231. if ((tonic > max_stress) || (max_stress <= 4))
  1232. vowel_stress[max_stress_posn] = (char)tonic;
  1233. max_stress = tonic;
  1234. }
  1235. // produce output phoneme string
  1236. p = phonetic;
  1237. v = 1;
  1238. if (!(control & 1) && ((ph = phoneme_tab[*p]) != NULL)) {
  1239. while ((ph->type == phSTRESS) || (*p == phonEND_WORD)) {
  1240. p++;
  1241. ph = phoneme_tab[p[0]];
  1242. }
  1243. if ((tr->langopts.vowel_pause & 0x30) && (ph->type == phVOWEL)) {
  1244. // word starts with a vowel
  1245. if ((tr->langopts.vowel_pause & 0x20) && (vowel_stress[1] >= 4))
  1246. *output++ = phonPAUSE_NOLINK; // not to be replaced by link
  1247. else
  1248. *output++ = phonPAUSE_VSHORT; // break, but no pause
  1249. }
  1250. }
  1251. p = phonetic;
  1252. while (((phcode = *p++) != 0) && (output < max_output)) {
  1253. if ((ph = phoneme_tab[phcode]) == NULL)
  1254. continue;
  1255. if (ph->type == phPAUSE)
  1256. tr->prev_last_stress = 0;
  1257. else if (((ph->type == phVOWEL) && !(ph->phflags & phNONSYLLABIC)) || (*p == phonSYLLABIC)) {
  1258. // a vowel, or a consonant followed by a syllabic consonant marker
  1259. v_stress = vowel_stress[v];
  1260. tr->prev_last_stress = v_stress;
  1261. if (v_stress <= 1) {
  1262. if ((v > 1) && (max_stress >= 2) && (stressflags & S_FINAL_DIM) && (v == (vowel_count-1))) {
  1263. // option: mark unstressed final syllable as diminished
  1264. v_stress = 0;
  1265. } else if ((stressflags & S_NO_DIM) || (v == 1) || (v == (vowel_count-1))) {
  1266. // first or last syllable, or option 'don't set diminished stress'
  1267. v_stress = 1;
  1268. } else if ((v == (vowel_count-2)) && (vowel_stress[vowel_count-1] <= 1)) {
  1269. // penultimate syllable, followed by an unstressed final syllable
  1270. v_stress = 1;
  1271. } else {
  1272. // unstressed syllable within a word
  1273. if ((vowel_stress[v-1] < 0) || ((stressflags & S_MID_DIM) == 0)) {
  1274. v_stress = 0; // change to 0 (diminished stress)
  1275. vowel_stress[v] = v_stress;
  1276. }
  1277. }
  1278. }
  1279. if ((v_stress == 0) || (v_stress > 1))
  1280. *output++ = stress_phonemes[v_stress]; // mark stress of all vowels except 1 (unstressed)
  1281. if (vowel_stress[v] > max_stress)
  1282. max_stress = vowel_stress[v];
  1283. if ((*p == phonLENGTHEN) && ((opt_length = tr->langopts.param[LOPT_IT_LENGTHEN]) & 1)) {
  1284. // remove lengthen indicator from non-stressed syllables
  1285. int shorten = 0;
  1286. if (opt_length & 0x10) {
  1287. // only allow lengthen indicator on the highest stress syllable in the word
  1288. if (v != max_stress_posn)
  1289. shorten = 1;
  1290. } else if (v_stress < 4) {
  1291. // only allow lengthen indicator if stress >= 4.
  1292. shorten = 1;
  1293. }
  1294. if (shorten)
  1295. p++;
  1296. }
  1297. v++;
  1298. }
  1299. if (phcode != 1)
  1300. *output++ = phcode;
  1301. }
  1302. *output++ = 0;
  1303. return;
  1304. }
  1305. void AppendPhonemes(Translator *tr, char *string, int size, const char *ph)
  1306. {
  1307. /* Add new phoneme string "ph" to "string"
  1308. Keeps count of the number of vowel phonemes in the word, and whether these
  1309. can be stressed syllables. These values can be used in translation rules
  1310. */
  1311. const char *p;
  1312. unsigned char c;
  1313. int unstress_mark;
  1314. int length;
  1315. length = strlen(ph) + strlen(string);
  1316. if (length >= size)
  1317. return;
  1318. // any stressable vowel ?
  1319. unstress_mark = 0;
  1320. p = ph;
  1321. while ((c = *p++) != 0) {
  1322. if (c >= n_phoneme_tab) continue;
  1323. if (phoneme_tab[c]->type == phSTRESS) {
  1324. if (phoneme_tab[c]->std_length < 4)
  1325. unstress_mark = 1;
  1326. } else {
  1327. if (phoneme_tab[c]->type == phVOWEL) {
  1328. if (((phoneme_tab[c]->phflags & phUNSTRESSED) == 0) &&
  1329. (unstress_mark == 0)) {
  1330. tr->word_stressed_count++;
  1331. }
  1332. unstress_mark = 0;
  1333. tr->word_vowel_count++;
  1334. }
  1335. }
  1336. }
  1337. if (string != NULL)
  1338. strcat(string, ph);
  1339. }
  1340. static void MatchRule(Translator *tr, char *word[], char *word_start, int group_length, char *rule, MatchRecord *match_out, int word_flags, int dict_flags)
  1341. {
  1342. /* Checks a specified word against dictionary rules.
  1343. Returns with phoneme code string, or NULL if no match found.
  1344. word (indirect) points to current character group within the input word
  1345. This is advanced by this procedure as characters are consumed
  1346. group: the initial characters used to choose the rules group
  1347. rule: address of dictionary rule data for this character group
  1348. match_out: returns best points score
  1349. word_flags: indicates whether this is a retranslation after a suffix has been removed
  1350. */
  1351. unsigned char rb; // current instuction from rule
  1352. unsigned char letter; // current letter from input word, single byte
  1353. int letter_w; // current letter, wide character
  1354. int last_letter_w; // last letter, wide character
  1355. int letter_xbytes; // number of extra bytes of multibyte character (num bytes - 1)
  1356. unsigned char last_letter;
  1357. char *pre_ptr;
  1358. char *post_ptr; // pointer to first character after group
  1359. char *rule_start; // start of current match template
  1360. char *p;
  1361. int ix;
  1362. int match_type; // left, right, or consume
  1363. int failed;
  1364. int unpron_ignore;
  1365. int consumed; // number of letters consumed from input
  1366. int syllable_count;
  1367. int vowel;
  1368. int letter_group;
  1369. int distance_right;
  1370. int distance_left;
  1371. int lg_pts;
  1372. int n_bytes;
  1373. int add_points;
  1374. int command;
  1375. int check_atstart;
  1376. unsigned int *flags;
  1377. MatchRecord match;
  1378. static MatchRecord best;
  1379. int total_consumed; // letters consumed for best match
  1380. unsigned char condition_num;
  1381. char *common_phonemes; // common to a group of entries
  1382. char *group_chars;
  1383. char word_buf[N_WORD_BYTES];
  1384. group_chars = *word;
  1385. if (rule == NULL) {
  1386. match_out->points = 0;
  1387. (*word)++;
  1388. return;
  1389. }
  1390. total_consumed = 0;
  1391. common_phonemes = NULL;
  1392. best.points = 0;
  1393. best.phonemes = "";
  1394. best.end_type = 0;
  1395. best.del_fwd = NULL;
  1396. // search through dictionary rules
  1397. while (rule[0] != RULE_GROUP_END) {
  1398. unpron_ignore = word_flags & FLAG_UNPRON_TEST;
  1399. match_type = 0;
  1400. consumed = 0;
  1401. letter = 0;
  1402. letter_w = 0;
  1403. distance_right = -6; // used to reduce points for matches further away the current letter
  1404. distance_left = -2;
  1405. check_atstart = 0;
  1406. match.points = 1;
  1407. match.end_type = 0;
  1408. match.del_fwd = NULL;
  1409. pre_ptr = *word;
  1410. post_ptr = *word + group_length;
  1411. // work through next rule until end, or until no-match proved
  1412. rule_start = rule;
  1413. failed = 0;
  1414. while (!failed) {
  1415. rb = *rule++;
  1416. if (rb <= RULE_LINENUM) {
  1417. switch (rb)
  1418. {
  1419. case 0: // no phoneme string for this rule, use previous common rule
  1420. if (common_phonemes != NULL) {
  1421. match.phonemes = common_phonemes;
  1422. while (((rb = *match.phonemes++) != 0) && (rb != RULE_PHONEMES)) {
  1423. if (rb == RULE_CONDITION)
  1424. match.phonemes++; // skip over condition number
  1425. if (rb == RULE_LINENUM)
  1426. match.phonemes += 2; // skip over line number
  1427. }
  1428. } else
  1429. match.phonemes = "";
  1430. rule--; // so we are still pointing at the 0
  1431. failed = 2; // matched OK
  1432. break;
  1433. case RULE_PRE_ATSTART: // pre rule with implied 'start of word'
  1434. check_atstart = 1;
  1435. unpron_ignore = 0;
  1436. match_type = RULE_PRE;
  1437. break;
  1438. case RULE_PRE:
  1439. match_type = RULE_PRE;
  1440. if (word_flags & FLAG_UNPRON_TEST) {
  1441. // checking the start of the word for unpronouncable character sequences, only
  1442. // consider rules which explicitly match the start of a word
  1443. // Note: Those rules now use RULE_PRE_ATSTART
  1444. failed = 1;
  1445. }
  1446. break;
  1447. case RULE_POST:
  1448. match_type = RULE_POST;
  1449. break;
  1450. case RULE_PHONEMES:
  1451. match.phonemes = rule;
  1452. failed = 2; // matched OK
  1453. break;
  1454. case RULE_PH_COMMON:
  1455. common_phonemes = rule;
  1456. break;
  1457. case RULE_CONDITION:
  1458. // conditional rule, next byte gives condition number
  1459. condition_num = *rule++;
  1460. if (condition_num >= 32) {
  1461. // allow the rule only if the condition number is NOT set
  1462. if ((tr->dict_condition & (1L << (condition_num-32))) != 0)
  1463. failed = 1;
  1464. } else {
  1465. // allow the rule only if the condition number is set
  1466. if ((tr->dict_condition & (1L << condition_num)) == 0)
  1467. failed = 1;
  1468. }
  1469. if (!failed)
  1470. match.points++; // add one point for a matched conditional rule
  1471. break;
  1472. case RULE_LINENUM:
  1473. rule += 2;
  1474. break;
  1475. }
  1476. continue;
  1477. }
  1478. add_points = 0;
  1479. switch (match_type)
  1480. {
  1481. case 0:
  1482. // match and consume this letter
  1483. letter = *post_ptr++;
  1484. if ((letter == rb) || ((letter == (unsigned char)REPLACED_E) && (rb == 'e'))) {
  1485. if ((letter & 0xc0) != 0x80)
  1486. add_points = 21; // don't add point for non-initial UTF-8 bytes
  1487. consumed++;
  1488. } else
  1489. failed = 1;
  1490. break;
  1491. case RULE_POST:
  1492. // continue moving forwards
  1493. distance_right += 6;
  1494. if (distance_right > 18)
  1495. distance_right = 19;
  1496. last_letter = letter;
  1497. last_letter_w = letter_w;
  1498. letter_xbytes = utf8_in(&letter_w, post_ptr)-1;
  1499. letter = *post_ptr++;
  1500. switch (rb)
  1501. {
  1502. case RULE_LETTERGP:
  1503. letter_group = LetterGroupNo(rule++);
  1504. if (IsLetter(tr, letter_w, letter_group)) {
  1505. lg_pts = 20;
  1506. if (letter_group == 2)
  1507. lg_pts = 19; // fewer points for C, general consonant
  1508. add_points = (lg_pts-distance_right);
  1509. post_ptr += letter_xbytes;
  1510. } else
  1511. failed = 1;
  1512. break;
  1513. case RULE_LETTERGP2: // match against a list of utf-8 strings
  1514. letter_group = LetterGroupNo(rule++);
  1515. if ((n_bytes = IsLetterGroup(tr, post_ptr-1, letter_group, 0)) >= 0) {
  1516. add_points = (20-distance_right);
  1517. if (n_bytes > 0) // move pointer, if non-zero length group was found
  1518. post_ptr += (n_bytes-1);
  1519. } else
  1520. failed = 1;
  1521. break;
  1522. case RULE_NOTVOWEL:
  1523. if (IsLetter(tr, letter_w, 0) || ((letter_w == ' ') && (word_flags & FLAG_SUFFIX_VOWEL)))
  1524. failed = 1;
  1525. else {
  1526. add_points = (20-distance_right);
  1527. post_ptr += letter_xbytes;
  1528. }
  1529. break;
  1530. case RULE_DIGIT:
  1531. if (IsDigit(letter_w)) {
  1532. add_points = (20-distance_right);
  1533. post_ptr += letter_xbytes;
  1534. } else if (tr->langopts.tone_numbers) {
  1535. // also match if there is no digit
  1536. add_points = (20-distance_right);
  1537. post_ptr--;
  1538. } else
  1539. failed = 1;
  1540. break;
  1541. case RULE_NONALPHA:
  1542. if (!iswalpha2(letter_w)) {
  1543. add_points = (21-distance_right);
  1544. post_ptr += letter_xbytes;
  1545. } else
  1546. failed = 1;
  1547. break;
  1548. case RULE_DOUBLE:
  1549. if (letter_w == last_letter_w)
  1550. add_points = (21-distance_right);
  1551. else
  1552. failed = 1;
  1553. break;
  1554. case RULE_DOLLAR:
  1555. command = *rule++;
  1556. if (command == DOLLAR_UNPR)
  1557. match.end_type = SUFX_UNPRON; // $unpron
  1558. else if (command == DOLLAR_NOPREFIX) { // $noprefix
  1559. if (word_flags & FLAG_PREFIX_REMOVED)
  1560. failed = 1; // a prefix has been removed
  1561. else
  1562. add_points = 1;
  1563. } else if ((command & 0xf0) == 0x10) {
  1564. // $w_alt
  1565. if (dict_flags & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
  1566. add_points = 23;
  1567. else
  1568. failed = 1;
  1569. } else if (((command & 0xf0) == 0x20) || (command == DOLLAR_LIST)) {
  1570. // $list or $p_alt
  1571. // make a copy of the word up to the post-match characters
  1572. ix = *word - word_start + consumed + group_length + 1;
  1573. memcpy(word_buf, word_start-1, ix);
  1574. word_buf[ix] = ' ';
  1575. word_buf[ix+1] = 0;
  1576. LookupFlags(tr, &word_buf[1], &flags);
  1577. if ((command == DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY))
  1578. add_points = 23;
  1579. else if (flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
  1580. add_points = 23;
  1581. else
  1582. failed = 1;
  1583. }
  1584. break;
  1585. case '-':
  1586. if ((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN_AFTER)))
  1587. add_points = (22-distance_right); // one point more than match against space
  1588. else
  1589. failed = 1;
  1590. break;
  1591. case RULE_SYLLABLE:
  1592. {
  1593. // more than specified number of vowel letters to the right
  1594. char *p = post_ptr + letter_xbytes;
  1595. int vowel_count = 0;
  1596. syllable_count = 1;
  1597. while (*rule == RULE_SYLLABLE) {
  1598. rule++;
  1599. syllable_count += 1; // number of syllables to match
  1600. }
  1601. vowel = 0;
  1602. while (letter_w != RULE_SPACE) {
  1603. if ((vowel == 0) && IsLetter(tr, letter_w, LETTERGP_VOWEL2)) {
  1604. // this is counting vowels which are separated by non-vowel letters
  1605. vowel_count++;
  1606. }
  1607. vowel = IsLetter(tr, letter_w, LETTERGP_VOWEL2);
  1608. p += utf8_in(&letter_w, p);
  1609. }
  1610. if (syllable_count <= vowel_count)
  1611. add_points = (18+syllable_count-distance_right);
  1612. else
  1613. failed = 1;
  1614. }
  1615. break;
  1616. case RULE_NOVOWELS:
  1617. {
  1618. char *p = post_ptr + letter_xbytes;
  1619. while (letter_w != RULE_SPACE) {
  1620. if (IsLetter(tr, letter_w, LETTERGP_VOWEL2)) {
  1621. failed = 1;
  1622. break;
  1623. }
  1624. p += utf8_in(&letter_w, p);
  1625. }
  1626. if (!failed)
  1627. add_points = (19-distance_right);
  1628. }
  1629. break;
  1630. case RULE_SKIPCHARS:
  1631. {
  1632. // '(Jxy' means 'skip characters until xy'
  1633. char *p = post_ptr + letter_xbytes;
  1634. char *p2 = p; // pointer to the previous character in the word
  1635. int rule_w; // first wide character of skip rule
  1636. utf8_in(&rule_w, rule);
  1637. int g_bytes = 0; // bytes of successfully found character group
  1638. while ((letter_w != rule_w) && (letter_w != RULE_SPACE) && (letter_w != 0) && (g_bytes == 0)) {
  1639. p2 = p;
  1640. p += utf8_in(&letter_w, p);
  1641. if (rule_w == RULE_LETTERGP2)
  1642. g_bytes = IsLetterGroup(tr, p2, LetterGroupNo(rule + 1), 0);
  1643. }
  1644. if ((letter_w == rule_w) || (g_bytes > 0))
  1645. post_ptr = p2;
  1646. }
  1647. break;
  1648. case RULE_INC_SCORE:
  1649. add_points = 20; // force an increase in points
  1650. break;
  1651. case RULE_DEC_SCORE:
  1652. add_points = -20; // force an decrease in points
  1653. break;
  1654. case RULE_DEL_FWD:
  1655. // find the next 'e' in the word and replace by 'E'
  1656. for (p = *word + group_length; p < post_ptr; p++) {
  1657. if (*p == 'e') {
  1658. match.del_fwd = p;
  1659. break;
  1660. }
  1661. }
  1662. break;
  1663. case RULE_ENDING:
  1664. {
  1665. int end_type;
  1666. // next 3 bytes are a (non-zero) ending type. 2 bytes of flags + suffix length
  1667. end_type = (rule[0] << 16) + ((rule[1] & 0x7f) << 8) + (rule[2] & 0x7f);
  1668. if ((tr->word_vowel_count == 0) && !(end_type & SUFX_P) && (tr->langopts.param[LOPT_SUFFIX] & 1))
  1669. failed = 1; // don't match a suffix rule if there are no previous syllables (needed for lang=tr).
  1670. else {
  1671. match.end_type = end_type;
  1672. rule += 3;
  1673. }
  1674. }
  1675. break;
  1676. case RULE_NO_SUFFIX:
  1677. if (word_flags & FLAG_SUFFIX_REMOVED)
  1678. failed = 1; // a suffix has been removed
  1679. else
  1680. add_points = 1;
  1681. break;
  1682. default:
  1683. if (letter == rb) {
  1684. if ((letter & 0xc0) != 0x80) {
  1685. // not for non-initial UTF-8 bytes
  1686. add_points = (21-distance_right);
  1687. }
  1688. } else
  1689. failed = 1;
  1690. break;
  1691. }
  1692. break;
  1693. case RULE_PRE:
  1694. // match backwards from start of current group
  1695. distance_left += 2;
  1696. if (distance_left > 18)
  1697. distance_left = 19;
  1698. last_letter = *pre_ptr;
  1699. utf8_in(&last_letter_w, pre_ptr);
  1700. pre_ptr--;
  1701. letter_xbytes = utf8_in2(&letter_w, pre_ptr, 1)-1;
  1702. letter = *pre_ptr;
  1703. switch (rb)
  1704. {
  1705. case RULE_LETTERGP:
  1706. letter_group = LetterGroupNo(rule++);
  1707. if (IsLetter(tr, letter_w, letter_group)) {
  1708. lg_pts = 20;
  1709. if (letter_group == 2)
  1710. lg_pts = 19; // fewer points for C, general consonant
  1711. add_points = (lg_pts-distance_left);
  1712. pre_ptr -= letter_xbytes;
  1713. } else
  1714. failed = 1;
  1715. break;
  1716. case RULE_LETTERGP2: // match against a list of utf-8 strings
  1717. letter_group = LetterGroupNo(rule++);
  1718. if ((n_bytes = IsLetterGroup(tr, pre_ptr, letter_group, 1)) >= 0) {
  1719. add_points = (20-distance_right);
  1720. if (n_bytes > 0) // move pointer, if non-zero length group was found
  1721. pre_ptr -= (n_bytes-1);
  1722. } else
  1723. failed = 1;
  1724. break;
  1725. case RULE_NOTVOWEL:
  1726. if (!IsLetter(tr, letter_w, 0)) {
  1727. add_points = (20-distance_left);
  1728. pre_ptr -= letter_xbytes;
  1729. } else
  1730. failed = 1;
  1731. break;
  1732. case RULE_DOUBLE:
  1733. if (letter_w == last_letter_w)
  1734. add_points = (21-distance_left);
  1735. else
  1736. failed = 1;
  1737. break;
  1738. case RULE_DIGIT:
  1739. if (IsDigit(letter_w)) {
  1740. add_points = (21-distance_left);
  1741. pre_ptr -= letter_xbytes;
  1742. } else
  1743. failed = 1;
  1744. break;
  1745. case RULE_NONALPHA:
  1746. if (!iswalpha2(letter_w)) {
  1747. add_points = (21-distance_right);
  1748. pre_ptr -= letter_xbytes;
  1749. } else
  1750. failed = 1;
  1751. break;
  1752. case RULE_DOLLAR:
  1753. command = *rule++;
  1754. if ((command == DOLLAR_LIST) || ((command & 0xf0) == 0x20)) {
  1755. // $list or $p_alt
  1756. // make a copy of the word up to the current character
  1757. ix = *word - word_start + 1;
  1758. memcpy(word_buf, word_start-1, ix);
  1759. word_buf[ix] = ' ';
  1760. word_buf[ix+1] = 0;
  1761. LookupFlags(tr, &word_buf[1], &flags);
  1762. if ((command == DOLLAR_LIST) && (flags[0] & FLAG_FOUND) && !(flags[1] & FLAG_ONLY))
  1763. add_points = 23;
  1764. else if (flags[0] & (1 << (BITNUM_FLAG_ALT + (command & 0xf))))
  1765. add_points = 23;
  1766. else
  1767. failed = 1;
  1768. }
  1769. break;
  1770. case RULE_SYLLABLE:
  1771. // more than specified number of vowels to the left
  1772. syllable_count = 1;
  1773. while (*rule == RULE_SYLLABLE) {
  1774. rule++;
  1775. syllable_count++; // number of syllables to match
  1776. }
  1777. if (syllable_count <= tr->word_vowel_count)
  1778. add_points = (18+syllable_count-distance_left);
  1779. else
  1780. failed = 1;
  1781. break;
  1782. case RULE_STRESSED:
  1783. if (tr->word_stressed_count > 0)
  1784. add_points = 19;
  1785. else
  1786. failed = 1;
  1787. break;
  1788. case RULE_NOVOWELS:
  1789. {
  1790. char *p = pre_ptr - letter_xbytes - 1;
  1791. while (letter_w != RULE_SPACE) {
  1792. if (IsLetter(tr, letter_w, LETTERGP_VOWEL2)) {
  1793. failed = 1;
  1794. break;
  1795. }
  1796. p -= utf8_in2(&letter_w, p, 1);
  1797. }
  1798. if (!failed)
  1799. add_points = 3;
  1800. }
  1801. break;
  1802. case RULE_IFVERB:
  1803. if (tr->expect_verb)
  1804. add_points = 1;
  1805. else
  1806. failed = 1;
  1807. break;
  1808. case RULE_CAPITAL:
  1809. if (word_flags & FLAG_FIRST_UPPER)
  1810. add_points = 1;
  1811. else
  1812. failed = 1;
  1813. break;
  1814. case '.':
  1815. // dot in pre- section, match on any dot before this point in the word
  1816. for (p = pre_ptr; *p != ' '; p--) {
  1817. if (*p == '.') {
  1818. add_points = 50;
  1819. break;
  1820. }
  1821. }
  1822. if (*p == ' ')
  1823. failed = 1;
  1824. break;
  1825. case '-':
  1826. if ((letter == '-') || ((letter == ' ') && (word_flags & FLAG_HYPHEN)))
  1827. add_points = (22-distance_right); // one point more than match against space
  1828. else
  1829. failed = 1;
  1830. break;
  1831. case RULE_SKIPCHARS: {
  1832. // 'xyJ)' means 'skip characters backwards until xy'
  1833. char *p = pre_ptr; // pointer to current character in word
  1834. char *p2 = p; // pointer to previous character in word
  1835. int g_bytes = 0; // bytes of successfully found character group
  1836. while ((*p != *rule) && (*p != RULE_SPACE) && (*p != 0) && (g_bytes == 0)) {
  1837. p2 = p;
  1838. p--;
  1839. if (*rule == RULE_LETTERGP2)
  1840. g_bytes = IsLetterGroup(tr, p2, LetterGroupNo(rule + 1), 1);
  1841. }
  1842. // if succeed, set pre_ptr to next character after 'xy' and remaining
  1843. // 'xy' part is checked as usual in following cycles of PRE rule characters
  1844. if (*p == *rule)
  1845. pre_ptr = p2;
  1846. if (g_bytes > 0)
  1847. pre_ptr = p2 + 1;
  1848. }
  1849. break;
  1850. default:
  1851. if (letter == rb) {
  1852. if (letter == RULE_SPACE)
  1853. add_points = 4;
  1854. else if ((letter & 0xc0) != 0x80) {
  1855. // not for non-initial UTF-8 bytes
  1856. add_points = (21-distance_left);
  1857. }
  1858. } else
  1859. failed = 1;
  1860. break;
  1861. }
  1862. break;
  1863. }
  1864. if (failed == 0)
  1865. match.points += add_points;
  1866. }
  1867. if ((failed == 2) && (unpron_ignore == 0)) {
  1868. // do we also need to check for 'start of word' ?
  1869. if ((check_atstart == 0) || (pre_ptr[-1] == ' ')) {
  1870. if (check_atstart)
  1871. match.points += 4;
  1872. // matched OK, is this better than the last best match ?
  1873. if (match.points >= best.points) {
  1874. memcpy(&best, &match, sizeof(match));
  1875. total_consumed = consumed;
  1876. }
  1877. if ((option_phonemes & espeakPHONEMES_TRACE) && (match.points > 0) && ((word_flags & FLAG_NO_TRACE) == 0)) {
  1878. // show each rule that matches, and it's points score
  1879. int pts;
  1880. char decoded_phonemes[80];
  1881. pts = match.points;
  1882. if (group_length > 1)
  1883. pts += 35; // to account for an extra letter matching
  1884. DecodePhonemes(match.phonemes, decoded_phonemes);
  1885. fprintf(f_trans, "%3d\t%s [%s]\n", pts, DecodeRule(group_chars, group_length, rule_start, word_flags), decoded_phonemes);
  1886. }
  1887. }
  1888. }
  1889. // skip phoneme string to reach start of next template
  1890. while (*rule++ != 0) ;
  1891. }
  1892. // advance input data pointer
  1893. total_consumed += group_length;
  1894. if (total_consumed == 0)
  1895. total_consumed = 1; // always advance over 1st letter
  1896. *word += total_consumed;
  1897. if (best.points == 0)
  1898. best.phonemes = "";
  1899. memcpy(match_out, &best, sizeof(MatchRecord));
  1900. }
  1901. int TranslateRules(Translator *tr, char *p_start, char *phonemes, int ph_size, char *end_phonemes, int word_flags, unsigned int *dict_flags)
  1902. {
  1903. /* Translate a word bounded by space characters
  1904. Append the result to 'phonemes' and any standard prefix/suffix in 'end_phonemes' */
  1905. unsigned char c, c2;
  1906. unsigned int c12;
  1907. int wc = 0;
  1908. int wc_bytes;
  1909. char *p2; // copy of p for use in double letter chain match
  1910. int found;
  1911. int g; // group chain number
  1912. int g1; // first group for this letter
  1913. int n;
  1914. int letter;
  1915. int any_alpha = 0;
  1916. int ix;
  1917. unsigned int digit_count = 0;
  1918. char *p;
  1919. ALPHABET *alphabet;
  1920. int dict_flags0 = 0;
  1921. MatchRecord match1;
  1922. MatchRecord match2;
  1923. char ph_buf[40];
  1924. char word_copy[N_WORD_BYTES];
  1925. static const char str_pause[2] = { phonPAUSE_NOLINK, 0 };
  1926. if (tr->data_dictrules == NULL)
  1927. return 0;
  1928. if (dict_flags != NULL)
  1929. dict_flags0 = dict_flags[0];
  1930. for (ix = 0; ix < (N_WORD_BYTES-1);) {
  1931. c = p_start[ix];
  1932. word_copy[ix++] = c;
  1933. if (c == 0)
  1934. break;
  1935. }
  1936. word_copy[ix] = 0;
  1937. if ((option_phonemes & espeakPHONEMES_TRACE) && ((word_flags & FLAG_NO_TRACE) == 0)) {
  1938. char wordbuf[120];
  1939. unsigned int ix;
  1940. for (ix = 0; ((c = p_start[ix]) != ' ') && (c != 0) && (ix < (sizeof(wordbuf)-1)); ix++)
  1941. wordbuf[ix] = c;
  1942. wordbuf[ix] = 0;
  1943. if (word_flags & FLAG_UNPRON_TEST)
  1944. fprintf(f_trans, "Unpronouncable? '%s'\n", wordbuf);
  1945. else
  1946. fprintf(f_trans, "Translate '%s'\n", wordbuf);
  1947. }
  1948. p = p_start;
  1949. tr->word_vowel_count = 0;
  1950. tr->word_stressed_count = 0;
  1951. if (end_phonemes != NULL)
  1952. end_phonemes[0] = 0;
  1953. while (((c = *p) != ' ') && (c != 0)) {
  1954. wc_bytes = utf8_in(&wc, p);
  1955. if (IsAlpha(wc))
  1956. any_alpha++;
  1957. n = tr->groups2_count[c];
  1958. if (IsDigit(wc) && ((tr->langopts.tone_numbers == 0) || !any_alpha)) {
  1959. // lookup the number in *_list not *_rules
  1960. char string[8];
  1961. char buf[40];
  1962. string[0] = '_';
  1963. memcpy(&string[1], p, wc_bytes);
  1964. string[1+wc_bytes] = 0;
  1965. Lookup(tr, string, buf);
  1966. if (++digit_count >= 2) {
  1967. strcat(buf, str_pause);
  1968. digit_count = 0;
  1969. }
  1970. AppendPhonemes(tr, phonemes, ph_size, buf);
  1971. p += wc_bytes;
  1972. continue;
  1973. } else {
  1974. digit_count = 0;
  1975. found = 0;
  1976. if (((ix = wc - tr->letter_bits_offset) >= 0) && (ix < 128)) {
  1977. if (tr->groups3[ix] != NULL) {
  1978. MatchRule(tr, &p, p_start, wc_bytes, tr->groups3[ix], &match1, word_flags, dict_flags0);
  1979. found = 1;
  1980. }
  1981. }
  1982. if (!found && (n > 0)) {
  1983. // there are some 2 byte chains for this initial letter
  1984. c2 = p[1];
  1985. c12 = c + (c2 << 8); // 2 characters
  1986. g1 = tr->groups2_start[c];
  1987. for (g = g1; g < (g1+n); g++) {
  1988. if (tr->groups2_name[g] == c12) {
  1989. found = 1;
  1990. p2 = p;
  1991. MatchRule(tr, &p2, p_start, 2, tr->groups2[g], &match2, word_flags, dict_flags0);
  1992. if (match2.points > 0)
  1993. match2.points += 35; // to acount for 2 letters matching
  1994. // now see whether single letter chain gives a better match ?
  1995. MatchRule(tr, &p, p_start, 1, tr->groups1[c], &match1, word_flags, dict_flags0);
  1996. if (match2.points >= match1.points) {
  1997. // use match from the 2-letter group
  1998. memcpy(&match1, &match2, sizeof(MatchRecord));
  1999. p = p2;
  2000. }
  2001. }
  2002. }
  2003. }
  2004. if (!found) {
  2005. // alphabetic, single letter chain
  2006. if (tr->groups1[c] != NULL)
  2007. MatchRule(tr, &p, p_start, 1, tr->groups1[c], &match1, word_flags, dict_flags0);
  2008. else {
  2009. // no group for this letter, use default group
  2010. MatchRule(tr, &p, p_start, 0, tr->groups1[0], &match1, word_flags, dict_flags0);
  2011. if ((match1.points == 0) && ((option_sayas & 0x10) == 0)) {
  2012. n = utf8_in(&letter, p-1)-1;
  2013. if (tr->letter_bits_offset > 0) {
  2014. // not a Latin alphabet, switch to the default Latin alphabet language
  2015. if ((letter <= 0x241) && iswalpha2(letter)) {
  2016. sprintf(phonemes, "%c%s", phonSWITCH, tr->langopts.ascii_language);
  2017. return 0;
  2018. }
  2019. }
  2020. // is it a bracket ?
  2021. if (letter == 0xe000+'(') {
  2022. if (pre_pause < tr->langopts.param2[LOPT_BRACKET_PAUSE])
  2023. pre_pause = tr->langopts.param2[LOPT_BRACKET_PAUSE]; // a bracket, aleady spoken by AnnouncePunctuation()
  2024. }
  2025. if (IsBracket(letter)) {
  2026. if (pre_pause < tr->langopts.param[LOPT_BRACKET_PAUSE])
  2027. pre_pause = tr->langopts.param[LOPT_BRACKET_PAUSE];
  2028. }
  2029. // no match, try removing the accent and re-translating the word
  2030. if ((letter >= 0xc0) && (letter < N_REMOVE_ACCENT) && ((ix = remove_accent[letter-0xc0]) != 0)) {
  2031. // within range of the remove_accent table
  2032. if ((p[-2] != ' ') || (p[n] != ' ')) {
  2033. // not the only letter in the word
  2034. p2 = p-1;
  2035. p[-1] = ix;
  2036. while ((p[0] = p[n]) != ' ') p++;
  2037. while (n-- > 0) *p++ = ' '; // replacement character must be no longer than original
  2038. if (tr->langopts.param[LOPT_DIERESES] && (lookupwchar(diereses_list, letter) > 0)) {
  2039. // vowel with dieresis, replace and continue from this point
  2040. p = p2;
  2041. continue;
  2042. }
  2043. phonemes[0] = 0; // delete any phonemes which have been produced so far
  2044. p = p_start;
  2045. tr->word_vowel_count = 0;
  2046. tr->word_stressed_count = 0;
  2047. continue; // start again at the beginning of the word
  2048. }
  2049. }
  2050. if (((alphabet = AlphabetFromChar(letter)) != NULL) && (alphabet->offset != tr->letter_bits_offset)) {
  2051. if (tr->langopts.alt_alphabet == alphabet->offset) {
  2052. sprintf(phonemes, "%c%s", phonSWITCH, WordToString2(tr->langopts.alt_alphabet_lang));
  2053. return 0;
  2054. }
  2055. if (alphabet->flags & AL_WORDS) {
  2056. // switch to the nominated language for this alphabet
  2057. sprintf(phonemes, "%c%s", phonSWITCH, WordToString2(alphabet->language));
  2058. return 0;
  2059. }
  2060. }
  2061. }
  2062. }
  2063. if (match1.points == 0) {
  2064. if ((wc >= 0x300) && (wc <= 0x36f)) {
  2065. // combining accent inside a word, ignore
  2066. } else if (IsAlpha(wc)) {
  2067. if ((any_alpha > 1) || (p[wc_bytes-1] > ' ')) {
  2068. // an unrecognised character in a word, abort and then spell the word
  2069. phonemes[0] = 0;
  2070. if (dict_flags != NULL)
  2071. dict_flags[0] |= FLAG_SPELLWORD;
  2072. break;
  2073. }
  2074. } else {
  2075. LookupLetter(tr, wc, -1, ph_buf, 0);
  2076. if (ph_buf[0]) {
  2077. match1.phonemes = ph_buf;
  2078. match1.points = 1;
  2079. }
  2080. }
  2081. p += (wc_bytes-1);
  2082. } else
  2083. tr->phonemes_repeat_count = 0;
  2084. }
  2085. }
  2086. if (match1.phonemes == NULL)
  2087. match1.phonemes = "";
  2088. if (match1.points > 0) {
  2089. if (word_flags & FLAG_UNPRON_TEST)
  2090. return match1.end_type | 1;
  2091. if ((match1.phonemes[0] == phonSWITCH) && ((word_flags & FLAG_DONT_SWITCH_TRANSLATOR) == 0)) {
  2092. // an instruction to switch language, return immediately so we can re-translate
  2093. strcpy(phonemes, match1.phonemes);
  2094. return 0;
  2095. }
  2096. if ((option_phonemes & espeakPHONEMES_TRACE) && ((word_flags & FLAG_NO_TRACE) == 0))
  2097. fprintf(f_trans, "\n");
  2098. match1.end_type &= ~SUFX_UNPRON;
  2099. if ((match1.end_type != 0) && (end_phonemes != NULL)) {
  2100. // a standard ending has been found, re-translate the word without it
  2101. if ((match1.end_type & SUFX_P) && (word_flags & FLAG_NO_PREFIX)) {
  2102. // ignore the match on a prefix
  2103. } else {
  2104. if ((match1.end_type & SUFX_P) && ((match1.end_type & 0x7f) == 0)) {
  2105. // no prefix length specified
  2106. match1.end_type |= p - p_start;
  2107. }
  2108. strcpy(end_phonemes, match1.phonemes);
  2109. memcpy(p_start, word_copy, strlen(word_copy));
  2110. return match1.end_type;
  2111. }
  2112. }
  2113. if (match1.del_fwd != NULL)
  2114. *match1.del_fwd = REPLACED_E;
  2115. AppendPhonemes(tr, phonemes, ph_size, match1.phonemes);
  2116. }
  2117. }
  2118. memcpy(p_start, word_copy, strlen(word_copy));
  2119. return 0;
  2120. }
  2121. void ApplySpecialAttribute2(Translator *tr, char *phonemes, int dict_flags)
  2122. {
  2123. // apply after the translation is complete
  2124. int ix;
  2125. int len;
  2126. char *p;
  2127. len = strlen(phonemes);
  2128. if (tr->langopts.param[LOPT_ALT] & 2) {
  2129. for (ix = 0; ix < (len-1); ix++) {
  2130. if (phonemes[ix] == phonSTRESS_P) {
  2131. p = &phonemes[ix+1];
  2132. if ((dict_flags & FLAG_ALT2_TRANS) != 0) {
  2133. if (*p == PhonemeCode('E'))
  2134. *p = PhonemeCode('e');
  2135. if (*p == PhonemeCode('O'))
  2136. *p = PhonemeCode('o');
  2137. } else {
  2138. if (*p == PhonemeCode('e'))
  2139. *p = PhonemeCode('E');
  2140. if (*p == PhonemeCode('o'))
  2141. *p = PhonemeCode('O');
  2142. }
  2143. break;
  2144. }
  2145. }
  2146. }
  2147. }
  2148. int TransposeAlphabet(Translator *tr, char *text)
  2149. {
  2150. // transpose cyrillic alphabet (for example) into ascii (single byte) character codes
  2151. // return: number of bytes, bit 6: 1=used compression
  2152. int c;
  2153. int c2;
  2154. int ix;
  2155. int offset;
  2156. int min;
  2157. int max;
  2158. const char *map;
  2159. char *p = text;
  2160. char *p2;
  2161. int all_alpha = 1;
  2162. int bits;
  2163. int acc;
  2164. int pairs_start;
  2165. const short *pairs_list;
  2166. int bufix;
  2167. char buf[N_WORD_BYTES+1];
  2168. offset = tr->transpose_min - 1;
  2169. min = tr->transpose_min;
  2170. max = tr->transpose_max;
  2171. map = tr->transpose_map;
  2172. pairs_start = max - min + 2;
  2173. bufix = 0;
  2174. do {
  2175. p += utf8_in(&c, p);
  2176. if (c != 0) {
  2177. if ((c >= min) && (c <= max)) {
  2178. if (map == NULL)
  2179. buf[bufix++] = c - offset;
  2180. else {
  2181. // get the code from the transpose map
  2182. if (map[c - min] > 0)
  2183. buf[bufix++] = map[c - min];
  2184. else {
  2185. all_alpha = 0;
  2186. break;
  2187. }
  2188. }
  2189. } else {
  2190. all_alpha = 0;
  2191. break;
  2192. }
  2193. }
  2194. } while ((c != 0) && (bufix < N_WORD_BYTES));
  2195. buf[bufix] = 0;
  2196. if (all_alpha) {
  2197. // compress to 6 bits per character
  2198. acc = 0;
  2199. bits = 0;
  2200. p = buf;
  2201. p2 = buf;
  2202. while ((c = *p++) != 0) {
  2203. if ((pairs_list = tr->frequent_pairs) != NULL) {
  2204. c2 = c + (*p << 8);
  2205. for (ix = 0; c2 >= pairs_list[ix]; ix++) {
  2206. if (c2 == pairs_list[ix]) {
  2207. // found an encoding for a 2-character pair
  2208. c = ix + pairs_start; // 2-character codes start after the single letter codes
  2209. p++;
  2210. break;
  2211. }
  2212. }
  2213. }
  2214. acc = (acc << 6) + (c & 0x3f);
  2215. bits += 6;
  2216. if (bits >= 8) {
  2217. bits -= 8;
  2218. *p2++ = (acc >> bits);
  2219. }
  2220. }
  2221. if (bits > 0)
  2222. *p2++ = (acc << (8-bits));
  2223. *p2 = 0;
  2224. ix = p2 - buf;
  2225. memcpy(text, buf, ix);
  2226. return ix | 0x40; // bit 6 indicates compressed characters
  2227. }
  2228. return strlen(text);
  2229. }
  2230. /* Find an entry in the word_dict file for a specified word.
  2231. Returns NULL if no match, else returns 'word_end'
  2232. word zero terminated word to match
  2233. word2 pointer to next word(s) in the input text (terminated by space)
  2234. flags: returns dictionary flags which are associated with a matched word
  2235. end_flags: indicates whether this is a retranslation after removing a suffix
  2236. */
  2237. static const char *LookupDict2(Translator *tr, const char *word, const char *word2,
  2238. char *phonetic, unsigned int *flags, int end_flags, WORD_TAB *wtab)
  2239. {
  2240. char *p;
  2241. char *next;
  2242. int hash;
  2243. int phoneme_len;
  2244. int wlen;
  2245. unsigned char flag;
  2246. unsigned int dictionary_flags;
  2247. unsigned int dictionary_flags2;
  2248. int condition_failed = 0;
  2249. int n_chars;
  2250. int no_phonemes;
  2251. int skipwords;
  2252. int ix;
  2253. int c;
  2254. const char *word_end;
  2255. const char *word1;
  2256. int wflags = 0;
  2257. int lookup_symbol;
  2258. char word_buf[N_WORD_BYTES+1];
  2259. char dict_flags_buf[80];
  2260. if (wtab != NULL)
  2261. wflags = wtab->flags;
  2262. lookup_symbol = flags[1] & FLAG_LOOKUP_SYMBOL;
  2263. word1 = word;
  2264. if (tr->transpose_min > 0) {
  2265. strncpy0(word_buf, word, N_WORD_BYTES);
  2266. wlen = TransposeAlphabet(tr, word_buf); // bit 6 indicates compressed characters
  2267. word = word_buf;
  2268. } else
  2269. wlen = strlen(word);
  2270. hash = HashDictionary(word);
  2271. p = tr->dict_hashtab[hash];
  2272. if (p == NULL) {
  2273. if (flags != NULL)
  2274. *flags = 0;
  2275. return 0;
  2276. }
  2277. // Find the first entry in the list for this hash value which matches.
  2278. // This corresponds to the last matching entry in the *_list file.
  2279. while (*p != 0) {
  2280. next = p + p[0];
  2281. if (((p[1] & 0x7f) != wlen) || (memcmp(word, &p[2], wlen & 0x3f) != 0)) {
  2282. // bit 6 of wlen indicates whether the word has been compressed; so we need to match on this also.
  2283. p = next;
  2284. continue;
  2285. }
  2286. // found matching entry. Decode the phonetic string
  2287. word_end = word2;
  2288. dictionary_flags = 0;
  2289. dictionary_flags2 = 0;
  2290. no_phonemes = p[1] & 0x80;
  2291. p += ((p[1] & 0x3f) + 2);
  2292. if (no_phonemes) {
  2293. phonetic[0] = 0;
  2294. phoneme_len = 0;
  2295. } else {
  2296. strcpy(phonetic, p);
  2297. phoneme_len = strlen(p);
  2298. p += (phoneme_len + 1);
  2299. }
  2300. while (p < next) {
  2301. // examine the flags which follow the phoneme string
  2302. flag = *p++;
  2303. if (flag >= 100) {
  2304. // conditional rule
  2305. if (flag >= 132) {
  2306. // fail if this condition is set
  2307. if ((tr->dict_condition & (1 << (flag-132))) != 0)
  2308. condition_failed = 1;
  2309. } else {
  2310. // allow only if this condition is set
  2311. if ((tr->dict_condition & (1 << (flag-100))) == 0)
  2312. condition_failed = 1;
  2313. }
  2314. } else if (flag > 80) {
  2315. // flags 81 to 90 match more than one word
  2316. // This comes after the other flags
  2317. n_chars = next - p;
  2318. skipwords = flag - 80;
  2319. // don't use the contraction if any of the words are emphasized
  2320. // or has an embedded command, such as MARK
  2321. if (wtab != NULL) {
  2322. for (ix = 0; ix <= skipwords; ix++) {
  2323. if (wtab[ix].flags & FLAG_EMPHASIZED2)
  2324. condition_failed = 1;
  2325. }
  2326. }
  2327. if (memcmp(word2, p, n_chars) != 0)
  2328. condition_failed = 1;
  2329. if (condition_failed) {
  2330. p = next;
  2331. break;
  2332. }
  2333. dictionary_flags |= FLAG_SKIPWORDS;
  2334. dictionary_skipwords = skipwords;
  2335. p = next;
  2336. word_end = word2 + n_chars;
  2337. } else if (flag > 64) {
  2338. // stressed syllable information, put in bits 0-3
  2339. dictionary_flags = (dictionary_flags & ~0xf) | (flag & 0xf);
  2340. if ((flag & 0xc) == 0xc)
  2341. dictionary_flags |= FLAG_STRESS_END;
  2342. } else if (flag >= 32)
  2343. dictionary_flags2 |= (1L << (flag-32));
  2344. else
  2345. dictionary_flags |= (1L << flag);
  2346. }
  2347. if (condition_failed) {
  2348. condition_failed = 0;
  2349. continue;
  2350. }
  2351. if ((end_flags & FLAG_SUFX) == 0) {
  2352. // no suffix has been removed
  2353. if (dictionary_flags2 & FLAG_STEM)
  2354. continue; // this word must have a suffix
  2355. }
  2356. if ((end_flags & SUFX_P) && (dictionary_flags2 & (FLAG_ONLY | FLAG_ONLY_S)))
  2357. continue; // $only or $onlys, don't match if a prefix has been removed
  2358. if (end_flags & FLAG_SUFX) {
  2359. // a suffix was removed from the word
  2360. if (dictionary_flags2 & FLAG_ONLY)
  2361. continue; // no match if any suffix
  2362. if ((dictionary_flags2 & FLAG_ONLY_S) && ((end_flags & FLAG_SUFX_S) == 0)) {
  2363. // only a 's' suffix allowed, but the suffix wasn't 's'
  2364. continue;
  2365. }
  2366. }
  2367. if (dictionary_flags2 & FLAG_HYPHENATED) {
  2368. if (!(wflags & FLAG_HYPHEN_AFTER))
  2369. continue;
  2370. }
  2371. if (dictionary_flags2 & FLAG_CAPITAL) {
  2372. if (!(wflags & FLAG_FIRST_UPPER))
  2373. continue;
  2374. }
  2375. if (dictionary_flags2 & FLAG_ALLCAPS) {
  2376. if (!(wflags & FLAG_ALL_UPPER))
  2377. continue;
  2378. }
  2379. if (dictionary_flags & FLAG_NEEDS_DOT) {
  2380. if (!(wflags & FLAG_HAS_DOT))
  2381. continue;
  2382. }
  2383. if ((dictionary_flags2 & FLAG_ATEND) && (word_end < translator->clause_end) && (lookup_symbol == 0)) {
  2384. // only use this pronunciation if it's the last word of the clause, or called from Lookup()
  2385. continue;
  2386. }
  2387. if ((dictionary_flags2 & FLAG_ATSTART) && !(wtab->flags & FLAG_FIRST_WORD)) {
  2388. // only use this pronunciation if it's the first word of a clause
  2389. continue;
  2390. }
  2391. if ((dictionary_flags2 & FLAG_SENTENCE) && !(translator->clause_terminator & CLAUSE_BIT_SENTENCE)) {
  2392. // only if this clause is a sentence , i.e. terminator is {. ? !} not {, : :}
  2393. continue;
  2394. }
  2395. if (dictionary_flags2 & FLAG_VERB) {
  2396. // this is a verb-form pronunciation
  2397. if (tr->expect_verb || (tr->expect_verb_s && (end_flags & FLAG_SUFX_S))) {
  2398. // OK, we are expecting a verb
  2399. if ((tr->translator_name == L('e', 'n')) && (tr->prev_dict_flags[0] & FLAG_ALT7_TRANS) && (end_flags & FLAG_SUFX_S)) {
  2400. // lang=en, don't use verb form after 'to' if the word has 's' suffix
  2401. continue;
  2402. }
  2403. } else {
  2404. // don't use the 'verb' pronunciation unless we are expecting a verb
  2405. continue;
  2406. }
  2407. }
  2408. if (dictionary_flags2 & FLAG_PAST) {
  2409. if (!tr->expect_past) {
  2410. // don't use the 'past' pronunciation unless we are expecting past tense
  2411. continue;
  2412. }
  2413. }
  2414. if (dictionary_flags2 & FLAG_NOUN) {
  2415. if ((!tr->expect_noun) || (end_flags & SUFX_V)) {
  2416. // don't use the 'noun' pronunciation unless we are expecting a noun
  2417. continue;
  2418. }
  2419. }
  2420. if (dictionary_flags2 & FLAG_NATIVE) {
  2421. if (tr != translator)
  2422. continue; // don't use if we've switched translators
  2423. }
  2424. if (dictionary_flags & FLAG_ALT2_TRANS) {
  2425. // language specific
  2426. if ((tr->translator_name == L('h', 'u')) && !(tr->prev_dict_flags[0] & FLAG_ALT_TRANS))
  2427. continue;
  2428. }
  2429. if (flags != NULL) {
  2430. flags[0] = dictionary_flags | FLAG_FOUND_ATTRIBUTES;
  2431. flags[1] = dictionary_flags2;
  2432. }
  2433. if (phoneme_len == 0) {
  2434. if (option_phonemes & espeakPHONEMES_TRACE) {
  2435. print_dictionary_flags(flags, dict_flags_buf, sizeof(dict_flags_buf));
  2436. fprintf(f_trans, "Flags: %s %s\n", word1, dict_flags_buf);
  2437. }
  2438. return 0; // no phoneme translation found here, only flags. So use rules
  2439. }
  2440. if (flags != NULL)
  2441. flags[0] |= FLAG_FOUND; // this flag indicates word was found in dictionary
  2442. if (option_phonemes & espeakPHONEMES_TRACE) {
  2443. char ph_decoded[N_WORD_PHONEMES];
  2444. int textmode;
  2445. DecodePhonemes(phonetic, ph_decoded);
  2446. if ((dictionary_flags & FLAG_TEXTMODE) == 0)
  2447. textmode = 0;
  2448. else
  2449. textmode = 1;
  2450. if (textmode == translator->langopts.textmode) {
  2451. // only show this line if the word translates to phonemes, not replacement text
  2452. if ((dictionary_flags & FLAG_SKIPWORDS) && (wtab != NULL)) {
  2453. // matched more than one word
  2454. // (check for wtab prevents showing RULE_SPELLING byte when speaking individual letters)
  2455. memcpy(word_buf, word2, word_end-word2);
  2456. word_buf[word_end-word2-1] = 0;
  2457. fprintf(f_trans, "Found: '%s %s\n", word1, word_buf);
  2458. } else
  2459. fprintf(f_trans, "Found: '%s", word1);
  2460. print_dictionary_flags(flags, dict_flags_buf, sizeof(dict_flags_buf));
  2461. fprintf(f_trans, "' [%s] %s\n", ph_decoded, dict_flags_buf);
  2462. }
  2463. }
  2464. ix = utf8_in(&c, word);
  2465. if ((word[ix] == 0) && !IsAlpha(c))
  2466. flags[0] |= FLAG_MAX3;
  2467. return word_end;
  2468. }
  2469. return 0;
  2470. }
  2471. /* Lookup a specified word in the word dictionary.
  2472. Returns phonetic data in 'phonetic' and bits in 'flags'
  2473. end_flags: indicates if a suffix has been removed
  2474. */
  2475. int LookupDictList(Translator *tr, char **wordptr, char *ph_out, unsigned int *flags, int end_flags, WORD_TAB *wtab)
  2476. {
  2477. int length;
  2478. const char *found;
  2479. const char *word1;
  2480. const char *word2;
  2481. unsigned char c;
  2482. int nbytes;
  2483. int len;
  2484. char word[N_WORD_BYTES];
  2485. static char word_replacement[N_WORD_BYTES];
  2486. length = 0;
  2487. word2 = word1 = *wordptr;
  2488. while ((word2[nbytes = utf8_nbytes(word2)] == ' ') && (word2[nbytes+1] == '.')) {
  2489. // look for an abbreviation of the form a.b.c
  2490. // try removing the spaces between the dots and looking for a match
  2491. memcpy(&word[length], word2, nbytes);
  2492. length += nbytes;
  2493. word[length++] = '.';
  2494. word2 += nbytes+3;
  2495. }
  2496. if (length > 0) {
  2497. // found an abbreviation containing dots
  2498. nbytes = 0;
  2499. while (((c = word2[nbytes]) != 0) && (c != ' '))
  2500. nbytes++;
  2501. memcpy(&word[length], word2, nbytes);
  2502. word[length+nbytes] = 0;
  2503. found = LookupDict2(tr, word, word2, ph_out, flags, end_flags, wtab);
  2504. if (found) {
  2505. // set the skip words flag
  2506. flags[0] |= FLAG_SKIPWORDS;
  2507. dictionary_skipwords = length;
  2508. return 1;
  2509. }
  2510. }
  2511. for (length = 0; length < (N_WORD_BYTES-1); length++) {
  2512. if (((c = *word1++) == 0) || (c == ' '))
  2513. break;
  2514. if ((c == '.') && (length > 0) && (IsDigit09(word[length-1])))
  2515. break; // needed for lang=hu, eg. "december 2.-ig"
  2516. word[length] = c;
  2517. }
  2518. word[length] = 0;
  2519. found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);
  2520. if (flags[0] & FLAG_MAX3) {
  2521. if (strcmp(ph_out, tr->phonemes_repeat) == 0) {
  2522. tr->phonemes_repeat_count++;
  2523. if (tr->phonemes_repeat_count > 3)
  2524. ph_out[0] = 0;
  2525. } else {
  2526. strncpy0(tr->phonemes_repeat, ph_out, sizeof(tr->phonemes_repeat));
  2527. tr->phonemes_repeat_count = 1;
  2528. }
  2529. } else
  2530. tr->phonemes_repeat_count = 0;
  2531. if ((found == 0) && (flags[1] & FLAG_ACCENT)) {
  2532. int letter;
  2533. word2 = word;
  2534. if (*word2 == '_') word2++;
  2535. len = utf8_in(&letter, word2);
  2536. LookupAccentedLetter(tr, letter, ph_out);
  2537. found = word2 + len;
  2538. }
  2539. if (found == 0) {
  2540. ph_out[0] = 0;
  2541. // try modifications to find a recognised word
  2542. if ((end_flags & FLAG_SUFX_E_ADDED) && (word[length-1] == 'e')) {
  2543. // try removing an 'e' which has been added by RemoveEnding
  2544. word[length-1] = 0;
  2545. found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);
  2546. } else if ((end_flags & SUFX_D) && (word[length-1] == word[length-2])) {
  2547. // try removing a double letter
  2548. word[length-1] = 0;
  2549. found = LookupDict2(tr, word, word1, ph_out, flags, end_flags, wtab);
  2550. }
  2551. }
  2552. if (found) {
  2553. // if textmode is the default, then words which have phonemes are marked.
  2554. if (tr->langopts.textmode)
  2555. *flags ^= FLAG_TEXTMODE;
  2556. if (*flags & FLAG_TEXTMODE) {
  2557. // the word translates to replacement text, not to phonemes
  2558. if (end_flags & FLAG_ALLOW_TEXTMODE) {
  2559. // only use replacement text if this is the original word, not if a prefix or suffix has been removed
  2560. word_replacement[0] = 0;
  2561. word_replacement[1] = ' ';
  2562. sprintf(&word_replacement[2], "%s ", ph_out); // replacement word, preceded by zerochar and space
  2563. word1 = *wordptr;
  2564. *wordptr = &word_replacement[2];
  2565. if (option_phonemes & espeakPHONEMES_TRACE) {
  2566. len = found - word1;
  2567. memcpy(word, word1, len); // include multiple matching words
  2568. word[len] = 0;
  2569. fprintf(f_trans, "Replace: %s %s\n", word, *wordptr);
  2570. }
  2571. }
  2572. ph_out[0] = 0;
  2573. return 0;
  2574. }
  2575. return 1;
  2576. }
  2577. ph_out[0] = 0;
  2578. return 0;
  2579. }
  2580. extern char word_phonemes[N_WORD_PHONEMES]; // a word translated into phoneme codes
  2581. int Lookup(Translator *tr, const char *word, char *ph_out)
  2582. {
  2583. // Look up in *_list, returns dictionary flags[0] and phonemes
  2584. int flags0;
  2585. unsigned int flags[2];
  2586. int say_as;
  2587. char *word1 = (char *)word;
  2588. char text[80];
  2589. flags[0] = 0;
  2590. flags[1] = FLAG_LOOKUP_SYMBOL;
  2591. if ((flags0 = LookupDictList(tr, &word1, ph_out, flags, FLAG_ALLOW_TEXTMODE, NULL)) != 0)
  2592. flags0 = flags[0];
  2593. if (flags[0] & FLAG_TEXTMODE) {
  2594. say_as = option_sayas;
  2595. option_sayas = 0; // don't speak replacement word as letter names
  2596. strncpy0(text, word1, sizeof(text));
  2597. flags0 = TranslateWord(tr, text, NULL, NULL);
  2598. strcpy(ph_out, word_phonemes);
  2599. option_sayas = say_as;
  2600. }
  2601. return flags0;
  2602. }
  2603. int LookupFlags(Translator *tr, const char *word, unsigned int **flags_out)
  2604. {
  2605. char buf[100];
  2606. static unsigned int flags[2];
  2607. char *word1 = (char *)word;
  2608. flags[0] = flags[1] = 0;
  2609. LookupDictList(tr, &word1, buf, flags, 0, NULL);
  2610. *flags_out = flags;
  2611. return flags[0];
  2612. }
  2613. int RemoveEnding(Translator *tr, char *word, int end_type, char *word_copy)
  2614. {
  2615. /* Removes a standard suffix from a word, once it has been indicated by the dictionary rules.
  2616. end_type: bits 0-6 number of letters
  2617. bits 8-14 suffix flags
  2618. word_copy: make a copy of the original word
  2619. This routine is language specific. In English it deals with reversing y->i and e-dropping
  2620. that were done when the suffix was added to the original word.
  2621. */
  2622. int i;
  2623. char *word_end;
  2624. int len_ending;
  2625. int end_flags;
  2626. const char *p;
  2627. int len;
  2628. char ending[50];
  2629. // these lists are language specific, but are only relevent if the 'e' suffix flag is used
  2630. static const char *add_e_exceptions[] = {
  2631. "ion", NULL
  2632. };
  2633. static const char *add_e_additions[] = {
  2634. "c", "rs", "ir", "ur", "ath", "ns", "u", NULL
  2635. };
  2636. for (word_end = word; *word_end != ' '; word_end++) {
  2637. // replace discarded 'e's
  2638. if (*word_end == REPLACED_E)
  2639. *word_end = 'e';
  2640. }
  2641. i = word_end - word;
  2642. if (word_copy != NULL) {
  2643. memcpy(word_copy, word, i);
  2644. word_copy[i] = 0;
  2645. }
  2646. // look for multibyte characters to increase the number of bytes to remove
  2647. for (len_ending = i = (end_type & 0x3f); i > 0; i--) { // num.of characters of the suffix
  2648. word_end--;
  2649. while ((*word_end & 0xc0) == 0x80) {
  2650. word_end--; // for multibyte characters
  2651. len_ending++;
  2652. }
  2653. }
  2654. // remove bytes from the end of the word and replace them by spaces
  2655. for (i = 0; (i < len_ending) && (i < (int)sizeof(ending)-1); i++) {
  2656. ending[i] = word_end[i];
  2657. word_end[i] = ' ';
  2658. }
  2659. ending[i] = 0;
  2660. word_end--; // now pointing at last character of stem
  2661. end_flags = (end_type & 0xfff0) | FLAG_SUFX;
  2662. /* add an 'e' to the stem if appropriate,
  2663. if stem ends in vowel+consonant
  2664. or stem ends in 'c' (add 'e' to soften it) */
  2665. if (end_type & SUFX_I) {
  2666. if (word_end[0] == 'i')
  2667. word_end[0] = 'y';
  2668. }
  2669. if (end_type & SUFX_E) {
  2670. if (tr->translator_name == L('n', 'l')) {
  2671. 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])) {
  2672. // double the vowel before the (ascii) final consonant
  2673. word_end[1] = word_end[0];
  2674. word_end[0] = word_end[-1];
  2675. word_end[2] = ' ';
  2676. }
  2677. } else if (tr->translator_name == L('e', 'n')) {
  2678. // add 'e' to end of stem
  2679. if (IsLetter(tr, word_end[-1], LETTERGP_VOWEL2) && IsLetter(tr, word_end[0], 1)) {
  2680. // vowel(incl.'y') + hard.consonant
  2681. for (i = 0; (p = add_e_exceptions[i]) != NULL; i++) {
  2682. len = strlen(p);
  2683. if (memcmp(p, &word_end[1-len], len) == 0)
  2684. break;
  2685. }
  2686. if (p == NULL)
  2687. end_flags |= FLAG_SUFX_E_ADDED; // no exception found
  2688. } else {
  2689. for (i = 0; (p = add_e_additions[i]) != NULL; i++) {
  2690. len = strlen(p);
  2691. if (memcmp(p, &word_end[1-len], len) == 0) {
  2692. end_flags |= FLAG_SUFX_E_ADDED;
  2693. break;
  2694. }
  2695. }
  2696. }
  2697. } else if (tr->langopts.suffix_add_e != 0)
  2698. end_flags |= FLAG_SUFX_E_ADDED;
  2699. if (end_flags & FLAG_SUFX_E_ADDED) {
  2700. utf8_out(tr->langopts.suffix_add_e, &word_end[1]);
  2701. if (option_phonemes & espeakPHONEMES_TRACE)
  2702. fprintf(f_trans, "add e\n");
  2703. }
  2704. }
  2705. if ((end_type & SUFX_V) && (tr->expect_verb == 0))
  2706. tr->expect_verb = 1; // this suffix indicates the verb pronunciation
  2707. if ((strcmp(ending, "s") == 0) || (strcmp(ending, "es") == 0))
  2708. end_flags |= FLAG_SUFX_S;
  2709. if (ending[0] == '\'')
  2710. end_flags &= ~FLAG_SUFX; // don't consider 's as an added suffix
  2711. return end_flags;
  2712. }