Initial commit
This commit is contained in:
commit
035bc05637
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.venv
|
||||
**/__pycache__
|
||||
*.bin
|
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# [Cemantix](https://cemantix.certitudes.org/) CLI (fully local)
|
||||
|
||||
Download models at [Jean-Philippe Fauconnier's website](https://fauconnier.github.io/#data) (`frWac_non_lem_no_postag_no_phrase_200_skip_cut100.bin` recommanded)
|
82
cemantix.py
Normal file
82
cemantix.py
Normal file
@ -0,0 +1,82 @@
|
||||
from gensim.models import KeyedVectors
|
||||
from colorama import Fore, Back, Style
|
||||
import argparse
|
||||
import random
|
||||
|
||||
|
||||
def cemantix(model, word=None):
|
||||
if word is None:
|
||||
rd = random.randint(0, len(model))
|
||||
word = model.index_to_key[rd]
|
||||
while "_" in word:
|
||||
rd += 1
|
||||
word = model.index_to_key[rd]
|
||||
|
||||
nearest = [i[0] for i in model.most_similar(word, topn=1000)]
|
||||
def get_rank(guess):
|
||||
if guess not in nearest:
|
||||
return None
|
||||
return 1000 - nearest.index(guess)
|
||||
|
||||
def formatted_status(guesses, last=None):
|
||||
text = ""
|
||||
for w, temp, rank in guesses:
|
||||
if rank is not None:
|
||||
text += Back.RED+Fore.BLACK+Style.BRIGHT+str(rank)+Style.RESET_ALL
|
||||
text += "\t"
|
||||
if temp >= 75:
|
||||
text += Fore.BLUE
|
||||
text += str(temp) + Style.RESET_ALL + "\t"
|
||||
if last == w:
|
||||
text += Style.BRIGHT+Back.WHITE+Fore.BLACK
|
||||
text += w + Style.RESET_ALL+"\n"
|
||||
return text[:-1]
|
||||
|
||||
def tried(word, guessed):
|
||||
return word in [i[0] for i in guessed]
|
||||
|
||||
guesses = []
|
||||
|
||||
while True:
|
||||
try:
|
||||
guess = input(Style.BRIGHT+"Your guess > "+Style.RESET_ALL).strip()
|
||||
except (EOFError, KeyboardInterrupt):
|
||||
print("The word was "+Style.BRIGHT+word+Style.RESET_ALL)
|
||||
print("Goodbye!")
|
||||
return -1
|
||||
try:
|
||||
dist = round(round(model.distance(word, guess), 4)*100, 2)
|
||||
except KeyError:
|
||||
print(Fore.RED+"Key not present"+Style.RESET_ALL)
|
||||
continue
|
||||
|
||||
if not tried(guess, guesses):
|
||||
guesses.append((guess, dist, get_rank(guess)))
|
||||
guesses.sort(key=lambda x:-x[1])
|
||||
print(chr(27) + "[2J")
|
||||
print(formatted_status(guesses, last=guess))
|
||||
if guess == word:
|
||||
print(Fore.GREEN+"Correct!"+Style.RESET_ALL+f" {len(guesses)} tries.")
|
||||
return len(guesses)
|
||||
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="local infinite cemantix")
|
||||
parser.add_argument("-w", "--word", dest="word", action="store",
|
||||
help="Specify goal word")
|
||||
parser.add_argument("-m", "--model", dest="model", action="store",
|
||||
default="frWac_non_lem_no_postag_no_phrase_200_skip_cut100.bin",
|
||||
help="Specify model to use")
|
||||
args = parser.parse_args()
|
||||
|
||||
model = KeyedVectors.load_word2vec_format(
|
||||
args.model,
|
||||
binary=True,
|
||||
unicode_errors="ignore"
|
||||
)
|
||||
cemantix(model, word=args.word)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
colorama
|
||||
gensim
|
Loading…
Reference in New Issue
Block a user