(w1, w2, w3, We, word2idx, idx2word)
| 45 | |
| 46 | # fast version |
| 47 | def find_analogies(w1, w2, w3, We, word2idx, idx2word): |
| 48 | V, D = We.shape |
| 49 | |
| 50 | king = We[word2idx[w1]] |
| 51 | man = We[word2idx[w2]] |
| 52 | woman = We[word2idx[w3]] |
| 53 | v0 = king - man + woman |
| 54 | |
| 55 | for dist in ('euclidean', 'cosine'): |
| 56 | distances = pairwise_distances(v0.reshape(1, D), We, metric=dist).reshape(V) |
| 57 | # idx = distances.argmin() |
| 58 | # best_word = idx2word[idx] |
| 59 | idx = distances.argsort()[:4] |
| 60 | best_idx = -1 |
| 61 | keep_out = [word2idx[w] for w in (w1, w2, w3)] |
| 62 | for i in idx: |
| 63 | if i not in keep_out: |
| 64 | best_idx = i |
| 65 | break |
| 66 | best_word = idx2word[best_idx] |
| 67 | |
| 68 | |
| 69 | print("closest match by", dist, "distance:", best_word) |
| 70 | print(w1, "-", w2, "=", best_word, "-", w3) |
| 71 | |
| 72 | |
| 73 | class Tree: |
no outgoing calls
no test coverage detected