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

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