-
Notifications
You must be signed in to change notification settings - Fork 0
/
indel.py
164 lines (132 loc) · 5.6 KB
/
indel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from flask import Flask, render_template, request, session
import os
import spellchecker
import logging
import numpy as np
import json
import random
import Levenshtein
from datetime import datetime
with open('words.json', 'r') as f:
word_list = json.load(f)
def generate_word_pair():
i = 0
iter_range = [10, 16]
while True:
try:
starting_word = random.choice(word_list)
prev_words = [starting_word]
for _ in range(random.randrange(*iter_range)):
word_len = len(prev_words[-1])
working_list = [x for x in word_list if any([len(x) == word_len-1, len(x) == word_len, len(x) == word_len + 1])]
good_list = [(x, sum([Levenshtein.distance(x, y) for y in prev_words])) for x in working_list if Levenshtein.distance(x, prev_words[-1]) == 1 and x not in prev_words]
# keep only the words with the most cumulative distance from all previous words
# to try to maximize our travel in word space
most_cumulative_distance = max([x[1] for x in good_list])
good_list = [x for x in good_list if x[1] == most_cumulative_distance]
pick = random.choice(good_list)
prev_words.append(pick[0])
if pick[1] > 50:
break
break
except ValueError:
i += 1
if i == 10:
iter_range = [5, 11]
continue
print(prev_words)
word_path_info = [
starting_word,
prev_words[-1],
datetime.now().day,
len(prev_words) - 1,
'INDEL',
0
]
return word_path_info
word_path_info = generate_word_pair()
checker = spellchecker.SpellChecker()
app = Flask(__name__)
try:
app.secret_key = os.environ['SESSION_KEY']
except KeyError:
logging.warning('$SESSION_KEY not in environment')
app.secret_key = 'BAD_SECRET_KEY_DEV_ONLY'
def check_word(word, prev_word):
spellcheck_answers = len(checker.unknown([word])) == 0
word_list_answers = word.lower() in word_list
if spellcheck_answers and not word_list_answers:
word_list.append(word)
return {
'word': spellcheck_answers or word_list_answers,
'distance': bool(Levenshtein.distance(word, prev_word) <= 1)
}
app.logger.info(word_path_info)
@app.route('/', methods = ['GET', 'POST'])
def index():
if request.method == 'GET':
if datetime.now().day != word_path_info[2]:
with open('words.json', 'w') as f:
app.logger.info('Dumping word list')
json.dump(word_list, f)
word_path_info[0:5] = generate_word_pair()
app.logger.info(word_path_info)
try:
if session['starting_word'] != word_path_info[0]:
session['starting_word'] = word_path_info[0]
if 'your_best' in session:
del session['your_best']
except KeyError:
session['starting_word'] = word_path_info[0]
if 'your_best' in session:
del session['your_best']
return render_template('indel.html', night_theme = request.args.get('theme') == 'dark')
elif request.method == 'POST':
req_json = request.get_json()
# check distance
if req_json['action'] == 'check_word':
return json.dumps(
check_word(req_json['word'], req_json['prev_word'])
), 200, {'ContentType': 'application/json'}
elif req_json['action'] == 'setup':
app.logger.info('your_best' in session)
app.logger.info(session)
return json.dumps(
{
'start_word': word_path_info[0],
'target_word': word_path_info[1],
'current_best': word_path_info[3],
'current_best_player': word_path_info[4],
'other_players': word_path_info[5],
'has_solved': 'your_best' in session
}
), 200, {'ContentType': 'application/json'}
elif req_json['action'] == 'validate_new_winner':
try:
num_words = len(req_json['path'])
assert req_json['path'][0].lower() == word_path_info[0]
assert req_json['path'][-1].lower() == word_path_info[1]
for i in range(1, num_words):
last_word = req_json['path'][i - 1]
word = req_json['path'][i]
dist = Levenshtein.distance(last_word, word)
assert dist <= 1
prev_best = session.get('your_best')
if prev_best is None:
prev_best = 1e6
app.logger.info('Prev:', prev_best)
app.logger.info('New:', num_words - 1)
if prev_best > num_words - 1:
session['your_best'] = num_words - 1
app.logger.info('New best')
if num_words - 1 >= word_path_info[3]:
word_path_info[5] = word_path_info[5] + 1
tied = 1
elif num_words - 1 < word_path_info[3]:
word_path_info[3] = len(req_json['path']) - 1
word_path_info[4] = req_json['player']
word_path_info[5] = 0
tied = 0
return json.dumps({'player': 'good', 'tied': tied}), 200, {'ContentType': 'application/json'}
except AssertionError:
return json.dumps({'player': 'cheater'}), 200, {'ContentType': 'application/json'}