Import readline

This commit is contained in:
augustin64 2024-10-11 16:06:44 +02:00
parent 035bc05637
commit d2a76adde6

View File

@ -1,18 +1,20 @@
from gensim.models import KeyedVectors from gensim.models import KeyedVectors
from colorama import Fore, Back, Style from colorama import Fore, Back, Style
import argparse import argparse
import readline
import random import random
import time
def cemantix(model, word=None): def cemantix(model, word=None):
if word is None: if word is None:
rd = random.randint(0, len(model)) rd = random.randint(0, len(model))
word = model.index_to_key[rd] word = model.index_to_key[rd]
while "_" in word: while '_' in word or '-' in word:
rd += 1 rd += 1
word = model.index_to_key[rd] word = model.index_to_key[rd]
nearest = [i[0] for i in model.most_similar(word, topn=1000)] nearest = [word]+[i[0] for i in model.most_similar(word, topn=999)]
def get_rank(guess): def get_rank(guess):
if guess not in nearest: if guess not in nearest:
return None return None
@ -40,6 +42,7 @@ def cemantix(model, word=None):
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)
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!")
@ -56,6 +59,7 @@ def cemantix(model, word=None):
print(chr(27) + "[2J") print(chr(27) + "[2J")
print(formatted_status(guesses, last=guess)) print(formatted_status(guesses, last=guess))
if guess == word: if guess == word:
time.sleep(1)
print(Fore.GREEN+"Correct!"+Style.RESET_ALL+f" {len(guesses)} tries.") print(Fore.GREEN+"Correct!"+Style.RESET_ALL+f" {len(guesses)} tries.")
return len(guesses) return len(guesses)