eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.

compiledict.c 39KB

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