All problems
Word2Vec Analogy
Problem
Solve a word analogy by vector arithmetic. Compute target = vectors[a] - vectors[b] + vectors[c], then return the word not equal to a, b, or c whose vector has the highest cosine similarity with target.
Break ties alphabetically and return an empty string when there are no candidates.
Hint
Build the analogy vector, then rerank every word that is not one of the three inputs.
Test cases
case 1
in: ["king","man","woman",{"king":[1,1],"man":[1,0],"woman":[0,1],"queen":[0,2],"apple":[-1,-1]}]
expected: "queen"
case 2
in: ["man","king","queen",{"king":[1,1],"man":[1,0],"woman":[0,1],"queen":[0,2],"apple":[-1,-1]}]
expected: "woman"
case 3
in: ["king","man","apple",{"king":[1,1],"man":[1,0],"woman":[0,1],"queen":[0,2],"apple":[-1,-1]}]
expected: "queen"
case 4
in: ["king","man","woman",{"king":[1,0],"man":[1,0],"woman":[1,0],"queen":[1,0]}]
expected: "queen"
Starter codePython
import math
def word_analogy(a, b, c, vectors):
# Your code here
pass4 test cases · run your code against them in the editor