Add integrated commands

This commit is contained in:
augustin64 2024-10-14 12:01:27 +02:00
parent d2a76adde6
commit e92f753ece

View File

@ -6,15 +6,26 @@ import random
import time import time
def cemantix(model, word=None): def random_word(model, k=5, dist=100):
if word is None: base_words = [
rd = random.randint(0, len(model)) model.index_to_key[random.randint(0, len(model))]
word = model.index_to_key[rd] for _ in range(k)
while '_' in word or '-' in word: ]
rd += 1
word = model.index_to_key[rd]
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): def get_rank(guess):
if guess not in nearest: if guess not in nearest:
return None return None
@ -37,12 +48,26 @@ def cemantix(model, word=None):
def tried(word, guessed): def tried(word, guessed):
return word in [i[0] for i in 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: while True:
try: try:
guess = input(Style.BRIGHT+"Your guess > "+Style.RESET_ALL).strip() guess = input(Style.BRIGHT+"Your guess > "+Style.RESET_ALL).strip()
readline.add_history(guess) readline.add_history(guess)
if guess.endswith("()"):
guesses = interpret_command(guess[:-2], guesses)
continue
except (EOFError, KeyboardInterrupt): except (EOFError, KeyboardInterrupt):
print("The word was "+Style.BRIGHT+word+Style.RESET_ALL) print("The word was "+Style.BRIGHT+word+Style.RESET_ALL)
print("Goodbye!") print("Goodbye!")