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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420
  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
  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. } // end of Make WorkFreqList
  1395. //******************************************************************************************************
  1396. void ConvertToUtf8()
  1397. {//=================
  1398. // Convert a file from 8bit to UTF8, according to the current voice
  1399. unsigned int c;
  1400. int ix;
  1401. FILE *f_in;
  1402. FILE *f_out;
  1403. char buf[200];
  1404. wxString fname = wxFileSelector(_T("Convert file to UTF8"),wxString(path_home,wxConvLocal),
  1405. _T(""),_T(""),_T("*"),wxOPEN);
  1406. if(fname.IsEmpty())
  1407. return;
  1408. strcpy(buf,fname.mb_str(wxConvLocal));
  1409. f_in = fopen(buf,"r");
  1410. if(f_in == NULL)
  1411. {
  1412. wxLogError(_T("Can't read file: ")+fname);
  1413. return;
  1414. }
  1415. strcat(buf,"_1");
  1416. f_out = fopen(buf,"w");
  1417. if(f_out == NULL)
  1418. {
  1419. wxLogError(_T("Can't create file: ")+wxString(buf,wxConvLocal));
  1420. fclose(f_in);
  1421. return;
  1422. }
  1423. while(!feof(f_in))
  1424. {
  1425. c = fgetc(f_in) & 0xff;
  1426. if(c >= 0xa0)
  1427. c = translator->charset_a0[c-0xa0];
  1428. ix = utf8_out(c,buf);
  1429. fwrite(buf,ix,1,f_out);
  1430. }
  1431. fclose(f_in);
  1432. fclose(f_out);
  1433. wxLogStatus(_T("Written to: ")+fname+_T("_1"));
  1434. } // end of ConvertToUtf8
  1435. //******************************************************************************************************
  1436. #define N_SORT_LIST 10000
  1437. void DictionarySort(const char *dictname)
  1438. {//======================================
  1439. // Sort rules in *_rules file between lines which begin with //sort and //endsort
  1440. FILE *f_in;
  1441. FILE *f_out;
  1442. int ix;
  1443. char *p;
  1444. char *p_end;
  1445. char *p_pre;
  1446. int sorting;
  1447. int sort_ix=0;
  1448. int sort_count=0;
  1449. int line_len;
  1450. int key_len;
  1451. char buf[200];
  1452. char key[200];
  1453. char fname_in[200];
  1454. char fname_out[200];
  1455. char *sort_list[N_SORT_LIST];
  1456. wxLogMessage(_T("Sorts the *_rules file, between lines which begin with\n//sort\n and\n//endsort"));
  1457. // try with and without '.txt' extension
  1458. sprintf(fname_in,"%s%s_rules.txt",path_dsource,dictname);
  1459. if((f_in = fopen(fname_in,"r")) == NULL)
  1460. {
  1461. sprintf(fname_in,"%s%s_rules",path_dsource,dictname);
  1462. if((f_in = fopen(fname_in,"r")) == NULL)
  1463. {
  1464. wxLogError(_T("Can't open rules file: ") + wxString(fname_in,wxConvLocal));
  1465. return;
  1466. }
  1467. }
  1468. sprintf(fname_out,"%s%s_rules_sorted",path_dsource,dictname);
  1469. if((f_out = fopen(fname_out,"w")) == NULL)
  1470. {
  1471. wxLogError(_T("Can't write to file: ") + wxString(fname_out,wxConvLocal));
  1472. fclose(f_in);
  1473. return;
  1474. }
  1475. sorting = 0;
  1476. while(fgets(buf, sizeof(buf)-1, f_in) != NULL)
  1477. {
  1478. buf[sizeof(buf)-1] = 0; // ensure zero byte terminator
  1479. line_len = strlen(buf);
  1480. if(memcmp(buf,"//endsort",9)==0)
  1481. {
  1482. sort_count++;
  1483. sorting = 0;
  1484. qsort((void *)sort_list, sort_ix, sizeof(char *), (int(*)(const void *, const void *))string_sorter);
  1485. // write out the sorted lines
  1486. for(ix=0; ix<sort_ix; ix++)
  1487. {
  1488. key_len = strlen(sort_list[ix]);
  1489. p = &sort_list[ix][key_len+1]; // the original line is after the key
  1490. fprintf(f_out,"%s",p);
  1491. free(sort_list[ix]);
  1492. }
  1493. }
  1494. if(sorting == 0)
  1495. {
  1496. if(memcmp(buf,"//sort",6)==0)
  1497. {
  1498. sorting = 1;
  1499. sort_ix = 0;
  1500. }
  1501. fwrite(buf, line_len, 1, f_out);
  1502. continue;
  1503. }
  1504. p_end = strstr(buf,"//");
  1505. if(p_end == NULL)
  1506. p_end = &buf[line_len];
  1507. // add to the list of lines to be sorted
  1508. p = buf;
  1509. while((*p==' ') || (*p == '\t')) p++; // skip leading spaces
  1510. if(*p == '?')
  1511. {
  1512. // conditional rule, skip the condition
  1513. while(!isspace(*p) && (*p != 0)) p++;
  1514. }
  1515. // skip any pre -condition
  1516. p_pre = p;
  1517. while(p < p_end)
  1518. {
  1519. if(*p == ')')
  1520. {
  1521. p_pre = p+1;
  1522. break;
  1523. }
  1524. p++;
  1525. }
  1526. p = p_pre;
  1527. while((*p==' ') || (*p == '\t')) p++; // skip spaces
  1528. ix = 0;
  1529. while(!isspace(*p) && (*p != 0))
  1530. {
  1531. key[ix++] = *p++;
  1532. }
  1533. while((*p==' ') || (*p == '\t')) p++; // skip spaces
  1534. if(*p == '(')
  1535. {
  1536. // post-condition
  1537. p++; // skip '('
  1538. while(!isspace(*p) && (*p != 0) && (p < p_end))
  1539. {
  1540. key[ix++] = *p++;
  1541. }
  1542. }
  1543. key[ix] = 0;
  1544. key_len = strlen(key);
  1545. p = (char *)malloc(key_len + line_len + 8);
  1546. sprintf(p,"%s%6d",key,sort_ix); // include the line number (within the sort section) in case the keys are otherwise equal
  1547. strcpy(p, key);
  1548. strcpy(&p[key_len+1], buf);
  1549. sort_list[sort_ix++] = p;
  1550. if(sort_ix >= N_SORT_LIST)
  1551. {
  1552. wxLogError(_T("Too many lines to sort, > %d"), N_SORT_LIST);
  1553. break;
  1554. }
  1555. }
  1556. fclose(f_in);
  1557. fclose(f_out);
  1558. if(sorting != 0)
  1559. {
  1560. wxLogError(_T("Missing //$endsort"));
  1561. }
  1562. wxLogStatus(_T("Sorted %d sections. Written to file: ") + wxString(fname_out,wxConvLocal),sort_count);
  1563. } // end of DictionarySort
  1564. void DictionaryFormat(const char *dictname)
  1565. {//========================================
  1566. // Format the *_rules file for the current voice
  1567. FILE *f_in;
  1568. FILE *f_out;
  1569. char *p;
  1570. char *p_start;
  1571. unsigned short *pw;
  1572. unsigned short *pw_match;
  1573. unsigned short *pw_post = NULL;
  1574. unsigned short *pw_phonemes = NULL;
  1575. int c;
  1576. int ix;
  1577. int n_pre;
  1578. int n_match;
  1579. int n_post;
  1580. int n_phonemes;
  1581. int n_spaces;
  1582. int n_out;
  1583. int formatting;
  1584. int comment;
  1585. char buf[200];
  1586. unsigned short bufw[200];
  1587. char conditional[80];
  1588. char fname_in[200];
  1589. char fname_out[200];
  1590. const int tab1 = 8;
  1591. const int tab2 = 18;
  1592. const int tab3 = 28;
  1593. // try with and without '.txt' extension
  1594. sprintf(fname_in,"%s%s_rules.txt",path_dsource,dictname);
  1595. if((f_in = fopen(fname_in,"r")) == NULL)
  1596. {
  1597. sprintf(fname_in,"%s%s_rules",path_dsource,dictname);
  1598. if((f_in = fopen(fname_in,"r")) == NULL)
  1599. {
  1600. wxLogError(_T("Can't open rules file: ") + wxString(fname_in,wxConvLocal));
  1601. return;
  1602. }
  1603. }
  1604. sprintf(fname_out,"%s%s_rules_formatted",path_dsource,dictname);
  1605. if((f_out = fopen(fname_out,"w")) == NULL)
  1606. {
  1607. wxLogError(_T("Can't write to file: ") + wxString(fname_out,wxConvLocal));
  1608. fclose(f_in);
  1609. return;
  1610. }
  1611. formatting = 0;
  1612. n_match = 0;
  1613. while(fgets(buf, sizeof(buf)-1, f_in) != NULL)
  1614. {
  1615. buf[sizeof(buf)-1] = 0; // ensure zero byte terminator
  1616. ix = strlen(buf) - 1;
  1617. while((buf[ix]=='\n') || (buf[ix]==' ') || (buf[ix]=='\t')) ix--;
  1618. buf[ix+1] = 0; // strip tailing spaces
  1619. p_start = buf;
  1620. while((*p_start==' ') || (*p_start == '\t')) p_start++; // skip leading spaces
  1621. comment = 0;
  1622. if((p_start[0]=='/') && (p_start[1]=='/'))
  1623. comment = 1;
  1624. ix = 0;
  1625. if(*p_start == '?')
  1626. {
  1627. // conditional rule
  1628. while(!isspace(*p_start) && (*p_start != 0))
  1629. {
  1630. conditional[ix++] = *p_start++;
  1631. }
  1632. while((*p_start == ' ') || (*p_start == '\t')) p_start++;
  1633. }
  1634. conditional[ix] = 0;
  1635. if(buf[0] == '.')
  1636. {
  1637. formatting = 0;
  1638. }
  1639. if(memcmp(p_start, ".group", 6) == 0)
  1640. {
  1641. formatting = 2;
  1642. if(n_match > 0)
  1643. {
  1644. // previous line was not blank, so add a blank line
  1645. fprintf(f_out,"\n");
  1646. }
  1647. }
  1648. n_match = 0;
  1649. if((formatting == 1) && (comment==0))
  1650. {
  1651. // convert from UTF-8 to UTF-16
  1652. p = p_start;
  1653. pw = bufw;
  1654. do {
  1655. p += utf8_in(&c, p);
  1656. *pw++ = c;
  1657. } while (c != 0);
  1658. pw = bufw;
  1659. while((*pw != ')') && (*pw != 0) && !iswspace(*pw)) pw++;
  1660. n_pre = 0;
  1661. n_post = 0;
  1662. n_phonemes = 0;
  1663. n_spaces = 0;
  1664. if(*pw != 0)
  1665. n_spaces = tab1;
  1666. if(*pw == ')')
  1667. {
  1668. // there is a pre-condition
  1669. n_pre = pw - bufw + 1;
  1670. n_spaces = tab1 - n_pre - 1;
  1671. pw++;
  1672. while((*pw==' ') || (*pw=='\t')) pw++;
  1673. }
  1674. else
  1675. {
  1676. pw = bufw;
  1677. }
  1678. pw_match = pw;
  1679. while(((c = *pw)!= ' ') && (c != '\t') && (c != '(') && (c != 0))
  1680. {
  1681. pw++;
  1682. }
  1683. n_match = pw - pw_match;
  1684. while(((c = *pw)==' ') || (c == '\t')) pw++;
  1685. if(*pw == '(')
  1686. {
  1687. pw_post = pw;
  1688. while(((c = *pw)!=' ') && (c != '\t') && (c != 0)) pw++;
  1689. n_post = pw - pw_post;
  1690. while(((c = *pw)==' ') || (c == '\t')) pw++;
  1691. }
  1692. if((*pw != 0) && ((*pw != '/') || (pw[1] != '/')))
  1693. {
  1694. pw_phonemes = pw;
  1695. while(((c = *pw)!=' ') && (c != '\t') && (c != 0)) pw++;
  1696. n_phonemes = pw - pw_phonemes;
  1697. while(((c = *pw)==' ') || (c == '\t')) pw++;
  1698. }
  1699. // write formatted line
  1700. p = buf;
  1701. if(conditional[0] != 0)
  1702. {
  1703. ix = 0;
  1704. while(conditional[ix] != 0)
  1705. {
  1706. *p++ = conditional[ix++];
  1707. n_spaces--;
  1708. }
  1709. *p++ = ' ';
  1710. n_spaces--;
  1711. }
  1712. while(n_spaces-- > 0)
  1713. {
  1714. *p++ = ' ';
  1715. }
  1716. if(n_pre > 0)
  1717. {
  1718. ix = 0;
  1719. for(ix=0; ix<n_pre; ix++)
  1720. {
  1721. p += utf8_out(bufw[ix], p);
  1722. }
  1723. *p++ = ' ';
  1724. }
  1725. // write the match condition
  1726. if(n_match > 0)
  1727. {
  1728. ix = 0;
  1729. for(ix=0; ix<n_match; ix++)
  1730. {
  1731. p += utf8_out(pw_match[ix], p);
  1732. }
  1733. *p++ = ' ';
  1734. }
  1735. // write the post condition
  1736. if(n_post > 0)
  1737. {
  1738. for(ix=0; ix<n_post; ix++)
  1739. {
  1740. p += utf8_out(pw_post[ix], p);
  1741. }
  1742. *p++ = ' ';
  1743. n_post++;
  1744. }
  1745. n_out = tab1 + n_match + n_post;
  1746. if(n_pre >= tab1)
  1747. n_out += (n_pre - tab1 + 1);
  1748. if(n_phonemes > 0)
  1749. {
  1750. n_spaces = tab2 - n_out;
  1751. while(n_spaces-- > 0)
  1752. {
  1753. *p++ = ' ';
  1754. n_out++;
  1755. }
  1756. // write the phoneme codes
  1757. for(ix=0; ix<n_phonemes; ix++)
  1758. {
  1759. p += utf8_out(pw_phonemes[ix], p);
  1760. }
  1761. }
  1762. if(*pw != 0)
  1763. {
  1764. *p++ = ' ';
  1765. n_phonemes++;
  1766. n_spaces = tab3 - (n_out + n_phonemes);
  1767. while(n_spaces-- > 0)
  1768. {
  1769. *p++ = ' ';
  1770. }
  1771. }
  1772. // write the remainer of the line
  1773. while(*pw != 0)
  1774. {
  1775. p+= utf8_out(*pw++, p);
  1776. }
  1777. *p = 0;
  1778. }
  1779. if(formatting > 1)
  1780. formatting--;
  1781. fprintf(f_out, "%s\n", buf);
  1782. }
  1783. fclose(f_in);
  1784. fclose(f_out);
  1785. remove(fname_in);
  1786. if(rename(fname_out, fname_in) == 0)
  1787. wxLogStatus(_("Written to file: ") + wxString(fname_in,wxConvLocal));
  1788. else
  1789. wxLogStatus(_("Failed to rename: ") + wxString(fname_out,wxConvLocal));
  1790. } // end of DictionaryFormat
  1791. //******************************************************************************************************
  1792. //#define calcspeedtab
  1793. #ifdef calcspeedtab
  1794. // used to set up the presets in the speed_lookup table
  1795. // interpolate between a set of measured wpm values
  1796. void SetSpeedTab(void)
  1797. {//===================
  1798. #define N_WPM 19
  1799. // Interpolation table to translate from words-per-minute to internal speed
  1800. // words-per-minute values (measured)
  1801. static float wpm1[N_WPM] =
  1802. {0, 82, 96, 108, 124, 134, 147, 162, 174, 189, 224, 259, 273, 289, 307, 326, 346, 361, 370 };
  1803. // corresponding internal speed values
  1804. static float wpm2[N_WPM] =
  1805. {0,253,200, 170, 140, 125, 110, 95, 85, 75, 55, 40, 35, 30, 25, 20, 15, 10, 5 };
  1806. unsigned char speed_lookup[290];
  1807. unsigned int ix;
  1808. float x;
  1809. int speed_wpm;
  1810. FILE *f;
  1811. // convert from word-per-minute to internal speed code
  1812. for(speed_wpm=80; speed_wpm<370; speed_wpm++)
  1813. {
  1814. for(ix=2; ix<N_WPM-2; ix++)
  1815. {
  1816. if(speed_wpm < wpm1[ix])
  1817. break;
  1818. }
  1819. x = polint(&wpm1[ix-1], &wpm2[ix-1], 3, speed_wpm);
  1820. speed_lookup[speed_wpm-80] = (unsigned char)x;
  1821. }
  1822. f = fopen("speed_lookup","w");
  1823. if(f == NULL) return;
  1824. for(ix=0; ix<sizeof(speed_lookup); ix++)
  1825. {
  1826. fprintf(f,"%4d,",speed_lookup[ix]);
  1827. if((ix % 5) == 4)
  1828. fprintf(f,"\t//%4d\n\t",(ix / 5)*5 + 80);
  1829. }
  1830. fclose(f);
  1831. } // end of SetSpeedTab
  1832. #endif
  1833. //#define xcharset
  1834. #ifdef xcharset
  1835. #include "iconv.h"
  1836. void CharsetToUnicode(const char *charset)
  1837. {//=======================================
  1838. // write a 8bit charset to unicode translation table to file
  1839. // charset: eg. "ISO-8859-1"
  1840. iconv_t cd;
  1841. unsigned char inbuf[4];
  1842. size_t n_inbuf;
  1843. unsigned char outbuf[12];
  1844. size_t n_outbuf;
  1845. int n;
  1846. int ix;
  1847. int x, y;
  1848. FILE *f;
  1849. char *p_inbuf;
  1850. char *p_outbuf;
  1851. f = fopen("/home/jsd1/tmp1/unicode1","a");
  1852. cd = iconv_open("WCHAR_T",charset);
  1853. if (cd == (iconv_t) -1)
  1854. {
  1855. fprintf(stderr,"Error - iconv_open\n");
  1856. return;
  1857. }
  1858. fprintf(f,"towlower_tab\n ");
  1859. for(ix=0x80; ix<=0x241; ix++)
  1860. {
  1861. y = 0;
  1862. if(iswalpha(ix))
  1863. {
  1864. x = towlower(ix);
  1865. if(x == ix)
  1866. y = 0xff;
  1867. else
  1868. y = x - ix;
  1869. }
  1870. if((y == 0xff) || (y < 0))
  1871. fprintf(f,"0xff,"); // ignore the 5 obscure cases where uc > lc
  1872. else
  1873. {
  1874. fprintf(f,"%4d,",y);
  1875. }
  1876. if((ix&15)==15)
  1877. fprintf(f," // %x\n ",ix & ~15);
  1878. }
  1879. fprintf(f,"\n%s\n ",charset);
  1880. for(ix=0x80; ix<0x100; ix++)
  1881. {
  1882. inbuf[0] = ix;
  1883. inbuf[1] = 0;
  1884. inbuf[2] = 0;
  1885. outbuf[0] = 0;
  1886. outbuf[1] = 0;
  1887. n_inbuf = 2;
  1888. n_outbuf = sizeof(outbuf);
  1889. p_inbuf = (char *)inbuf;
  1890. p_outbuf = (char *)outbuf;
  1891. n = iconv(cd, &p_inbuf, &n_inbuf, &p_outbuf, &n_outbuf);
  1892. fprintf(f,"0x%.2x%.2x, ",outbuf[1],outbuf[0]);
  1893. if((ix&7)==7)
  1894. fprintf(f,"// %.2x\n ",ix & ~7);
  1895. }
  1896. fclose(f);
  1897. iconv_close(cd);
  1898. }
  1899. #endif
  1900. #ifdef deleted
  1901. void Test2()
  1902. {
  1903. //
  1904. char buf[120];
  1905. FILE *f;
  1906. FILE *f_out;
  1907. unsigned char *p;
  1908. f = fopen("/home/jsd1/tmp1/list","r");
  1909. if(f == NULL) return;
  1910. f_out = fopen("/home/jsd1/tmp1/list_out","w");
  1911. if(f_out == NULL) return;
  1912. while(!feof(f))
  1913. {
  1914. if(fgets(buf,sizeof(buf),f) == NULL)
  1915. break;
  1916. p = (unsigned char *)buf;
  1917. while(*p > ' ') p++;
  1918. *p = 0;
  1919. fprintf(f_out,"%s . . .\n",buf);
  1920. }
  1921. fclose(f);
  1922. fclose(f_out);
  1923. }
  1924. #endif
  1925. #define MAX_WALPHA 0x24f
  1926. void Make_walpha_tab()
  1927. {//===================
  1928. int ix;
  1929. int value;
  1930. int c;
  1931. short exceptions[40];
  1932. int ex = 0;
  1933. FILE *f;
  1934. f = fopen("/home/jsd1/walpha_tab.txt","w");
  1935. for(ix=0x80; ix<=MAX_WALPHA; ix++)
  1936. {
  1937. value = 0;
  1938. if(iswalpha(ix))
  1939. {
  1940. value = 0xfe; // no case
  1941. if(iswlower(ix))
  1942. {
  1943. value = 0xff;
  1944. }
  1945. else if(iswupper(ix))
  1946. {
  1947. c = towlower(ix);
  1948. if(c > 0)
  1949. value = c - ix;
  1950. if((value < 0) || (value > 0xfc))
  1951. {
  1952. exceptions[ex] = ix;
  1953. exceptions[ex+1] = c;
  1954. ex+=2;
  1955. value = 0xfd;
  1956. }
  1957. }
  1958. }
  1959. if(value > 0xfc)
  1960. fprintf(f," 0x%.2x,", value);
  1961. else
  1962. fprintf(f," %4d,", value);
  1963. if((ix % 16) == 15)
  1964. {
  1965. fprintf(f," // %.3x\n", ix & 0xfff0);
  1966. }
  1967. }
  1968. fprintf(f, "\nstatic const short wchar_tolower[] = {\n");
  1969. for(ix=0; ix<ex; ix+=2)
  1970. {
  1971. fprintf(f,"\t0x%.3x, 0x%.3x,\n", exceptions[ix], exceptions[ix+1]);
  1972. }
  1973. fprintf(f, "\t0,0 };\n");
  1974. fprintf(f, "\nstatic const short wchar_toupper[] = {\n");
  1975. for(ix=0x80; ix<=MAX_WALPHA; ix++)
  1976. {
  1977. if(iswlower(ix))
  1978. {
  1979. c = towupper(ix);
  1980. value = ix - c;
  1981. if((value != 32) && (value != 1))
  1982. {
  1983. fprintf(f,"\t0x%.3x, 0x%.3x,\n", ix, c);
  1984. }
  1985. }
  1986. }
  1987. fprintf(f, "\t0,0 };\n");
  1988. fclose(f);
  1989. }
  1990. const char* text1 = "Hello world. Testing.";
  1991. extern void TestCompile2(void);
  1992. void TestTest(int control)
  1993. {//=======================
  1994. FILE *f;
  1995. unsigned int c;
  1996. unsigned int ix=0;
  1997. char textbuf[2000];
  1998. espeak_VOICE voice;
  1999. static unsigned int unique_identifier= 123;
  2000. static int user_data = 456;
  2001. //CharsetToUnicode("ISO-8859-4");
  2002. //CharsetToUnicode("ISCII");
  2003. if(control==2)
  2004. {
  2005. return;
  2006. }
  2007. memset(&voice,0,sizeof(voice));
  2008. f = fopen("/home/jsd1/speechdata/text/test.txt","r");
  2009. if(f==NULL)
  2010. return;
  2011. while(!feof(f) && (ix < sizeof(textbuf)-2))
  2012. {
  2013. c = fgetc(f);
  2014. if(!feof(f))
  2015. textbuf[ix++] = c;
  2016. }
  2017. textbuf[ix] = 0;
  2018. fclose(f);
  2019. f_wavtest = OpenWaveFile3("/home/jsd1/speechdata/text/test.wav");
  2020. f_events = fopen("/home/jsd1/speechdata/text/events","w");
  2021. fprintf(f_events,"Type Audio Text Length Id\n");
  2022. fclose(f_events);
  2023. espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 1000, NULL, espeakINITIALIZE_PHONEME_EVENTS);
  2024. espeak_SetSynthCallback(TestSynthCallback);
  2025. espeak_SetUriCallback(TestUriCallback);
  2026. voice.languages = "fr";
  2027. espeak_SetVoiceByProperties(&voice);
  2028. // espeak_SetVoiceByName("fr");
  2029. espeak_Synth("1", 5, 0, POS_CHARACTER, 0, espeakSSML|espeakCHARS_UTF8, &unique_identifier, (void *)user_data);
  2030. // voice.languages = "en";
  2031. // espeak_SetVoiceByProperties(&voice);
  2032. espeak_SetVoiceByName("de");
  2033. espeak_Synth(textbuf, strlen(textbuf)+1, 0, POS_CHARACTER, 0, espeakSSML|espeakCHARS_UTF8, &unique_identifier, (void *)user_data);
  2034. // espeak_Synth(text1, strlen(text1)+1, 0, POS_CHARACTER, 0, espeakSSML|espeakCHARS_UTF8, &unique_identifier, (void *)(user_data+1));
  2035. espeak_SetParameter(espeakPUNCTUATION, 1, 0);
  2036. espeak_Synchronize();
  2037. // espeak_Cancel();
  2038. espeak_SetParameter(espeakPUNCTUATION, 1, 0);
  2039. }