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

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