Debian Codegraph @

examples/ai/word2vec/poem-i-feel.ck

raw
1 //------------------------------------------------------------------------------
2 // name: poem-i-feel.ck
3 // desc: a sus poem made with chAI and word2vec
4 //
5 // version: need chuck version 1.5.0.0 or higher
6 // sorting: part of ChAI (ChucK for AI)
7 //
8 // "i feel"
9 // -- a stream of unconsciousness poem generator
10 //
11 // NOTE: need a pre-trained word vector model, e.g., from
12 // https://chuck.stanford.edu/chai/data/glove/
13 // glove-wiki-gigaword-50-tsne-2.txt (400000 words x 2 dimensions)
14 //
15 // author: Ge Wang
16 // date: Spring 2023
17 //------------------------------------------------------------------------------
18
19 // random seed (if set, sequence can be reproduce)
20 // Math.srandom( 515 );
21
22 // instantiate
23 Word2Vec model;
24 // pre-trained model to load
25 me.dir() + "glove-wiki-gigaword-50-tsne-2.txt" => string filepath;
26 // load pre-trained model (see URLs above for download)
27 if( !model.load( filepath ) )
28 {
29 <<< "cannot load model:", filepath >>>;
30 me.exit();
31 }
32
33 // starter "i feel" emotion words with positive connotations
34 ["fine", "joy", "excited", "inspired", "included",
35 "understood", "artful", "playful", "free", "myself",
36 "grateful", "optimistic" ] @=> string positives[];
37
38 // starter "i feel" emotion words with negative connotations
39 ["sad", "bored", "meh", "spent", "worried",
40 "afraid", "marginalized", "misunderstood", "powerless",
41 "undone", "small", "guilt", "cringe", "dread" ] @=> string negatives[];
42
43 // starter "i feel" emotion words with other connotations
44 ["numb", "nothing", "weird", "meh", "disjointed", "scattered",
45 "complicated", "sus" ] @=> string others[];
46
47 // conditions
48 3 => int LINES_PER_STANZA;
49 3 => int NUM_SECTIONS; // here each section is a fixed stanzas format
50
51 // number of nearest words to retrieve for each word
52 // higher this number the higher the variance per word
53 10 => int K_NEAREST;
54
55 // timing
56 400::ms => dur T_WORD; // duration per word
57 false => int shouldScaleTimeToWordLength; // longer words take longer?
58 T_WORD => dur T_LINE_PAUSE; // a little pause after each line
59 T_WORD * 2 => dur T_STANZA_PAUSE; // pause after each stanza
60
61 // sound
62 ModalBar crystal => Gain left => dac.left;
63 crystal => Gain right => dac.right;
64 1 => crystal.preset;
65 // feedback delay for echos
66 left => DelayL DL => left; .5 => DL.gain; T_WORD*1.5 => DL.max => DL.delay;
67 right => DelayL DR => right; .3 => DR.gain; T_WORD*2 => DR.max => DR.delay;
68
69 // current word
70 string feeling;
71 // word vector
72 float vec[model.dim()];
73 // search results
74 string words[K_NEAREST];
75
76 // line break
77 chout <= IO.newline(); chout.flush();
78
79 // loop over stanzas
80 for( int s; s < NUM_SECTIONS; s++ )
81 {
82 // grab a word out of the "good" bag
83 positives[Math.random2(0,positives.size()-1)] => feeling;
84 // print a stanza
85 stanza( feeling, LINES_PER_STANZA, 48, 64 );
86
87 // grab a word out of the "bad" bag
88 negatives[Math.random2(0,negatives.size()-1)] => feeling;
89 // print a stanza
90 stanza( feeling, LINES_PER_STANZA, 48, 63 );
91
92 // grab a word out of the "other" bag
93 others[Math.random2(0,others.size()-1)] => feeling;
94 // print a stanza
95 stanza( feeling, LINES_PER_STANZA, 36, Math.random2(33,36)*2 );
96
97 endStanza();
98 chout <= IO.newline() <= IO.newline(); chout.flush();
99 // pause at end of line
100 T_STANZA_PAUSE => now;
101 }
102
103 // print at the end
104 chout <= "\"i feel\"" <= IO.newline();
105 chout <= "-- a stream of unconsciousness poem" <= IO.newline();
106 chout.flush();
107
108 // print a stanza from a starter feeling word
109 fun void stanza( string feeling, int numLines, int pitch1, int pitch2 )
110 {
111 // loop over lines in a stanza
112 for( int n; n < numLines; n++ )
113 {
114 // a line
115 say("i"); play(pitch1); wait();
116 say("feel"); wait();
117 say(feeling); play(pitch2); wait();
118 endl(); wait();
119
120 // get similar words
121 model.getSimilar( feeling, words.size(), words );
122 // choose one at random
123 words[Math.random2(0,words.size()-1)] => feeling;
124 }
125 // next line!
126 chout <= IO.newline(); chout.flush();
127 // pause at end of line
128 T_LINE_PAUSE => now;
129 }
130
131 fun void endStanza()
132 {
133 // stanza break; no new sound for now
134 say("one"); wait();
135 say("day"); wait();
136 say("ends"); wait();
137 say("as"); wait();
138 say("another"); wait();
139 say("day"); wait();
140 say("dawns..."); wait();
141 }
142
143 // say a word with space after
144 fun void say( string word )
145 {
146 say( word, " " );
147 }
148
149 // say a word
150 fun void say( string word, string append )
151 {
152 // print it
153 chout <= word <= append; chout.flush();
154 }
155
156 // sonify
157 fun void play( int pitch )
158 {
159 play( pitch, Math.random2f(.8,1) );
160 }
161
162 // sonify
163 fun void play( int pitch, float velocity )
164 {
165 // convert pitch to frequency and set it
166 pitch => Std.mtof => crystal.freq;
167 // note on
168 velocity => crystal.noteOn;
169 }
170
171 // wait
172 fun void wait()
173 {
174 wait( T_WORD );
175 }
176
177 // wait
178 fun void wait( dur T )
179 {
180 // let time pass, let sound...sound
181 T => now;
182 }
183
184 // new line with timing
185 fun void endl()
186 {
187 endl( T_WORD );
188 }
189
190 // new line with timing
191 fun void endl( dur T )
192 {
193 // new line
194 chout <= IO.newline(); chout.flush();
195 // let time pass
196 T => now;
197 }