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

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