Last active
January 6, 2019 00:46
-
-
Save shoffing/2239faa5d70aa6cd6e60698f0194c914 to your computer and use it in GitHub Desktop.
Bot to play codenames as an agent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# Distance function | |
dist = lambda a, b: np.linalg.norm(a - b) | |
# Load word2vec data file (gl0ve) | |
w2v = {} | |
with open("glove/glove.6B.50d.txt", "r", encoding="utf-8") as w2v_file: | |
for word_enc in w2v_file: | |
word = word_enc[:word_enc.index(" ")] | |
if len(word) > 1 and word.isalpha(): | |
w2v[word] = np.array([float(n) for n in word_enc.split(" ")[1:]]) | |
words_in_play = set() | |
while True: | |
print(", ".join(words_in_play)) | |
command = input("> ").split(" ") | |
action = command[0] | |
# add / remove words | |
if action.startswith("w"): | |
words = command[1:] | |
for word in words: | |
if word in w2v: | |
if action == "wa": | |
words_in_play.add(word) | |
if action == "wr": | |
words_in_play.remove(word) | |
else: | |
print(f"word not found: {word}") | |
# guess words for a given hint and number | |
if action == "g": | |
hint = command[1] | |
n = int(command[2]) | |
if hint in w2v: | |
hint_vec = w2v[hint] | |
guesses = sorted(words_in_play, key=lambda w: dist(w2v[w], hint_vec))[:n] | |
for guess in guesses: | |
print(f"{guess} ({dist(w2v[guess], hint_vec)})") | |
else: | |
print(f"word not found: {hint}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment