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

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