| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// name: word2vec-basic.ck |
| 3 |
// desc: word2vec basic usage |
| 4 |
// |
| 5 |
// version: need chuck version 1.5.0.0 or higher |
| 6 |
// sorting: part of ChAI (ChucK for AI) |
| 7 |
// |
| 8 |
// NOTE: download pre-trained word vector model from: |
| 9 |
// https://chuck.stanford.edu/chai/data/glove/ |
| 10 |
// | (can use any of these) |
| 11 |
// glove-wiki-gigaword-50.txt (400000 words x 50 dimensions) |
| 12 |
// glove-wiki-gigaword-50-pca-3.txt (400000 words x 3 dimensions) |
| 13 |
// glove-wiki-gigaword-50-tsne-2.txt (400000 words x 2 dimensions) |
| 14 |
// |
| 15 |
// author: Yikai Li and Ge Wang |
| 16 |
// date: Spring 2023 |
| 17 |
//------------------------------------------------------------------------------ |
| 18 |
|
| 19 |
// instantiate |
| 20 |
Word2Vec model; |
| 21 |
|
| 22 |
// pre-trained model to load |
| 23 |
"glove-wiki-gigaword-50.txt" => string filepath; |
| 24 |
|
| 25 |
<<< "loading model:", filepath >>>; |
| 26 |
<<< "(this could take a few seconds)...", "" >>>; |
| 27 |
// NOTE: while loading can take a long time initially; this is internally cached; |
| 28 |
// subsequent loads on the same filepath will use cached dictionary, as long as |
| 29 |
// ChucK VM keeps running |
| 30 |
// HINT: while caching should "just work" in miniAudicle, in command-line chuck |
| 31 |
// one could keep one chuck running (using --loop) and chuk code to it from a |
| 32 |
// second terminal (using chuck + code). for example: |
| 33 |
//------------------------------------------------------------------------------ |
| 34 |
// terminal-1> chuck --loop ***** terminal-2> chuck + word2vec-basic |
| 35 |
//------------------------------------------------------------------------------ |
| 36 |
// load a pre-trained model (see URLs above for download) |
| 37 |
if( !model.load( me.dir() + filepath ) ) |
| 38 |
{ |
| 39 |
<<< "cannot load model file...", "" >>>; |
| 40 |
me.exit(); |
| 41 |
} |
| 42 |
|
| 43 |
// print info |
| 44 |
<<< "dictionary size:", model.size() >>>; |
| 45 |
<<< "embedding dimensions:", model.dim() >>>; |
| 46 |
<<< "using KDTree for search:", model.useKDTree() ? "YES" : "NO" >>>; |
| 47 |
float mins[0], maxs[0]; |
| 48 |
// get bounds for each dimension |
| 49 |
model.minMax( mins, maxs ); |
| 50 |
// print the min/max for each dimension |
| 51 |
for( int i; i < model.dim(); i++ ) |
| 52 |
{ cherr <= "dimension " <= i <= ": [" <= mins[i] |
| 53 |
<= " <-range-> " <= maxs[i] <= "]" <= IO.newline(); } |
| 54 |
|
| 55 |
string words[10]; |
| 56 |
<<< "=========================================================", "" >>>; |
| 57 |
|
| 58 |
// get vector |
| 59 |
<<< "vector associated with 'cat' is:", "" >>>; |
| 60 |
float vector[model.dim()]; |
| 61 |
model.getVector("cat", vector); |
| 62 |
for( int i; i < vector.size(); i++ ) { chout <= vector[i] <= " "; } |
| 63 |
chout <= IO.newline(); |
| 64 |
|
| 65 |
<<< "=========================================================", "" >>>; |
| 66 |
|
| 67 |
// get most similar by word |
| 68 |
<<< words.size(), "words most similar to 'stanford' are:", "" >>>; |
| 69 |
model.getSimilar("stanford", words.size(), words); |
| 70 |
for( int i; i < words.size(); i++ ) |
| 71 |
{ <<< words[i], "" >>>; } |
| 72 |
|
| 73 |
<<< "=========================================================", "" >>>; |
| 74 |
|
| 75 |
// get similar words via vector |
| 76 |
<<< words.size(), "words closest to the vector of 'cat' are:", "" >>>; |
| 77 |
model.getSimilar( vector, words.size(), words ); |
| 78 |
for( int i; i < words.size(); i++ ) |
| 79 |
{ <<< words[i], "" >>>; } |
| 80 |
|
| 81 |
<<< "=========================================================", "" >>>; |
| 82 |
|
| 83 |
// eval an expression |
| 84 |
<<< "similar words to expression 'rome - italy + china':", "" >>>; |
| 85 |
W2V.eval( model, "rome - italy + china", 10 ) @=> words; |
| 86 |
for( 0 => int i; i < words.size(); i++ ) |
| 87 |
{ <<< words[i], "" >>>; } |
| 88 |
|
| 89 |
<<< "=========================================================", "" >>>; |
| 90 |
|
| 91 |
"dog" => string A; "puppy" => string B; "cat" => string C; |
| 92 |
// logical analogy |
| 93 |
<<< A, "is to", B, "as", C, "is to..." >>>; |
| 94 |
// e.g., dog:puppy::cat:[?] (this returns words for [?]) |
| 95 |
W2V.analogy( model, A, B, C, 10 ) @=> words; |
| 96 |
for( 0 => int i; i < words.size(); i++ ) |
| 97 |
{ <<< words[i], "" >>>; } |
| 98 |
|
| 99 |
<<< "=========================================================", "" >>>; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
//------------------------------------------------------------------------------ |
| 105 |
// word2vec helper |
| 106 |
//------------------------------------------------------------------------------ |
| 107 |
class W2V |
| 108 |
{ |
| 109 |
fun static void copy( float to[], float from[] ) |
| 110 |
{ |
| 111 |
to.size(from.size()); |
| 112 |
// add into element |
| 113 |
for( int i; i < to.size(); i++ ) from[i] => to[i]; |
| 114 |
} |
| 115 |
|
| 116 |
fun static float[] dup( float from[] ) |
| 117 |
{ |
| 118 |
float to[from.size()]; |
| 119 |
// add into element |
| 120 |
for( int i; i < to.size(); i++ ) from[i] => to[i]; |
| 121 |
// return |
| 122 |
return to; |
| 123 |
} |
| 124 |
|
| 125 |
// result stored in x |
| 126 |
fun static void add( float x[], float y[] ) |
| 127 |
{ |
| 128 |
// smaller of the two |
| 129 |
x.size() < y.size() ? x.size() : y.size() => int size; |
| 130 |
// add into element |
| 131 |
for( int i; i < size; i++ ) y[i] +=> x[i]; |
| 132 |
} |
| 133 |
|
| 134 |
// result stored in x |
| 135 |
fun static void minus( float x[], float y[] ) |
| 136 |
{ |
| 137 |
// smaller of the two |
| 138 |
x.size() < y.size() ? x.size() : y.size() => int size; |
| 139 |
// add into element |
| 140 |
for( int i; i < size; i++ ) y[i] -=> x[i]; |
| 141 |
} |
| 142 |
|
| 143 |
fun static void scale( float x[], float scalar ) |
| 144 |
{ |
| 145 |
// scale |
| 146 |
for( int i; i < x.size(); i++ ) scalar *=> x[i]; |
| 147 |
} |
| 148 |
|
| 149 |
fun static void scale( float result[], float x[], float scalar ) |
| 150 |
{ |
| 151 |
// scale |
| 152 |
for( int i; i < x.size(); i++ ) scalar * x[i] => result[i]; |
| 153 |
} |
| 154 |
|
| 155 |
fun static string[] eval( Word2Vec @ w, string expr, int k ) |
| 156 |
{ |
| 157 |
// init |
| 158 |
int pos; |
| 159 |
1.0 => float multiplier; |
| 160 |
string word; |
| 161 |
float wordVector[w.dim()]; |
| 162 |
float exprVector[w.dim()]; |
| 163 |
|
| 164 |
// compute |
| 165 |
while( true ) |
| 166 |
{ |
| 167 |
expr.find(" ") => pos; |
| 168 |
if( pos == -1 ) |
| 169 |
{ |
| 170 |
w.getVector(expr, wordVector); |
| 171 |
for( 0 => int i; i < w.dim(); i++ ) |
| 172 |
{ |
| 173 |
wordVector[i] * multiplier +=> exprVector[i]; |
| 174 |
} |
| 175 |
break; |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
expr.substring(0, pos) => word; |
| 180 |
w.getVector(word, wordVector); |
| 181 |
for( 0 => int i; i < w.dim(); i++ ) |
| 182 |
{ |
| 183 |
wordVector[i] * multiplier +=> exprVector[i]; |
| 184 |
} |
| 185 |
expr.substring(pos + 1) => expr; |
| 186 |
if( expr.charAt(0) == '+' ) |
| 187 |
{ |
| 188 |
1.0 => multiplier; |
| 189 |
expr.substring(2) => expr; |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
-1.0 => multiplier; |
| 194 |
expr.substring(2) => expr; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
// return |
| 200 |
string results[k]; |
| 201 |
w.getSimilar( exprVector, k, results ); |
| 202 |
|
| 203 |
return results; |
| 204 |
} |
| 205 |
|
| 206 |
// logical analogy: A to B is as C is to [what this function returns] |
| 207 |
fun static string[] analogy( Word2Vec @ w, string A, string B, string C, int k ) |
| 208 |
{ |
| 209 |
// the vector sum |
| 210 |
B + " - " + A + " + " + C => string v; |
| 211 |
return eval( w, v, k ); |
| 212 |
} |
| 213 |
} |