# worddict.py
#
# Builds:
#   words[length] = [word1, word2, ...]
#
# Designed for ppgen.py and the dwyl words_alpha.txt word list.

import os
from collections import defaultdict

WORDLIST_PATH = os.path.join(os.path.dirname(__file__), "words.txt")

words = defaultdict(list)

def load_wordlist():
    if not os.path.exists(WORDLIST_PATH):
        raise FileNotFoundError(
            f"Missing wordlist file: {WORDLIST_PATH}\n"
            "Download https://github.com/dwyl/english-words/blob/master/words_alpha.txt "
            "and save it as words.txt"
        )

    with open(WORDLIST_PATH, "r", encoding="utf-8") as f:
        for line in f:
            w = line.strip()
            if not w:
                continue

            # dwyl list is already lowercase and alphabetical —
            # but we'll enforce that anyway:
            if not w.isalpha():
                continue

            words[len(w)].append(w)

# Load at import time
load_wordlist()
