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

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