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

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