-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
57 lines (43 loc) · 1.79 KB
/
app.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
import re
from unidecode import unidecode
from flask import Flask, render_template, request, jsonify
import os
app = Flask(__name__)
dico = set([unidecode(line.upper().strip()) for line in open("./data/words.txt").readlines()])
dico = sorted(list(dico))
def all_yellow_in_word(yellow_letters, word):
for letter in yellow_letters:
if unidecode(letter.upper()).isalpha() and unidecode(letter.upper()) not in word:
return False
return True
def grey_in_word(grey_letters, word):
for letter in grey_letters:
if unidecode(letter.upper()) in word:
return True
return False
def lookup(attempt, yellow_letters, grey_letters):
attempt = re.sub("[^A-Z\.]", "", unidecode(attempt.upper().strip()))
regex = re.compile(f"^{attempt}$")
for word in dico:
if regex.search(word) and all_yellow_in_word(yellow_letters, word) and not grey_in_word(grey_letters, word):
yield word
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST' and "attempt" in request.form:
attempt = request.form["attempt"]
yellow_letters = request.form["yellow"]
grey_letters = request.form["grey"]
candidates = list(lookup(attempt, yellow_letters, grey_letters))
print(candidates)
if len(candidates) == 0:
return render_template("index.html", error_message="Error")
else:
return render_template('index.html', candidates=candidates)
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=os.environ.get("PORT", 5002))
# while True:
# attempt = input("attempt:\n> ")
# yellow = input("yellow:\n> ")
# for c in lookup(attempt, yellow):
# print(c)