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

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