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

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