From 550e47cb2c06d326e5215a255e7cfaf0f1119b3e Mon Sep 17 00:00:00 2001 From: augustin64 Date: Fri, 6 Dec 2024 11:24:25 +0100 Subject: [PATCH] Add remote server --- requirements.txt | 2 ++ src/cemantix.py | 52 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index dbab0f3..7532c6d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ colorama gensim +json +requests diff --git a/src/cemantix.py b/src/cemantix.py index ab4ad6f..fb5dbaf 100644 --- a/src/cemantix.py +++ b/src/cemantix.py @@ -1,18 +1,21 @@ from gensim.models import KeyedVectors from colorama import Fore, Back, Style import argparse +import requests import readline import random +import json import time import numpy as np np.seterr(divide='ignore', invalid='ignore') class Server(): + inverse_order = False def __init__(self): pass - def set_random_word(self): + def init_word(self): pass def get_rank(self, guess): @@ -28,6 +31,7 @@ class Server(): raise NotImplementedError class LocalServer(Server): + inverse_order = True def __init__(self, word=None, file="models/selected_word2vec_model.bin"): self.model = KeyedVectors.load_word2vec_format( file, @@ -37,6 +41,7 @@ class LocalServer(Server): self.word = word self.nearest = [] + def init_word(self, k=1, dist=100): while (self.word is None or len(self.word) < 5 or '-' in self.word or '_' in self.word): @@ -68,6 +73,41 @@ class LocalServer(Server): def _reveal_word(self): return self.word +class CemantixServer(Server): + def __init__(self): + self.words = {} + self.solvers = None + self.num = None + + def _try(self, word): + url = 'https://cemantix.certitudes.org/score' + headers = {"Origin": "https://cemantix.certitudes.org"} + data = {'word': word} + # Need some additional cookies to be a valid client + r = requests.post(url, headers=headers, data=data) + + assert r.ok + + data = json.loads(r.text) + self.solvers = int(data["solvers"]) + self.num = int(data["num"]) + self.words[word] = {"score" : float(data["score"])} + if "percentile" in data: + self.words[word]["percentile"] = data["percentile"] + + + def get_rank(self, guess): + if guess not in self.words: + self._try(guess) + + return self.words[guess].get("percentile", None) + + def get_temp(self, guess): + if guess not in self.words: + self._try(guess) + + return round(self.words[guess]["score"]*100, 2) + def cemantix(server: Server): server.init_word() @@ -129,7 +169,7 @@ def cemantix(server: Server): if not tried(guess, guesses): guesses.append((guess, dist, server.get_rank(guess))) - guesses.sort(key=lambda x:-x[1]) + guesses.sort(key=lambda x:-x[1] if server.inverse_order else x[1]) print(chr(27) + "[2J") print(formatted_status(guesses, last=guess)) if server.get_rank(guess) == 1000: @@ -147,9 +187,15 @@ def main(): parser.add_argument("-m", "--model", dest="model", action="store", default="models/selected_word2vec_model.bin", help="Specify model to use") + parser.add_argument("-r", "--remote", dest="remote", action="store_true", + default=False, + help="Use cemantix.certitudes.org instead of local model") + args = parser.parse_args() - cemantix(LocalServer(word=args.word, file=args.model)) + if args.remote: + return cemantix(CemantixServer()) + return cemantix(LocalServer(word=args.word, file=args.model)) if __name__ == "__main__": main()