eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compiledict.c 37KB

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