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

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