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

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