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

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