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.

compiledict.c 40KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. /*
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015-2017 Reece H. Dunn
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write see:
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include "config.h"
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <wctype.h>
  29. #include <espeak-ng/espeak_ng.h>
  30. #include <espeak-ng/speak_lib.h>
  31. #include <espeak-ng/encoding.h>
  32. #include "common.h" // for strncpy0
  33. #include "compiledict.h"
  34. #include "dictionary.h" // for EncodePhonemes, HashDicti...
  35. #include "error.h" // for create_file_error_context
  36. #include "mnemonics.h" // for LookupMnemName, MNEM_TAB
  37. #include "phoneme.h" // for PHONEME_TAB_LIST, phonSWITCH, phone...
  38. #include "speech.h" // for path_home
  39. #include "synthesize.h" // for Write4Bytes
  40. static const MNEM_TAB mnem_rules[] = {
  41. { "unpr", DOLLAR_UNPR },
  42. { "noprefix", DOLLAR_NOPREFIX }, // rule fails if a prefix has been removed
  43. { "list", DOLLAR_LIST }, // a pronunciation is given in the *_list file
  44. { "w_alt1", 0x11 },
  45. { "w_alt2", 0x12 },
  46. { "w_alt3", 0x13 },
  47. { "w_alt4", 0x14 },
  48. { "w_alt5", 0x15 },
  49. { "w_alt6", 0x16 },
  50. { "w_alt", 0x11 }, // note: put longer names before their sub-strings
  51. { "p_alt1", 0x21 },
  52. { "p_alt2", 0x22 },
  53. { "p_alt3", 0x23 },
  54. { "p_alt4", 0x24 },
  55. { "p_alt5", 0x25 },
  56. { "p_alt6", 0x26 },
  57. { "p_alt", 0x21 },
  58. { NULL, -1 }
  59. };
  60. static const MNEM_TAB mnem_flags[] = {
  61. // these in the first group put a value in bits0-3 of dictionary_flags
  62. { "$1", 0x41 }, // stress on 1st syllable
  63. { "$2", 0x42 }, // stress on 2nd syllable
  64. { "$3", 0x43 },
  65. { "$4", 0x44 },
  66. { "$5", 0x45 },
  67. { "$6", 0x46 },
  68. { "$7", 0x47 },
  69. { "$u", 0x48 }, // reduce to unstressed
  70. { "$u1", 0x49 },
  71. { "$u2", 0x4a },
  72. { "$u3", 0x4b },
  73. { "$u+", 0x4c }, // reduce to unstressed, but stress at end of clause
  74. { "$u1+", 0x4d },
  75. { "$u2+", 0x4e },
  76. { "$u3+", 0x4f },
  77. // these set the corresponding numbered bit if dictionary_flags
  78. { "$pause", 8 }, // ensure pause before this word
  79. { "$strend", 9 }, // full stress if at end of clause
  80. { "$strend2", 10 }, // full stress if at end of clause, or only followed by unstressed
  81. { "$unstressend", 11 }, // reduce stress at end of clause
  82. { "$accent_before", 12 }, // used with accent names, say this accent name before the letter name
  83. { "$abbrev", 13 }, // use this pronuciation rather than split into letters
  84. // language specific
  85. { "$double", 14 }, // IT double the initial consonant of next word
  86. { "$alt", 15 }, // use alternative pronunciation
  87. { "$alt1", 15 }, // synonym for $alt
  88. { "$alt2", 16 },
  89. { "$alt3", 17 },
  90. { "$alt4", 18 },
  91. { "$alt5", 19 },
  92. { "$alt6", 20 },
  93. { "$alt7", 21 },
  94. { "$combine", 23 }, // Combine with the next word
  95. { "$dot", 24 }, // ignore '.' after this word (abbreviation)
  96. { "$hasdot", 25 }, // use this pronunciation if there is a dot after the word
  97. { "$max3", 27 }, // limit to 3 repetitions
  98. { "$brk", 28 }, // a shorter $pause
  99. { "$text", 29 }, // word translates to replcement text, not phonemes
  100. // flags in dictionary word 2
  101. { "$verbf", 0x20 }, // verb follows
  102. { "$verbsf", 0x21 }, // verb follows, allow -s suffix
  103. { "$nounf", 0x22 }, // noun follows
  104. { "$pastf", 0x23 }, // past tense follows
  105. { "$verb", 0x24 }, // use this pronunciation when its a verb
  106. { "$noun", 0x25 }, // use this pronunciation when its a noun
  107. { "$past", 0x26 }, // use this pronunciation when its past tense
  108. { "$verbextend", 0x28 }, // extend influence of 'verb follows'
  109. { "$capital", 0x29 }, // use this pronunciation if initial letter is upper case
  110. { "$allcaps", 0x2a }, // use this pronunciation if initial letter is upper case
  111. { "$accent", 0x2b }, // character name is base-character name + accent name
  112. { "$sentence", 0x2d }, // only if this clause is a sentence (i.e. terminator is {. ? !} not {, ; :}
  113. { "$only", 0x2e }, // only match on this word without suffix
  114. { "$onlys", 0x2f }, // only match with none, or with 's' suffix
  115. { "$stem", 0x30 }, // must have a suffix
  116. { "$atend", 0x31 }, // use this pronunciation if at end of clause
  117. { "$atstart", 0x32 }, // use this pronunciation at start of clause
  118. { "$native", 0x33 }, // not if we've switched translators
  119. // doesn't set dictionary_flags
  120. { "$?", 100 }, // conditional rule, followed by byte giving the condition number
  121. { "$textmode", 200 },
  122. { "$phonememode", 201 },
  123. { NULL, -1 }
  124. };
  125. #define LEN_GROUP_NAME 12
  126. typedef struct {
  127. char name[LEN_GROUP_NAME+1];
  128. void *start;
  129. size_t length;
  130. int group3_ix;
  131. } RGROUP;
  132. typedef enum
  133. {
  134. LINE_PARSER_WORD = 0,
  135. LINE_PARSER_END_OF_WORD = 1,
  136. LINE_PARSER_MULTIPLE_WORDS = 2,
  137. LINE_PARSER_END_OF_WORDS = 3,
  138. LINE_PARSER_PRONUNCIATION = 4,
  139. LINE_PARSER_END_OF_PRONUNCIATION = 5,
  140. } LINE_PARSER_STATES;
  141. typedef struct {
  142. FILE *f_log;
  143. char word_phonemes[N_WORD_PHONEMES]; // a word translated into phoneme codes
  144. int linenum;
  145. int error_count;
  146. bool text_mode;
  147. int debug_flag;
  148. int error_need_dictionary;
  149. // A hash chain is a linked-list of hash chain entry objects:
  150. // struct hash_chain_entry {
  151. // hash_chain_entry *next_entry;
  152. // // dict_line output from compile_line:
  153. // uint8_t length;
  154. // char contents[length];
  155. // };
  156. char *hash_chains[N_HASH_DICT];
  157. char letterGroupsDefined[N_LETTER_GROUPS];
  158. char rule_cond[80];
  159. char rule_pre[80];
  160. char rule_post[80];
  161. char rule_match[80];
  162. char rule_phonemes[80];
  163. char group_name[LEN_GROUP_NAME+1];
  164. int group3_ix;
  165. } CompileContext;
  166. static void clean_context(CompileContext *ctx) {
  167. for (int i = 0; i < N_HASH_DICT; i++) {
  168. char *p;
  169. while ((p = ctx->hash_chains[i])) {
  170. memcpy(&p, ctx->hash_chains[i], sizeof(char*));
  171. free(ctx->hash_chains[i]);
  172. ctx->hash_chains[i] = p;
  173. }
  174. }
  175. free(ctx);
  176. }
  177. void print_dictionary_flags(unsigned int *flags, char *buf, int buf_len)
  178. {
  179. int stress;
  180. int ix;
  181. const char *name;
  182. int len;
  183. int total = 0;
  184. buf[0] = 0;
  185. if ((stress = flags[0] & 0xf) != 0) {
  186. sprintf(buf, "%s", LookupMnemName(mnem_flags, stress + 0x40));
  187. total = strlen(buf);
  188. buf += total;
  189. }
  190. for (ix = 8; ix < 64; ix++) {
  191. if (((ix < 30) && (flags[0] & (1 << ix))) || ((ix >= 0x20) && (flags[1] & (1 << (ix-0x20))))) {
  192. name = LookupMnemName(mnem_flags, ix);
  193. len = strlen(name) + 1;
  194. total += len;
  195. if (total >= buf_len)
  196. continue;
  197. sprintf(buf, " %s", name);
  198. buf += len;
  199. }
  200. }
  201. }
  202. char *DecodeRule(const char *group_chars, int group_length, char *rule, int control, char *output)
  203. {
  204. // Convert compiled match template to ascii
  205. unsigned char rb;
  206. unsigned char c;
  207. char *p;
  208. char *p_end;
  209. int ix;
  210. int match_type;
  211. bool finished = false;
  212. int value;
  213. int linenum = 0;
  214. int flags;
  215. int suffix_char;
  216. int condition_num = 0;
  217. bool at_start = false;
  218. const char *name;
  219. char buf[200];
  220. char buf_pre[200];
  221. char suffix[20];
  222. static const char symbols[] = {
  223. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  224. '&', '%', '+', '#', 'S', 'D', 'Z', 'A', 'L', '!',
  225. ' ', '@', '?', 'J', 'N', 'K', 'V', '?', 'T', 'X',
  226. '?', 'W'
  227. };
  228. static const char symbols_lg[] = { 'A', 'B', 'C', 'H', 'F', 'G', 'Y' };
  229. match_type = 0;
  230. buf_pre[0] = 0;
  231. for (ix = 0; ix < group_length; ix++)
  232. buf[ix] = group_chars[ix];
  233. buf[ix] = 0;
  234. p = &buf[strlen(buf)];
  235. while (!finished) {
  236. rb = *rule++;
  237. if (rb <= RULE_LINENUM) {
  238. switch (rb)
  239. {
  240. case 0:
  241. case RULE_PHONEMES:
  242. finished = true;
  243. break;
  244. case RULE_PRE_ATSTART:
  245. at_start = true;
  246. // fallthrough:
  247. case RULE_PRE:
  248. match_type = RULE_PRE;
  249. *p = 0;
  250. p = buf_pre;
  251. break;
  252. case RULE_POST:
  253. match_type = RULE_POST;
  254. *p = 0;
  255. strcat(buf, " (");
  256. p = &buf[strlen(buf)];
  257. break;
  258. case RULE_PH_COMMON:
  259. break;
  260. case RULE_CONDITION:
  261. // conditional rule, next byte gives condition number
  262. condition_num = *rule++;
  263. break;
  264. case RULE_LINENUM:
  265. value = (rule[1] & 0xff) - 1;
  266. linenum = (rule[0] & 0xff) - 1 + (value * 255);
  267. rule += 2;
  268. break;
  269. }
  270. continue;
  271. }
  272. if (rb == RULE_DOLLAR) {
  273. value = *rule++ & 0xff;
  274. if ((value != 0x01) || (control & FLAG_UNPRON_TEST)) {
  275. // TODO write the string backwards if in RULE_PRE
  276. p[0] = '$';
  277. name = LookupMnemName(mnem_rules, value);
  278. strcpy(&p[1], name);
  279. p += (strlen(name)+1);
  280. }
  281. c = ' ';
  282. } else if (rb == RULE_ENDING) {
  283. static const char flag_chars[] = "eipvdfq tba ";
  284. flags = ((rule[0] & 0x7f)<< 8) + (rule[1] & 0x7f);
  285. suffix_char = 'S';
  286. if (flags & (SUFX_P >> 8))
  287. suffix_char = 'P';
  288. sprintf(suffix, "%c%d", suffix_char, rule[2] & 0x7f);
  289. rule += 3;
  290. for (ix = 0; ix < 9; ix++) {
  291. if (flags & 1)
  292. sprintf(&suffix[strlen(suffix)], "%c", flag_chars[ix]);
  293. flags = (flags >> 1);
  294. }
  295. strcpy(p, suffix);
  296. p += strlen(suffix);
  297. c = ' ';
  298. } else if (rb == RULE_LETTERGP)
  299. c = symbols_lg[*rule++ - 'A'];
  300. else if (rb == RULE_LETTERGP2) {
  301. value = *rule++ - 'A';
  302. if (value < 0)
  303. value += 256;
  304. p[0] = 'L';
  305. p[1] = (value / 10) + '0';
  306. c = (value % 10) + '0';
  307. if (match_type == RULE_PRE) {
  308. p[0] = c;
  309. c = 'L';
  310. }
  311. p += 2;
  312. } else if (rb <= RULE_LAST_RULE)
  313. c = symbols[rb];
  314. else if (rb == RULE_SPACE)
  315. c = '_';
  316. else
  317. c = rb;
  318. *p++ = c;
  319. }
  320. *p = 0;
  321. p = output;
  322. p_end = p + sizeof(output) - 1;
  323. if (linenum > 0) {
  324. sprintf(p, "%5d:\t", linenum);
  325. p += 7;
  326. }
  327. if (condition_num > 0) {
  328. sprintf(p, "?%d ", condition_num);
  329. p = &p[strlen(p)];
  330. }
  331. if (((ix = strlen(buf_pre)) > 0) || at_start) {
  332. if (at_start)
  333. *p++ = '_';
  334. while ((--ix >= 0) && (p < p_end-3))
  335. *p++ = buf_pre[ix];
  336. *p++ = ')';
  337. *p++ = ' ';
  338. }
  339. *p = 0;
  340. buf[p_end - p] = 0; // prevent overflow in output[]
  341. strcat(p, buf);
  342. ix = strlen(output);
  343. while (ix < 8)
  344. output[ix++] = ' ';
  345. output[ix] = 0;
  346. return output;
  347. }
  348. static int compile_line(CompileContext *ctx, char *linebuf, char *dict_line, int n_dict_line, int *hash)
  349. {
  350. // Compile a line in the language_list file
  351. unsigned char c;
  352. char *p;
  353. char *word;
  354. char *phonetic;
  355. char *phonetic_end;
  356. unsigned int ix;
  357. LINE_PARSER_STATES step;
  358. unsigned int n_flag_codes = 0;
  359. int flagnum;
  360. int flag_offset;
  361. int length;
  362. int multiple_words = 0;
  363. bool multiple_numeric_hyphen = false;
  364. char *multiple_string = NULL;
  365. char *multiple_string_end = NULL;
  366. int len_word;
  367. int len_phonetic;
  368. bool text_not_phonemes = false; // this word specifies replacement text, not phonemes
  369. unsigned int wc;
  370. bool all_upper_case;
  371. char *mnemptr;
  372. unsigned char flag_codes[100];
  373. char encoded_ph[200];
  374. char bad_phoneme_str[4];
  375. int bad_phoneme;
  376. static const char nullstring[] = { 0 };
  377. phonetic = word = (char*)nullstring;
  378. p = linebuf;
  379. step = LINE_PARSER_WORD;
  380. c = *p;
  381. while (c != '\n' && c != '\0') {
  382. c = *p;
  383. if ((c == '?') && (step == 0)) {
  384. // conditional rule, allow only if the numbered condition is set for the voice
  385. flag_offset = 100;
  386. p++;
  387. if (*p == '!') {
  388. // allow only if the numbered condition is NOT set
  389. flag_offset = 132;
  390. p++;
  391. }
  392. ix = 0;
  393. if (IsDigit09(*p)) {
  394. ix += (*p-'0');
  395. p++;
  396. }
  397. if (IsDigit09(*p)) {
  398. ix = ix*10 + (*p-'0');
  399. p++;
  400. }
  401. flag_codes[n_flag_codes++] = ix + flag_offset;
  402. c = *p;
  403. }
  404. if ((c == '$') && isalnum(p[1])) {
  405. // read keyword parameter
  406. mnemptr = p;
  407. while (!isspace2(c = *p)) p++;
  408. *p = 0;
  409. flagnum = LookupMnem(mnem_flags, mnemptr);
  410. if (flagnum > 0) {
  411. if (flagnum == 200)
  412. ctx->text_mode = true;
  413. else if (flagnum == 201)
  414. ctx->text_mode = false;
  415. else if (flagnum == BITNUM_FLAG_TEXTMODE)
  416. text_not_phonemes = true;
  417. else
  418. flag_codes[n_flag_codes++] = flagnum;
  419. } else {
  420. fprintf(ctx->f_log, "%5d: Unknown keyword: %s\n", ctx->linenum, mnemptr);
  421. ctx->error_count++;
  422. }
  423. }
  424. if ((c == '/') && (p[1] == '/') && (multiple_words == 0))
  425. c = '\n'; // "//" treat comment as end of line
  426. switch (step)
  427. {
  428. case LINE_PARSER_WORD:
  429. if (c == '(') {
  430. multiple_words = 1;
  431. word = p+1;
  432. step = LINE_PARSER_END_OF_WORD;
  433. } else if (!isspace2(c)) {
  434. word = p;
  435. step = LINE_PARSER_END_OF_WORD;
  436. }
  437. break;
  438. case LINE_PARSER_END_OF_WORD:
  439. if ((c == '-') && multiple_words) {
  440. if (IsDigit09(word[0]))
  441. multiple_numeric_hyphen = true;
  442. flag_codes[n_flag_codes++] = BITNUM_FLAG_HYPHENATED;
  443. c = ' ';
  444. }
  445. if (isspace2(c)) {
  446. p[0] = 0; // terminate english word
  447. if (multiple_words) {
  448. multiple_string = multiple_string_end = p+1;
  449. step = LINE_PARSER_MULTIPLE_WORDS;
  450. } else
  451. step = LINE_PARSER_END_OF_WORDS;
  452. } else if (c == ')') {
  453. if (multiple_words) {
  454. p[0] = 0;
  455. multiple_words = 0;
  456. step = LINE_PARSER_END_OF_WORDS;
  457. } else if (word[0] != '_') {
  458. fprintf(ctx->f_log, "%5d: Missing '('\n", ctx->linenum);
  459. ctx->error_count++;
  460. step = LINE_PARSER_END_OF_WORDS;
  461. }
  462. }
  463. break;
  464. case LINE_PARSER_MULTIPLE_WORDS:
  465. if (isspace2(c))
  466. multiple_words++;
  467. else if (c == ')') {
  468. p[0] = ' '; // terminate extra string
  469. multiple_string_end = p+1;
  470. step = LINE_PARSER_END_OF_WORDS;
  471. }
  472. break;
  473. case LINE_PARSER_END_OF_WORDS:
  474. if (!isspace2(c)) {
  475. phonetic = p;
  476. step = LINE_PARSER_PRONUNCIATION;
  477. }
  478. break;
  479. case LINE_PARSER_PRONUNCIATION:
  480. if (isspace2(c)) {
  481. phonetic_end = p;
  482. p[0] = 0; // terminate phonetic
  483. step = LINE_PARSER_END_OF_PRONUNCIATION;
  484. }
  485. break;
  486. case LINE_PARSER_END_OF_PRONUNCIATION:
  487. if (!isspace2(c)) {
  488. *phonetic_end = ' ';
  489. step = LINE_PARSER_PRONUNCIATION;
  490. }
  491. break;
  492. }
  493. p++;
  494. }
  495. if (word[0] == 0)
  496. return 0; // blank line
  497. if (ctx->text_mode)
  498. text_not_phonemes = true;
  499. if (text_not_phonemes) {
  500. if (word[0] == '_') {
  501. // This is a special word, used by eSpeak. Translate this into phonemes now
  502. strcat(phonetic, " "); // need a space to indicate word-boundary
  503. // PROBLEM vowel reductions are not applied to the translated phonemes
  504. // condition rules are not applied
  505. TranslateWord(translator, phonetic, NULL, NULL);
  506. text_not_phonemes = false;
  507. strncpy0(encoded_ph, ctx->word_phonemes, N_WORD_BYTES-4);
  508. if ((ctx->word_phonemes[0] == 0) && (ctx->error_need_dictionary < 3)) {
  509. // the dictionary was not loaded, we need a second attempt
  510. ctx->error_need_dictionary++;
  511. fprintf(ctx->f_log, "%5d: Need to compile dictionary again\n", ctx->linenum);
  512. }
  513. } else
  514. // this is replacement text, so don't encode as phonemes. Restrict the length of the replacement word
  515. strncpy0(encoded_ph, phonetic, N_WORD_BYTES-4);
  516. } else {
  517. EncodePhonemes(phonetic, encoded_ph, &bad_phoneme);
  518. if (strchr(encoded_ph, phonSWITCH) != 0)
  519. flag_codes[n_flag_codes++] = BITNUM_FLAG_ONLY_S; // don't match on suffixes (except 's') when switching languages
  520. // check for errors in the phonemes codes
  521. if (bad_phoneme != 0) {
  522. // unrecognised phoneme, report error
  523. bad_phoneme_str[utf8_out(bad_phoneme, bad_phoneme_str)] = 0;
  524. fprintf(ctx->f_log, "%5d: Bad phoneme [%s] (U+%x) in: %s %s\n", ctx->linenum, bad_phoneme_str, bad_phoneme, word, phonetic);
  525. ctx->error_count++;
  526. }
  527. }
  528. if (text_not_phonemes != translator->langopts.textmode)
  529. flag_codes[n_flag_codes++] = BITNUM_FLAG_TEXTMODE;
  530. if (sscanf(word, "U+%x", &wc) == 1) {
  531. // Character code
  532. ix = utf8_out(wc, word);
  533. word[ix] = 0;
  534. } else if (word[0] != '_') {
  535. // convert to lower case, and note if the word is all-capitals
  536. int c2;
  537. all_upper_case = true;
  538. for (p = word;;) {
  539. // this assumes that the lower case char is the same length as the upper case char
  540. // OK, except for Turkish "I", but use towlower() rather than towlower2()
  541. ix = utf8_in(&c2, p);
  542. if (c2 == 0)
  543. break;
  544. if (iswupper(c2))
  545. utf8_out(towlower2(c2, translator), p);
  546. else
  547. all_upper_case = false;
  548. p += ix;
  549. }
  550. if (all_upper_case)
  551. flag_codes[n_flag_codes++] = BITNUM_FLAG_ALLCAPS;
  552. }
  553. len_word = strlen(word);
  554. if (translator->transpose_min > 0)
  555. len_word = TransposeAlphabet(translator, word);
  556. *hash = HashDictionary(word);
  557. len_phonetic = strlen(encoded_ph);
  558. dict_line[1] = len_word; // bit 6 indicates whether the word has been compressed
  559. len_word &= 0x3f;
  560. memcpy(&dict_line[2], word, len_word);
  561. if (len_phonetic == 0) {
  562. // no phonemes specified. set bit 7
  563. dict_line[1] |= 0x80;
  564. length = len_word + 2;
  565. } else {
  566. length = len_word + len_phonetic + 3;
  567. if (length < n_dict_line) {
  568. strcpy(&dict_line[(len_word)+2], encoded_ph);
  569. } else {
  570. fprintf(ctx->f_log, "%5d: Dictionary line length would overflow the data buffer: %d\n", ctx->linenum, length);
  571. ctx->error_count++;
  572. // no phonemes specified. set bit 7
  573. dict_line[1] |= 0x80;
  574. length = len_word + 2;
  575. }
  576. }
  577. for (ix = 0; ix < n_flag_codes; ix++)
  578. dict_line[ix+length] = flag_codes[ix];
  579. length += n_flag_codes;
  580. if ((multiple_string != NULL) && (multiple_words > 0)) {
  581. if (multiple_words > 10) {
  582. fprintf(ctx->f_log, "%5d: Two many parts in a multi-word entry: %d\n", ctx->linenum, multiple_words);
  583. ctx->error_count++;
  584. } else {
  585. dict_line[length++] = 80 + multiple_words;
  586. ix = multiple_string_end - multiple_string;
  587. if (multiple_numeric_hyphen)
  588. dict_line[length++] = ' '; // ???
  589. memcpy(&dict_line[length], multiple_string, ix);
  590. length += ix;
  591. }
  592. }
  593. *((uint8_t *)dict_line) = (uint8_t)length;
  594. return length;
  595. }
  596. static void compile_dictlist_start(CompileContext *ctx)
  597. {
  598. // initialise dictionary list
  599. int ix;
  600. char *p;
  601. char *p2;
  602. for (ix = 0; ix < N_HASH_DICT; ix++) {
  603. p = ctx->hash_chains[ix];
  604. while (p != NULL) {
  605. memcpy(&p2, p, sizeof(char *));
  606. free(p);
  607. p = p2;
  608. }
  609. ctx->hash_chains[ix] = NULL;
  610. }
  611. }
  612. static void compile_dictlist_end(CompileContext *ctx, FILE *f_out)
  613. {
  614. // Write out the compiled dictionary list
  615. int hash;
  616. int length;
  617. char *p;
  618. for (hash = 0; hash < N_HASH_DICT; hash++) {
  619. p = ctx->hash_chains[hash];
  620. while (p != NULL) {
  621. length = *(uint8_t *)(p+sizeof(char *));
  622. fwrite(p+sizeof(char *), length, 1, f_out);
  623. memcpy(&p, p, sizeof(char *));
  624. }
  625. fputc(0, f_out);
  626. }
  627. }
  628. static int compile_dictlist_file(CompileContext *ctx, const char *path, const char *filename)
  629. {
  630. int length;
  631. int hash;
  632. char *p;
  633. int count = 0;
  634. FILE *f_in;
  635. char buf[200];
  636. char fname[sizeof(path_home)+45];
  637. char dict_line[256]; // length is uint8_t, so an entry can't take up more than 256 bytes
  638. ctx->text_mode = false;
  639. // try with and without '.txt' extension
  640. sprintf(fname, "%s%s.txt", path, filename);
  641. if ((f_in = fopen(fname, "r")) == NULL) {
  642. sprintf(fname, "%s%s", path, filename);
  643. if ((f_in = fopen(fname, "r")) == NULL)
  644. return -1;
  645. }
  646. if (ctx->f_log != NULL)
  647. fprintf(ctx->f_log, "Compiling: '%s'\n", fname);
  648. ctx->linenum = 0;
  649. while (fgets(buf, sizeof(buf), f_in) != NULL) {
  650. ctx->linenum++;
  651. length = compile_line(ctx, buf, dict_line, sizeof(dict_line), &hash);
  652. if (length == 0) continue; // blank line
  653. p = (char *)malloc(length+sizeof(char *));
  654. if (p == NULL) {
  655. if (ctx->f_log != NULL) {
  656. fprintf(ctx->f_log, "Can't allocate memory\n");
  657. ctx->error_count++;
  658. }
  659. break;
  660. }
  661. memcpy(p, &ctx->hash_chains[hash], sizeof(char *));
  662. ctx->hash_chains[hash] = p;
  663. // NOTE: dict_line[0] is the entry length (0-255)
  664. memcpy(p+sizeof(char *), dict_line, length);
  665. count++;
  666. }
  667. if (ctx->f_log != NULL)
  668. fprintf(ctx->f_log, "\t%d entries\n", count);
  669. fclose(f_in);
  670. return 0;
  671. }
  672. #define N_RULES 3000 // max rules for each group
  673. static int isHexDigit(int c)
  674. {
  675. if ((c >= '0') && (c <= '9'))
  676. return c - '0';
  677. if ((c >= 'a') && (c <= 'f'))
  678. return c - 'a' + 10;
  679. if ((c >= 'A') && (c <= 'F'))
  680. return c - 'A' + 10;
  681. return -1;
  682. }
  683. static void copy_rule_string(CompileContext *ctx, char *string, int *state_out)
  684. {
  685. // state 0: conditional, 1=pre, 2=match, 3=post, 4=phonemes
  686. char * const outbuf[5] = { ctx->rule_cond, ctx->rule_pre, ctx->rule_match, ctx->rule_post, ctx->rule_phonemes };
  687. static const int next_state[5] = { 2, 2, 4, 4, 4 };
  688. char *output;
  689. char *p;
  690. int ix;
  691. int len;
  692. char c;
  693. int c2, c3;
  694. int sxflags;
  695. int value;
  696. bool literal;
  697. bool hexdigit_input = false;
  698. int state = *state_out;
  699. const MNEM_TAB *mr;
  700. if (string[0] == 0) return;
  701. output = outbuf[state];
  702. if (state == 4) {
  703. // append to any previous phoneme string, i.e. allow spaces in the phoneme string
  704. len = strlen(ctx->rule_phonemes);
  705. if (len > 0)
  706. ctx->rule_phonemes[len++] = ' ';
  707. output = &ctx->rule_phonemes[len];
  708. }
  709. sxflags = 0x808000; // to ensure non-zero bytes
  710. for (p = string, ix = 0;;) {
  711. literal = false;
  712. c = *p++;
  713. if ((c == '0') && (p[0] == 'x') && (isHexDigit(p[1]) >= 0) && (isHexDigit(p[2]) >= 0)) {
  714. hexdigit_input = true;
  715. c = p[1];
  716. p += 2;
  717. }
  718. if (c == '\\') {
  719. c = *p++; // treat next character literally
  720. if ((c >= '0') && (c <= '3') && (p[0] >= '0') && (p[0] <= '7') && (p[1] >= '0') && (p[1] <= '7')) {
  721. // character code given by 3 digit octal value;
  722. c = (c-'0')*64 + (p[0]-'0')*8 + (p[1]-'0');
  723. p += 2;
  724. }
  725. literal = true;
  726. }
  727. if (hexdigit_input) {
  728. if (((c2 = isHexDigit(c)) >= 0) && ((c3 = isHexDigit(p[0])) >= 0)) {
  729. c = c2 * 16 + c3;
  730. literal = true;
  731. p++;
  732. } else
  733. hexdigit_input = false;
  734. }
  735. if ((state == 1) || (state == 3)) {
  736. // replace special characters (note: 'E' is reserved for a replaced silent 'e')
  737. if (literal == false) {
  738. static const char lettergp_letters[9] = { LETTERGP_A, LETTERGP_B, LETTERGP_C, 0, 0, LETTERGP_F, LETTERGP_G, LETTERGP_H, LETTERGP_Y };
  739. switch (c)
  740. {
  741. case '_':
  742. c = RULE_SPACE;
  743. break;
  744. case 'Y':
  745. c = 'I';
  746. // fallthrough:
  747. case 'A': // vowel
  748. case 'B':
  749. case 'C':
  750. case 'H':
  751. case 'F':
  752. case 'G':
  753. if (state == 1) {
  754. // pre-rule, put the number before the RULE_LETTERGP;
  755. output[ix++] = lettergp_letters[c-'A'] + 'A';
  756. c = RULE_LETTERGP;
  757. } else {
  758. output[ix++] = RULE_LETTERGP;
  759. c = lettergp_letters[c-'A'] + 'A';
  760. }
  761. break;
  762. case 'D':
  763. c = RULE_DIGIT;
  764. break;
  765. case 'K':
  766. c = RULE_NOTVOWEL;
  767. break;
  768. case 'N':
  769. c = RULE_NO_SUFFIX;
  770. break;
  771. case 'V':
  772. c = RULE_IFVERB;
  773. break;
  774. case 'Z':
  775. c = RULE_NONALPHA;
  776. break;
  777. case '+':
  778. c = RULE_INC_SCORE;
  779. break;
  780. case '<': // Can't use - as opposite for + because it is used literally as part of word
  781. c = RULE_DEC_SCORE;
  782. break;
  783. case '@':
  784. c = RULE_SYLLABLE;
  785. break;
  786. case '&':
  787. c = RULE_STRESSED;
  788. break;
  789. case '%':
  790. c = RULE_DOUBLE;
  791. break;
  792. case '#':
  793. c = RULE_DEL_FWD;
  794. break;
  795. case '!':
  796. c = RULE_CAPITAL;
  797. break;
  798. case 'T':
  799. output[ix++] = RULE_DOLLAR;
  800. c = 0x11;
  801. break;
  802. case 'W':
  803. c = RULE_SPELLING;
  804. break;
  805. case 'X':
  806. c = RULE_NOVOWELS;
  807. break;
  808. case 'J':
  809. c = RULE_SKIPCHARS;
  810. break;
  811. case 'L':
  812. // expect two digits
  813. c = *p++ - '0';
  814. value = *p++ - '0';
  815. c = c * 10 + value;
  816. if ((value < 0) || (value > 9)) {
  817. c = 0;
  818. fprintf(ctx->f_log, "%5d: Expected 2 digits after 'L'\n", ctx->linenum);
  819. ctx->error_count++;
  820. } else if ((c <= 0) || (c >= N_LETTER_GROUPS) || (ctx->letterGroupsDefined[(int)c] == 0)) {
  821. fprintf(ctx->f_log, "%5d: Letter group L%.2d not defined\n", ctx->linenum, c);
  822. ctx->error_count++;
  823. }
  824. c += 'A';
  825. if (state == 1) {
  826. // pre-rule, put the group number before the RULE_LETTERGP command
  827. output[ix++] = c;
  828. c = RULE_LETTERGP2;
  829. } else
  830. output[ix++] = RULE_LETTERGP2;
  831. break;
  832. case '$':
  833. value = 0;
  834. mr = mnem_rules;
  835. while (mr->mnem != NULL) {
  836. len = strlen(mr->mnem);
  837. if (strncmp(p, mr->mnem, len) == 0) {
  838. value = mr->value;
  839. p += len;
  840. break;
  841. }
  842. mr++;
  843. }
  844. if (state == 1) {
  845. // pre-rule, put the number before the RULE_DOLLAR
  846. output[ix++] = value;
  847. c = RULE_DOLLAR;
  848. } else {
  849. output[ix++] = RULE_DOLLAR;
  850. c = value;
  851. }
  852. if (value == 0) {
  853. fprintf(ctx->f_log, "%5d: $ command not recognized\n", ctx->linenum);
  854. ctx->error_count++;
  855. }
  856. break;
  857. case 'P': // Prefix
  858. sxflags |= SUFX_P;
  859. // fallthrough
  860. case 'S': // Suffix
  861. output[ix++] = RULE_ENDING;
  862. value = 0;
  863. while (!isspace2(c = *p++) && (c != 0)) {
  864. switch (c)
  865. {
  866. case 'e':
  867. sxflags |= SUFX_E;
  868. break;
  869. case 'i':
  870. sxflags |= SUFX_I;
  871. break;
  872. case 'p': // obsolete, replaced by 'P' above
  873. sxflags |= SUFX_P;
  874. break;
  875. case 'v':
  876. sxflags |= SUFX_V;
  877. break;
  878. case 'd':
  879. sxflags |= SUFX_D;
  880. break;
  881. case 'f':
  882. sxflags |= SUFX_F;
  883. break;
  884. case 'q':
  885. sxflags |= SUFX_Q;
  886. break;
  887. case 't':
  888. sxflags |= SUFX_T;
  889. break;
  890. case 'b':
  891. sxflags |= SUFX_B;
  892. break;
  893. case 'a':
  894. sxflags |= SUFX_A;
  895. break;
  896. case 'm':
  897. sxflags |= SUFX_M;
  898. break;
  899. default:
  900. if (IsDigit09(c))
  901. value = (value*10) + (c - '0');
  902. break;
  903. }
  904. }
  905. p--;
  906. output[ix++] = sxflags >> 16;
  907. output[ix++] = sxflags >> 8;
  908. c = value | 0x80;
  909. break;
  910. }
  911. }
  912. }
  913. output[ix++] = c;
  914. if (c == 0) break;
  915. }
  916. *state_out = next_state[state];
  917. }
  918. static char *compile_rule(CompileContext *ctx, char *input)
  919. {
  920. int ix;
  921. unsigned char c;
  922. int wc;
  923. char *p;
  924. char *prule;
  925. int len;
  926. int len_name;
  927. int start;
  928. int state = 2;
  929. bool finish = false;
  930. char buf[80];
  931. char output[150];
  932. int bad_phoneme;
  933. char bad_phoneme_str[4];
  934. buf[0] = 0;
  935. ctx->rule_cond[0] = 0;
  936. ctx->rule_pre[0] = 0;
  937. ctx->rule_post[0] = 0;
  938. ctx->rule_match[0] = 0;
  939. ctx->rule_phonemes[0] = 0;
  940. p = buf;
  941. for (ix = 0; finish == false; ix++) {
  942. switch (c = input[ix])
  943. {
  944. case ')': // end of prefix section
  945. *p = 0;
  946. state = 1;
  947. copy_rule_string(ctx, buf, &state);
  948. p = buf;
  949. break;
  950. case '(': // start of suffix section
  951. *p = 0;
  952. state = 2;
  953. copy_rule_string(ctx, buf, &state);
  954. state = 3;
  955. p = buf;
  956. if (input[ix+1] == ' ') {
  957. fprintf(ctx->f_log, "%5d: Syntax error. Space after (, or negative score for previous rule\n", ctx->linenum);
  958. ctx->error_count++;
  959. }
  960. break;
  961. case '\n': // end of line
  962. case '\r':
  963. case 0: // end of line
  964. *p = 0;
  965. copy_rule_string(ctx, buf, &state);
  966. finish = true;
  967. break;
  968. case '\t': // end of section section
  969. case ' ':
  970. *p = 0;
  971. copy_rule_string(ctx, buf, &state);
  972. p = buf;
  973. break;
  974. case '?':
  975. if (state == 2)
  976. state = 0;
  977. else
  978. *p++ = c;
  979. break;
  980. default:
  981. *p++ = c;
  982. break;
  983. }
  984. }
  985. if (strcmp(ctx->rule_match, "$group") == 0)
  986. strcpy(ctx->rule_match, ctx->group_name);
  987. if (ctx->rule_match[0] == 0) {
  988. if (ctx->rule_post[0] != 0) {
  989. fprintf(ctx->f_log, "%5d: Syntax error\n", ctx->linenum);
  990. ctx->error_count++;
  991. }
  992. return NULL;
  993. }
  994. EncodePhonemes(ctx->rule_phonemes, buf, &bad_phoneme);
  995. if (bad_phoneme != 0) {
  996. bad_phoneme_str[utf8_out(bad_phoneme, bad_phoneme_str)] = 0;
  997. fprintf(ctx->f_log, "%5d: Bad phoneme [%s] (U+%x) in: %s\n", ctx->linenum, bad_phoneme_str, bad_phoneme, input);
  998. ctx->error_count++;
  999. }
  1000. strcpy(output, buf);
  1001. len = strlen(buf)+1;
  1002. len_name = strlen(ctx->group_name);
  1003. if ((len_name > 0) && (memcmp(ctx->rule_match, ctx->group_name, len_name) != 0)) {
  1004. utf8_in(&wc, ctx->rule_match);
  1005. if ((ctx->group_name[0] == '9') && IsDigit(wc)) {
  1006. // numeric group, rule_match starts with a digit, so OK
  1007. } else {
  1008. fprintf(ctx->f_log, "%5d: Wrong initial letters '%s' for group '%s'\n", ctx->linenum, ctx->rule_match, ctx->group_name);
  1009. ctx->error_count++;
  1010. }
  1011. }
  1012. strcpy(&output[len], ctx->rule_match);
  1013. len += strlen(ctx->rule_match);
  1014. if (ctx->debug_flag) {
  1015. output[len] = RULE_LINENUM;
  1016. output[len+1] = (ctx->linenum % 255) + 1;
  1017. output[len+2] = (ctx->linenum / 255) + 1;
  1018. len += 3;
  1019. }
  1020. if (ctx->rule_cond[0] != 0) {
  1021. if (ctx->rule_cond[0] == '!') {
  1022. // allow the rule only if the condition number is NOT set for the voice
  1023. ix = atoi(&ctx->rule_cond[1]) + 32;
  1024. } else {
  1025. // allow the rule only if the condition number is set for the voice
  1026. ix = atoi(ctx->rule_cond);
  1027. }
  1028. if ((ix > 0) && (ix < 255)) {
  1029. output[len++] = RULE_CONDITION;
  1030. output[len++] = ix;
  1031. } else {
  1032. fprintf(ctx->f_log, "%5d: bad condition number ?%d\n", ctx->linenum, ix);
  1033. ctx->error_count++;
  1034. }
  1035. }
  1036. if (ctx->rule_pre[0] != 0) {
  1037. start = 0;
  1038. if (ctx->rule_pre[0] == RULE_SPACE) {
  1039. // omit '_' at the beginning of the pre-string and imply it by using RULE_PRE_ATSTART
  1040. c = RULE_PRE_ATSTART;
  1041. start = 1;
  1042. } else
  1043. c = RULE_PRE;
  1044. output[len++] = c;
  1045. // output PRE string in reverse order
  1046. for (ix = strlen(ctx->rule_pre)-1; ix >= start; ix--)
  1047. output[len++] = ctx->rule_pre[ix];
  1048. }
  1049. if (ctx->rule_post[0] != 0) {
  1050. sprintf(&output[len], "%c%s", RULE_POST, ctx->rule_post);
  1051. len += (strlen(ctx->rule_post)+1);
  1052. }
  1053. output[len++] = 0;
  1054. if ((prule = (char *)malloc(len)) != NULL)
  1055. memcpy(prule, output, len);
  1056. return prule;
  1057. }
  1058. static int __cdecl string_sorter(char **a, char **b)
  1059. {
  1060. char *pa, *pb;
  1061. int ix;
  1062. if ((ix = strcmp(pa = *a, pb = *b)) != 0)
  1063. return ix;
  1064. pa += (strlen(pa)+1);
  1065. pb += (strlen(pb)+1);
  1066. return strcmp(pa, pb);
  1067. }
  1068. static int __cdecl rgroup_sorter(RGROUP *a, RGROUP *b)
  1069. {
  1070. // Sort long names before short names
  1071. int ix;
  1072. ix = strlen(b->name) - strlen(a->name);
  1073. if (ix != 0) return ix;
  1074. ix = strcmp(a->name, b->name);
  1075. if (ix != 0) return ix;
  1076. return (uintptr_t)a->start - (uintptr_t)b->start;
  1077. }
  1078. static void* output_rule_group(int n_rules, char **rules, char *name, size_t *outsize)
  1079. {
  1080. int ix;
  1081. int len1;
  1082. int len2;
  1083. int len_name;
  1084. char *p;
  1085. char *p2, *p3;
  1086. const char *common;
  1087. char *outptr = NULL;
  1088. size_t outpos, outlen = 0;
  1089. short nextchar_count[256];
  1090. memset(nextchar_count, 0, sizeof(nextchar_count));
  1091. len_name = strlen(name);
  1092. // sort the rules in this group by their phoneme string
  1093. common = "";
  1094. qsort((void *)rules, n_rules, sizeof(char *), (int(__cdecl *)(const void *, const void *))string_sorter);
  1095. if (strcmp(name, "9") == 0)
  1096. len_name = 0; // don't remove characters from numeric match strings
  1097. for (ix = 0; ix < n_rules; ix++) {
  1098. p = rules[ix];
  1099. len1 = strlen(p) + 1; // phoneme string
  1100. p3 = &p[len1];
  1101. p2 = p3 + len_name; // remove group name from start of match string
  1102. len2 = strlen(p2);
  1103. nextchar_count[(unsigned char)(p2[0])]++; // the next byte after the group name
  1104. outpos = outlen;
  1105. if ((common[0] != 0) && (strcmp(p, common) == 0)) {
  1106. outlen += len2 + 1;
  1107. outptr = realloc(outptr, outlen);
  1108. memmove(outptr + outpos, p2, len2);
  1109. outptr[outlen-1] = 0;
  1110. } else {
  1111. if ((ix < n_rules-1) && (strcmp(p, rules[ix+1]) == 0)) {
  1112. outlen ++;
  1113. outptr = realloc(outptr, outlen);
  1114. common = rules[ix]; // phoneme string is same as next, set as common
  1115. outptr[outpos++] = RULE_PH_COMMON;
  1116. }
  1117. outlen += len2 + 1 + len1;
  1118. outptr = realloc(outptr, outlen);
  1119. memmove(outptr + outpos, p2, len2);
  1120. outpos += len2;
  1121. outptr[outpos++] = RULE_PHONEMES;
  1122. memmove(outptr + outpos, p, len1);
  1123. }
  1124. }
  1125. if (outsize) *outsize = outlen;
  1126. return outptr;
  1127. }
  1128. static int compile_lettergroup(CompileContext *ctx, char *input, FILE *f_out)
  1129. {
  1130. char *p;
  1131. char *p_start;
  1132. int group;
  1133. int ix;
  1134. int n_items;
  1135. int length;
  1136. int max_length = 0;
  1137. #define N_LETTERGP_ITEMS 200
  1138. char *items[N_LETTERGP_ITEMS];
  1139. char item_length[N_LETTERGP_ITEMS];
  1140. p = input;
  1141. if (!IsDigit09(p[0]) || !IsDigit09(p[1])) {
  1142. fprintf(ctx->f_log, "%5d: Expected 2 digits after '.L'\n", ctx->linenum);
  1143. ctx->error_count++;
  1144. return 1;
  1145. }
  1146. group = atoi(&p[0]);
  1147. if (group >= N_LETTER_GROUPS) {
  1148. fprintf(ctx->f_log, "%5d: lettergroup out of range (01-%.2d)\n", ctx->linenum, N_LETTER_GROUPS-1);
  1149. ctx->error_count++;
  1150. return 1;
  1151. }
  1152. while (!isspace2(*p)) p++;
  1153. fputc(RULE_GROUP_START, f_out);
  1154. fputc(RULE_LETTERGP2, f_out);
  1155. fputc(group + 'A', f_out);
  1156. if (ctx->letterGroupsDefined[group] != 0) {
  1157. fprintf(ctx->f_log, "%5d: lettergroup L%.2d is already defined\n", ctx->linenum, group);
  1158. ctx->error_count++;
  1159. }
  1160. ctx->letterGroupsDefined[group] = 1;
  1161. n_items = 0;
  1162. while (n_items < N_LETTERGP_ITEMS) {
  1163. while (isspace2(*p)) p++;
  1164. if (*p == 0)
  1165. break;
  1166. items[n_items] = p_start = p;
  1167. while ((*p & 0xff) > ' ') {
  1168. if (*p == '_') *p = ' '; // allow '_' for word break
  1169. p++;
  1170. }
  1171. *p++ = 0;
  1172. length = p - p_start;
  1173. if (length > max_length)
  1174. max_length = length;
  1175. item_length[n_items++] = length;
  1176. }
  1177. // write out the items, longest first
  1178. while (max_length > 1) {
  1179. for (ix = 0; ix < n_items; ix++) {
  1180. if (item_length[ix] == max_length)
  1181. fwrite(items[ix], 1, max_length, f_out);
  1182. }
  1183. max_length--;
  1184. }
  1185. fputc(RULE_GROUP_END, f_out);
  1186. return 0;
  1187. }
  1188. static void free_rules(char **rules, int n_rules)
  1189. {
  1190. for (int i = 0; i < n_rules; ++i) {
  1191. free(*rules);
  1192. *rules++ = NULL;
  1193. }
  1194. }
  1195. static espeak_ng_STATUS compile_dictrules(CompileContext *ctx, FILE *f_in, FILE *f_out)
  1196. {
  1197. char *prule;
  1198. unsigned char *p;
  1199. int ix;
  1200. int c;
  1201. int gp;
  1202. int n_rules = 0;
  1203. int count = 0;
  1204. int different;
  1205. int wc;
  1206. int err_n_rules = 0;
  1207. const char *prev_rgroup_name;
  1208. unsigned int char_code;
  1209. int compile_mode = 0;
  1210. char *buf;
  1211. char buf1[500];
  1212. char *rules[N_RULES];
  1213. int n_rgroups = 0;
  1214. int n_groups3 = 0;
  1215. RGROUP rgroup[N_RULE_GROUP2];
  1216. ctx->linenum = 0;
  1217. ctx->group_name[0] = 0;
  1218. for (;;) {
  1219. ctx->linenum++;
  1220. buf = fgets(buf1, sizeof(buf1), f_in);
  1221. if (buf != NULL) {
  1222. if ((p = (unsigned char *)strstr(buf, "//")) != NULL)
  1223. *p = 0;
  1224. if (buf[0] == '\r') buf++; // ignore extra \r in \r\n
  1225. }
  1226. if ((buf == NULL) || (buf[0] == '.')) {
  1227. // next .group or end of file, write out the previous group
  1228. if (n_rules > 0) {
  1229. strcpy(rgroup[n_rgroups].name, ctx->group_name);
  1230. rgroup[n_rgroups].group3_ix = ctx->group3_ix;
  1231. rgroup[n_rgroups].start = output_rule_group(n_rules, rules, ctx->group_name, &rgroup[n_rgroups].length);
  1232. n_rgroups++;
  1233. count += n_rules;
  1234. free_rules(rules, n_rules);
  1235. }
  1236. n_rules = 0;
  1237. err_n_rules = 0;
  1238. if (compile_mode == 2) {
  1239. // end of the character replacements section
  1240. fwrite(&n_rules, 1, 4, f_out); // write a zero word to terminate the replacemenmt list
  1241. fputc(RULE_GROUP_END, f_out);
  1242. compile_mode = 0;
  1243. }
  1244. if (buf == NULL) break; // end of file
  1245. if (memcmp(buf, ".L", 2) == 0) {
  1246. compile_lettergroup(ctx, &buf[2], f_out);
  1247. continue;
  1248. }
  1249. if (memcmp(buf, ".replace", 8) == 0) {
  1250. compile_mode = 2;
  1251. fputc(RULE_GROUP_START, f_out);
  1252. fputc(RULE_REPLACEMENTS, f_out);
  1253. // advance to next word boundary
  1254. while ((ftell(f_out) & 3) != 0)
  1255. fputc(0, f_out);
  1256. }
  1257. if (memcmp(buf, ".group", 6) == 0) {
  1258. compile_mode = 1;
  1259. p = (unsigned char *)&buf[6];
  1260. while ((p[0] == ' ') || (p[0] == '\t')) p++; // Note: Windows isspace(0xe1) gives TRUE !
  1261. ix = 0;
  1262. while ((*p > ' ') && (ix < LEN_GROUP_NAME))
  1263. ctx->group_name[ix++] = *p++;
  1264. ctx->group_name[ix] = 0;
  1265. ctx->group3_ix = 0;
  1266. if (sscanf(ctx->group_name, "0x%x", &char_code) == 1) {
  1267. // group character is given as a character code (max 16 bits)
  1268. p = (unsigned char *)ctx->group_name;
  1269. if (char_code > 0x100)
  1270. *p++ = (char_code >> 8);
  1271. *p++ = char_code;
  1272. *p = 0;
  1273. } else {
  1274. if (translator->letter_bits_offset > 0) {
  1275. utf8_in(&wc, ctx->group_name);
  1276. if (((ix = (wc - translator->letter_bits_offset)) >= 0) && (ix < 128))
  1277. ctx->group3_ix = ix+1; // not zero
  1278. }
  1279. }
  1280. if ((ctx->group3_ix == 0) && (strlen(ctx->group_name) > 2)) {
  1281. if (utf8_in(&c, ctx->group_name) < 2) {
  1282. fprintf(ctx->f_log, "%5d: Group name longer than 2 bytes (UTF8)", ctx->linenum);
  1283. ctx->error_count++;
  1284. }
  1285. ctx->group_name[2] = 0;
  1286. }
  1287. }
  1288. continue;
  1289. }
  1290. switch (compile_mode)
  1291. {
  1292. case 1: // .group
  1293. prule = compile_rule(ctx, buf);
  1294. if (prule != NULL) {
  1295. if (n_rules < N_RULES)
  1296. rules[n_rules++] = prule;
  1297. else {
  1298. if (err_n_rules == 0) {
  1299. fprintf(stderr, "\nExceeded limit of rules (%d) in group '%s'\n", N_RULES, ctx->group_name);
  1300. ctx->error_count++;
  1301. err_n_rules = 1;
  1302. }
  1303. }
  1304. }
  1305. break;
  1306. case 2: // .replace
  1307. p = (unsigned char *)buf;
  1308. while (isspace2(*p)) p++;
  1309. if ((unsigned char)(*p) > 0x20) {
  1310. while ((unsigned char)(*p) > 0x20) { // not space or zero-byte
  1311. fputc(*p, f_out);
  1312. p++;
  1313. }
  1314. fputc(0, f_out);
  1315. while (isspace2(*p)) p++;
  1316. while ((unsigned char)(*p) > 0x20) {
  1317. fputc(*p, f_out);
  1318. p++;
  1319. }
  1320. fputc(0, f_out);
  1321. }
  1322. break;
  1323. }
  1324. }
  1325. qsort((void *)rgroup, n_rgroups, sizeof(rgroup[0]), (int(__cdecl *)(const void *, const void *))rgroup_sorter);
  1326. prev_rgroup_name = "\n";
  1327. for (gp = 0; gp < n_rgroups; gp++) {
  1328. if ((different = strcmp(rgroup[gp].name, prev_rgroup_name)) != 0) {
  1329. // not the same as the previous group
  1330. if (gp > 0)
  1331. fputc(RULE_GROUP_END, f_out);
  1332. fputc(RULE_GROUP_START, f_out);
  1333. if (rgroup[gp].group3_ix != 0) {
  1334. n_groups3++;
  1335. fputc(1, f_out);
  1336. fputc(rgroup[gp].group3_ix, f_out);
  1337. } else
  1338. fprintf(f_out, "%s", prev_rgroup_name = rgroup[gp].name);
  1339. fputc(0, f_out);
  1340. }
  1341. fwrite(rgroup[gp].start, rgroup[gp].length, 1, f_out);
  1342. }
  1343. fputc(RULE_GROUP_END, f_out);
  1344. fputc(0, f_out);
  1345. fprintf(ctx->f_log, "\t%d rules, %d groups (%d)\n\n", count, n_rgroups, n_groups3);
  1346. free_rules(rules, n_rules);
  1347. for (gp = 0; gp < n_rgroups; gp++) {
  1348. free(rgroup[gp].start);
  1349. }
  1350. return ENS_OK;
  1351. }
  1352. #pragma GCC visibility push(default)
  1353. ESPEAK_NG_API espeak_ng_STATUS espeak_ng_CompileDictionary(const char *dsource, const char *dict_name, FILE *log, int flags, espeak_ng_ERROR_CONTEXT *context)
  1354. {
  1355. if (!log) log = stderr;
  1356. if (!dict_name) dict_name = dictionary_name;
  1357. // fname: space to write the filename in case of error
  1358. // flags: bit 0: include source line number information, for debug purposes.
  1359. FILE *f_in;
  1360. FILE *f_out;
  1361. int offset_rules = 0;
  1362. int value;
  1363. char fname_in[sizeof(path_home)+45];
  1364. char fname_out[sizeof(path_home)+15];
  1365. char path[sizeof(path_home)+40]; // path_dsource+20
  1366. CompileContext *ctx = calloc(1, sizeof(CompileContext));
  1367. ctx->error_count = 0;
  1368. ctx->error_need_dictionary = 0;
  1369. memset(ctx->letterGroupsDefined, 0, sizeof(ctx->letterGroupsDefined));
  1370. ctx->debug_flag = flags & 1;
  1371. if (dsource == NULL)
  1372. dsource = "";
  1373. ctx->f_log = log;
  1374. if (ctx->f_log == NULL)
  1375. ctx->f_log = stderr;
  1376. // try with and without '.txt' extension
  1377. sprintf(path, "%s%s_", dsource, dict_name);
  1378. sprintf(fname_in, "%srules.txt", path);
  1379. if ((f_in = fopen(fname_in, "r")) == NULL) {
  1380. sprintf(fname_in, "%srules", path);
  1381. if ((f_in = fopen(fname_in, "r")) == NULL) {
  1382. clean_context(ctx);
  1383. return create_file_error_context(context, errno, fname_in);
  1384. }
  1385. }
  1386. sprintf(fname_out, "%s%c%s_dict", path_home, PATHSEP, dict_name);
  1387. if ((f_out = fopen(fname_out, "wb+")) == NULL) {
  1388. int error = errno;
  1389. fclose(f_in);
  1390. clean_context(ctx);
  1391. return create_file_error_context(context, error, fname_out);
  1392. }
  1393. value = N_HASH_DICT;
  1394. Write4Bytes(f_out, value);
  1395. Write4Bytes(f_out, offset_rules);
  1396. compile_dictlist_start(ctx);
  1397. fprintf(ctx->f_log, "Using phonemetable: '%s'\n", phoneme_tab_list[phoneme_tab_number].name);
  1398. compile_dictlist_file(ctx, path, "roots");
  1399. if (translator->langopts.listx) {
  1400. compile_dictlist_file(ctx, path, "list");
  1401. compile_dictlist_file(ctx, path, "listx");
  1402. } else {
  1403. compile_dictlist_file(ctx, path, "listx");
  1404. compile_dictlist_file(ctx, path, "list");
  1405. }
  1406. compile_dictlist_file(ctx, path, "emoji");
  1407. compile_dictlist_file(ctx, path, "extra");
  1408. compile_dictlist_end(ctx, f_out);
  1409. offset_rules = ftell(f_out);
  1410. fprintf(ctx->f_log, "Compiling: '%s'\n", fname_in);
  1411. espeak_ng_STATUS status = compile_dictrules(ctx, f_in, f_out);
  1412. fclose(f_in);
  1413. fseek(f_out, 4, SEEK_SET);
  1414. Write4Bytes(f_out, offset_rules);
  1415. fclose(f_out);
  1416. fflush(ctx->f_log);
  1417. if (status != ENS_OK) {
  1418. clean_context(ctx);
  1419. return status;
  1420. }
  1421. LoadDictionary(translator, dict_name, 0);
  1422. status = ctx->error_count > 0 ? ENS_COMPILE_ERROR : ENS_OK;
  1423. clean_context(ctx);
  1424. return status;
  1425. }
  1426. #pragma GCC visibility pop