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

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