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

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