68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
from pathlib import Path
|
|
from flask import Flask, render_template, request
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
def load_words() -> list[str]:
|
|
data_path = Path(__file__).parent / "data" / "words_de_5.txt"
|
|
if not data_path.exists():
|
|
return []
|
|
words: list[str] = []
|
|
with data_path.open("r", encoding="utf-8") as f:
|
|
for line in f:
|
|
word = line.strip().lower()
|
|
if len(word) == 5 and word.isalpha():
|
|
words.append(word)
|
|
return words
|
|
|
|
|
|
def filter_words(words: list[str], position_letters: list[str], includes_text: str, excludes_text: str) -> list[str]:
|
|
results: list[str] = []
|
|
includes_letters = [ch for ch in includes_text.lower() if ch.isalpha()]
|
|
excludes_letters = [ch for ch in excludes_text.lower() if ch.isalpha()]
|
|
for word in words:
|
|
# feste Positionen
|
|
if any(ch and word[idx] != ch for idx, ch in enumerate(position_letters)):
|
|
continue
|
|
# muss-enthalten
|
|
if not all(ch in word for ch in includes_letters):
|
|
continue
|
|
# darf-nicht-enthalten
|
|
if any(ch in word for ch in excludes_letters):
|
|
continue
|
|
results.append(word)
|
|
return results
|
|
|
|
|
|
@app.route("/", methods=["GET", "POST"])
|
|
def index():
|
|
all_words = load_words()
|
|
results: list[str] | None = None
|
|
pos: list[str] = ["", "", "", "", ""]
|
|
includes: str = ""
|
|
excludes: str = ""
|
|
if request.method == "POST":
|
|
pos = [
|
|
(request.form.get("pos1") or "").strip().lower(),
|
|
(request.form.get("pos2") or "").strip().lower(),
|
|
(request.form.get("pos3") or "").strip().lower(),
|
|
(request.form.get("pos4") or "").strip().lower(),
|
|
(request.form.get("pos5") or "").strip().lower(),
|
|
]
|
|
includes = (request.form.get("includes") or "").strip()
|
|
excludes = (request.form.get("excludes") or "").strip()
|
|
results = filter_words(all_words, pos, includes, excludes)
|
|
return render_template(
|
|
"index.html",
|
|
results=results,
|
|
pos=pos,
|
|
includes=includes,
|
|
excludes=excludes,
|
|
words_count=len(all_words),
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True)
|