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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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. // If first letter is uppercase, convert to lower case. (Only if it's 7bit ascii)
  299. // ??? need to consider utf8 here
  300. word[0] = tolower(word[0]);
  301. }
  302. len_word = strlen(word);
  303. if(transpose_offset > 0)
  304. {
  305. len_word = TransposeAlphabet(word, transpose_offset, transpose_min, transpose_max);
  306. }
  307. *hash = HashDictionary(word);
  308. len_phonetic = strlen(encoded_ph);
  309. dict_line[1] = len_word; // bit 6 indicates whether the word has been compressed
  310. len_word &= 0x3f;
  311. memcpy(&dict_line[2],word,len_word);
  312. if(len_phonetic == 0)
  313. {
  314. // no phonemes specified. set bit 7
  315. dict_line[1] |= 0x80;
  316. length = len_word + 2;
  317. }
  318. else
  319. {
  320. length = len_word + len_phonetic + 3;
  321. strcpy(&dict_line[(len_word)+2],encoded_ph);
  322. }
  323. for(ix=0; ix<n_flag_codes; ix++)
  324. {
  325. dict_line[ix+length] = flag_codes[ix];
  326. }
  327. length += n_flag_codes;
  328. if((multiple_string != NULL) && (multiple_words > 0))
  329. {
  330. if(multiple_words > 10)
  331. {
  332. fprintf(f_log,"%5d: Two many parts in a multi-word entry: %d\n",linenum,multiple_words);
  333. }
  334. else
  335. {
  336. dict_line[length++] = 40 + multiple_words;
  337. ix = multiple_string_end - multiple_string;
  338. memcpy(&dict_line[length],multiple_string,ix);
  339. length += ix;
  340. }
  341. }
  342. dict_line[0] = length;
  343. #ifdef OPT_FORMAT
  344. spaces = 16;
  345. for(ix=0; ix<n_flag_codes; ix++)
  346. {
  347. if(flag_codes[ix] >= 100)
  348. {
  349. fprintf(f_log,"?%d ",flag_codes[ix]-100);
  350. spaces -= 3;
  351. }
  352. }
  353. fprintf(f_log,"%s",word);
  354. spaces -= strlen(word);
  355. DecodePhonemes(encoded_ph,decoded_ph);
  356. while(spaces-- > 0) fputc(' ',f_log);
  357. spaces += (14 - strlen(decoded_ph));
  358. fprintf(f_log," %s",decoded_ph);
  359. while(spaces-- > 0) fputc(' ',f_log);
  360. for(ix=0; ix<n_flag_codes; ix++)
  361. {
  362. if(flag_codes[ix] < 100)
  363. fprintf(f_log," %s",lookup_mnem(mnem_flags,flag_codes[ix]));
  364. }
  365. if(comment != NULL)
  366. fprintf(f_log," %s",comment);
  367. else
  368. fputc('\n',f_log);
  369. #endif
  370. return(length);
  371. } /* end of compile_line */
  372. void compile_dictlist_start(void)
  373. {//==============================
  374. // initialise dictionary list
  375. int ix;
  376. char *p;
  377. char *p2;
  378. for(ix=0; ix<N_HASH_DICT; ix++)
  379. {
  380. p = hash_chains[ix];
  381. while(p != NULL)
  382. {
  383. memcpy(&p2,p,sizeof(char *));
  384. free(p);
  385. p = p2;
  386. }
  387. hash_chains[ix] = NULL;
  388. hash_counts[ix]=0;
  389. }
  390. }
  391. void compile_dictlist_end(FILE *f_out)
  392. {//===================================
  393. // Write out the compiled dictionary list
  394. int hash;
  395. int length;
  396. char *p;
  397. if(f_log != NULL)
  398. {
  399. #ifdef OUTPUT_FORMAT
  400. for(hash=0; hash<N_HASH_DICT; hash++)
  401. {
  402. fprintf(f_log,"%8d",hash_counts[hash]);
  403. if((hash & 7) == 7)
  404. fputc('\n',f_log);
  405. }
  406. fflush(f_log);
  407. #endif
  408. }
  409. for(hash=0; hash<N_HASH_DICT; hash++)
  410. {
  411. p = hash_chains[hash];
  412. hash_counts[hash] = (int)ftell(f_out);
  413. while(p != NULL)
  414. {
  415. length = *(p+sizeof(char *));
  416. fwrite(p+sizeof(char *),length,1,f_out);
  417. memcpy(&p,p,sizeof(char *));
  418. }
  419. fputc(0,f_out);
  420. }
  421. }
  422. int compile_dictlist_file(const char *path, const char* filename)
  423. {//==============================================================
  424. int length;
  425. int hash;
  426. char *p;
  427. int count=0;
  428. FILE *f_in;
  429. char buf[sizeof(path_home)+45];
  430. char dict_line[128];
  431. sprintf(buf,"%s%s",path,filename);
  432. if((f_in = fopen(buf,"r")) == NULL)
  433. return(-1);
  434. fprintf(f_log,"Compiling: '%s'\n",buf);
  435. linenum=0;
  436. while(fgets(buf,sizeof(buf),f_in) != NULL)
  437. {
  438. linenum++;
  439. length = compile_line(buf,dict_line,&hash);
  440. if(length == 0) continue; /* blank line */
  441. hash_counts[hash]++;
  442. p = (char *)malloc(length+sizeof(char *));
  443. if(p == NULL)
  444. {
  445. if(f_log != NULL)
  446. {
  447. fprintf(f_log,"Can't allocate memory\n");
  448. error_count++;
  449. }
  450. break;
  451. }
  452. memcpy(p,&hash_chains[hash],sizeof(char *));
  453. hash_chains[hash] = p;
  454. memcpy(p+sizeof(char *),dict_line,length);
  455. count++;
  456. }
  457. fprintf(f_log,"\t%d entries\n",count);
  458. fclose(f_in);
  459. return(0);
  460. } /* end of compile_dictlist_file */
  461. char rule_cond[80];
  462. char rule_pre[80];
  463. char rule_post[80];
  464. char rule_match[80];
  465. char rule_phonemes[80];
  466. char group_name[12];
  467. #define N_RULES 2000 // max rules for each group
  468. int hexdigit(char c)
  469. {//=================
  470. if(isdigit(c))
  471. return(c - '0');
  472. return(tolower(c) - 'a' + 10);
  473. }
  474. void copy_rule_string(char *string, int &state)
  475. {//============================================
  476. // state 0: conditional, 1=pre, 2=match, 3=post, 4=phonemes
  477. static char *outbuf[5] = {rule_cond, rule_pre, rule_match, rule_post, rule_phonemes};
  478. static int next_state[5] = {2,2,4,4,4};
  479. char *output;
  480. char *p;
  481. int ix;
  482. int len;
  483. char c;
  484. int sxflags;
  485. int value;
  486. int literal;
  487. if(string[0] == 0) return;
  488. output = outbuf[state];
  489. if(state==4)
  490. {
  491. // append to any previous phoneme string, i.e. allow spaces in the phoneme string
  492. len = strlen(rule_phonemes);
  493. if(len > 0)
  494. rule_phonemes[len++] = ' ';
  495. output = &rule_phonemes[len];
  496. }
  497. sxflags = 0x808000; // to ensure non-zero bytes
  498. for(p=string,ix=0;;)
  499. {
  500. literal = 0;
  501. c = *p++;
  502. if(c == '\\')
  503. {
  504. c = *p++; // treat next character literally
  505. if((c >= '0') && (c <= '2') && (p[0] >= '0') && (p[0] <= '7') && (p[1] >= '0') && (p[1] <= '7'))
  506. {
  507. // character code given by 3 digit octal value;
  508. c = (c-'0')*64 + (p[0]-'0')*8 + (p[1]-'0');
  509. p += 2;
  510. }
  511. literal = 1;
  512. }
  513. if((state==1) || (state==3))
  514. {
  515. // replace special characters (note: 'E' is reserved for a replaced silent 'e')
  516. if(literal == 0)
  517. {
  518. static const char lettergp_letters[9] = {LETTERGP_A,LETTERGP_B,LETTERGP_C,0,0,LETTERGP_F,LETTERGP_G,LETTERGP_H,LETTERGP_Y};
  519. switch(c)
  520. {
  521. case '_':
  522. c = RULE_SPACE;
  523. break;
  524. case 'Y':
  525. c = 'I'; // drop through to next case
  526. case 'A': // vowel
  527. case 'B':
  528. case 'C':
  529. case 'H':
  530. case 'F':
  531. case 'G':
  532. if(state == 1)
  533. {
  534. // pre-rule, put the number before the RULE_LETTERGP;
  535. output[ix++] = lettergp_letters[c-'A'] + 'A';
  536. c = RULE_LETTERGP;
  537. }
  538. else
  539. {
  540. output[ix++] = RULE_LETTERGP;
  541. c = lettergp_letters[c-'A'] + 'A';
  542. }
  543. break;
  544. case 'D':
  545. c = RULE_DIGIT;
  546. break;
  547. case 'K':
  548. c = RULE_NOTVOWEL;
  549. break;
  550. case 'N':
  551. c = RULE_NO_SUFFIX;
  552. break;
  553. case 'V':
  554. c = RULE_IFVERB;
  555. break;
  556. case 'Z':
  557. c = RULE_NONALPHA;
  558. break;
  559. case '+':
  560. c = RULE_INC_SCORE;
  561. break;
  562. case '@':
  563. c = RULE_SYLLABLE;
  564. break;
  565. case '&':
  566. c = RULE_STRESSED;
  567. break;
  568. case '%':
  569. c = RULE_DOUBLE;
  570. break;
  571. case '#':
  572. c = RULE_DEL_FWD;
  573. break;
  574. case '!':
  575. c = RULE_CAPITAL;
  576. break;
  577. case 'T':
  578. c = RULE_ALT1;
  579. break;
  580. case 'W':
  581. c = RULE_SPELLING;
  582. break;
  583. case 'X':
  584. c = RULE_NOVOWELS;
  585. break;
  586. case 'L':
  587. // expect two digits
  588. c = *p++ - '0';
  589. value = *p++ - '0';
  590. c = c * 10 + value;
  591. if((value < 0) || (value > 9) || (c <= 0) || (c >= N_LETTER_GROUPS))
  592. {
  593. c = 0;
  594. fprintf(f_log,"%5d: Expected 2 digits after 'L'",linenum);
  595. error_count++;
  596. }
  597. c += 'A';
  598. if(state == 1)
  599. {
  600. // pre-rule, put the group number before the RULE_LETTERGP command
  601. output[ix++] = c;
  602. c = RULE_LETTERGP2;
  603. }
  604. else
  605. {
  606. output[ix++] = RULE_LETTERGP2;
  607. }
  608. break;
  609. case 'P':
  610. sxflags |= SUFX_P; // Prefix, now drop through to Suffix
  611. case '$': // obsolete, replaced by S
  612. case 'S':
  613. output[ix++] = RULE_ENDING;
  614. value = 0;
  615. while(!isspace2(c = *p++) && (c != 0))
  616. {
  617. switch(c)
  618. {
  619. case 'e':
  620. sxflags |= SUFX_E;
  621. break;
  622. case 'i':
  623. sxflags |= SUFX_I;
  624. break;
  625. case 'p': // obsolete, replaced by 'P' above
  626. sxflags |= SUFX_P;
  627. break;
  628. case 'v':
  629. sxflags |= SUFX_V;
  630. break;
  631. case 'd':
  632. sxflags |= SUFX_D;
  633. break;
  634. case 'f':
  635. sxflags |= SUFX_F;
  636. break;
  637. case 'q':
  638. sxflags |= SUFX_Q;
  639. break;
  640. case 't':
  641. sxflags |= SUFX_T;
  642. break;
  643. default:
  644. if(isdigit(c))
  645. value = (value*10) + (c - '0');
  646. break;
  647. }
  648. }
  649. p--;
  650. output[ix++] = sxflags >> 16;
  651. output[ix++] = sxflags >> 8;
  652. c = value | 0x80;
  653. break;
  654. }
  655. }
  656. }
  657. output[ix++] = c;
  658. if(c == 0) break;
  659. }
  660. state = next_state[state];
  661. } // end of copy_rule_string
  662. char *compile_rule(char *input)
  663. {//============================
  664. int ix;
  665. unsigned char c;
  666. int wc;
  667. char *p;
  668. char *prule;
  669. int len;
  670. int len_name;
  671. int state=2;
  672. int finish=0;
  673. int pre_bracket=0;
  674. char buf[80];
  675. char output[150];
  676. unsigned char bad_phoneme[4];
  677. buf[0]=0;
  678. rule_cond[0]=0;
  679. rule_pre[0]=0;
  680. rule_post[0]=0;
  681. rule_match[0]=0;
  682. rule_phonemes[0]=0;
  683. p = buf;
  684. for(ix=0; finish==0; ix++)
  685. {
  686. c = input[ix];
  687. switch(c = input[ix])
  688. {
  689. case ')': // end of prefix section
  690. *p = 0;
  691. state = 1;
  692. pre_bracket = 1;
  693. copy_rule_string(buf,state);
  694. p = buf;
  695. break;
  696. case '(': // start of suffix section
  697. *p = 0;
  698. state = 2;
  699. copy_rule_string(buf,state);
  700. state = 3;
  701. p = buf;
  702. break;
  703. case '\n': // end of line
  704. case '\r':
  705. case 0: // end of line
  706. *p = 0;
  707. copy_rule_string(buf,state);
  708. finish=1;
  709. break;
  710. case '\t': // end of section section
  711. case ' ':
  712. *p = 0;
  713. copy_rule_string(buf,state);
  714. p = buf;
  715. break;
  716. case '?':
  717. if(state==2)
  718. state=0;
  719. else
  720. *p++ = c;
  721. break;
  722. default:
  723. *p++ = c;
  724. break;
  725. }
  726. }
  727. if(strcmp(rule_match,"$group")==0)
  728. strcpy(rule_match,group_name);
  729. if(rule_match[0]==0)
  730. return(NULL);
  731. EncodePhonemes(rule_phonemes,buf,bad_phoneme);
  732. for(ix=0;; ix++)
  733. {
  734. if((c = buf[ix])==0) break;
  735. if(c==255)
  736. {
  737. fprintf(f_log,"%5d: Bad phoneme [%c] in %s",linenum,bad_phoneme[0],input);
  738. error_count++;
  739. break;
  740. }
  741. }
  742. strcpy(output,buf);
  743. len = strlen(buf)+1;
  744. len_name = strlen(group_name);
  745. if((len_name > 0) && (memcmp(rule_match,group_name,len_name) != 0))
  746. {
  747. utf8_in(&wc,rule_match,0);
  748. if((group_name[0] == '9') && IsDigit(wc))
  749. {
  750. // numeric group, rule_match starts with a digit, so OK
  751. }
  752. else
  753. {
  754. fprintf(f_log,"%5d: Wrong initial letters '%s' for group '%s'\n",linenum,rule_match,group_name);
  755. error_count++;
  756. }
  757. }
  758. strcpy(&output[len],rule_match);
  759. len += strlen(rule_match);
  760. if(rule_cond[0] != 0)
  761. {
  762. ix = -1;
  763. if(rule_cond[0] == '!')
  764. {
  765. // allow the rule only if the condition number is NOT set for the voice
  766. ix = atoi(&rule_cond[1]) + 32;
  767. }
  768. else
  769. {
  770. // allow the rule only if the condition number is set for the voice
  771. ix = atoi(rule_cond);
  772. }
  773. if((ix > 0) && (ix < 255))
  774. {
  775. output[len++] = RULE_CONDITION;
  776. output[len++] = ix;
  777. }
  778. else
  779. {
  780. fprintf(f_log,"%5d: bad condition number ?%d\n",linenum,ix);
  781. error_count++;
  782. }
  783. }
  784. if(rule_pre[0] != 0)
  785. {
  786. output[len++] = RULE_PRE;
  787. // output PRE string in reverse order
  788. for(ix = strlen(rule_pre)-1; ix>=0; ix--)
  789. output[len++] = rule_pre[ix];
  790. }
  791. if(rule_post[0] != 0)
  792. {
  793. sprintf(&output[len],"%c%s",RULE_POST,rule_post);
  794. len += (strlen(rule_post)+1);
  795. }
  796. output[len++]=0;
  797. prule = (char *)malloc(len);
  798. memcpy(prule,output,len);
  799. return(prule);
  800. } // end of compile_rule
  801. static int __cdecl string_sorter(char **a, char **b)
  802. {//=================================================
  803. char *pa, *pb;
  804. int ix;
  805. if((ix = strcmp(pa = *a,pb = *b)) != 0)
  806. return(ix);
  807. pa += (strlen(pa)+1);
  808. pb += (strlen(pb)+1);
  809. return(strcmp(pa,pb));
  810. } /* end of string_sorter */
  811. static int __cdecl rgroup_sorter(RGROUP *a, RGROUP *b)
  812. {//===================================================
  813. int ix;
  814. ix = strcmp(a->name,b->name);
  815. if(ix != 0) return(ix);
  816. return(a->start-b->start);
  817. }
  818. #ifdef OUTPUT_FORMAT
  819. void print_rule_group(FILE *f_out, int n_rules, char **rules, char *name)
  820. {//======================================================================
  821. int rule;
  822. int ix;
  823. unsigned char c;
  824. int len1;
  825. int len2;
  826. int spaces;
  827. char *p;
  828. char *pout;
  829. int condition;
  830. char buf[80];
  831. char suffix[12];
  832. static unsigned char symbols[] = {'@','&','%','+','#','$','D','Z','A','B','C','F'};
  833. fprintf(f_out,"\n$group %s\n",name);
  834. for(rule=0; rule<n_rules; rule++)
  835. {
  836. p = rules[rule];
  837. len1 = strlen(p) + 1;
  838. p = &p[len1];
  839. len2 = strlen(p);
  840. rule_match[0]=0;
  841. rule_pre[0]=0;
  842. rule_post[0]=0;
  843. condition = 0;
  844. pout = rule_match;
  845. for(ix=0; ix<len2; ix++)
  846. {
  847. switch(c = p[ix])
  848. {
  849. case RULE_PRE:
  850. *pout = 0;
  851. pout = rule_pre;
  852. break;
  853. case RULE_POST:
  854. *pout = 0;
  855. pout = rule_post;
  856. break;
  857. case RULE_CONDITION:
  858. condition = p[++ix];
  859. break;
  860. case RULE_ENDING:
  861. sprintf(suffix,"$%d[%x]",(p[ix+2]),p[ix+1] & 0x7f);
  862. ix += 2;
  863. strcpy(pout,suffix);
  864. pout += strlen(suffix);
  865. break;
  866. default:
  867. if(c <= RULE_LETTER7)
  868. c = symbols[c-RULE_SYLLABLE];
  869. if(c == ' ')
  870. c = '_';
  871. *pout++ = c;
  872. break;
  873. }
  874. }
  875. *pout = 0;
  876. spaces = 12;
  877. if(condition > 0)
  878. {
  879. sprintf(buf,"?%d ",condition);
  880. spaces -= strlen(buf);
  881. fprintf(f_out,"%s",buf);
  882. }
  883. if(rule_pre[0] != 0)
  884. {
  885. p = buf;
  886. for(ix=strlen(rule_pre)-1;ix>=0;ix--)
  887. *p++ = rule_pre[ix];
  888. sprintf(p,") ");
  889. spaces -= strlen(buf);
  890. for(ix=0; ix<spaces; ix++)
  891. fputc(' ',f_out);
  892. fprintf(f_out,"%s",buf);
  893. spaces = 0;
  894. }
  895. for(ix=0; ix<spaces; ix++)
  896. fputc(' ',f_out);
  897. spaces = 14;
  898. sprintf(buf," %s ",rule_match);
  899. if(rule_post[0] != 0)
  900. {
  901. p = &buf[strlen(buf)];
  902. sprintf(p,"(%s ",rule_post);
  903. }
  904. fprintf(f_out,"%s",buf);
  905. spaces -= strlen(buf);
  906. for(ix=0; ix<spaces; ix++)
  907. fputc(' ',f_out);
  908. DecodePhonemes(rules[rule],buf);
  909. fprintf(f_out,"%s\n",buf); // phonemes
  910. }
  911. }
  912. #endif
  913. //#define LIST_GROUP_INFO
  914. void output_rule_group(FILE *f_out, int n_rules, char **rules, char *name)
  915. {//=======================================================================
  916. int ix;
  917. int len1;
  918. int len2;
  919. int len_name;
  920. char *p;
  921. char *p2, *p3;
  922. const char *common;
  923. short nextchar_count[256];
  924. memset(nextchar_count,0,sizeof(nextchar_count));
  925. len_name = strlen(name);
  926. #ifdef OUTPUT_FORMAT
  927. print_rule_group(f_log,n_rules,rules,name);
  928. #endif
  929. // sort the rules in this group by their phoneme string
  930. common = "";
  931. qsort((void *)rules,n_rules,sizeof(char *),(int (__cdecl *)(const void *,const void *))string_sorter);
  932. if(strcmp(name,"9")==0)
  933. len_name = 0; // don't remove characters from numeric match strings
  934. for(ix=0; ix<n_rules; ix++)
  935. {
  936. p = rules[ix];
  937. len1 = strlen(p) + 1; // phoneme string
  938. p3 = &p[len1];
  939. p2 = p3 + len_name; // remove group name from start of match string
  940. len2 = strlen(p2);
  941. nextchar_count[(unsigned char)(p2[0])]++; // the next byte after the group name
  942. if((common[0] != 0) && (strcmp(p,common)==0))
  943. {
  944. fwrite(p2,len2,1,f_out);
  945. fputc(0,f_out); // no phoneme string, it's the same as previous rule
  946. }
  947. else
  948. {
  949. if((ix < n_rules-1) && (strcmp(p,rules[ix+1])==0))
  950. {
  951. common = rules[ix]; // phoneme string is same as next, set as common
  952. fputc(RULE_PH_COMMON,f_out);
  953. }
  954. fwrite(p2,len2,1,f_out);
  955. fputc(RULE_PHONEMES,f_out);
  956. fwrite(p,len1,1,f_out);
  957. }
  958. }
  959. #ifdef LIST_GROUP_INFO
  960. for(ix=32; ix<256; ix++)
  961. {
  962. if(nextchar_count[ix] > 30)
  963. printf("Group %s %c %d\n",name,ix,nextchar_count[ix]);
  964. }
  965. #endif
  966. } // end of output_rule_group
  967. static int compile_lettergroup(char *input, FILE *f_out)
  968. {//=====================================================
  969. char *p;
  970. int group;
  971. p = input;
  972. if(!isdigit(p[0]) || !isdigit(p[1]))
  973. {
  974. return(1);
  975. }
  976. group = atoi(&p[1]);
  977. if(group >= N_LETTER_GROUPS)
  978. return(1);
  979. while(!isspace2(*p)) p++;
  980. fputc(RULE_GROUP_START,f_out);
  981. fputc(RULE_LETTERGP2,f_out);
  982. fputc(group + 'A', f_out);
  983. for(;;)
  984. {
  985. while(isspace2(*p)) p++;
  986. if(*p == 0)
  987. break;
  988. while((*p & 0xff) > ' ')
  989. {
  990. fputc(*p++, f_out);
  991. }
  992. fputc(0,f_out);
  993. }
  994. fputc(RULE_GROUP_END,f_out);
  995. return(0);
  996. }
  997. static int compile_dictrules(FILE *f_in, FILE *f_out, char *fname_temp)
  998. {//====================================================================
  999. char *prule;
  1000. unsigned char *p;
  1001. int ix;
  1002. int c;
  1003. int gp;
  1004. FILE *f_temp;
  1005. int n_rules=0;
  1006. int count=0;
  1007. int different;
  1008. char *prev_rgroup_name;
  1009. unsigned int char_code;
  1010. int compile_mode=0;
  1011. char *buf;
  1012. char buf1[120];
  1013. char *rules[N_RULES];
  1014. int n_rgroups = 0;
  1015. RGROUP rgroup[N_RULE_GROUP2];
  1016. linenum = 0;
  1017. group_name[0] = 0;
  1018. if((f_temp = fopen_log(fname_temp,"wb")) == NULL)
  1019. return(1);
  1020. for(;;)
  1021. {
  1022. linenum++;
  1023. buf = fgets(buf1,sizeof(buf1),f_in);
  1024. if(buf != NULL)
  1025. {
  1026. if((p = (unsigned char *)strstr(buf,"//")) != NULL)
  1027. *p = 0;
  1028. if(buf[0] == '\r') buf++; // ignore extra \r in \r\n
  1029. }
  1030. if((buf != NULL) && (memcmp(buf,".L",2)==0))
  1031. {
  1032. if(compile_lettergroup(&buf[2], f_out) != 0)
  1033. {
  1034. fprintf(f_log,"%5d: Bad lettergroup\n",linenum);
  1035. error_count++;
  1036. }
  1037. continue;
  1038. }
  1039. if((buf == NULL) || (buf[0] == '.'))
  1040. {
  1041. // next .group or end of file, write out the previous group
  1042. if(n_rules > 0)
  1043. {
  1044. strcpy(rgroup[n_rgroups].name,group_name);
  1045. rgroup[n_rgroups].start = ftell(f_temp);
  1046. output_rule_group(f_temp,n_rules,rules,group_name);
  1047. rgroup[n_rgroups].length = ftell(f_temp) - rgroup[n_rgroups].start;
  1048. n_rgroups++;
  1049. count += n_rules;
  1050. }
  1051. n_rules = 0;
  1052. if(compile_mode == 2)
  1053. {
  1054. // end of the character replacements section
  1055. fwrite(&n_rules,1,4,f_out); // write a zero word to terminate the replacemenmt list
  1056. }
  1057. if(buf == NULL) break; // end of file
  1058. if(memcmp(buf,".replace",8)==0)
  1059. {
  1060. compile_mode = 2;
  1061. fputc(RULE_GROUP_START,f_out);
  1062. fputc(RULE_REPLACEMENTS,f_out);
  1063. // advance to next word boundary
  1064. while((ftell(f_out) & 3) != 0)
  1065. fputc(0,f_out);
  1066. }
  1067. if(memcmp(buf,".group",6)==0)
  1068. {
  1069. compile_mode = 1;
  1070. p = (unsigned char *)&buf[6];
  1071. while((p[0]==' ') || (p[0]=='\t')) p++; // Note: Windows isspace(0xe1) gives TRUE !
  1072. ix = 0;
  1073. while((*p > ' ') && (ix<12))
  1074. group_name[ix++] = *p++;
  1075. group_name[ix]=0;
  1076. if(sscanf(group_name,"0x%x",&char_code)==1)
  1077. {
  1078. // group character is given as a character code (max 16 bits)
  1079. p = (unsigned char *)group_name;
  1080. if(char_code > 0x100)
  1081. {
  1082. *p++ = (char_code >> 8);
  1083. }
  1084. *p++ = char_code;
  1085. *p = 0;
  1086. }
  1087. if(strlen(group_name) > 2)
  1088. {
  1089. if(utf8_in(&c,group_name,0) < 2)
  1090. {
  1091. fprintf(f_log,"%5d: Group name longer than 2 bytes (UTF8)",linenum);
  1092. error_count++;
  1093. }
  1094. group_name[2] = 0;
  1095. }
  1096. }
  1097. continue;
  1098. }
  1099. switch(compile_mode)
  1100. {
  1101. case 1: // .group
  1102. prule = compile_rule(buf);
  1103. if((prule != NULL) && (n_rules < N_RULES))
  1104. {
  1105. rules[n_rules++] = prule;
  1106. }
  1107. break;
  1108. case 2: // .replace
  1109. {
  1110. int replace1;
  1111. int replace2;
  1112. char *p;
  1113. p = buf;
  1114. replace1 = 0;
  1115. replace2 = 0;
  1116. while(isspace2(*p)) p++;
  1117. ix = 0;
  1118. while((unsigned char)(*p) > 0x20) // not space or zero-byte
  1119. {
  1120. p += utf8_in(&c,p,0);
  1121. replace1 += (c << ix);
  1122. ix += 16;
  1123. }
  1124. while(isspace2(*p)) p++;
  1125. ix = 0;
  1126. while((unsigned char)(*p) > 0x20)
  1127. {
  1128. p += utf8_in(&c,p,0);
  1129. replace2 += (c << ix);
  1130. ix += 16;
  1131. }
  1132. if(replace1 != 0)
  1133. {
  1134. fwrite(&replace1,1,4,f_out);
  1135. fwrite(&replace2,1,4,f_out);
  1136. }
  1137. }
  1138. break;
  1139. }
  1140. }
  1141. fclose(f_temp);
  1142. qsort((void *)rgroup,n_rgroups,sizeof(rgroup[0]),(int (__cdecl *)(const void *,const void *))rgroup_sorter);
  1143. if((f_temp = fopen(fname_temp,"rb"))==NULL)
  1144. return(2);
  1145. prev_rgroup_name = "\n";
  1146. for(gp = 0; gp < n_rgroups; gp++)
  1147. {
  1148. fseek(f_temp,rgroup[gp].start,SEEK_SET);
  1149. if((different = strcmp(rgroup[gp].name, prev_rgroup_name)) != 0)
  1150. {
  1151. // not the same as the previous group
  1152. if(gp > 0)
  1153. fputc(RULE_GROUP_END,f_out);
  1154. fputc(RULE_GROUP_START,f_out);
  1155. fprintf(f_out, prev_rgroup_name = rgroup[gp].name);
  1156. fputc(0,f_out);
  1157. }
  1158. for(ix=rgroup[gp].length; ix>0; ix--)
  1159. {
  1160. c = fgetc(f_temp);
  1161. fputc(c,f_out);
  1162. }
  1163. if(different)
  1164. {
  1165. }
  1166. }
  1167. fputc(RULE_GROUP_END,f_out);
  1168. fputc(0,f_out);
  1169. fclose(f_temp);
  1170. remove(fname_temp);
  1171. fprintf(f_log,"\t%d rules, %d groups\n\n",count,n_rgroups);
  1172. return(0);
  1173. } // end of compile_dictrules
  1174. int CompileDictionary(const char *dsource, const char *dict_name, FILE *log, char *fname_err)
  1175. {//==========================================================================================
  1176. // fname: space to write the filename in case of error
  1177. FILE *f_in;
  1178. FILE *f_out;
  1179. int offset_rules=0;
  1180. int value;
  1181. char fname_in[sizeof(path_home)+45];
  1182. char fname_out[sizeof(path_home)+15];
  1183. char fname_temp[sizeof(path_home)+15];
  1184. char path[sizeof(path_home)+40]; // path_dsource+20
  1185. error_count = 0;
  1186. if(dsource == NULL)
  1187. dsource = "";
  1188. f_log = log;
  1189. //f_log = fopen("log2.txt","w");
  1190. if(f_log == NULL)
  1191. f_log = stderr;
  1192. sprintf(path,"%s%s_",dsource,dict_name);
  1193. sprintf(fname_in,"%srules",path);
  1194. f_in = fopen_log(fname_in,"r");
  1195. if(f_in == NULL)
  1196. {
  1197. if(fname_err)
  1198. strcpy(fname_err,fname_in);
  1199. return(-1);
  1200. }
  1201. sprintf(fname_out,"%s%c%s_dict",path_home,PATHSEP,dict_name);
  1202. if((f_out = fopen_log(fname_out,"wb+")) == NULL)
  1203. {
  1204. if(fname_err)
  1205. strcpy(fname_err,fname_in);
  1206. return(-1);
  1207. }
  1208. sprintf(fname_temp,"%s%ctemp",path_home,PATHSEP);
  1209. transpose_offset = 0;
  1210. if(strcmp(dict_name,"ru") == 0)
  1211. {
  1212. // transpose cyrillic alphabet from unicode to iso8859-5
  1213. // transpose_offset = 0x430-0xd0;
  1214. transpose_offset = 0x42f; // range 0x01 to 0x22
  1215. transpose_min = 0x430;
  1216. transpose_max = 0x451;
  1217. }
  1218. value = N_HASH_DICT;
  1219. fwrite(&value,4,1,f_out);
  1220. fwrite(&offset_rules,4,1,f_out);
  1221. compile_dictlist_start();
  1222. fprintf(f_log,"Using phonemetable: '%s'\n",PhonemeTabName());
  1223. compile_dictlist_file(path,"roots");
  1224. compile_dictlist_file(path,"listx");
  1225. compile_dictlist_file(path,"list");
  1226. compile_dictlist_file(path,"extra");
  1227. compile_dictlist_end(f_out);
  1228. offset_rules = ftell(f_out);
  1229. fprintf(f_log,"Compiling: '%s'\n",fname_in);
  1230. compile_dictrules(f_in,f_out,fname_temp);
  1231. fclose(f_in);
  1232. fseek(f_out,4,SEEK_SET);
  1233. fwrite(&offset_rules,4,1,f_out);
  1234. fclose(f_out);
  1235. translator->LoadDictionary(dict_name,0);
  1236. return(error_count);
  1237. } // end of compile_dictionary