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

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