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

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