Nearest word
Ask a tiny embedding table which word sits closest to an idea.
In an embedding table every word has a vector, and words with similar meaning have vectors pointing the same way. Finding the nearest word is how search, recommendations and analogies work.
Write nearestWord(table, query): table is an object mapping each word to a vector, and query is a vector of the same length. Return the word whose vector has the highest cosine similarity to query (dot product divided by both lengths; treat a zero-length vector as similarity 0). On a tie, the word that comes first in the table wins. An empty table returns null.
Call show(similarities) with the score for each word, in table order, and the stage draws them as bars.
Example: nearestWord({ cat: [1, 0], car: [0, 1] }, [0.9, 0.1]) returns "cat".
Tests you can see
nearestWord({"cat":[1,0],"car":[0,1]}, [0.9,0.1])"cat"nearestWord({"dog":[0.9,0.1,0.2],"fish":[0.1,0.9,0.3],"bird":[0.3,0.2,0.9]}, [0.2,0.3,1])"bird"nearestWord({"small":[1,1],"big":[10,0]}, [3,3])"small"
More tests are hidden. The server runs them all before a time counts.
The idea behind this, in a two-minute live pageCall show() while your code works and the run plays back here.
Tab indents · Cmd/Ctrl+Enter runs