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.

extras.cpp 50KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418
  1. /***************************************************************************
  2. * Copyright (C) 2006 to 2011 by Jonathan Duddington *
  3. * email: [email protected] *
  4. * Copyright (C) 2013 by Reece H. Dunn *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write see: *
  18. * <http://www.gnu.org/licenses/>. *
  19. ***************************************************************************/
  20. #include "wx/wx.h"
  21. #include <wx/dirdlg.h>
  22. #include "wx/filename.h"
  23. #include "wx/sound.h"
  24. #include "wx/dir.h"
  25. #include <sys/stat.h>
  26. #include "speak_lib.h"
  27. #include "main.h"
  28. #include "speech.h"
  29. #include "phoneme.h"
  30. #include "synthesize.h"
  31. #include "voice.h"
  32. #include "spect.h"
  33. #include "translate.h"
  34. #include "options.h"
  35. extern char word_phonemes[N_WORD_PHONEMES]; // a word translated into phoneme codes
  36. extern int __cdecl string_sorter(char **a, char **b);
  37. //******************************************************************************************************
  38. FILE *f_wavtest = NULL;
  39. FILE *f_events = NULL;
  40. FILE *OpenWaveFile3(const char *path)
  41. /***********************************/
  42. {
  43. int *p;
  44. FILE *f;
  45. static unsigned char wave_hdr[44] = {
  46. 'R','I','F','F',0,0,0,0,'W','A','V','E','f','m','t',' ',
  47. 0x10,0,0,0,1,0,1,0, 9,0x3d,0,0,0x12,0x7a,0,0,
  48. 2,0,0x10,0,'d','a','t','a', 0,0,0,0 };
  49. if(path == NULL)
  50. return(NULL);
  51. // set the sample rate in the header
  52. p = (int *)(&wave_hdr[24]);
  53. p[0] = samplerate;
  54. p[1] = samplerate * 2;
  55. f = fopen(path,"wb");
  56. if(f != NULL)
  57. {
  58. fwrite(wave_hdr,1,sizeof(wave_hdr),f);
  59. }
  60. return(f);
  61. } // end of OpenWaveFile
  62. void CloseWaveFile3(FILE *f)
  63. /*************************/
  64. {
  65. unsigned int pos;
  66. static int value;
  67. if(f == NULL)
  68. return;
  69. fflush(f);
  70. pos = ftell(f);
  71. value = pos - 8;
  72. fseek(f,4,SEEK_SET);
  73. fwrite(&value,4,1,f);
  74. value = samplerate;
  75. fseek(f,24,SEEK_SET);
  76. fwrite(&value,4,1,f);
  77. value = samplerate*2;
  78. fseek(f,28,SEEK_SET);
  79. fwrite(&value,4,1,f);
  80. value = pos - 44;
  81. fseek(f,40,SEEK_SET);
  82. fwrite(&value,4,1,f);
  83. fclose(f);
  84. } // end of CloseWaveFile3
  85. int TestUriCallback(int type, const char *uri, const char *base)
  86. {//=============================================================
  87. if(strcmp(uri,"hello")==0)
  88. return(1);
  89. return(0);
  90. }
  91. int TestSynthCallback(short *wav, int numsamples, espeak_EVENT *events)
  92. {//====================================================================
  93. int type;
  94. f_events = fopen("/home/jsd1/speechdata/text/events","a");
  95. fprintf(f_events,"--\n");
  96. while((type = events->type) != 0)
  97. {
  98. fprintf(f_events,"%2d (%4d %4ld) %5d %5d (%3d) ",type,events->unique_identifier,(long)events->user_data,events->audio_position,events->text_position,events->length);
  99. if((type==3) || (type==4))
  100. fprintf(f_events,"'%s'\n",events->id.name);
  101. else
  102. if(type==espeakEVENT_PHONEME)
  103. {
  104. // char buf[10];
  105. fprintf(f_events,"[%s]\n",WordToString(events->id.number)); //old version, only 4 characters bytes
  106. // memcpy(buf, events->id.string, 8);
  107. // buf[8] = 0;
  108. // fprintf(f_events,"[%s]\n", buf);
  109. }
  110. else
  111. fprintf(f_events,"%d\n",events->id.number);
  112. events++;
  113. }
  114. if((wav == NULL) && (f_wavtest != NULL))
  115. {
  116. fprintf(f_events,"Finished\n");
  117. CloseWaveFile3(f_wavtest);
  118. f_wavtest = NULL;
  119. }
  120. fclose(f_events);
  121. if(f_wavtest == NULL) return(0);
  122. fwrite(wav,numsamples*2,1,f_wavtest);
  123. return(0);
  124. }
  125. //******************************************************************************************************
  126. #ifdef deleted
  127. static int RuLex_sorter(char **a, char **b)
  128. {//=======================================
  129. char *pa, *pb;
  130. int xa, xb;
  131. int ix;
  132. pa = *a;
  133. pb = *b;
  134. xa = strlen(pa)-1;
  135. xb = strlen(pb)-1;
  136. while((xa >= 0) && (xb >= 0))
  137. {
  138. if((ix = (pa[xa] - pb[xb])) != 0)
  139. return(ix);
  140. xa--;
  141. xb--;
  142. }
  143. return(pa - pb);
  144. } /* end of strcmp2 */
  145. #endif
  146. static const unsigned short KOI8_R[0x60] = {
  147. 0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556, // a0
  148. 0x2557, 0x2558, 0x2559, 0x255a, 0x255b, 0x255c, 0x255d, 0x255e, // a8
  149. 0x255f, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565, // b0
  150. 0x2566, 0x2567, 0x2568, 0x2569, 0x256a, 0x256b, 0x256c, 0x00a9, // b8
  151. 0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433, // c0
  152. 0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, // c8
  153. 0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432, // d0
  154. 0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a, // d8
  155. 0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413, // e0
  156. 0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, // e8
  157. 0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412, // f0
  158. 0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a, // f8
  159. };
  160. #define N_CHARS 34
  161. #define PH(c1,c2) (c2<<8)+c1 // combine two characters into an integer for phoneme name
  162. static void DecodePhonemes2(const char *inptr, char *outptr)
  163. //===================================================
  164. // Translate from internal phoneme codes into phoneme mnemonics
  165. // This version is for Lexicon_De()
  166. {
  167. unsigned char phcode;
  168. unsigned char c;
  169. unsigned int mnem;
  170. PHONEME_TAB *ph;
  171. const char *p;
  172. int ix;
  173. int j;
  174. int start;
  175. static const char *stress_chars = "==,,'* ";
  176. unsigned int replace_ph[] = {',',PH('@','-'),'W','3','y','A',PH('A',':'),'*',PH('_','!'),PH('_','|'),PH('O','I'),PH('Y',':'),PH('p','F'),PH('E','2'),0};
  177. const char *replace_ph2[] = {NULL,NULL,"9","@r","Y","a:", "a:", "r", "?", "?", "OY", "2:", "pf" ,"E",NULL};
  178. start = 1;
  179. for(ix=0; (phcode = inptr[ix]) != 0; ix++)
  180. {
  181. if(phcode == 255)
  182. continue; /* indicates unrecognised phoneme */
  183. if((ph = phoneme_tab[phcode]) == NULL)
  184. continue;
  185. if((ph->type == phSTRESS) && (ph->std_length <= 4) && (ph->program == 0))
  186. {
  187. if(ph->std_length > 2)
  188. *outptr++ = stress_chars[ph->std_length];
  189. }
  190. else
  191. {
  192. mnem = ph->mnemonic;
  193. if(ph->type == phPAUSE)
  194. {
  195. if(start)
  196. continue; // omit initial [?]
  197. if(inptr[ix+1] == phonSCHWA_SHORT)
  198. continue; // omit [?] before [@-*]
  199. }
  200. start = 0;
  201. p = NULL;
  202. for(j=0;;j++)
  203. {
  204. if(replace_ph[j] == 0)
  205. break;
  206. if(mnem == replace_ph[j])
  207. {
  208. p = replace_ph2[j];
  209. if(p == NULL)
  210. mnem = 0;
  211. break;
  212. }
  213. }
  214. if(p != NULL)
  215. {
  216. while((c = *p++) != 0)
  217. {
  218. *outptr++ = c;
  219. }
  220. }
  221. else
  222. if(mnem != 0)
  223. {
  224. while((c = (mnem & 0xff)) != 0)
  225. {
  226. *outptr++ = c;
  227. mnem = mnem >> 8;
  228. }
  229. }
  230. }
  231. }
  232. *outptr = 0; /* string terminator */
  233. } // end of DecodePhonemes2
  234. void Lexicon_It(int pass)
  235. {//======================
  236. // Reads a lexicon of pairs of words: normal spelling and spelling with accents
  237. // Creates file: dictsource/it_listx which includes corrections for stress position and [E]/[e] and [O]/[o] phonemes
  238. // Words which are still in error are listed in file: it_compare (in the directory of the lexicon file).
  239. int count=0;
  240. int matched=0;
  241. int ix;
  242. int c;
  243. char *p, *p2;
  244. int len;
  245. int vowel_ix;
  246. int stress_posn1;
  247. int stress_posn2;
  248. int stress_vowel1;
  249. int stress_vowel2;
  250. int use_phonemes;
  251. FILE *f_in;
  252. FILE *f_out;
  253. FILE *f_listx;
  254. FILE *f_list_in = NULL;
  255. int listx_count;
  256. long int displ;
  257. const char *alt_string;
  258. wxString str;
  259. static wxString fname_lex;
  260. char buf[200];
  261. char word[80];
  262. char word1[80];
  263. char word2[80];
  264. char word_stem[80];
  265. char temp[80];
  266. char phonemes[80];
  267. char phonemes2[80];
  268. char phonemes3[80];
  269. char buf_out[200];
  270. char buf_error[200];
  271. char last_listx[200];
  272. static const char *vowels1 = "aeiou";
  273. static const char *vowels2 = "aeou";
  274. static const char ex1[] = {'a',0xc3,0xac,0}; // aì
  275. static const char ex2[] = {'e',0xc3,0xac,0}; // eì
  276. static const char ex3[] = {0xc3,0xb9,'a',0}; // ùa
  277. static const char ex4[] = {0xc3,0xb9,'e',0}; // ùe
  278. static const char ex5[] = {0xc3,0xb9,'i',0}; // ùi
  279. static const char ex6[] = {0xc3,0xb9,'o',0}; // ùo
  280. static const char ex7[] = {'c',0xc3,0xac,'a',0}; // cìa
  281. static const char ex8[] = {'c',0xc3,0xac,'o',0}; // cìo
  282. static const char ex9[] = {'c',0xc3,0xac,'u',0}; // cìu
  283. static const char ex10[] = {'g','l',0xc3,0xac,0}; // glì
  284. static const char *exceptions[] = {ex1, ex2, ex3, ex4, ex5, ex6, ex7, ex8, ex9, ex10, NULL};
  285. if(pass == 1)
  286. {
  287. fname_lex = wxFileSelector(_T("Italian Lexicon"),path_dir1,_T(""),_T(""),_T("*"),wxOPEN);
  288. }
  289. strcpy(buf,fname_lex.mb_str(wxConvLocal));
  290. if((f_in = fopen(buf,"r")) == NULL)
  291. {
  292. wxLogError(_T("Can't read file ")+fname_lex);
  293. return;
  294. }
  295. path_dir1 = wxFileName(fname_lex).GetPath();
  296. strcpy(buf_out, path_dir1.mb_str(wxConvLocal));
  297. sprintf(buf, "%s/IT_errors", buf_out);
  298. if((f_out = fopen(buf,"w")) == NULL)
  299. {
  300. str = wxString(buf, wxConvLocal);
  301. wxLogError(_T("Can't write file: ") + str);
  302. return;
  303. }
  304. listx_count = 0;
  305. last_listx[0] = 0;
  306. if(pass == 1)
  307. {
  308. sprintf(buf,"%s/it_listx",path_dsource);
  309. remove(buf);
  310. CompileDictionary(path_dsource,"it",NULL,NULL,0);
  311. f_listx = fopen(buf,"w");
  312. wxLogStatus(_T("Pass 1"));
  313. }
  314. else
  315. {
  316. CompileDictionary(path_dsource,"it",NULL,NULL,0);
  317. sprintf(buf,"%s/it_listx2",path_dsource);
  318. f_listx = fopen(buf,"w");
  319. sprintf(buf,"%s/it_listx",path_dsource);
  320. if((f_list_in = fopen(buf,"r")) == NULL)
  321. {
  322. wxLogError(_T("Can't read file: it_listx"));
  323. return;
  324. }
  325. }
  326. if(f_listx == NULL)
  327. {
  328. wxLogError(_T("Can't write file: it_listx"));
  329. return;
  330. }
  331. LoadVoice("it",0);
  332. while(!feof(f_in))
  333. {
  334. count++;
  335. if(fgets(buf,sizeof(buf),f_in) == NULL)
  336. break;
  337. if((p = strstr(buf,"//")) != NULL)
  338. *p = 0;
  339. if((sscanf(buf,"%s %s",word,temp)) < 2)
  340. continue;
  341. if(strlen(word) < 8)
  342. sprintf(buf_error,"%s\t\t%s\t",word,temp);
  343. else
  344. sprintf(buf_error,"%s\t%s",word,temp);
  345. sprintf(word1," %s ",word);
  346. // should we remove a vowel ending to produce a stem ?
  347. strcpy(word_stem, word);
  348. len = strlen(word) - 1;
  349. utf8_in(&c, temp);
  350. // if(iswlower(c))
  351. {
  352. if((word[len] == 'a') && (strchr(vowels1, word[len-1]) == NULL))
  353. word_stem[len] = 0;
  354. else
  355. if((word[len] == 'o') && (strchr(vowels2, word[len-1]) == NULL))
  356. word_stem[len] = 0;
  357. }
  358. // convert word to lower-case
  359. word2[0] = ' ';
  360. for(ix=0, p=&word2[1];;)
  361. {
  362. ix += utf8_in(&c,&temp[ix]);
  363. c = towlower(c);
  364. p += utf8_out(c,p);
  365. if(c == 0)
  366. break;
  367. }
  368. strcat(word2," ");
  369. use_phonemes = 0;
  370. for(ix=0; ; ix++)
  371. {
  372. if(exceptions[ix] == NULL)
  373. break;
  374. if(strstr(word2, exceptions[ix]) != NULL)
  375. {
  376. // the word contains a string for which we must do a complete phoneme translation
  377. use_phonemes = 1;
  378. strcpy(word_stem, word);
  379. break;
  380. }
  381. }
  382. // translate
  383. TranslateWord(translator,&word1[1],0, NULL, NULL);
  384. DecodePhonemes(word_phonemes,phonemes);
  385. stress_posn1 = 0;
  386. stress_posn2 = 0;
  387. stress_vowel1 = 0;
  388. stress_vowel2 = 0;
  389. vowel_ix = 1;
  390. for(ix=0; ;ix++)
  391. {
  392. if((c = word_phonemes[ix]) == 0)
  393. break;
  394. if(c == phonSTRESS_P)
  395. {
  396. stress_posn1 = vowel_ix;
  397. stress_vowel1 = word_phonemes[ix+1];
  398. }
  399. if((c != phonSCHWA_SHORT) && (phoneme_tab[c]->type == phVOWEL))
  400. vowel_ix++;
  401. }
  402. TranslateWord(translator,&word2[1],0, NULL, NULL);
  403. DecodePhonemes(word_phonemes,phonemes2);
  404. vowel_ix = 1;
  405. for(ix=0; ;ix++)
  406. {
  407. if((c = word_phonemes[ix]) == 0)
  408. break;
  409. if(c == phonSTRESS_P)
  410. {
  411. stress_posn2 = vowel_ix;
  412. stress_vowel2 = word_phonemes[ix+1];
  413. }
  414. if((c != phonSCHWA_SHORT) && (phoneme_tab[c]->type == phVOWEL))
  415. vowel_ix++;
  416. }
  417. if(stress_posn2 == (vowel_ix-1))
  418. {
  419. // stress is on the final vowel, don't renove it
  420. strcpy(word_stem, word);
  421. }
  422. if(pass == 1)
  423. {
  424. if(use_phonemes)
  425. {
  426. fprintf(f_listx,"%s ", word_stem);
  427. for(p = phonemes2; *p != 0; p++)
  428. {
  429. if(*p != ',')
  430. fputc(*p, f_listx); // omit secondary stress marks
  431. }
  432. fputc('\n',f_listx);
  433. }
  434. else
  435. if((stress_posn1 != stress_posn2) && (stress_posn1 > 0) && (stress_posn2 > 0))
  436. {
  437. fprintf(f_listx,"%s $%d\n", word_stem, stress_posn2);
  438. }
  439. }
  440. // reduce [E] and [O] to [e] and [o] if not stressed
  441. for(ix=0; phonemes[ix] != 0; ix++)
  442. {
  443. if((phonemes[ix] == 'E') || (phonemes[ix] == 'O'))
  444. {
  445. if((pass == 2) || (ix==0) || (phonemes[ix-1] != '\''))
  446. phonemes[ix] = tolower(phonemes[ix]);
  447. }
  448. }
  449. for(ix=0; phonemes2[ix] != 0; ix++)
  450. {
  451. if((phonemes2[ix] == 'E') || (phonemes2[ix] == 'O'))
  452. {
  453. if((pass == 2) || (ix==0) || (phonemes2[ix-1] != '\''))
  454. phonemes2[ix] = tolower(phonemes2[ix]);
  455. }
  456. }
  457. if(strcmp(phonemes,phonemes2) == 0)
  458. {
  459. alt_string = NULL;
  460. if((pass == 2) && (stress_posn1 > 0) && (stress_posn2 > 0))
  461. {
  462. if(((stress_vowel1 == PhonemeCode('E')) && (stress_vowel2 == PhonemeCode('e'))) ||
  463. ((stress_vowel1 == PhonemeCode('O')) && (stress_vowel2 == PhonemeCode('o'))))
  464. {
  465. alt_string = " $alt2";
  466. }
  467. else
  468. if(((stress_vowel1 == PhonemeCode('e')) && (stress_vowel2 == PhonemeCode('E'))) ||
  469. ((stress_vowel1 == PhonemeCode('o')) && (stress_vowel2 == PhonemeCode('O'))))
  470. {
  471. alt_string = " $alt";
  472. }
  473. if(alt_string != NULL)
  474. {
  475. while(!feof(f_list_in))
  476. {
  477. displ = ftell(f_list_in);
  478. if(fgets(buf, sizeof(buf), f_list_in) == NULL)
  479. break;
  480. sscanf(buf, "%s", word1);
  481. if(strcmp(word1, word_stem) < 0)
  482. {
  483. sprintf(buf_out,"%s",buf); // copy it_listx from pass 1 until we reach the matching word
  484. }
  485. else
  486. {
  487. if(strcmp(word1, word_stem) == 0)
  488. {
  489. p = buf;
  490. while((*p != '\n') && (*p != 0)) *p++;
  491. *p = 0;
  492. sprintf(buf_out,"%s %s\n",buf,alt_string); // add $alt or $alt2 to the entry
  493. }
  494. else
  495. {
  496. sprintf(buf_out,"%s %s\n", word_stem, alt_string); // add a new word with $alt or $alt2
  497. fseek(f_list_in, displ, SEEK_SET);
  498. }
  499. if(strcmp(buf_out, last_listx) != 0)
  500. {
  501. fprintf(f_listx, "%s", buf_out);
  502. listx_count++;
  503. strcpy(last_listx, buf_out);
  504. }
  505. break;
  506. }
  507. if(strcmp(buf_out, last_listx) != 0)
  508. {
  509. fprintf(f_listx, "%s", buf_out);
  510. listx_count++;
  511. strcpy(last_listx, buf_out);
  512. }
  513. }
  514. }
  515. }
  516. matched++;
  517. }
  518. else
  519. {
  520. // allow if the only difference is no primary stress
  521. p2 = phonemes2;
  522. p = phonemes3;
  523. while(*p2 != 0)
  524. {
  525. *p = *p2++;
  526. if((*p2 == ':') && (strchr("aeiouEO", *p) != NULL)) p2++; // change lone vowels to short by removing ':'
  527. if(*p == '\'') *p = ','; // change primary to secondary stress
  528. p++;
  529. }
  530. *p = 0;
  531. if(strcmp(phonemes, phonemes3) == 0)
  532. {
  533. matched++;
  534. }
  535. else
  536. {
  537. // still doesn't match, report this word
  538. fprintf(f_out,"%s\t%s\t%s\n",buf_error,phonemes,phonemes2);
  539. }
  540. }
  541. }
  542. if(pass == 2)
  543. {
  544. while(fgets(buf, sizeof(buf), f_list_in) != NULL)
  545. {
  546. if(strcmp(buf, last_listx) != 0) // check for duplicate entries
  547. {
  548. fprintf(f_listx, "%s", buf); // copy the remaining entries from pass 1
  549. listx_count++;
  550. strcpy(last_listx, buf);
  551. }
  552. }
  553. fclose(f_list_in);
  554. }
  555. fclose(f_in);
  556. fclose(f_out);
  557. fclose(f_listx);
  558. if(pass == 2)
  559. {
  560. sprintf(buf,"%s/it_listx",path_dsource);
  561. remove(buf);
  562. sprintf(buf_out,"%s/it_listx2",path_dsource);
  563. rename(buf_out, buf);
  564. wxLogStatus(_T("Created file 'it_listx', entries=%d errors=%d total words=%d"),listx_count, count-matched, count);
  565. }
  566. else
  567. {
  568. wxLogStatus(_T("Pass 1, equal=%d different=%d"),matched,count-matched);
  569. }
  570. } // end of Lexicon_It
  571. void Lexicon_De()
  572. {//==============
  573. // Compare eSpeak's translation of German words with a pronunciation lexicon
  574. FILE *f_in;
  575. FILE *f_out;
  576. int ix;
  577. int c;
  578. int c2;
  579. char *p;
  580. int stress;
  581. int count=0;
  582. int start;
  583. int matched=0;
  584. int defer_stress = 0;
  585. char buf[200];
  586. char word[80];
  587. char word2[80];
  588. char type[80];
  589. char pronounce[80];
  590. char pronounce2[80];
  591. char phonemes[80];
  592. char phonemes2[80];
  593. static const char *vowels = "aeiouyAEIOUY29@";
  594. wxString fname = wxFileSelector(_T("German Lexicon"),path_dir1,_T(""),_T(""),_T("*"),wxOPEN);
  595. strcpy(buf,fname.mb_str(wxConvLocal));
  596. if((f_in = fopen(buf,"r")) == NULL)
  597. {
  598. wxLogError(_T("Can't read file ")+fname);
  599. return;
  600. }
  601. path_dir1 = wxFileName(fname).GetPath();
  602. if((f_out = fopen("compare_de","w")) == NULL)
  603. {
  604. wxLogError(_T("Can't write file "));
  605. return;
  606. }
  607. LoadVoice("de",0);
  608. word2[0] = ' ';
  609. while(!feof(f_in))
  610. {
  611. count++;
  612. if(fgets(buf,sizeof(buf),f_in) == NULL)
  613. break;
  614. sscanf(buf,"%s %s %s",word,type,pronounce);
  615. // convert word to lower-case
  616. for(ix=0, p=&word2[1];;)
  617. {
  618. ix += utf8_in(&c,&word[ix]);
  619. c = towlower(c);
  620. p += utf8_out(c,p);
  621. if(c == 0)
  622. break;
  623. }
  624. strcpy(word,&word2[1]);
  625. strcat(&word2[1]," ");
  626. // remove | syllable boundaries
  627. stress=0;
  628. start=1;
  629. for(ix=0, p=pronounce2;;ix++)
  630. {
  631. c = pronounce[ix];
  632. if(c == '\'')
  633. {
  634. stress=4;
  635. continue;
  636. }
  637. if(c == ',')
  638. {
  639. stress=3;
  640. continue;
  641. }
  642. if(c == '|')
  643. continue;
  644. if((c == '?') && start)
  645. continue; // omit initial [?]
  646. if(c == '<')
  647. {
  648. if((c2 = pronounce[ix+1]) == 'i')
  649. {
  650. defer_stress =1;
  651. #ifdef deleted
  652. if(stress == 4)
  653. {
  654. *p++ = 'i';
  655. c =':';
  656. }
  657. else
  658. #endif
  659. {
  660. c = 'I';
  661. }
  662. ix++;
  663. }
  664. }
  665. start =0;
  666. if(defer_stress)
  667. {
  668. defer_stress = 0;
  669. }
  670. else
  671. if(stress && (strchr(vowels,c) != NULL))
  672. {
  673. if(stress == 4)
  674. *p++ = '\'';
  675. if(stress == 3)
  676. *p++ = ',';
  677. stress = 0;
  678. }
  679. *p++ = c;
  680. if(c == 0)
  681. break;
  682. if(strchr("eiouy",c) && pronounce[ix+1] != ':')
  683. *p++ = ':'; // ensure [;] after these vowels
  684. }
  685. // translate
  686. TranslateWord(translator,&word2[1],0, NULL, NULL);
  687. DecodePhonemes2(word_phonemes,phonemes); // also need to change some phoneme names
  688. if(strcmp(phonemes,pronounce2) == 0)
  689. {
  690. matched++;
  691. }
  692. else
  693. {
  694. // remove secondary stress
  695. strcpy(phonemes2,phonemes);
  696. p = phonemes;
  697. for(ix=0; ;ix++)
  698. {
  699. if((c = phonemes2[ix]) != ',')
  700. *p++ = c;
  701. if(c == 0)
  702. break;
  703. }
  704. if(strcmp(phonemes,pronounce2) == 0)
  705. {
  706. matched++;
  707. }
  708. else
  709. {
  710. if(strlen(word) < 8)
  711. strcat(word,"\t");
  712. fprintf(f_out,"%s\t%s\t%s\n",word,phonemes,pronounce2);
  713. }
  714. }
  715. }
  716. fclose(f_in);
  717. fclose(f_out);
  718. wxLogStatus(_T("Completed, equal=%d different=%d"),matched,count-matched);
  719. } // end of Lexicon_De
  720. extern int IsVowel(Translator *tr, int letter);
  721. void Lexicon_Test()
  722. {//================
  723. int c1, c2, c3;
  724. char *p;
  725. int prev_c1=0;
  726. int prev_c2=0;
  727. int prev_c3 = 0;
  728. FILE *f_in;
  729. FILE *f_out;
  730. char buf[200];
  731. wxString s_fname = wxFileSelector(_T("List of UTF-8 words with Combining Grave Accent U+300 to indicate stress"),path_dir1,
  732. _T(""),_T(""),_T("*"),wxOPEN);
  733. if(s_fname.IsEmpty())
  734. return;
  735. strcpy(buf,s_fname.mb_str(wxConvLocal));
  736. path_dir1 = wxFileName(s_fname).GetPath();
  737. if((f_in = fopen(buf,"r")) == NULL)
  738. {
  739. wxLogError(_T("Can't read file: ") + wxString(buf,wxConvLocal));
  740. return;
  741. }
  742. strcat(buf,"_1");
  743. if((f_out = fopen(buf,"w")) == NULL)
  744. {
  745. wxLogError(_T("Can't write file: ") + wxString(buf,wxConvLocal));
  746. fclose(f_in);
  747. return;
  748. }
  749. while(!feof(f_in))
  750. {
  751. if((p = fgets(buf,sizeof(buf),f_in)) == NULL)
  752. break;
  753. if(buf[0] == 0)
  754. continue;
  755. p += utf8_in(&c1, p);
  756. p += utf8_in(&c2, p);
  757. p += utf8_in(&c3, p);
  758. c1 = towlower(c1);
  759. c2 = towlower(c2);
  760. c3 = towlower(c3);
  761. if(IsVowel(translator, c1))
  762. continue;
  763. if(IsVowel(translator, c2))
  764. continue;
  765. if((prev_c1 != c1) || (prev_c2 != c2) || ((prev_c3 != c3) && !IsVowel(translator,c3)))
  766. fputc('\n',f_out);
  767. prev_c1 = c1;
  768. prev_c2 = c2;
  769. prev_c3 = c3;
  770. fprintf(f_out,"%s",buf);
  771. }
  772. fclose(f_in);
  773. fclose(f_out);
  774. } // end of Lexicon_Test
  775. void Lexicon_Bg()
  776. {//==============
  777. // Bulgarian: compare stress markup in a list of words with lookup using bg_rules
  778. char *p;
  779. char *pw;
  780. char *pw1;
  781. int cc;
  782. int vcount;
  783. int lex_stress;
  784. int input_length;
  785. int n_words=0;
  786. int n_wrong=0;
  787. int n_out=0;
  788. int n_stress;
  789. int max_stress;
  790. int max_stress_posn;
  791. int stress_first;
  792. int done;
  793. PHONEME_TAB *ph;
  794. FILE *f_in;
  795. FILE *f_out;
  796. char word[80];
  797. char word_in[80];
  798. char phonemes[N_WORD_PHONEMES];
  799. char buf[200];
  800. char fname[sizeof(path_dsource)+20];
  801. static unsigned short bg_vowels[] = {0x430, 0x435, 0x438, 0x43e, 0x443, 0x44a, 0x44d, 0x44e, 0x44f, 0x450, 0x451, 0x45d, 0};
  802. if(gui_flag == 0)
  803. return;
  804. wxString s_fname = wxFileSelector(_T("List of UTF-8 words with Combining Grave Accent U+300 to indicate stress"),path_dir1,
  805. _T(""),_T(""),_T("*"),wxOPEN);
  806. if(s_fname.IsEmpty())
  807. return;
  808. strcpy(buf,s_fname.mb_str(wxConvLocal));
  809. path_dir1 = wxFileName(s_fname).GetPath();
  810. if((f_in = fopen(buf,"r")) == NULL)
  811. {
  812. wxLogError(_T("Can't read file: ") + wxString(buf,wxConvLocal));
  813. return;
  814. }
  815. input_length = GetFileLength(buf);
  816. sprintf(fname,"%s%c%s",path_dsource,PATHSEP,"bg_listx");
  817. remove(fname);
  818. CompileDictionary(path_dsource,"bg",NULL,NULL,0);
  819. if((f_out = fopen(fname,"w")) == NULL)
  820. {
  821. wxLogError(_T("Can't write to: ")+wxString(fname,wxConvLocal));
  822. fclose(f_in);
  823. return;
  824. }
  825. LoadVoice("bg",0);
  826. progress = new wxProgressDialog(_T("Lexicon"),_T(""),input_length);
  827. for(;;)
  828. {
  829. if((n_words & 0x3ff) == 0)
  830. {
  831. progress->Update(ftell(f_in));
  832. }
  833. if(fgets(buf,sizeof(buf),f_in) == NULL)
  834. break;
  835. if(isspace2(buf[0]))
  836. continue;
  837. // convert from UTF-8 to Unicode
  838. word[0] = 0;
  839. word[1] = ' ';
  840. pw = &word[2];
  841. pw1 = word_in;
  842. p = buf;
  843. while(*p == ' ') p++;
  844. vcount = 0;
  845. lex_stress = 0;
  846. n_stress = 0;
  847. stress_first = 0;
  848. // find the marked stress position
  849. for(;;)
  850. {
  851. p += utf8_in(&cc, p);
  852. if(iswspace(cc))
  853. break;
  854. if(cc == 0xfeff)
  855. continue; // ignore UTF-8 indication
  856. if(cc == '`')
  857. cc = '\'';
  858. pw1 += utf8_out(towlower(cc), pw1); // copy UTF-8 to 'word_in'
  859. if(lookupwchar(bg_vowels, cc) != 0)
  860. vcount++;
  861. if((cc == '\'') || (cc == 0x300) || (cc == 0x450) || (cc == 0x45d))
  862. {
  863. // backprime (before the vowel), combining grave accent, or accented vowel character
  864. if(cc == '\'')
  865. lex_stress = vcount+1;
  866. else
  867. lex_stress = vcount;
  868. n_stress++;
  869. if(vcount == 1)
  870. stress_first = 1;
  871. if((cc == '\'') || (cc == 0x300))
  872. continue; // discard backprime or combining accent
  873. if(cc == 0x450)
  874. cc = 0x435; // remove accent from vowel
  875. if(cc == 0x45d)
  876. cc = 0x438;
  877. }
  878. pw += utf8_out(cc, pw); // copy UTF-8 to 'word'
  879. }
  880. *pw++ = ' ';
  881. *pw = 0;
  882. *pw1 = 0;
  883. // translate
  884. TranslateWord(translator, &word[2],0, NULL, NULL);
  885. DecodePhonemes(word_phonemes,phonemes);
  886. // find the stress position in the translation
  887. max_stress = 0;
  888. max_stress_posn = -1;
  889. vcount = 0;
  890. ph = phoneme_tab[phonPAUSE];
  891. for(p=word_phonemes; *p != 0; p++)
  892. {
  893. ph = phoneme_tab[(unsigned int)*p];
  894. if(ph == NULL)
  895. continue;
  896. if(ph->type == phVOWEL)
  897. vcount++;
  898. if(ph->type == phSTRESS)
  899. {
  900. if(ph->std_length > max_stress)
  901. {
  902. max_stress = ph->std_length;
  903. max_stress_posn = vcount+1;
  904. }
  905. }
  906. }
  907. if(n_stress > 1) n_stress = 1;
  908. done = 0;
  909. if(vcount < 2)
  910. {
  911. // don't list words with only one vowel
  912. }
  913. else
  914. if((lex_stress != max_stress_posn) || (n_stress != 1))
  915. {
  916. if((vcount > 0) && (lex_stress > 0) && (lex_stress <= 7))
  917. {
  918. if((n_stress == 2) && (stress_first))
  919. {
  920. done = 1;
  921. fprintf(f_out,"%s\t$%d\n",&word[2],lex_stress);
  922. }
  923. if(n_stress == 1)
  924. {
  925. done = 1;
  926. fprintf(f_out,"%s\t$%d\n",&word[2],lex_stress);
  927. }
  928. }
  929. if(done == 0)
  930. {
  931. n_wrong++;
  932. fprintf(f_out,"// %s\t$text %s\n", &word[2], word_in);
  933. }
  934. if(done)
  935. n_out++;
  936. }
  937. n_words++;
  938. }
  939. fclose(f_in);
  940. fclose(f_out);
  941. CompileDictionary(path_dsource,"bg",NULL,NULL,0);
  942. delete progress;
  943. sprintf(buf,"Lexicon: Input %d, Output %d, $text %d",n_words,n_out,n_wrong);
  944. wxLogStatus(wxString(buf,wxConvLocal));
  945. } // end of Lexicon_Bg
  946. void Lexicon_Ru()
  947. {//==============
  948. // compare stress markings in Russian RuLex file with lookup in ru_rules
  949. // Input file contains a list of Russian words (UTF-8), one per line
  950. // Stress position can be indicated either by:
  951. // A $ sign and syllable number after the word, eg:
  952. // абажура $3
  953. // or by a + sign after the stressed vowel, eg:
  954. // абажу+ра
  955. // espeakedit produces a file: dictsource/ru_listx and a log file dictsource/ru_log
  956. int ix;
  957. char *p;
  958. int c;
  959. FILE *f_in;
  960. FILE *f_out;
  961. FILE *f_log;
  962. PHONEME_TAB *ph;
  963. int ph_code;
  964. int vcount;
  965. int ru_stress;
  966. int max_stress;
  967. int max_stress_posn;
  968. int n_words=0;
  969. int n_wrong=0;
  970. int n_errors=0;
  971. int wlength;
  972. int input_length;
  973. int check_root;
  974. char word[80];
  975. char word2[80];
  976. int counts[20][20][10];
  977. char phonemes[N_WORD_PHONEMES];
  978. char buf[200];
  979. char fname[sizeof(path_dsource)+20];
  980. // character codes for Russian vowels
  981. static unsigned short ru_vowels[] = {0x430,0x435,0x438,0x439,0x43e,0x443,0x44d,0x44e,0x44f,0x450,0x451,0};
  982. typedef struct {
  983. const char *suffix;
  984. int syllables;
  985. } SUFFIX;
  986. #ifdef deleted
  987. FILE *f_roots;
  988. int sfx;
  989. const char *suffix;
  990. int wlen;
  991. int len;
  992. static SUFFIX suffixes[] = {
  993. {NULL,0},
  994. {"ичу",2},
  995. {"ского",2},
  996. {"ская",2},
  997. {"ски",1},
  998. {"ские",2},
  999. {"ский",1},
  1000. {"ским",1},
  1001. {"ское",2},
  1002. {"ской",1},
  1003. {"ском",1},
  1004. {"скую",2},
  1005. {"а",1},
  1006. {"е",1},
  1007. {"и",1},
  1008. {NULL,0}};
  1009. #endif
  1010. memset(counts,0,sizeof(counts));
  1011. if(gui_flag)
  1012. {
  1013. wxString fname = wxFileSelector(_T("Read lexicon.dict"),path_dictsource,
  1014. _T(""),_T(""),_T("*"),wxOPEN);
  1015. if(fname.IsEmpty())
  1016. return;
  1017. strcpy(buf,fname.mb_str(wxConvLocal));
  1018. }
  1019. else
  1020. {
  1021. strcpy(buf,"lexicon.dict");
  1022. }
  1023. if((f_in = fopen(buf,"r")) == NULL)
  1024. {
  1025. if(gui_flag)
  1026. wxLogError(_T("Can't read file: ") + wxString(buf,wxConvLocal));
  1027. else
  1028. fprintf(stderr,"Can't read file: %s\n",buf);
  1029. return;
  1030. }
  1031. input_length = GetFileLength(buf);
  1032. sprintf(fname,"%s%c%s",path_dsource,PATHSEP,"ru_listx");
  1033. remove(fname);
  1034. // compile ru_dict without ru_listx
  1035. CompileDictionary(path_dsource,"ru",NULL,NULL,0);
  1036. if((f_out = fopen(fname,"w")) == NULL)
  1037. {
  1038. wxLogError(_T("Can't write to: ")+wxString(fname,wxConvLocal));
  1039. fclose(f_in);
  1040. return;
  1041. }
  1042. sprintf(fname,"%s%c%s",path_dsource,PATHSEP,"ru_log");
  1043. f_log = fopen(fname,"w");
  1044. sprintf(fname,"%s%c%s",path_dsource,PATHSEP,"ru_roots_1");
  1045. // f_roots = fopen(fname,"w");
  1046. LoadVoice("ru",0);
  1047. if(gui_flag)
  1048. progress = new wxProgressDialog(_T("Lexicon"),_T(""),input_length);
  1049. else
  1050. fprintf(stderr,"Processing lexicon.dict\n");
  1051. for(;;)
  1052. {
  1053. if(((n_words & 0x3ff) == 0) && gui_flag)
  1054. {
  1055. progress->Update(ftell(f_in));
  1056. }
  1057. if(fgets(buf,sizeof(buf),f_in) == NULL)
  1058. break;
  1059. if((p = strstr(buf,"//")) != NULL)
  1060. *p = '\n'; // truncate at comment
  1061. p = buf;
  1062. while((*p == ' ') || (*p == '\t')) p++;
  1063. if(*p == '\n')
  1064. continue; // blank line
  1065. ix = 0;
  1066. wlength = 0;
  1067. vcount = 0;
  1068. ru_stress = -1;
  1069. for(;;)
  1070. {
  1071. p += utf8_in(&c, p);
  1072. if(isspace(c))
  1073. break;
  1074. if(lookupwchar(ru_vowels, c))
  1075. vcount++;
  1076. if(c == '+')
  1077. ru_stress = vcount;
  1078. else
  1079. ix += utf8_out(c, &word[ix]);
  1080. }
  1081. word[ix] = 0;
  1082. sprintf(word2," %s ",word); // surround word by spaces before calling TranslateWord()
  1083. // find the marked stress position, if it has not been marked by a + after the stressed vowel
  1084. if(ru_stress == -1)
  1085. {
  1086. while((*p == ' ') || (*p == '\t')) p++;
  1087. sscanf(p,"$%d",&ru_stress);
  1088. if(ru_stress == -1)
  1089. {
  1090. n_errors++;
  1091. fprintf(f_log,"%s",buf);
  1092. continue; // stress position not found
  1093. }
  1094. }
  1095. // translate
  1096. TranslateWord(translator, &word2[1],0, NULL, NULL);
  1097. DecodePhonemes(word_phonemes,phonemes);
  1098. // find the stress position in the translation
  1099. max_stress = 0;
  1100. max_stress_posn = -1;
  1101. vcount = 0;
  1102. check_root = 0;
  1103. ph = phoneme_tab[phonPAUSE];
  1104. for(p=word_phonemes; (ph_code = *p & 0xff) != 0; p++)
  1105. {
  1106. ph = phoneme_tab[ph_code];
  1107. if(ph == NULL)
  1108. continue;
  1109. if((ph->type == phVOWEL) && (ph_code != phonSCHWA_SHORT))
  1110. vcount++;
  1111. if(ph->type == phSTRESS)
  1112. {
  1113. if(ph->std_length > max_stress)
  1114. {
  1115. max_stress = ph->std_length;
  1116. max_stress_posn = vcount+1;
  1117. }
  1118. }
  1119. }
  1120. n_words++;
  1121. if(ru_stress > vcount)
  1122. {
  1123. if(f_log != NULL)
  1124. {
  1125. fprintf(f_log,"%s\t $%d\t // %s\n",word,ru_stress,phonemes);
  1126. }
  1127. n_errors++;
  1128. }
  1129. else
  1130. {
  1131. counts[vcount][ru_stress][ph->type]++;
  1132. if((vcount > 1) && (ru_stress != max_stress_posn))
  1133. {
  1134. n_wrong++;
  1135. if((ru_stress==0) || (ru_stress > 7))
  1136. {
  1137. fprintf(f_out,"// "); // we only have $1 to $7 to indicate stress position
  1138. if(f_log != NULL)
  1139. {
  1140. fprintf(f_log,"%s\t $%d\t // %s\n",word,ru_stress,phonemes);
  1141. }
  1142. n_errors++;
  1143. }
  1144. else
  1145. check_root = 1;
  1146. #define X_COMPACT
  1147. fprintf(f_out,"%s",word);
  1148. #ifdef X_COMPACT
  1149. if(wlength < 8) fputc('\t',f_out);
  1150. if(wlength < 16) fputc('\t',f_out);
  1151. fprintf(f_out," $%d\n",ru_stress);
  1152. #else
  1153. while(wlength++ < 20)
  1154. fputc(' ',f_out);
  1155. fprintf(f_out," $%d //%d %s\n",ru_stress,max_stress_posn,phonemes);
  1156. #endif
  1157. //CharStats();
  1158. }
  1159. }
  1160. #ifdef deleted
  1161. if(check_root)
  1162. {
  1163. // does this word match any suffixes ?
  1164. wlen = strlen(word);
  1165. for(sfx=0;(suffix = suffixes[sfx].suffix) != NULL; sfx++)
  1166. {
  1167. len = strlen(suffix);
  1168. if(len >= (wlen-2))
  1169. continue;
  1170. if(ru_stress > (vcount - suffixes[sfx].syllables))
  1171. continue;
  1172. if(strcmp(suffix,&word[wlen-len])==0)
  1173. {
  1174. strcpy(word2,word);
  1175. word2[wlen-len] = 0;
  1176. // fprintf(f_roots,"%s\t $%d\t\\ %s\n",word2,ru_stress,suffix);
  1177. fprintf(f_roots,"%s\t $%d\n",word2,ru_stress);
  1178. }
  1179. }
  1180. }
  1181. #endif
  1182. }
  1183. fclose(f_in);
  1184. fclose(f_out);
  1185. // fclose(f_roots);
  1186. sprintf(buf,"Lexicon: Total %d OK %d fixed %d errors %d (see ru_log)",n_words,n_words-n_wrong,n_wrong,n_errors);
  1187. if(gui_flag)
  1188. {
  1189. delete progress;
  1190. wxLogStatus(wxString(buf,wxConvLocal));
  1191. }
  1192. else
  1193. {
  1194. fprintf(stderr,"%s\n",buf);
  1195. }
  1196. if(f_log != NULL)
  1197. {
  1198. #ifdef deleted
  1199. // list tables of frequency of stress position for words of different syllable lengths
  1200. int j,k;
  1201. for(ix=2; ix<12; ix++)
  1202. {
  1203. fprintf(f_log,"%2d syllables\n",ix);
  1204. for(k=0; k<10; k++)
  1205. {
  1206. fprintf(f_log," %2d :",k);
  1207. for(j=1; j<=ix; j++)
  1208. {
  1209. fprintf(f_log,"%6d ",counts[ix][j][k]);
  1210. }
  1211. fprintf(f_log,"\n");
  1212. }
  1213. fprintf(f_log,"\n\n");
  1214. }
  1215. #endif
  1216. fclose(f_log);
  1217. }
  1218. } // end of Lexicon_Ru
  1219. void CompareLexicon(int id)
  1220. {//========================
  1221. switch(id)
  1222. {
  1223. case MENU_LEXICON_RU:
  1224. Lexicon_Ru();
  1225. break;
  1226. case MENU_LEXICON_BG:
  1227. Lexicon_Bg();
  1228. break;
  1229. case MENU_LEXICON_DE:
  1230. Lexicon_De();
  1231. break;
  1232. case MENU_LEXICON_IT:
  1233. Lexicon_It(1);
  1234. Lexicon_It(2);
  1235. break;
  1236. case MENU_LEXICON_TEST:
  1237. Lexicon_Test();
  1238. break;
  1239. }
  1240. } // end of CompareLexicon
  1241. //******************************************************************************************************
  1242. extern int HashDictionary(const char *string);
  1243. static int n_words;
  1244. struct wcount {
  1245. struct wcount *link;
  1246. int count;
  1247. char *word;
  1248. };
  1249. static int wfreq_sorter(wcount **p1, wcount **p2)
  1250. {//==============================================
  1251. int x;
  1252. wcount *a, *b;
  1253. a = *p1;
  1254. b = *p2;
  1255. if((x = b->count - a->count) != 0)
  1256. return(x);
  1257. return(strcmp(a->word,b->word));
  1258. }
  1259. static void wfreq_add(const char *word, wcount **hashtab)
  1260. {//======================================================
  1261. wcount *p;
  1262. wcount **p2;
  1263. int len;
  1264. int hash;
  1265. hash = HashDictionary(word);
  1266. p2 = &hashtab[hash];
  1267. p = *p2;
  1268. while(p != NULL)
  1269. {
  1270. if(strcmp(p->word,word)==0)
  1271. {
  1272. p->count++;
  1273. return;
  1274. }
  1275. p2 = &p->link;
  1276. p = *p2;
  1277. }
  1278. // word not found, add it to the list
  1279. len = strlen(word) + 1;
  1280. if((p = (wcount *)malloc(sizeof(wcount)+len)) == NULL)
  1281. return;
  1282. p->count = 1;
  1283. p->link = NULL;
  1284. p->word = (char *)p + sizeof(wcount);
  1285. strcpy(p->word,word);
  1286. *p2 = p;
  1287. n_words++;
  1288. }
  1289. void CountWordFreq(wxString path, wcount **hashtab)
  1290. {//================================================
  1291. // Count the occurances of words in this file
  1292. FILE *f_in;
  1293. unsigned char c;
  1294. int wc;
  1295. unsigned int ix, j, k;
  1296. int n_chars;
  1297. char buf[80];
  1298. char wbuf[80];
  1299. if((f_in = fopen(path.mb_str(wxConvLocal),"rb")) == NULL)
  1300. return;
  1301. while(!feof(f_in))
  1302. {
  1303. while((c = fgetc(f_in)) < 'A')
  1304. {
  1305. // skip leading spaces, numbers, etc
  1306. if(feof(f_in)) break;
  1307. }
  1308. // read utf8 bytes until a space, number or punctuation
  1309. ix = 0;
  1310. while(!feof(f_in) && (c >= 'A') && (ix < sizeof(buf)-1))
  1311. {
  1312. buf[ix++] = c;
  1313. c = fgetc(f_in);
  1314. }
  1315. buf[ix++] = 0;
  1316. buf[ix] = 0;
  1317. // the buf may contain non-alphabetic characters
  1318. j = 0;
  1319. n_chars = 0;
  1320. for(k=0; k<ix; )
  1321. {
  1322. k += utf8_in(&wc,&buf[k]);
  1323. wc = towlower2(wc); // convert to lower case
  1324. if(iswalpha2(wc))
  1325. {
  1326. j += utf8_out(wc,&wbuf[j]);
  1327. n_chars++;
  1328. }
  1329. else
  1330. {
  1331. wbuf[j] = 0;
  1332. if(n_chars > 2)
  1333. {
  1334. wfreq_add(wbuf,hashtab);
  1335. }
  1336. j = 0;
  1337. n_chars = 0;
  1338. }
  1339. }
  1340. }
  1341. fclose(f_in);
  1342. } // end of CountWordFreq
  1343. void MakeWordFreqList()
  1344. {//====================
  1345. // Read text files from a specified directory and make a list of the most frequently occuring words.
  1346. struct wcount *whashtab[N_HASH_DICT];
  1347. wcount **w_list;
  1348. int ix;
  1349. int j;
  1350. int hash;
  1351. wcount *p;
  1352. FILE *f_out;
  1353. char buf[200];
  1354. char buf2[200];
  1355. wxString dir = wxDirSelector(_T("Directory of text files"),path_speaktext);
  1356. if(dir.IsEmpty()) return;
  1357. memset(whashtab,0,sizeof(whashtab));
  1358. wxString path = wxFindFirstFile(dir+_T("/*"),wxFILE);
  1359. while (!path.empty())
  1360. {
  1361. if(path.AfterLast(PATHSEP) != _T("!wordcounts"))
  1362. {
  1363. CountWordFreq(path,whashtab);
  1364. path = wxFindNextFile();
  1365. }
  1366. }
  1367. // put all the words into a list and then sort it
  1368. w_list = (wcount **)malloc(sizeof(wcount *) * n_words);
  1369. ix = 0;
  1370. for(hash=0; hash < N_HASH_DICT; hash++)
  1371. {
  1372. p = whashtab[hash];
  1373. while((p != NULL) && (ix < n_words))
  1374. {
  1375. w_list[ix++] = p;
  1376. p = p->link;
  1377. }
  1378. }
  1379. qsort((void *)w_list,ix,sizeof(wcount *),(int(*)(const void *,const void *))wfreq_sorter);
  1380. // write out the sorted list
  1381. strcpy(buf,dir.mb_str(wxConvLocal));
  1382. sprintf(buf2,"%s/!wordcounts",buf);
  1383. if((f_out = fopen(buf2,"w")) == NULL)
  1384. return;
  1385. for(j=0; j<ix; j++)
  1386. {
  1387. p = w_list[j];
  1388. fprintf(f_out,"%5d %s\n",p->count,p->word);
  1389. free(p);
  1390. }
  1391. fclose(f_out);
  1392. } // end of Make WorkFreqList
  1393. //******************************************************************************************************
  1394. void ConvertToUtf8()
  1395. {//=================
  1396. // Convert a file from 8bit to UTF8, according to the current voice
  1397. unsigned int c;
  1398. int ix;
  1399. FILE *f_in;
  1400. FILE *f_out;
  1401. char buf[200];
  1402. wxString fname = wxFileSelector(_T("Convert file to UTF8"),wxString(path_home,wxConvLocal),
  1403. _T(""),_T(""),_T("*"),wxOPEN);
  1404. if(fname.IsEmpty())
  1405. return;
  1406. strcpy(buf,fname.mb_str(wxConvLocal));
  1407. f_in = fopen(buf,"r");
  1408. if(f_in == NULL)
  1409. {
  1410. wxLogError(_T("Can't read file: ")+fname);
  1411. return;
  1412. }
  1413. strcat(buf,"_1");
  1414. f_out = fopen(buf,"w");
  1415. if(f_out == NULL)
  1416. {
  1417. wxLogError(_T("Can't create file: ")+wxString(buf,wxConvLocal));
  1418. fclose(f_in);
  1419. return;
  1420. }
  1421. while(!feof(f_in))
  1422. {
  1423. c = fgetc(f_in) & 0xff;
  1424. if(c >= 0xa0)
  1425. c = translator->charset_a0[c-0xa0];
  1426. ix = utf8_out(c,buf);
  1427. fwrite(buf,ix,1,f_out);
  1428. }
  1429. fclose(f_in);
  1430. fclose(f_out);
  1431. wxLogStatus(_T("Written to: ")+fname+_T("_1"));
  1432. } // end of ConvertToUtf8
  1433. //******************************************************************************************************
  1434. #define N_SORT_LIST 10000
  1435. void DictionarySort(const char *dictname)
  1436. {//======================================
  1437. // Sort rules in *_rules file between lines which begin with //sort and //endsort
  1438. FILE *f_in;
  1439. FILE *f_out;
  1440. int ix;
  1441. char *p;
  1442. char *p_end;
  1443. char *p_pre;
  1444. int sorting;
  1445. int sort_ix=0;
  1446. int sort_count=0;
  1447. int line_len;
  1448. int key_len;
  1449. char buf[200];
  1450. char key[200];
  1451. char fname_in[200];
  1452. char fname_out[200];
  1453. char *sort_list[N_SORT_LIST];
  1454. wxLogMessage(_T("Sorts the *_rules file, between lines which begin with\n//sort\n and\n//endsort"));
  1455. // try with and without '.txt' extension
  1456. sprintf(fname_in,"%s%s_rules.txt",path_dsource,dictname);
  1457. if((f_in = fopen(fname_in,"r")) == NULL)
  1458. {
  1459. sprintf(fname_in,"%s%s_rules",path_dsource,dictname);
  1460. if((f_in = fopen(fname_in,"r")) == NULL)
  1461. {
  1462. wxLogError(_T("Can't open rules file: ") + wxString(fname_in,wxConvLocal));
  1463. return;
  1464. }
  1465. }
  1466. sprintf(fname_out,"%s%s_rules_sorted",path_dsource,dictname);
  1467. if((f_out = fopen(fname_out,"w")) == NULL)
  1468. {
  1469. wxLogError(_T("Can't write to file: ") + wxString(fname_out,wxConvLocal));
  1470. fclose(f_in);
  1471. return;
  1472. }
  1473. sorting = 0;
  1474. while(fgets(buf, sizeof(buf)-1, f_in) != NULL)
  1475. {
  1476. buf[sizeof(buf)-1] = 0; // ensure zero byte terminator
  1477. line_len = strlen(buf);
  1478. if(memcmp(buf,"//endsort",9)==0)
  1479. {
  1480. sort_count++;
  1481. sorting = 0;
  1482. qsort((void *)sort_list, sort_ix, sizeof(char *), (int(*)(const void *, const void *))string_sorter);
  1483. // write out the sorted lines
  1484. for(ix=0; ix<sort_ix; ix++)
  1485. {
  1486. key_len = strlen(sort_list[ix]);
  1487. p = &sort_list[ix][key_len+1]; // the original line is after the key
  1488. fprintf(f_out,"%s",p);
  1489. free(sort_list[ix]);
  1490. }
  1491. }
  1492. if(sorting == 0)
  1493. {
  1494. if(memcmp(buf,"//sort",6)==0)
  1495. {
  1496. sorting = 1;
  1497. sort_ix = 0;
  1498. }
  1499. fwrite(buf, line_len, 1, f_out);
  1500. continue;
  1501. }
  1502. p_end = strstr(buf,"//");
  1503. if(p_end == NULL)
  1504. p_end = &buf[line_len];
  1505. // add to the list of lines to be sorted
  1506. p = buf;
  1507. while((*p==' ') || (*p == '\t')) p++; // skip leading spaces
  1508. if(*p == '?')
  1509. {
  1510. // conditional rule, skip the condition
  1511. while(!isspace(*p) && (*p != 0)) p++;
  1512. }
  1513. // skip any pre -condition
  1514. p_pre = p;
  1515. while(p < p_end)
  1516. {
  1517. if(*p == ')')
  1518. {
  1519. p_pre = p+1;
  1520. break;
  1521. }
  1522. p++;
  1523. }
  1524. p = p_pre;
  1525. while((*p==' ') || (*p == '\t')) p++; // skip spaces
  1526. ix = 0;
  1527. while(!isspace(*p) && (*p != 0))
  1528. {
  1529. key[ix++] = *p++;
  1530. }
  1531. while((*p==' ') || (*p == '\t')) p++; // skip spaces
  1532. if(*p == '(')
  1533. {
  1534. // post-condition
  1535. p++; // skip '('
  1536. while(!isspace(*p) && (*p != 0) && (p < p_end))
  1537. {
  1538. key[ix++] = *p++;
  1539. }
  1540. }
  1541. key[ix] = 0;
  1542. key_len = strlen(key);
  1543. p = (char *)malloc(key_len + line_len + 8);
  1544. sprintf(p,"%s%6d",key,sort_ix); // include the line number (within the sort section) in case the keys are otherwise equal
  1545. strcpy(p, key);
  1546. strcpy(&p[key_len+1], buf);
  1547. sort_list[sort_ix++] = p;
  1548. if(sort_ix >= N_SORT_LIST)
  1549. {
  1550. wxLogError(_T("Too many lines to sort, > %d"), N_SORT_LIST);
  1551. break;
  1552. }
  1553. }
  1554. fclose(f_in);
  1555. fclose(f_out);
  1556. if(sorting != 0)
  1557. {
  1558. wxLogError(_T("Missing //$endsort"));
  1559. }
  1560. wxLogStatus(_T("Sorted %d sections. Written to file: ") + wxString(fname_out,wxConvLocal),sort_count);
  1561. } // end of DictionarySort
  1562. void DictionaryFormat(const char *dictname)
  1563. {//========================================
  1564. // Format the *_rules file for the current voice
  1565. FILE *f_in;
  1566. FILE *f_out;
  1567. char *p;
  1568. char *p_start;
  1569. unsigned short *pw;
  1570. unsigned short *pw_match;
  1571. unsigned short *pw_post = NULL;
  1572. unsigned short *pw_phonemes = NULL;
  1573. int c;
  1574. int ix;
  1575. int n_pre;
  1576. int n_match;
  1577. int n_post;
  1578. int n_phonemes;
  1579. int n_spaces;
  1580. int n_out;
  1581. int formatting;
  1582. int comment;
  1583. char buf[200];
  1584. unsigned short bufw[200];
  1585. char conditional[80];
  1586. char fname_in[200];
  1587. char fname_out[200];
  1588. const int tab1 = 8;
  1589. const int tab2 = 18;
  1590. const int tab3 = 28;
  1591. // try with and without '.txt' extension
  1592. sprintf(fname_in,"%s%s_rules.txt",path_dsource,dictname);
  1593. if((f_in = fopen(fname_in,"r")) == NULL)
  1594. {
  1595. sprintf(fname_in,"%s%s_rules",path_dsource,dictname);
  1596. if((f_in = fopen(fname_in,"r")) == NULL)
  1597. {
  1598. wxLogError(_T("Can't open rules file: ") + wxString(fname_in,wxConvLocal));
  1599. return;
  1600. }
  1601. }
  1602. sprintf(fname_out,"%s%s_rules_formatted",path_dsource,dictname);
  1603. if((f_out = fopen(fname_out,"w")) == NULL)
  1604. {
  1605. wxLogError(_T("Can't write to file: ") + wxString(fname_out,wxConvLocal));
  1606. fclose(f_in);
  1607. return;
  1608. }
  1609. formatting = 0;
  1610. n_match = 0;
  1611. while(fgets(buf, sizeof(buf)-1, f_in) != NULL)
  1612. {
  1613. buf[sizeof(buf)-1] = 0; // ensure zero byte terminator
  1614. ix = strlen(buf) - 1;
  1615. while((buf[ix]=='\n') || (buf[ix]==' ') || (buf[ix]=='\t')) ix--;
  1616. buf[ix+1] = 0; // strip tailing spaces
  1617. p_start = buf;
  1618. while((*p_start==' ') || (*p_start == '\t')) p_start++; // skip leading spaces
  1619. comment = 0;
  1620. if((p_start[0]=='/') && (p_start[1]=='/'))
  1621. comment = 1;
  1622. ix = 0;
  1623. if(*p_start == '?')
  1624. {
  1625. // conditional rule
  1626. while(!isspace(*p_start) && (*p_start != 0))
  1627. {
  1628. conditional[ix++] = *p_start++;
  1629. }
  1630. while((*p_start == ' ') || (*p_start == '\t')) p_start++;
  1631. }
  1632. conditional[ix] = 0;
  1633. if(buf[0] == '.')
  1634. {
  1635. formatting = 0;
  1636. }
  1637. if(memcmp(p_start, ".group", 6) == 0)
  1638. {
  1639. formatting = 2;
  1640. if(n_match > 0)
  1641. {
  1642. // previous line was not blank, so add a blank line
  1643. fprintf(f_out,"\n");
  1644. }
  1645. }
  1646. n_match = 0;
  1647. if((formatting == 1) && (comment==0))
  1648. {
  1649. // convert from UTF-8 to UTF-16
  1650. p = p_start;
  1651. pw = bufw;
  1652. do {
  1653. p += utf8_in(&c, p);
  1654. *pw++ = c;
  1655. } while (c != 0);
  1656. pw = bufw;
  1657. while((*pw != ')') && (*pw != 0) && !iswspace(*pw)) pw++;
  1658. n_pre = 0;
  1659. n_post = 0;
  1660. n_phonemes = 0;
  1661. n_spaces = 0;
  1662. if(*pw != 0)
  1663. n_spaces = tab1;
  1664. if(*pw == ')')
  1665. {
  1666. // there is a pre-condition
  1667. n_pre = pw - bufw + 1;
  1668. n_spaces = tab1 - n_pre - 1;
  1669. pw++;
  1670. while((*pw==' ') || (*pw=='\t')) pw++;
  1671. }
  1672. else
  1673. {
  1674. pw = bufw;
  1675. }
  1676. pw_match = pw;
  1677. while(((c = *pw)!= ' ') && (c != '\t') && (c != '(') && (c != 0))
  1678. {
  1679. pw++;
  1680. }
  1681. n_match = pw - pw_match;
  1682. while(((c = *pw)==' ') || (c == '\t')) pw++;
  1683. if(*pw == '(')
  1684. {
  1685. pw_post = pw;
  1686. while(((c = *pw)!=' ') && (c != '\t') && (c != 0)) pw++;
  1687. n_post = pw - pw_post;
  1688. while(((c = *pw)==' ') || (c == '\t')) pw++;
  1689. }
  1690. if((*pw != 0) && ((*pw != '/') || (pw[1] != '/')))
  1691. {
  1692. pw_phonemes = pw;
  1693. while(((c = *pw)!=' ') && (c != '\t') && (c != 0)) pw++;
  1694. n_phonemes = pw - pw_phonemes;
  1695. while(((c = *pw)==' ') || (c == '\t')) pw++;
  1696. }
  1697. // write formatted line
  1698. p = buf;
  1699. if(conditional[0] != 0)
  1700. {
  1701. ix = 0;
  1702. while(conditional[ix] != 0)
  1703. {
  1704. *p++ = conditional[ix++];
  1705. n_spaces--;
  1706. }
  1707. *p++ = ' ';
  1708. n_spaces--;
  1709. }
  1710. while(n_spaces-- > 0)
  1711. {
  1712. *p++ = ' ';
  1713. }
  1714. if(n_pre > 0)
  1715. {
  1716. ix = 0;
  1717. for(ix=0; ix<n_pre; ix++)
  1718. {
  1719. p += utf8_out(bufw[ix], p);
  1720. }
  1721. *p++ = ' ';
  1722. }
  1723. // write the match condition
  1724. if(n_match > 0)
  1725. {
  1726. ix = 0;
  1727. for(ix=0; ix<n_match; ix++)
  1728. {
  1729. p += utf8_out(pw_match[ix], p);
  1730. }
  1731. *p++ = ' ';
  1732. }
  1733. // write the post condition
  1734. if(n_post > 0)
  1735. {
  1736. for(ix=0; ix<n_post; ix++)
  1737. {
  1738. p += utf8_out(pw_post[ix], p);
  1739. }
  1740. *p++ = ' ';
  1741. n_post++;
  1742. }
  1743. n_out = tab1 + n_match + n_post;
  1744. if(n_pre >= tab1)
  1745. n_out += (n_pre - tab1 + 1);
  1746. if(n_phonemes > 0)
  1747. {
  1748. n_spaces = tab2 - n_out;
  1749. while(n_spaces-- > 0)
  1750. {
  1751. *p++ = ' ';
  1752. n_out++;
  1753. }
  1754. // write the phoneme codes
  1755. for(ix=0; ix<n_phonemes; ix++)
  1756. {
  1757. p += utf8_out(pw_phonemes[ix], p);
  1758. }
  1759. }
  1760. if(*pw != 0)
  1761. {
  1762. *p++ = ' ';
  1763. n_phonemes++;
  1764. n_spaces = tab3 - (n_out + n_phonemes);
  1765. while(n_spaces-- > 0)
  1766. {
  1767. *p++ = ' ';
  1768. }
  1769. }
  1770. // write the remainer of the line
  1771. while(*pw != 0)
  1772. {
  1773. p+= utf8_out(*pw++, p);
  1774. }
  1775. *p = 0;
  1776. }
  1777. if(formatting > 1)
  1778. formatting--;
  1779. fprintf(f_out, "%s\n", buf);
  1780. }
  1781. fclose(f_in);
  1782. fclose(f_out);
  1783. remove(fname_in);
  1784. if(rename(fname_out, fname_in) == 0)
  1785. wxLogStatus(_("Written to file: ") + wxString(fname_in,wxConvLocal));
  1786. else
  1787. wxLogStatus(_("Failed to rename: ") + wxString(fname_out,wxConvLocal));
  1788. } // end of DictionaryFormat
  1789. //******************************************************************************************************
  1790. //#define calcspeedtab
  1791. #ifdef calcspeedtab
  1792. // used to set up the presets in the speed_lookup table
  1793. // interpolate between a set of measured wpm values
  1794. void SetSpeedTab(void)
  1795. {//===================
  1796. #define N_WPM 19
  1797. // Interpolation table to translate from words-per-minute to internal speed
  1798. // words-per-minute values (measured)
  1799. static float wpm1[N_WPM] =
  1800. {0, 82, 96, 108, 124, 134, 147, 162, 174, 189, 224, 259, 273, 289, 307, 326, 346, 361, 370 };
  1801. // corresponding internal speed values
  1802. static float wpm2[N_WPM] =
  1803. {0,253,200, 170, 140, 125, 110, 95, 85, 75, 55, 40, 35, 30, 25, 20, 15, 10, 5 };
  1804. unsigned char speed_lookup[290];
  1805. unsigned int ix;
  1806. float x;
  1807. int speed_wpm;
  1808. FILE *f;
  1809. // convert from word-per-minute to internal speed code
  1810. for(speed_wpm=80; speed_wpm<370; speed_wpm++)
  1811. {
  1812. for(ix=2; ix<N_WPM-2; ix++)
  1813. {
  1814. if(speed_wpm < wpm1[ix])
  1815. break;
  1816. }
  1817. x = polint(&wpm1[ix-1], &wpm2[ix-1], 3, speed_wpm);
  1818. speed_lookup[speed_wpm-80] = (unsigned char)x;
  1819. }
  1820. f = fopen("speed_lookup","w");
  1821. if(f == NULL) return;
  1822. for(ix=0; ix<sizeof(speed_lookup); ix++)
  1823. {
  1824. fprintf(f,"%4d,",speed_lookup[ix]);
  1825. if((ix % 5) == 4)
  1826. fprintf(f,"\t//%4d\n\t",(ix / 5)*5 + 80);
  1827. }
  1828. fclose(f);
  1829. } // end of SetSpeedTab
  1830. #endif
  1831. //#define xcharset
  1832. #ifdef xcharset
  1833. #include "iconv.h"
  1834. void CharsetToUnicode(const char *charset)
  1835. {//=======================================
  1836. // write a 8bit charset to unicode translation table to file
  1837. // charset: eg. "ISO-8859-1"
  1838. iconv_t cd;
  1839. unsigned char inbuf[4];
  1840. size_t n_inbuf;
  1841. unsigned char outbuf[12];
  1842. size_t n_outbuf;
  1843. int n;
  1844. int ix;
  1845. int x, y;
  1846. FILE *f;
  1847. char *p_inbuf;
  1848. char *p_outbuf;
  1849. f = fopen("/home/jsd1/tmp1/unicode1","a");
  1850. cd = iconv_open("WCHAR_T",charset);
  1851. if (cd == (iconv_t) -1)
  1852. {
  1853. fprintf(stderr,"Error - iconv_open\n");
  1854. return;
  1855. }
  1856. fprintf(f,"towlower_tab\n ");
  1857. for(ix=0x80; ix<=0x241; ix++)
  1858. {
  1859. y = 0;
  1860. if(iswalpha(ix))
  1861. {
  1862. x = towlower(ix);
  1863. if(x == ix)
  1864. y = 0xff;
  1865. else
  1866. y = x - ix;
  1867. }
  1868. if((y == 0xff) || (y < 0))
  1869. fprintf(f,"0xff,"); // ignore the 5 obscure cases where uc > lc
  1870. else
  1871. {
  1872. fprintf(f,"%4d,",y);
  1873. }
  1874. if((ix&15)==15)
  1875. fprintf(f," // %x\n ",ix & ~15);
  1876. }
  1877. fprintf(f,"\n%s\n ",charset);
  1878. for(ix=0x80; ix<0x100; ix++)
  1879. {
  1880. inbuf[0] = ix;
  1881. inbuf[1] = 0;
  1882. inbuf[2] = 0;
  1883. outbuf[0] = 0;
  1884. outbuf[1] = 0;
  1885. n_inbuf = 2;
  1886. n_outbuf = sizeof(outbuf);
  1887. p_inbuf = (char *)inbuf;
  1888. p_outbuf = (char *)outbuf;
  1889. n = iconv(cd, &p_inbuf, &n_inbuf, &p_outbuf, &n_outbuf);
  1890. fprintf(f,"0x%.2x%.2x, ",outbuf[1],outbuf[0]);
  1891. if((ix&7)==7)
  1892. fprintf(f,"// %.2x\n ",ix & ~7);
  1893. }
  1894. fclose(f);
  1895. iconv_close(cd);
  1896. }
  1897. #endif
  1898. #ifdef deleted
  1899. void Test2()
  1900. {
  1901. //
  1902. char buf[120];
  1903. FILE *f;
  1904. FILE *f_out;
  1905. unsigned char *p;
  1906. f = fopen("/home/jsd1/tmp1/list","r");
  1907. if(f == NULL) return;
  1908. f_out = fopen("/home/jsd1/tmp1/list_out","w");
  1909. if(f_out == NULL) return;
  1910. while(!feof(f))
  1911. {
  1912. if(fgets(buf,sizeof(buf),f) == NULL)
  1913. break;
  1914. p = (unsigned char *)buf;
  1915. while(*p > ' ') p++;
  1916. *p = 0;
  1917. fprintf(f_out,"%s . . .\n",buf);
  1918. }
  1919. fclose(f);
  1920. fclose(f_out);
  1921. }
  1922. #endif
  1923. #define MAX_WALPHA 0x24f
  1924. void Make_walpha_tab()
  1925. {//===================
  1926. int ix;
  1927. int value;
  1928. int c;
  1929. short exceptions[40];
  1930. int ex = 0;
  1931. FILE *f;
  1932. f = fopen("/home/jsd1/walpha_tab.txt","w");
  1933. for(ix=0x80; ix<=MAX_WALPHA; ix++)
  1934. {
  1935. value = 0;
  1936. if(iswalpha(ix))
  1937. {
  1938. value = 0xfe; // no case
  1939. if(iswlower(ix))
  1940. {
  1941. value = 0xff;
  1942. }
  1943. else if(iswupper(ix))
  1944. {
  1945. c = towlower(ix);
  1946. if(c > 0)
  1947. value = c - ix;
  1948. if((value < 0) || (value > 0xfc))
  1949. {
  1950. exceptions[ex] = ix;
  1951. exceptions[ex+1] = c;
  1952. ex+=2;
  1953. value = 0xfd;
  1954. }
  1955. }
  1956. }
  1957. if(value > 0xfc)
  1958. fprintf(f," 0x%.2x,", value);
  1959. else
  1960. fprintf(f," %4d,", value);
  1961. if((ix % 16) == 15)
  1962. {
  1963. fprintf(f," // %.3x\n", ix & 0xfff0);
  1964. }
  1965. }
  1966. fprintf(f, "\nstatic const short wchar_tolower[] = {\n");
  1967. for(ix=0; ix<ex; ix+=2)
  1968. {
  1969. fprintf(f,"\t0x%.3x, 0x%.3x,\n", exceptions[ix], exceptions[ix+1]);
  1970. }
  1971. fprintf(f, "\t0,0 };\n");
  1972. fprintf(f, "\nstatic const short wchar_toupper[] = {\n");
  1973. for(ix=0x80; ix<=MAX_WALPHA; ix++)
  1974. {
  1975. if(iswlower(ix))
  1976. {
  1977. c = towupper(ix);
  1978. value = ix - c;
  1979. if((value != 32) && (value != 1))
  1980. {
  1981. fprintf(f,"\t0x%.3x, 0x%.3x,\n", ix, c);
  1982. }
  1983. }
  1984. }
  1985. fprintf(f, "\t0,0 };\n");
  1986. fclose(f);
  1987. }
  1988. const char* text1 = "Hello world. Testing.";
  1989. extern void TestCompile2(void);
  1990. void TestTest(int control)
  1991. {//=======================
  1992. FILE *f;
  1993. unsigned int c;
  1994. unsigned int ix=0;
  1995. char textbuf[2000];
  1996. espeak_VOICE voice;
  1997. static unsigned int unique_identifier= 123;
  1998. static int user_data = 456;
  1999. //CharsetToUnicode("ISO-8859-4");
  2000. //CharsetToUnicode("ISCII");
  2001. if(control==2)
  2002. {
  2003. return;
  2004. }
  2005. memset(&voice,0,sizeof(voice));
  2006. f = fopen("/home/jsd1/speechdata/text/test.txt","r");
  2007. if(f==NULL)
  2008. return;
  2009. while(!feof(f) && (ix < sizeof(textbuf)-2))
  2010. {
  2011. c = fgetc(f);
  2012. if(!feof(f))
  2013. textbuf[ix++] = c;
  2014. }
  2015. textbuf[ix] = 0;
  2016. fclose(f);
  2017. f_wavtest = OpenWaveFile3("/home/jsd1/speechdata/text/test.wav");
  2018. f_events = fopen("/home/jsd1/speechdata/text/events","w");
  2019. fprintf(f_events,"Type Audio Text Length Id\n");
  2020. fclose(f_events);
  2021. espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 1000, NULL, espeakINITIALIZE_PHONEME_EVENTS);
  2022. espeak_SetSynthCallback(TestSynthCallback);
  2023. espeak_SetUriCallback(TestUriCallback);
  2024. voice.languages = "fr";
  2025. espeak_SetVoiceByProperties(&voice);
  2026. // espeak_SetVoiceByName("fr");
  2027. espeak_Synth("1", 5, 0, POS_CHARACTER, 0, espeakSSML|espeakCHARS_UTF8, &unique_identifier, (void *)user_data);
  2028. // voice.languages = "en";
  2029. // espeak_SetVoiceByProperties(&voice);
  2030. espeak_SetVoiceByName("de");
  2031. espeak_Synth(textbuf, strlen(textbuf)+1, 0, POS_CHARACTER, 0, espeakSSML|espeakCHARS_UTF8, &unique_identifier, (void *)user_data);
  2032. // espeak_Synth(text1, strlen(text1)+1, 0, POS_CHARACTER, 0, espeakSSML|espeakCHARS_UTF8, &unique_identifier, (void *)(user_data+1));
  2033. espeak_SetParameter(espeakPUNCTUATION, 1, 0);
  2034. espeak_Synchronize();
  2035. // espeak_Cancel();
  2036. espeak_SetParameter(espeakPUNCTUATION, 1, 0);
  2037. }