├── LICENSE ├── README.md ├── language_games.jpg ├── language_games1.jpg ├── language_games2.jpg ├── language_games3.jpg ├── language_games4.jpg ├── play_game.py ├── requirements.txt └── words_alpha.txt /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Allen Roush 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Language-games 2 | Dead simple games made with Language Models and Word Vectors. 3 | Powered by the wonderful Word Vector Library: [Magnitude](https://github.com/plasticityai/magnitude) 4 | 5 | - Game 1: Competative Word Guessing - Players try to guess a hidden word 6 | - Game 2: Guessing the closest word from a list to a given word 7 | - Game 3: Guessing the word which DOESN'T match a list of other words 8 | - Game 4: A Semantic Scrabble-like game 9 | 10 | 11 | # Install Instructions 12 | 13 | 1. Clone the repo 14 | 2. type `pip install -r requirements.txt`or `pip3 install -r requirments.txt` as needed. 15 | 3. Download one of these sets of word vectors available [here](http://magnitude.plasticity.ai/fasttext/heavy/crawl-300d-2M.magnitude) and/or [here](http://magnitude.plasticity.ai/glove/heavy/glove.6B.100d.magnitude) 16 | 4. Enjoy the game by running `python3 play_game.py` 17 | 18 | # Screenshots 19 | 20 | ## Game 1: 21 | ![](https://raw.githubusercontent.com/Hellisotherpeople/Language-games/master/language_games1.jpg) 22 | 23 | ## Game 2: 24 | ![](https://raw.githubusercontent.com/Hellisotherpeople/Language-games/master/language_games2.jpg) 25 | 26 | ## Game 3: 27 | ![](https://raw.githubusercontent.com/Hellisotherpeople/Language-games/master/language_games3.jpg) 28 | 29 | ## Game 4: 30 | ![](https://raw.githubusercontent.com/Hellisotherpeople/Language-games/master/language_games4.jpg) 31 | -------------------------------------------------------------------------------- /language_games.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hellisotherpeople/Language-games/3bcc26b1d0a992db684eede08b9201951f954d52/language_games.jpg -------------------------------------------------------------------------------- /language_games1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hellisotherpeople/Language-games/3bcc26b1d0a992db684eede08b9201951f954d52/language_games1.jpg -------------------------------------------------------------------------------- /language_games2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hellisotherpeople/Language-games/3bcc26b1d0a992db684eede08b9201951f954d52/language_games2.jpg -------------------------------------------------------------------------------- /language_games3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hellisotherpeople/Language-games/3bcc26b1d0a992db684eede08b9201951f954d52/language_games3.jpg -------------------------------------------------------------------------------- /language_games4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hellisotherpeople/Language-games/3bcc26b1d0a992db684eede08b9201951f954d52/language_games4.jpg -------------------------------------------------------------------------------- /play_game.py: -------------------------------------------------------------------------------- 1 | import os 2 | import string 3 | from pymagnitude import * 4 | import sys 5 | import numpy as np 6 | from itertools import islice 7 | from collections import deque 8 | import csv 9 | from random import shuffle 10 | from time import time 11 | import random 12 | from collections import Counter 13 | 14 | 15 | 16 | 17 | def inner_product_rank_mag(a_set, use_weights=True): 18 | np_of_vecs = np.asarray(a_set) 19 | sims_mat = np.dot(np_of_vecs, np_of_vecs.transpose()) 20 | ranks = np.sum(sims_mat, axis = 0) 21 | if use_weights: 22 | weighted_avg = np.average(np_of_vecs, axis = 0, weights = ranks) 23 | else: 24 | weighted_avg = np.average(np_of_vecs, axis = 0) 25 | return weighted_avg 26 | 27 | 28 | def load_words(topic_word = "market", num_words = 10000): 29 | if topic_word: 30 | most_sims = vectors.most_similar_approx(topic_word, topn = num_words, effort = 0.5) 31 | sim_words = [item[0].lower() for item in most_sims] 32 | valid_words = sim_words 33 | else: 34 | with open('words_alpha.txt') as word_file: 35 | valid_words = set(word_file.read().split()) 36 | 37 | return valid_words 38 | 39 | 40 | 41 | def get_random_words(num_words = 5): 42 | sample = random.sample(english_words, num_words) 43 | return sample 44 | 45 | def get_random_word(): 46 | sample = random.sample(english_words, 1)[0] 47 | return sample 48 | 49 | 50 | def get_topic_words(topic = "buisness", num_words = 10000): 51 | most_sims = vectors.most_similar_approx(topic, topn = num_words, effort = 0.01) 52 | golden_tickets = [item[0].lower() for item in most_sims] 53 | 54 | def validWord(word, letterList): 55 | isvalid = True 56 | for char in word: 57 | if(word.count(char) > letterList.count(char)): 58 | isvalid = False 59 | break 60 | return isvalid 61 | 62 | #print(random.sample(english_words, 5)) 63 | 64 | 65 | 66 | 67 | 68 | #my_string = "For at least 20 years, upper-middle class, often tenured academics have been teaching young people that politics is a futile form of irony. I've watched Ivy League professors with tenure explain to graduate students with no health insurance that striking for pay is silly. I've heard smug male assholes with Ph.D.s describe registering voters as the" 69 | #print(my_string.split()) 70 | #set_vec = vectors.query(my_string.lower().split()) 71 | 72 | #pooled_vec = inner_product_rank_mag(set_vec, use_weights = True) 73 | 74 | #print(vectors.most_similar_approx(pooled_vec, topn = 10)) 75 | 76 | 77 | 78 | 79 | 80 | ### Game 1, competitive word guessing 81 | def game_loop_guessing(num_turns = 5, num_players = 2): 82 | print("I'm thinking of a word.....") 83 | 84 | random_word = get_random_word().lower() 85 | #random_word_list = rd_word_gen.get_random_words(minDictionaryCount = 5, minCorpusCount = 5) 86 | #lower_random_word_list = [x.lower() for x in random_word_list] 87 | print("Here are some words of inspiration that may guide you along your adventure :)") 88 | 89 | most_sims = vectors.most_similar_approx(random_word, topn = 100, effort = 0.001) 90 | golden_tickets = [item[0].lower() for item in most_sims] 91 | canidate_words = golden_tickets #lower_random_word_list + golden_tickets 92 | shuffle(canidate_words) 93 | print(canidate_words) 94 | print("\n") 95 | 96 | 97 | player_scores = [0] * num_players 98 | cur_player = 0 99 | 100 | turns_left = num_turns 101 | while turns_left > 0: 102 | while cur_player < num_players: 103 | print("Player " + str(cur_player) + " it is your turn to enter a word ") 104 | p_word = input("Enter a word ") 105 | print(p_word) 106 | score = vectors.similarity(p_word, random_word) 107 | print(score) 108 | if score > player_scores[cur_player]: 109 | print("You got closer to the right word! ") 110 | player_scores[cur_player] = score 111 | 112 | cur_player += 1 #flip the switch 113 | cur_player = 0 114 | turns_left -= 1 115 | print("Turns left: ") 116 | print(turns_left) 117 | 118 | print("THE GAME IS NOW OVER ") 119 | print("Final Scores: ") 120 | print(player_scores) 121 | print("The randomly chosen word by the computer was: " + random_word) 122 | print("The canidate words in the inspiration list were:") 123 | print(golden_tickets) 124 | 125 | 126 | 127 | 128 | def word_choice_logic(random_int_word = True, num_words = 5): 129 | ### game 2 helper function 130 | print("I'm thinking of a word.....") 131 | if random_int_word: 132 | given_word = get_random_word().lower() 133 | else: 134 | given_word = input("give a word that you will have to select the most similar word from") 135 | 136 | random_word_list = get_random_words(num_words = num_words) 137 | lower_random_word_list = [x.lower() for x in random_word_list] 138 | 139 | correct_word = vectors.most_similar_to_given(given_word, lower_random_word_list) 140 | 141 | return given_word, lower_random_word_list, correct_word 142 | 143 | 144 | 145 | ### Game 2, guess closest word to a given word 146 | def game_loop_guessing_to_given(num_turns = 5, num_players = 2, random_int_word = False, num_words = 5): 147 | print("I'm thinking of some words.....") 148 | if random_int_word: 149 | given_word = get_random_word().lower() 150 | else: 151 | given_word = input("give a word that you will have to select the most similar word from") 152 | 153 | random_word_list = get_random_words(num_words = num_words) 154 | lower_random_word_list = [x.lower() for x in random_word_list] 155 | 156 | correct_word = vectors.most_similar_to_given(given_word, lower_random_word_list) 157 | player_scores = [0] * num_players 158 | cur_player = 0 159 | 160 | turns_left = num_turns 161 | while turns_left > 0: 162 | while cur_player < num_players: 163 | print("Player " + str(cur_player) + " it is your turn!") 164 | print("Your given word is: ") 165 | print(given_word) 166 | print("The list of words to choose from: ") 167 | print(lower_random_word_list) 168 | print("What do you choose? ") 169 | chosen_word = input("Choose a word from the list which is most close to the given word: ") 170 | if chosen_word == correct_word: 171 | print("You are correct!") 172 | player_scores[cur_player] += 1 173 | print("Your current score is: ") 174 | print(player_scores[cur_player]) 175 | else: 176 | print("You were wrong! The correct word is: ") 177 | print(correct_word) 178 | given_word, lower_random_word_list, correct_word = word_choice_logic(random_int_word = random_int_word, num_words = num_words) 179 | cur_player += 1 180 | cur_player = 0 181 | turns_left -= 1 182 | print("Turns left: ") 183 | print(turns_left) 184 | 185 | print("THE GAME IS NOW OVER") 186 | print("Final Scores: ") 187 | print(player_scores) 188 | 189 | 190 | 191 | def game_3_logic(num_words = 5): 192 | print("I'm thinking of some words..... ") 193 | random_word_list = get_random_words(num_words = num_words) 194 | lower_random_word_list = [x.lower() for x in random_word_list] 195 | correct_word = vectors.doesnt_match(lower_random_word_list) 196 | return lower_random_word_list, correct_word 197 | 198 | 199 | def random_string(length): 200 | letters = string.ascii_lowercase 201 | return ''.join(random.choice(letters) for i in range(length)) 202 | 203 | 204 | ### Game 3, guess which word doesn't match the other words 205 | def game_loop_guess_not_matching(num_turns = 5, num_players = 2, num_words = 5): 206 | print("I'm thinking of some words..... ") 207 | random_word_list = get_random_words(num_words = num_words) 208 | lower_random_word_list = [x.lower() for x in random_word_list] 209 | player_scores = [0] * num_players 210 | cur_player = 0 211 | 212 | correct_word = vectors.doesnt_match(lower_random_word_list) 213 | 214 | turns_left = num_turns 215 | while turns_left > 0: 216 | while cur_player < num_players: 217 | print("Player " + str(cur_player) + " it is your turn!") 218 | print("Choose the word that DOESN'T MATCH the other words in the list: ") 219 | print(lower_random_word_list) 220 | not_match = input("Which word doesn't match the other words in the list?") 221 | if not_match == correct_word: 222 | print("You are correct!") 223 | player_scores[cur_player] += 1 224 | print("Your current score is: ") 225 | print(player_scores[cur_player]) 226 | else: 227 | print("You were wrong! The correct word is: ") 228 | print(correct_word) 229 | lower_random_word_list, correct_word = game_3_logic(num_words = num_words) 230 | cur_player += 1 231 | cur_player = 0 232 | turns_left -= 1 233 | print("Turns left: ") 234 | print(turns_left) 235 | 236 | print("THE GAME IS NOW OVER") 237 | print("Final Scores: ") 238 | print(player_scores) 239 | 240 | 241 | ### Game 6, 242 | 243 | 244 | def semantic_scrabble_logic(num_characters = 10, random_int_word = True): 245 | char_list = list(random_string(num_characters)) 246 | if random_int_word: 247 | starting_word = get_random_word().lower() 248 | else: 249 | starting_word = input("Please enter a starting word") 250 | return char_list, starting_word 251 | 252 | 253 | 254 | def game_loop_semantic_scrabble(num_turns = 5, num_players = 2, num_characters = 10, random_int_word = True): 255 | char_list = list(random_string(num_characters)) 256 | if random_int_word: 257 | starting_word = get_random_word().lower() 258 | else: 259 | starting_word = input("Please enter a starting word") 260 | 261 | 262 | 263 | 264 | player_scores = [0] * num_players 265 | cur_player = 0 266 | 267 | turns_left = num_turns 268 | while turns_left > 0: 269 | while cur_player < num_players: 270 | print("Player " + str(cur_player) + " it is your turn!") 271 | print("The word you are trying to match in meaning is: ") 272 | print(starting_word) 273 | print("Your char list is:") 274 | print(char_list) 275 | provided_word = input("Please input a valid word made from some or all of the provided characters") 276 | if validWord(str(provided_word), char_list): 277 | score = vectors.similarity(provided_word, starting_word) 278 | print(score) 279 | if score > player_scores[cur_player]: 280 | print("You got closer to the right word!") 281 | player_scores[cur_player] = score 282 | else: 283 | print("Your word isn't closer to the right word") 284 | #print(score) 285 | cur_player += 1 286 | else: 287 | print("That is not a valid word, try again!") 288 | char_list, starting_word = semantic_scrabble_logic(num_characters = num_characters, random_int_word = random_int_word) 289 | cur_player = 0 290 | turns_left -= 1 291 | print("Turns left: ") 292 | print(turns_left) 293 | 294 | print("THE GAME IS NOW OVER") 295 | print("Final Scores: ") 296 | print(player_scores) 297 | 298 | 299 | 300 | 301 | def game_route_logic(): 302 | print("Semantic Language Games v0.02, by Allen Roush \n") 303 | 304 | global vectors 305 | global english_words 306 | 307 | f_loc = input("Enter the file location of your word vectors (default is 'crawl-300d-2M.magnitude') ") 308 | if f_loc: 309 | vectors = Magnitude(f_loc) 310 | else: 311 | vectors = Magnitude("crawl-300d-2M.magnitude") 312 | 313 | u_tpc_word = input("Enter a topic word, or leave blank to use a large fully random dictionary (words_alpha.txt)") 314 | if u_tpc_word: 315 | u_n_tpc_words = int(input("Enter the number of words to populate the topic dictionary with (recommended is 10000)")) 316 | english_words = load_words(topic_word = u_tpc_word, num_words = u_n_tpc_words) 317 | else: 318 | english_words = load_words(topic_word = False) 319 | 320 | print("Please choose a game to play! \n") 321 | print("Your options are: ") 322 | print("Game 1: Competitive Word Guessing") 323 | print("Game 2: Guessing the Closest Word to a Given Word") 324 | print("Game 3: Guessing which words dont match the other words in a list") 325 | print("Game 4: A Semantic Scrabble-like game") 326 | print("All games can be played with any number of players") 327 | game_choice = input("Choose a game to play by typing a number between 1 and 4:") 328 | u_num_turns = int(input("How many turns do you want to play?")) 329 | u_num_players = int(input("How many players will play?")) 330 | if int(game_choice) == 1: 331 | game_loop_guessing(num_turns = u_num_turns, num_players = u_num_players) 332 | elif int(game_choice) == 2: 333 | u_rd_int = bool(int(input("Enter 0 if you want to choose your own words, 1 if you want it to be random"))) 334 | u_num_words = input("Enter the number of words that you want to choose from each round (default is 5)") 335 | print(u_rd_int) 336 | game_loop_guessing_to_given(num_turns = u_num_turns, num_players = u_num_players, random_int_word = bool(u_rd_int), num_words = int(u_num_words)) 337 | 338 | elif int(game_choice) == 3: 339 | u_num_words = input("Enter the number of words that you want to choose from each round (default is 5)") 340 | game_loop_guess_not_matching(num_turns = u_num_turns, num_players = u_num_players, num_words = int(u_num_words)) 341 | 342 | elif int(game_choice) == 4: 343 | u_char_num = int(input("Enter the number of characters to build words from each round (default is 10)")) 344 | u_rd_int = bool(int(input("Enter 0 if you want to choose your own words, 1 if you want it to be random"))) 345 | game_loop_semantic_scrabble(num_turns = u_num_turns, num_players = u_num_players, num_characters = u_char_num, random_int_word = bool(u_rd_int)) 346 | else: 347 | print("Invalid game choice, rerun the game!") 348 | 349 | game_route_logic() 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | #game_loop_semantic_scrabble() 360 | #print(validWord("sett", ["t", "e", "t", "s"])) 361 | 362 | 363 | 364 | #a_str = "sett" 365 | #car_list = ["t", "e", "t", "s"] 366 | 367 | 368 | 369 | 370 | 371 | #print(validWord("tets", ["t", "e", "t", "s"])) 372 | 373 | ### Game 2, guess most similar to given 374 | ### Game 3, guess which doesn't match 375 | ### Game 4, guess which is closest to x number of words and farthest away from y number of words (analogies) 376 | ### Game 5, enter a simple equation and guess what output is 377 | ### Game 6, semantic scrabble - get list of characters and create word closest in meaning to given word 378 | ### Game 7, guess the correct word to complete the sentence/paragraph (masked language modeling) (score based on closest meaning to word ) 379 | 380 | 381 | 382 | #my_string = "my dog is going for" 383 | #set_vec = vectors.query(my_string.lower().split()) 384 | 385 | #pooled_vec = inner_product_rank_mag(set_vec, use_weights = True) 386 | 387 | 388 | #print(vectors.most_similar(pooled_vec, topn = 10000)) # 10k similar word lookup is possible 389 | 390 | 391 | 392 | 393 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy 2 | pymagnitude 3 | --------------------------------------------------------------------------------