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

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