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

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