Add remote server
This commit is contained in:
parent
44df60fca2
commit
550e47cb2c
@ -1,2 +1,4 @@
|
|||||||
colorama
|
colorama
|
||||||
gensim
|
gensim
|
||||||
|
json
|
||||||
|
requests
|
||||||
|
@ -1,18 +1,21 @@
|
|||||||
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 requests
|
||||||
import readline
|
import readline
|
||||||
import random
|
import random
|
||||||
|
import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
np.seterr(divide='ignore', invalid='ignore')
|
np.seterr(divide='ignore', invalid='ignore')
|
||||||
|
|
||||||
class Server():
|
class Server():
|
||||||
|
inverse_order = False
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def set_random_word(self):
|
def init_word(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get_rank(self, guess):
|
def get_rank(self, guess):
|
||||||
@ -28,6 +31,7 @@ class Server():
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
class LocalServer(Server):
|
class LocalServer(Server):
|
||||||
|
inverse_order = True
|
||||||
def __init__(self, word=None, file="models/selected_word2vec_model.bin"):
|
def __init__(self, word=None, file="models/selected_word2vec_model.bin"):
|
||||||
self.model = KeyedVectors.load_word2vec_format(
|
self.model = KeyedVectors.load_word2vec_format(
|
||||||
file,
|
file,
|
||||||
@ -37,6 +41,7 @@ class LocalServer(Server):
|
|||||||
self.word = word
|
self.word = word
|
||||||
self.nearest = []
|
self.nearest = []
|
||||||
|
|
||||||
|
|
||||||
def init_word(self, k=1, dist=100):
|
def init_word(self, k=1, dist=100):
|
||||||
while (self.word is None or len(self.word) < 5
|
while (self.word is None or len(self.word) < 5
|
||||||
or '-' in self.word or '_' in self.word):
|
or '-' in self.word or '_' in self.word):
|
||||||
@ -68,6 +73,41 @@ class LocalServer(Server):
|
|||||||
def _reveal_word(self):
|
def _reveal_word(self):
|
||||||
return self.word
|
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):
|
def cemantix(server: Server):
|
||||||
server.init_word()
|
server.init_word()
|
||||||
@ -129,7 +169,7 @@ def cemantix(server: Server):
|
|||||||
|
|
||||||
if not tried(guess, guesses):
|
if not tried(guess, guesses):
|
||||||
guesses.append((guess, dist, server.get_rank(guess)))
|
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(chr(27) + "[2J")
|
||||||
print(formatted_status(guesses, last=guess))
|
print(formatted_status(guesses, last=guess))
|
||||||
if server.get_rank(guess) == 1000:
|
if server.get_rank(guess) == 1000:
|
||||||
@ -147,9 +187,15 @@ def main():
|
|||||||
parser.add_argument("-m", "--model", dest="model", action="store",
|
parser.add_argument("-m", "--model", dest="model", action="store",
|
||||||
default="models/selected_word2vec_model.bin",
|
default="models/selected_word2vec_model.bin",
|
||||||
help="Specify model to use")
|
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()
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user