//------------------------------------------------------------------------------ // name: etude2.ck //------------------------------------------------------------------------------ // Load model Word2Vec model; // me.dir() + "glove-wiki-gigaword-50-tsne-2-filtered.txt" => string filepath; me.dir() + "glove-wiki-gigaword-50-pca-3-filtered.txt" => string filepath; // me.dir() + "glove-wiki-gigaword-50-filtered.txt" => string filepath; if ( !model.load( filepath ) ) { <<< "cannot load model:", filepath >>>; me.exit(); } 3 => int K_NEAREST; string words[K_NEAREST]; // File I/O Parameters FileIO fio; StringTokenizer tokenizer; string line; string full_line[0]; me.sourceDir() + "poem.txt" => string filename; fio.open( filename, FileIO.READ ); if( !fio.good() ) { cherr <= "can't open file: " <= filename <= " for reading..." <= IO.newline(); me.exit(); } // Timing Parameters 1000::ms => dur T_WORD; // Sound Parameters SinOsc sin => Envelope e => JCRev r => dac; .8 => sin.gain; .1 => r.mix; [ 0, 2, 4, 5, 7, 9, 11 ] @=> int notes[]; 36 => int PITCH_BASE; 0 => int line_num; while( fio.more() ) { full_line.clear(); fio.readLine() => line; tokenizer.set( line ); while( tokenizer.more() ) { full_line << tokenizer.next(); } execute( full_line ); line_num + 1 => line_num; } fun void execute( string input[] ) { if (line_num == 0) { for( 0 => int i; i < input.size(); i++ ) { say(input[i]); } } else { for( 0 => int i; i < input.size() - 1; i++ ) { input[i] + " + " + input[i + 1] => string expr; eval(model, expr, words.size()) @=> words; Math.random2(0, words.size()-1) => int index; words[index] => string alt_word; T_WORD - (K_NEAREST::ms * 50) => dur wait_time; if (wait_time < 50::ms) { 50::ms => wait_time; } say(words[index]); play(PITCH_BASE); wait(wait_time); } } chout <= IO.newline(); chout.flush(); K_NEAREST + 1 => K_NEAREST; int words[K_NEAREST]; PITCH_BASE + 1 => PITCH_BASE; notes << line_num; } fun static string[] eval( Word2Vec @ w, string expr, int k ) { // init int pos; 1.0 => float multiplier; string word; float wordVector[w.dim()]; float exprVector[w.dim()]; // compute while( true ) { expr.find(" ") => pos; if( pos == -1 ) { w.getVector(expr, wordVector); for( 0 => int i; i < w.dim(); i++ ) { wordVector[i] * multiplier +=> exprVector[i]; } break; } else { expr.substring(0, pos) => word; w.getVector(word, wordVector); for( 0 => int i; i < w.dim(); i++ ) { wordVector[i] * multiplier +=> exprVector[i]; } expr.substring(pos + 1) => expr; if( expr.charAt(0) == '+' ) { 1.0 => multiplier; expr.substring(2) => expr; } else { -1.0 => multiplier; expr.substring(2) => expr; } } } // return string results[k]; w.getSimilar( exprVector, k, results ); return results; } // say a word with space after fun void say( string word ) { say( word, " " ); } // say a word fun void say( string word, string append ) { // print it chout <= word <= append; chout.flush(); } // sonify fun void play( int pitch ) { Math.random2f(10,500)::ms => dur t => e.duration; e.keyOn(); Std.mtof( pitch + (12 + notes[Math.random2(0, notes.size() - 1)]) ) => sin.freq; 100::ms => now; e.keyOff(); } // wait fun void wait() { wait( T_WORD ); } // wait fun void wait( dur T ) { // let time pass, let sound...sound T => now; }