eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compiledict.cpp 40KB

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