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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /***************************************************************************
  2. * Copyright (C) 2005,2006 by Jonathan Duddington *
  3. * [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 2 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 to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include "StdAfx.h"
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <wctype.h>
  26. #include "speak_lib.h"
  27. #include "speech.h"
  28. #include "phoneme.h"
  29. #include "synthesize.h"
  30. #include "translate.h"
  31. #define OPT_PH_COMMON // sort rules by phoneme string, common phoneme string output only once
  32. //#define OPT_FORMAT // format the text and write formatted copy to Log file
  33. //#define OUTPUT_FORMAT
  34. int HashDictionary(const char *string);
  35. static FILE *f_log = NULL;
  36. extern char *dir_dictionary;
  37. int linenum;
  38. static int error_count;
  39. static int transpose_offset; // transpose character range for LookupDictList()
  40. static int transpose_min;
  41. static int transpose_max;
  42. int hash_counts[N_HASH_DICT];
  43. char *hash_chains[N_HASH_DICT];
  44. MNEM_TAB mnem_flags[] = {
  45. // these in the first group put a value in bits0-3 of dictionary_flags
  46. {"$1", 0x41}, // stress on 1st syllable
  47. {"$2", 0x42}, // stress on 2nd syllable
  48. {"$3", 0x43},
  49. {"$4", 0x44},
  50. {"$5", 0x45},
  51. {"$6", 0x46},
  52. {"$7", 0x47},
  53. {"$u", 0x48}, // reduce to unstressed
  54. {"$u1", 0x49},
  55. {"$u2", 0x4a},
  56. {"$u3", 0x4b},
  57. {"$u+", 0x4c}, // reduce to unstressed, but stress at end of clause
  58. {"$u1+", 0x4d},
  59. {"$u2+", 0x4e},
  60. {"$u3+", 0x4f},
  61. // these set the corresponding numbered bit if dictionary_flags
  62. {"$pause", 8}, /* ensure pause before this word */
  63. {"$only", 9}, /* only match on this word without suffix */
  64. {"$onlys", 10}, /* only match with none, or with 's' suffix */
  65. {"$strend", 11}, /* full stress if at end of clause */
  66. {"$strend2", 12}, /* full stress if at end of clause, or only followed by unstressed */
  67. {"$unstressend",13}, /* reduce stress at end of clause */
  68. {"$atend", 14}, /* use this pronunciation if at end of clause */
  69. {"$capital", 15}, /* use this pronunciation if initial letter is upper case */
  70. {"$dot", 16}, /* ignore '.' after this word (abbreviation) */
  71. {"$abbrev", 17}, /* use this pronuciation rather than split into letters */
  72. {"$stem", 18}, // must have a suffix
  73. // language specific
  74. {"$double", 19}, // IT double the initial consonant of next word
  75. {"$alt", 20}, // use alternative pronunciation
  76. {"$alt2", 21},
  77. {"$verbf", 22}, /* verb follows */
  78. {"$verbsf", 23}, /* verb follows, allow -s suffix */
  79. {"$nounf", 24}, /* noun follows */
  80. {"$verb", 25}, /* use this pronunciation when its a verb */
  81. {"$past", 26}, /* use this pronunciation when its past tense */
  82. {"$pastf", 27}, /* past tense follows */
  83. {"$verbextend",28}, /* extend influence of 'verb follows' */
  84. {"$brk", 30}, /* a shorter $pause */
  85. // doesn't set dictionary_flags
  86. {"$?", 100}, // conditional rule, followed by byte giving the condition number
  87. {NULL, -1}
  88. };
  89. typedef struct {
  90. char name[6];
  91. unsigned int start;
  92. unsigned int length;
  93. } RGROUP;
  94. int isspace2(unsigned int c)
  95. {//=========================
  96. // can't use isspace() because on Windows, isspace(0xe1) gives TRUE !
  97. int c2;
  98. if(((c2 = (c & 0xff)) == 0) || (c > ' '))
  99. return(0);
  100. return(1);
  101. }
  102. static FILE *fopen_log(const char *fname,const char *access)
  103. {//==================================================
  104. // performs fopen, but produces error message to f_log if it fails
  105. FILE *f;
  106. if((f = fopen(fname,access)) == NULL)
  107. {
  108. if(f_log != NULL)
  109. fprintf(f_log,"Can't access (%s) file '%s'\n",access,fname);
  110. }
  111. return(f);
  112. }
  113. #ifdef OPT_FORMAT
  114. static const char *lookup_mnem(MNEM_TAB *table, int value)
  115. //========================================================
  116. /* Lookup a mnemonic string in a table, return its name */
  117. {
  118. while(table->mnem != NULL)
  119. {
  120. if(table->value==value)
  121. return(table->mnem);
  122. table++;
  123. }
  124. return("??"); /* not found */
  125. } /* end of mnem */
  126. #endif
  127. int compile_line(char *linebuf, char *dict_line, int *hash)
  128. {//========================================================
  129. // Compile a line in the language_list file
  130. unsigned char c;
  131. char *p;
  132. char *word;
  133. char *phonetic;
  134. int ix;
  135. int step;
  136. int n_flag_codes = 0;
  137. int length;
  138. int multiple_words = 0;
  139. char *multiple_string = NULL;
  140. char *multiple_string_end = NULL;
  141. int len_word;
  142. int len_phonetic;
  143. char *mnemptr;
  144. char *comment;
  145. unsigned char flag_codes[100];
  146. char encoded_ph[200];
  147. unsigned char bad_phoneme[4];
  148. p = linebuf;
  149. comment = NULL;
  150. phonetic = word = "";
  151. step = 0;
  152. c = 0;
  153. while(c != '\n')
  154. {
  155. c = *p;
  156. if((c == '?') && (step==0))
  157. {
  158. p++;
  159. ix = 0;
  160. if(isdigit(*p))
  161. {
  162. ix += (*p-'0');
  163. p++;
  164. }
  165. if(isdigit(*p))
  166. {
  167. ix = ix*10 + (*p-'0');
  168. p++;
  169. }
  170. flag_codes[n_flag_codes++] = ix+100;
  171. c = *p;
  172. }
  173. if((c == '$') && (step != 1))
  174. {
  175. /* read keyword parameter */
  176. mnemptr = p;
  177. while(!isspace2(c = *p)) p++;
  178. *p = 0;
  179. ix = LookupMnem(mnem_flags,mnemptr);
  180. if(ix > 0)
  181. flag_codes[n_flag_codes++] = ix;
  182. else
  183. {
  184. fprintf(f_log,"%5d: Unknown keyword: %s\n",linenum,mnemptr);
  185. error_count++;
  186. }
  187. }
  188. if((c == '/') && (p[1] == '/') && (multiple_words==0))
  189. {
  190. c = '\n'; /* "//" treat comment as end of line */
  191. comment = p;
  192. }
  193. switch(step)
  194. {
  195. case 0:
  196. if(c == '(')
  197. {
  198. multiple_words = 1;
  199. word = p+1;
  200. step = 1;
  201. }
  202. else
  203. if(!isspace2(c))
  204. {
  205. word = p;
  206. step = 1;
  207. }
  208. break;
  209. case 1:
  210. if(isspace2(c))
  211. {
  212. p[0] = 0; /* terminate english word */
  213. if(multiple_words)
  214. {
  215. multiple_string = multiple_string_end = p+1;
  216. step = 2;
  217. }
  218. else
  219. {
  220. step = 3;
  221. }
  222. }
  223. else
  224. if((c == ')') && multiple_words)
  225. {
  226. p[0] = 0;
  227. step = 3;
  228. multiple_words = 0;
  229. }
  230. break;
  231. case 2:
  232. if(isspace2(c))
  233. {
  234. multiple_words++;
  235. }
  236. else
  237. if(c == ')')
  238. {
  239. p[0] = ' '; // terminate extra string
  240. multiple_string_end = p+1;
  241. step = 3;
  242. }
  243. break;
  244. case 3:
  245. if(!isspace2(c))
  246. {
  247. phonetic = p;
  248. step = 4;
  249. }
  250. break;
  251. case 4:
  252. if(isspace2(c))
  253. {
  254. p[0] = 0; /* terminate phonetic */
  255. step = 5;
  256. }
  257. break;
  258. case 5:
  259. break;
  260. }
  261. p++;
  262. }
  263. if(word[0] == 0)
  264. {
  265. #ifdef OPT_FORMAT
  266. if(comment != NULL)
  267. fprintf(f_log,"%s",comment);
  268. else
  269. fputc('\n',f_log);
  270. #endif
  271. return(0); /* blank line */
  272. }
  273. EncodePhonemes(phonetic,encoded_ph,bad_phoneme);
  274. if(strchr(encoded_ph,phonSWITCH) != 0)
  275. {
  276. flag_codes[n_flag_codes++] = BITNUM_FLAG_ONLY_S;
  277. }
  278. for(ix=0; ix<255; ix++)
  279. {
  280. c = encoded_ph[ix];
  281. if(c == 0) break;
  282. if(c == 255)
  283. {
  284. /* unrecognised phoneme, report error */
  285. fprintf(f_log,"%5d: Bad phoneme [%c] (0x%x) in: %s %s\n",linenum,bad_phoneme[0],bad_phoneme[0],word,phonetic);
  286. error_count++;
  287. }
  288. }
  289. if((word[0] & 0x80)==0) // 7 bit ascii only
  290. {
  291. // 1st letter - need to consider utf8 here
  292. word[0] = tolower(word[0]);
  293. }
  294. len_word = strlen(word);
  295. if(transpose_offset > 0)
  296. {
  297. len_word = TransposeAlphabet(word, transpose_offset, transpose_min, transpose_max);
  298. }
  299. *hash = HashDictionary(word);
  300. len_phonetic = strlen(encoded_ph);
  301. dict_line[1] = len_word; // bit 6 indicates whether the word has been compressed
  302. len_word &= 0x3f;
  303. memcpy(&dict_line[2],word,len_word);
  304. if(len_phonetic == 0)
  305. {
  306. // no phonemes specified. set bit 7
  307. dict_line[1] |= 0x80;
  308. length = len_word + 2;
  309. }
  310. else
  311. {
  312. length = len_word + len_phonetic + 3;
  313. strcpy(&dict_line[(len_word)+2],encoded_ph);
  314. }
  315. for(ix=0; ix<n_flag_codes; ix++)
  316. {
  317. dict_line[ix+length] = flag_codes[ix];
  318. }
  319. length += n_flag_codes;
  320. if((multiple_string != NULL) && (multiple_words > 0) && (multiple_words <= 4))
  321. {
  322. dict_line[length++] = 40 + multiple_words;
  323. ix = multiple_string_end - multiple_string;
  324. memcpy(&dict_line[length],multiple_string,ix);
  325. length += ix;
  326. }
  327. dict_line[0] = length;
  328. #ifdef OPT_FORMAT
  329. spaces = 16;
  330. for(ix=0; ix<n_flag_codes; ix++)
  331. {
  332. if(flag_codes[ix] >= 100)
  333. {
  334. fprintf(f_log,"?%d ",flag_codes[ix]-100);
  335. spaces -= 3;
  336. }
  337. }
  338. fprintf(f_log,"%s",word);
  339. spaces -= strlen(word);
  340. DecodePhonemes(encoded_ph,decoded_ph);
  341. while(spaces-- > 0) fputc(' ',f_log);
  342. spaces += (14 - strlen(decoded_ph));
  343. fprintf(f_log," %s",decoded_ph);
  344. while(spaces-- > 0) fputc(' ',f_log);
  345. for(ix=0; ix<n_flag_codes; ix++)
  346. {
  347. if(flag_codes[ix] < 100)
  348. fprintf(f_log," %s",lookup_mnem(mnem_flags,flag_codes[ix]));
  349. }
  350. if(comment != NULL)
  351. fprintf(f_log," %s",comment);
  352. else
  353. fputc('\n',f_log);
  354. #endif
  355. return(length);
  356. } /* end of compile_line */
  357. void compile_dictlist_start(void)
  358. {//==============================
  359. // initialise dictionary list
  360. int ix;
  361. char *p;
  362. char *p2;
  363. for(ix=0; ix<N_HASH_DICT; ix++)
  364. {
  365. p = hash_chains[ix];
  366. while(p != NULL)
  367. {
  368. memcpy(&p2,p,sizeof(char *));
  369. free(p);
  370. p = p2;
  371. }
  372. hash_chains[ix] = NULL;
  373. hash_counts[ix]=0;
  374. }
  375. }
  376. void compile_dictlist_end(FILE *f_out)
  377. {//===================================
  378. // Write out the compiled dictionary list
  379. int hash;
  380. int length;
  381. char *p;
  382. if(f_log != NULL)
  383. {
  384. #ifdef OUTPUT_FORMAT
  385. for(hash=0; hash<N_HASH_DICT; hash++)
  386. {
  387. fprintf(f_log,"%8d",hash_counts[hash]);
  388. if((hash & 7) == 7)
  389. fputc('\n',f_log);
  390. }
  391. fflush(f_log);
  392. #endif
  393. }
  394. for(hash=0; hash<N_HASH_DICT; hash++)
  395. {
  396. p = hash_chains[hash];
  397. hash_counts[hash] = (int)ftell(f_out);
  398. while(p != NULL)
  399. {
  400. length = *(p+sizeof(char *));
  401. fwrite(p+sizeof(char *),length,1,f_out);
  402. memcpy(&p,p,sizeof(char *));
  403. }
  404. fputc(0,f_out);
  405. }
  406. }
  407. int compile_dictlist_file(const char *path, const char* filename)
  408. {//==============================================================
  409. int length;
  410. int hash;
  411. char *p;
  412. int count=0;
  413. FILE *f_in;
  414. char buf[256];
  415. char dict_line[128];
  416. sprintf(buf,"%s%s",path,filename);
  417. if((f_in = fopen(buf,"r")) == NULL)
  418. return(-1);
  419. fprintf(f_log,"Compiling: '%s'\n",buf);
  420. linenum=0;
  421. while(fgets(buf,sizeof(buf),f_in) != NULL)
  422. {
  423. linenum++;
  424. length = compile_line(buf,dict_line,&hash);
  425. if(length == 0) continue; /* blank line */
  426. hash_counts[hash]++;
  427. p = (char *)malloc(length+sizeof(char *));
  428. if(p == NULL)
  429. {
  430. if(f_log != NULL)
  431. {
  432. fprintf(f_log,"Can't allocate memory\n");
  433. error_count++;
  434. }
  435. break;
  436. }
  437. memcpy(p,&hash_chains[hash],sizeof(char *));
  438. hash_chains[hash] = p;
  439. memcpy(p+sizeof(char *),dict_line,length);
  440. count++;
  441. }
  442. fprintf(f_log,"\t%d entries\n",count);
  443. fclose(f_in);
  444. return(0);
  445. } /* end of compile_dictlist_file */
  446. char rule_cond[80];
  447. char rule_pre[80];
  448. char rule_post[80];
  449. char rule_match[80];
  450. char rule_phonemes[80];
  451. char group_name[12];
  452. #define N_RULES 2000 // max rules for each group
  453. int hexdigit(char c)
  454. {//=================
  455. if(isdigit(c))
  456. return(c - '0');
  457. return(tolower(c) - 'a' + 10);
  458. }
  459. void copy_rule_string(char *string, int &state)
  460. {//============================================
  461. // state 0: conditional, 1=pre, 2=match, 3=post, 4=phonemes
  462. static char *outbuf[5] = {rule_cond, rule_pre, rule_match, rule_post, rule_phonemes};
  463. static int next_state[5] = {2,2,4,4,4};
  464. char *output;
  465. char *p;
  466. int ix;
  467. int len;
  468. char c;
  469. int sxflags;
  470. int value;
  471. int literal;
  472. if(string[0] == 0) return;
  473. output = outbuf[state];
  474. if(state==4)
  475. {
  476. // append to any previous phoneme string, i.e. allow spaces in the phoneme string
  477. len = strlen(rule_phonemes);
  478. if(len > 0)
  479. rule_phonemes[len++] = ' ';
  480. output = &rule_phonemes[len];
  481. }
  482. sxflags = 0x808000; // to ensure non-zero bytes
  483. for(p=string,ix=0;;)
  484. {
  485. literal = 0;
  486. c = *p++;
  487. if(c == '\\')
  488. {
  489. c = *p++; // treat next character literally
  490. if((c >= '0') && (c <= '2') && (p[0] >= '0') && (p[0] <= '7') && (p[1] >= '0') && (p[1] <= '7'))
  491. {
  492. // character code given by 3 digit octal value;
  493. c = (c-'0')*64 + (p[0]-'0')*8 + (p[1]-'0');
  494. p += 2;
  495. }
  496. literal = 1;
  497. }
  498. if((state==1) || (state==3))
  499. {
  500. // replace special characters (note: 'E' is reserved for a replaced silent 'e')
  501. if(literal == 0)
  502. {
  503. static const char lettergp_letters[9] = {LETTERGP_A,LETTERGP_B,LETTERGP_C,0,0,LETTERGP_F,LETTERGP_G,LETTERGP_H,LETTERGP_Y};
  504. switch(c)
  505. {
  506. case '_':
  507. c = RULE_SPACE;
  508. break;
  509. case 'Y':
  510. c = 'I'; // drop through to next case
  511. case 'A': // vowel
  512. case 'B':
  513. case 'C':
  514. case 'H':
  515. case 'F':
  516. case 'G':
  517. if(state == 1)
  518. {
  519. // pre-rule, put the number before the RULE_LETTERGP;
  520. output[ix++] = lettergp_letters[c-'A'] + 'A';
  521. c = RULE_LETTERGP;
  522. }
  523. else
  524. {
  525. output[ix++] = RULE_LETTERGP;
  526. c = lettergp_letters[c-'A'] + 'A';
  527. }
  528. break;
  529. case 'D':
  530. c = RULE_DIGIT;
  531. break;
  532. case 'K':
  533. c = RULE_NOTVOWEL;
  534. break;
  535. case 'N':
  536. c = RULE_NO_SUFFIX;
  537. break;
  538. case 'V':
  539. c = RULE_IFVERB;
  540. break;
  541. case 'Z':
  542. c = RULE_NONALPHA;
  543. break;
  544. case '+':
  545. c = RULE_INC_SCORE;
  546. break;
  547. case '@':
  548. c = RULE_SYLLABLE;
  549. break;
  550. case '&':
  551. c = RULE_STRESSED;
  552. break;
  553. case '%':
  554. c = RULE_DOUBLE;
  555. break;
  556. case '#':
  557. c = RULE_DEL_FWD;
  558. break;
  559. case 'T':
  560. c = RULE_ALT1;
  561. break;
  562. case 'W':
  563. c = RULE_SPELLING;
  564. break;
  565. case 'X':
  566. c = RULE_NOVOWELS;
  567. break;
  568. case 'L':
  569. // expect two digits
  570. c = *p++ - '0';
  571. value = *p++ - '0';
  572. c = c * 10 + value;
  573. if((value < 0) || (value > 9) || (c <= 0) || (c >= N_LETTER_GROUPS))
  574. {
  575. c = 0;
  576. fprintf(f_log,"%5d: Expected 2 digits after 'L'",linenum);
  577. error_count++;
  578. }
  579. c += 'A';
  580. if(state == 1)
  581. {
  582. // pre-rule, put the group number before the RULE_LETTERGP command
  583. output[ix++] = c;
  584. c = RULE_LETTERGP2;
  585. }
  586. else
  587. {
  588. output[ix++] = RULE_LETTERGP2;
  589. }
  590. break;
  591. case 'P':
  592. sxflags |= SUFX_P; // Prefix, now drop through to Suffix
  593. case '$': // obsolete, replaced by S
  594. case 'S':
  595. output[ix++] = RULE_ENDING;
  596. value = 0;
  597. while(!isspace2(c = *p++) && (c != 0))
  598. {
  599. switch(c)
  600. {
  601. case 'e':
  602. sxflags |= SUFX_E;
  603. break;
  604. case 'i':
  605. sxflags |= SUFX_I;
  606. break;
  607. case 'p': // obsolete, replaced by 'P' above
  608. sxflags |= SUFX_P;
  609. break;
  610. case 'v':
  611. sxflags |= SUFX_V;
  612. break;
  613. case 'd':
  614. sxflags |= SUFX_D;
  615. break;
  616. case 'f':
  617. sxflags |= SUFX_F;
  618. break;
  619. case 'q':
  620. sxflags |= SUFX_Q;
  621. break;
  622. case 't':
  623. sxflags |= SUFX_T;
  624. break;
  625. default:
  626. if(isdigit(c))
  627. value = (value*10) + (c - '0');
  628. break;
  629. }
  630. }
  631. p--;
  632. output[ix++] = sxflags >> 16;
  633. output[ix++] = sxflags >> 8;
  634. c = value | 0x80;
  635. break;
  636. }
  637. }
  638. }
  639. output[ix++] = c;
  640. if(c == 0) break;
  641. }
  642. state = next_state[state];
  643. } // end of copy_rule_string
  644. char *compile_rule(char *input)
  645. {//============================
  646. int ix;
  647. unsigned char c;
  648. int wc;
  649. char *p;
  650. char *prule;
  651. int len;
  652. int len_name;
  653. int state=2;
  654. int finish=0;
  655. int pre_bracket=0;
  656. char buf[80];
  657. char output[150];
  658. unsigned char bad_phoneme[4];
  659. buf[0]=0;
  660. rule_cond[0]=0;
  661. rule_pre[0]=0;
  662. rule_post[0]=0;
  663. rule_match[0]=0;
  664. rule_phonemes[0]=0;
  665. p = buf;
  666. for(ix=0; finish==0; ix++)
  667. {
  668. c = input[ix];
  669. if((c=='/') && (input[ix+1]=='/'))
  670. c = input[ix] = '\n'; // treat command as end of line
  671. switch(c = input[ix])
  672. {
  673. case ')': // end of prefix section
  674. *p = 0;
  675. state = 1;
  676. pre_bracket = 1;
  677. copy_rule_string(buf,state);
  678. p = buf;
  679. break;
  680. case '(': // start of suffix section
  681. *p = 0;
  682. state = 2;
  683. copy_rule_string(buf,state);
  684. state = 3;
  685. p = buf;
  686. break;
  687. case '\n': // end of line
  688. case '\r':
  689. case 0: // end of line
  690. *p = 0;
  691. copy_rule_string(buf,state);
  692. finish=1;
  693. break;
  694. case '\t': // end of section section
  695. case ' ':
  696. *p = 0;
  697. copy_rule_string(buf,state);
  698. p = buf;
  699. break;
  700. case '?':
  701. if(state==2)
  702. state=0;
  703. else
  704. *p++ = c;
  705. break;
  706. default:
  707. *p++ = c;
  708. break;
  709. }
  710. }
  711. if(strcmp(rule_match,"$group")==0)
  712. strcpy(rule_match,group_name);
  713. if(rule_match[0]==0)
  714. return(NULL);
  715. EncodePhonemes(rule_phonemes,buf,bad_phoneme);
  716. for(ix=0;; ix++)
  717. {
  718. if((c = buf[ix])==0) break;
  719. if(c==255)
  720. {
  721. fprintf(f_log,"%5d: Bad phoneme [%c] in %s",linenum,bad_phoneme[0],input);
  722. error_count++;
  723. break;
  724. }
  725. }
  726. strcpy(output,buf);
  727. len = strlen(buf)+1;
  728. len_name = strlen(group_name);
  729. if((len_name > 0) && (memcmp(rule_match,group_name,len_name) != 0))
  730. {
  731. utf8_in(&wc,rule_match,0);
  732. if((group_name[0] == '9') && IsDigit(wc))
  733. {
  734. // numeric group, rule_match starts with a digit, so OK
  735. }
  736. else
  737. {
  738. fprintf(f_log,"%5d: Wrong initial letters '%s' for group '%s'\n",linenum,rule_match,group_name);
  739. error_count++;
  740. }
  741. }
  742. strcpy(&output[len],rule_match);
  743. len += strlen(rule_match);
  744. if(rule_cond[0] != 0)
  745. {
  746. ix = -1;
  747. ix = atoi(rule_cond);
  748. if((ix > 0) && (ix < 255))
  749. {
  750. output[len++] = RULE_CONDITION;
  751. output[len++] = ix;
  752. }
  753. else
  754. {
  755. fprintf(f_log,"%5d: bad condition number ?%d\n",linenum,ix);
  756. error_count++;
  757. }
  758. }
  759. if(rule_pre[0] != 0)
  760. {
  761. output[len++] = RULE_PRE;
  762. // output PRE string in reverse order
  763. for(ix = strlen(rule_pre)-1; ix>=0; ix--)
  764. output[len++] = rule_pre[ix];
  765. }
  766. if(rule_post[0] != 0)
  767. {
  768. sprintf(&output[len],"%c%s",RULE_POST,rule_post);
  769. len += (strlen(rule_post)+1);
  770. }
  771. output[len++]=0;
  772. prule = (char *)malloc(len);
  773. memcpy(prule,output,len);
  774. return(prule);
  775. } // end of compile_rule
  776. static int __cdecl string_sorter(char **a, char **b)
  777. {//=================================================
  778. char *pa, *pb;
  779. int ix;
  780. if((ix = strcmp(pa = *a,pb = *b)) != 0)
  781. return(ix);
  782. pa += (strlen(pa)+1);
  783. pb += (strlen(pb)+1);
  784. return(strcmp(pa,pb));
  785. } /* end of strcmp2 */
  786. static int __cdecl rgroup_sorter(RGROUP *a, RGROUP *b)
  787. {//===================================================
  788. int ix;
  789. ix = strcmp(a->name,b->name);
  790. if(ix != 0) return(ix);
  791. return(a->start-b->start);
  792. }
  793. #ifdef OUTPUT_FORMAT
  794. void print_rule_group(FILE *f_out, int n_rules, char **rules, char *name)
  795. {//======================================================================
  796. int rule;
  797. int ix;
  798. unsigned char c;
  799. int len1;
  800. int len2;
  801. int spaces;
  802. char *p;
  803. char *pout;
  804. int condition;
  805. char buf[80];
  806. char suffix[12];
  807. static unsigned char symbols[] = {'@','&','%','+','#','$','D','Z','A','B','C','F'};
  808. fprintf(f_out,"\n$group %s\n",name);
  809. for(rule=0; rule<n_rules; rule++)
  810. {
  811. p = rules[rule];
  812. len1 = strlen(p) + 1;
  813. p = &p[len1];
  814. len2 = strlen(p);
  815. rule_match[0]=0;
  816. rule_pre[0]=0;
  817. rule_post[0]=0;
  818. condition = 0;
  819. pout = rule_match;
  820. for(ix=0; ix<len2; ix++)
  821. {
  822. switch(c = p[ix])
  823. {
  824. case RULE_PRE:
  825. *pout = 0;
  826. pout = rule_pre;
  827. break;
  828. case RULE_POST:
  829. *pout = 0;
  830. pout = rule_post;
  831. break;
  832. case RULE_CONDITION:
  833. condition = p[++ix];
  834. break;
  835. case RULE_ENDING:
  836. sprintf(suffix,"$%d[%x]",(p[ix+2]),p[ix+1] & 0x7f);
  837. ix += 2;
  838. strcpy(pout,suffix);
  839. pout += strlen(suffix);
  840. break;
  841. default:
  842. if(c <= RULE_LETTER7)
  843. c = symbols[c-RULE_SYLLABLE];
  844. if(c == ' ')
  845. c = '_';
  846. *pout++ = c;
  847. break;
  848. }
  849. }
  850. *pout = 0;
  851. spaces = 12;
  852. if(condition > 0)
  853. {
  854. sprintf(buf,"?%d ",condition);
  855. spaces -= strlen(buf);
  856. fprintf(f_out,"%s",buf);
  857. }
  858. if(rule_pre[0] != 0)
  859. {
  860. p = buf;
  861. for(ix=strlen(rule_pre)-1;ix>=0;ix--)
  862. *p++ = rule_pre[ix];
  863. sprintf(p,") ");
  864. spaces -= strlen(buf);
  865. for(ix=0; ix<spaces; ix++)
  866. fputc(' ',f_out);
  867. fprintf(f_out,"%s",buf);
  868. spaces = 0;
  869. }
  870. for(ix=0; ix<spaces; ix++)
  871. fputc(' ',f_out);
  872. spaces = 14;
  873. sprintf(buf," %s ",rule_match);
  874. if(rule_post[0] != 0)
  875. {
  876. p = &buf[strlen(buf)];
  877. sprintf(p,"(%s ",rule_post);
  878. }
  879. fprintf(f_out,"%s",buf);
  880. spaces -= strlen(buf);
  881. for(ix=0; ix<spaces; ix++)
  882. fputc(' ',f_out);
  883. DecodePhonemes(rules[rule],buf);
  884. fprintf(f_out,"%s\n",buf); // phonemes
  885. }
  886. }
  887. #endif
  888. void output_rule_group(FILE *f_out, int n_rules, char **rules, char *name)
  889. {//=======================================================================
  890. int ix;
  891. int len1;
  892. int len2;
  893. int len_name;
  894. char *p;
  895. char *p2, *p3;
  896. const char *common;
  897. //fprintf(f_log,"Group %2s n=%2d at 0x%x\n",name,n_rules,ftell(f_out));
  898. len_name = strlen(name);
  899. #ifdef OUTPUT_FORMAT
  900. print_rule_group(f_log,n_rules,rules,name);
  901. #endif
  902. common = "";
  903. #ifdef OPT_PH_COMMON
  904. qsort((void *)rules,n_rules,sizeof(char *),(int (__cdecl *)(const void *,const void *))string_sorter);
  905. #endif
  906. if(strcmp(name,"9")==0)
  907. len_name = 0; // don't remove characters from numeric match strings
  908. for(ix=0; ix<n_rules; ix++)
  909. {
  910. p = rules[ix];
  911. len1 = strlen(p) + 1; // phoneme string
  912. p3 = &p[len1];
  913. p2 = p3 + len_name; // remove group name from start of match string
  914. len2 = strlen(p2);
  915. //fprintf(f_log,"%4x %2d len1=%2d %2x %2x %2x %2x len2=%2d %2x %2x %2x %2x %2x %2x %2x\n",
  916. // ftell(f_out),ix,len1,p[0],p[1],p[2],p[3],len2,p3[0],p3[1],p3[2],p3[3],p3[4],p3[5],p3[6]);
  917. if((common[0] != 0) && (strcmp(p,common)==0))
  918. {
  919. fwrite(p2,len2,1,f_out);
  920. fputc(0,f_out); // no phoneme string, it's the same as previous rule
  921. }
  922. else
  923. {
  924. #ifdef OPT_PH_COMMON
  925. if((ix < n_rules-1) && (strcmp(p,rules[ix+1])==0))
  926. {
  927. common = rules[ix]; // phoneme string is same as next, set as common
  928. fputc(RULE_PH_COMMON,f_out);
  929. }
  930. #endif
  931. fwrite(p2,len2,1,f_out);
  932. fputc(RULE_PHONEMES,f_out);
  933. fwrite(p,len1,1,f_out);
  934. }
  935. }
  936. } // end of output_rule_group
  937. static int compile_lettergroup(char *input, FILE *f_out)
  938. {//=====================================================
  939. char *p;
  940. int group;
  941. p = input;
  942. if(!isdigit(p[0]) || !isdigit(p[1]))
  943. {
  944. return(1);
  945. }
  946. group = atoi(&p[1]);
  947. if(group >= N_LETTER_GROUPS)
  948. return(1);
  949. while(!isspace2(*p)) p++;
  950. fputc(RULE_GROUP_START,f_out);
  951. fputc(RULE_LETTERGP2,f_out);
  952. fputc(group + 'A', f_out);
  953. for(;;)
  954. {
  955. while(isspace2(*p)) p++;
  956. if(*p == 0)
  957. break;
  958. while((*p & 0xff) > ' ')
  959. {
  960. fputc(*p++, f_out);
  961. }
  962. fputc(0,f_out);
  963. }
  964. fputc(RULE_GROUP_END,f_out);
  965. return(0);
  966. }
  967. static int compile_dictrules(FILE *f_in, FILE *f_out, char *fname_temp)
  968. {//====================================================================
  969. char *prule;
  970. unsigned char *p;
  971. int ix;
  972. int c;
  973. int gp;
  974. FILE *f_temp;
  975. int n_rules=0;
  976. int count=0;
  977. int different;
  978. char *prev_rgroup_name;
  979. unsigned int char_code;
  980. char *buf;
  981. char buf1[120];
  982. char *rules[N_RULES];
  983. int n_rgroups = 0;
  984. RGROUP rgroup[N_RULE_GROUP2];
  985. linenum = 0;
  986. group_name[0] = 0;
  987. if((f_temp = fopen_log(fname_temp,"wb")) == NULL)
  988. return(1);
  989. for(;;)
  990. {
  991. linenum++;
  992. buf = fgets(buf1,sizeof(buf1),f_in);
  993. if((buf != NULL) && (buf[0] == '\r')) buf++; // ignore extra \r in \r\n
  994. if((buf != NULL) && (memcmp(buf,".L",2)==0))
  995. {
  996. if(compile_lettergroup(&buf[2], f_out) != 0)
  997. {
  998. fprintf(f_log,"%5d: Bad lettergroup\n",linenum);
  999. error_count++;
  1000. }
  1001. continue;
  1002. }
  1003. if((buf == NULL) || (memcmp(buf,".group",6)==0))
  1004. {
  1005. // next .group or end of file, write out the previous group
  1006. if(n_rules > 0)
  1007. {
  1008. strcpy(rgroup[n_rgroups].name,group_name);
  1009. rgroup[n_rgroups].start = ftell(f_temp);
  1010. output_rule_group(f_temp,n_rules,rules,group_name);
  1011. rgroup[n_rgroups].length = ftell(f_temp) - rgroup[n_rgroups].start;
  1012. n_rgroups++;
  1013. count += n_rules;
  1014. }
  1015. n_rules = 0;
  1016. if(buf == NULL) break; // end of file
  1017. p = (unsigned char *)&buf[6];
  1018. while((p[0]==' ') || (p[0]=='\t')) p++; // Note: Windows isspace(0xe1) gives TRUE !
  1019. ix = 0;
  1020. while((*p > ' ') && (ix<12))
  1021. group_name[ix++] = *p++;
  1022. group_name[ix]=0;
  1023. if(sscanf(group_name,"0x%x",&char_code)==1)
  1024. {
  1025. // group character is given as a character code (max 16 bits)
  1026. p = (unsigned char *)group_name;
  1027. if(char_code > 0x100)
  1028. {
  1029. *p++ = (char_code >> 8);
  1030. }
  1031. *p++ = char_code;
  1032. *p = 0;
  1033. }
  1034. if(strlen(group_name) > 2)
  1035. {
  1036. if(utf8_in(&c,group_name,0) < 2)
  1037. {
  1038. fprintf(f_log,"%5d: Group name longer than 2 bytes (UTF8)",linenum);
  1039. error_count++;
  1040. }
  1041. group_name[2] = 0;
  1042. }
  1043. continue;
  1044. }
  1045. prule = compile_rule(buf);
  1046. if((prule != NULL) && (n_rules < N_RULES))
  1047. {
  1048. rules[n_rules++] = prule;
  1049. }
  1050. }
  1051. fclose(f_temp);
  1052. qsort((void *)rgroup,n_rgroups,sizeof(rgroup[0]),(int (__cdecl *)(const void *,const void *))rgroup_sorter);
  1053. if((f_temp = fopen(fname_temp,"rb"))==NULL)
  1054. return(2);
  1055. prev_rgroup_name = "\n";
  1056. for(gp = 0; gp < n_rgroups; gp++)
  1057. {
  1058. fseek(f_temp,rgroup[gp].start,SEEK_SET);
  1059. if((different = strcmp(rgroup[gp].name, prev_rgroup_name)) != 0)
  1060. {
  1061. // not the same as the previous group
  1062. if(gp > 0)
  1063. fputc(RULE_GROUP_END,f_out);
  1064. fputc(RULE_GROUP_START,f_out);
  1065. fprintf(f_out, prev_rgroup_name = rgroup[gp].name);
  1066. fputc(0,f_out);
  1067. }
  1068. for(ix=rgroup[gp].length; ix>0; ix--)
  1069. {
  1070. c = fgetc(f_temp);
  1071. fputc(c,f_out);
  1072. }
  1073. if(different)
  1074. {
  1075. }
  1076. }
  1077. fputc(RULE_GROUP_END,f_out);
  1078. fputc(0,f_out);
  1079. fclose(f_temp);
  1080. remove(fname_temp);
  1081. fprintf(f_log,"\t%d rules, %d groups\n\n",count,n_rgroups);
  1082. return(0);
  1083. } // end of compile_dictrules
  1084. int CompileDictionary(const char *dsource, const char *dict_name, FILE *log, char *fname)
  1085. {//======================================================================================
  1086. // fname: space to write the filename in case of error
  1087. FILE *f_in;
  1088. FILE *f_out;
  1089. int offset_rules=0;
  1090. int value;
  1091. char fname_buf[130];
  1092. char fname_temp[130];
  1093. char path[80];
  1094. error_count = 0;
  1095. if(dsource == NULL)
  1096. dsource = "";
  1097. if(fname == NULL)
  1098. fname = fname_buf;
  1099. f_log = log;
  1100. //f_log = fopen("log2.txt","w");
  1101. if(f_log == NULL)
  1102. f_log = stderr;
  1103. sprintf(path,"%s%s_",dsource,dict_name);
  1104. sprintf(fname,"%s%c%s_dict",path_home,PATHSEP,dict_name);
  1105. if((f_out = fopen_log(fname,"wb+")) == NULL)
  1106. {
  1107. return(-1);
  1108. }
  1109. sprintf(fname_temp,"%s%ctemp",path_home,PATHSEP);
  1110. transpose_offset = 0;
  1111. if(strcmp(dict_name,"ru") == 0)
  1112. {
  1113. // transpose cyrillic alphabet from unicode to iso8859-5
  1114. // transpose_offset = 0x430-0xd0;
  1115. transpose_offset = 0x42f; // range 0x01 to 0x22
  1116. transpose_min = 0x430;
  1117. transpose_max = 0x451;
  1118. }
  1119. value = N_HASH_DICT;
  1120. fwrite(&value,4,1,f_out);
  1121. fwrite(&offset_rules,4,1,f_out);
  1122. compile_dictlist_start();
  1123. fprintf(f_log,"Using phonemetable: '%s'\n",PhonemeTabName());
  1124. compile_dictlist_file(path,"roots");
  1125. compile_dictlist_file(path,"listx");
  1126. compile_dictlist_file(path,"list");
  1127. compile_dictlist_file(path,"extra");
  1128. compile_dictlist_end(f_out);
  1129. offset_rules = ftell(f_out);
  1130. sprintf(fname,"%srules",path);
  1131. fprintf(f_log,"Compiling: '%s'\n",fname);
  1132. f_in = fopen_log(fname,"r");
  1133. if(f_in == NULL)
  1134. {
  1135. return(-1);
  1136. }
  1137. compile_dictrules(f_in,f_out,fname_temp);
  1138. fclose(f_in);
  1139. fseek(f_out,4,SEEK_SET);
  1140. fwrite(&offset_rules,4,1,f_out);
  1141. fclose(f_out);
  1142. translator->LoadDictionary(dict_name,0);
  1143. return(error_count);
  1144. } // end of compile_dictionary