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

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