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

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