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

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