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

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