From e92f753eced9b4c9409f4099a446bb304c85bbfa Mon Sep 17 00:00:00 2001 From: augustin64 Date: Mon, 14 Oct 2024 12:01:27 +0200 Subject: [PATCH] Add integrated commands --- cemantix.py | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/cemantix.py b/cemantix.py index 697f005..a5820aa 100644 --- a/cemantix.py +++ b/cemantix.py @@ -6,15 +6,26 @@ import random import time -def cemantix(model, word=None): - if word is None: - rd = random.randint(0, len(model)) - word = model.index_to_key[rd] - while '_' in word or '-' in word: - rd += 1 - word = model.index_to_key[rd] +def random_word(model, k=5, dist=100): + base_words = [ + model.index_to_key[random.randint(0, len(model))] + for _ in range(k) + ] - nearest = [word]+[i[0] for i in model.most_similar(word, topn=999)] + complete_list = base_words.copy() + for word in base_words: + complete_list += [i[0] for i in model.most_similar(word, topn=dist)] + + rk_words = model.rank_by_centrality(complete_list) + return rk_words[random.randint(0,5)][1] + + +def cemantix(model, word=None): + while word is None or len(word) < 5 or '-' in word or '_' in word: + word = random_word(model, k=1, dist=1) # augment numbers to try a "smooth selection" + + nearest = [word]+[i[0] for i in model.most_similar(word, topn=1000)] + guesses = [] # guess, temp, rank def get_rank(guess): if guess not in nearest: return None @@ -37,12 +48,26 @@ def cemantix(model, word=None): def tried(word, guessed): return word in [i[0] for i in guessed] - guesses = [] + def interpret_command(cmd, guesses): + match cmd: + case "clear": + guesses = [g for g in guesses if g[1] <= 75.] + case "help": + best_rk = max([rk for _, _, rk in guesses if rk is not None]+[749]) + print("Maybe try "+Back.YELLOW+Fore.BLACK+nearest[999-best_rk]+Style.RESET_ALL) + case _: + print(Fore.RED+"Unknown command"+Style.RESET_ALL) + + return guesses + while True: try: guess = input(Style.BRIGHT+"Your guess > "+Style.RESET_ALL).strip() readline.add_history(guess) + if guess.endswith("()"): + guesses = interpret_command(guess[:-2], guesses) + continue except (EOFError, KeyboardInterrupt): print("The word was "+Style.BRIGHT+word+Style.RESET_ALL) print("Goodbye!")