| 274 | |
| 275 | |
| 276 | def analogy(pos1, neg1, pos2, neg2, word2idx, idx2word, W): |
| 277 | V, D = W.shape |
| 278 | |
| 279 | # don't actually use pos2 in calculation, just print what's expected |
| 280 | print("testing: %s - %s = %s - %s" % (pos1, neg1, pos2, neg2)) |
| 281 | for w in (pos1, neg1, pos2, neg2): |
| 282 | if w not in word2idx: |
| 283 | print("Sorry, %s not in word2idx" % w) |
| 284 | return |
| 285 | |
| 286 | p1 = W[word2idx[pos1]] |
| 287 | n1 = W[word2idx[neg1]] |
| 288 | p2 = W[word2idx[pos2]] |
| 289 | n2 = W[word2idx[neg2]] |
| 290 | |
| 291 | vec = p1 - n1 + n2 |
| 292 | |
| 293 | distances = pairwise_distances(vec.reshape(1, D), W, metric='cosine').reshape(V) |
| 294 | idx = distances.argsort()[:10] |
| 295 | |
| 296 | # pick one that's not p1, n1, or n2 |
| 297 | best_idx = -1 |
| 298 | keep_out = [word2idx[w] for w in (pos1, neg1, neg2)] |
| 299 | # print("keep_out:", keep_out) |
| 300 | for i in idx: |
| 301 | if i not in keep_out: |
| 302 | best_idx = i |
| 303 | break |
| 304 | # print("best_idx:", best_idx) |
| 305 | |
| 306 | print("got: %s - %s = %s - %s" % (pos1, neg1, idx2word[best_idx], neg2)) |
| 307 | print("closest 10:") |
| 308 | for i in idx: |
| 309 | print(idx2word[i], distances[i]) |
| 310 | |
| 311 | print("dist to %s:" % pos2, cos_dist(p2, vec)) |
| 312 | |
| 313 | |
| 314 | def test_model(word2idx, W, V): |