"""The challenge searched without the gate (a review, 2026-11-30 21:11, asked that competitors get the same latitude the rule gets).
Each of the nine members is sought by word in every window of five units of the seated Septuagint, Philo and Josephus; windows
are ranked by how many members they carry; the best window in each stretch of twenty units is kept. Same lexicon as search.py,
with F3 split so that bread alone or wine/cup alone counts half. A finder: it proposes, a reader decides. Writes search_ungated.json."""
import re, json, pathlib, unicodedata as U
from search import F, C, g
units = []
for f in sorted((C / "lxx-swete/text").glob("*.txt")) + sorted((C / "philo/text").glob("*.txt")) + sorted((C / "josephus/text").glob("*.txt")):
    for l in open(f, encoding="utf-8"):
        if "\t" in l:
            r, t = l.rstrip("\n").split("\t", 1); units.append((f.stem, r, g(t)))
def score(t):
    s = {k: bool(re.search(p, t)) for k, p in F.items() if p}
    s["F3"] = 1.0 if (re.search(r"αρτ[οωευ]", t) and re.search(r"οιν[οωευ]|ποτηρι|γλευκ", t)) else 0.5 if re.search(r"αρτ[οωευ]|οιν[οωευ]|ποτηρι", t) else 0
    return s
best = {}
for i in range(len(units) - 4):
    W = units[i:i + 5]
    if W[0][0] != W[-1][0]: continue
    s = score(" ".join(x[2] for x in W)); n = sum(float(v) for v in s.values())
    k = (W[0][0], i // 20)
    if k not in best or n > best[k]["score"]:
        best[k] = {"book": W[0][0], "from": W[0][1], "to": W[-1][1], "score": n, "features": [f for f, v in s.items() if v]}
top = sorted(best.values(), key=lambda h: -h["score"])
dist = {}
for h in best.values(): dist[h["score"]] = dist.get(h["score"], 0) + 1
json.dump({"stretches": len(best), "distribution": dict(sorted(dist.items())), "top": top[:40]}, open("search_ungated.json", "w"), ensure_ascii=False, indent=1)
print(len(units), "units;", len(best), "stretches;", dict(sorted(dist.items())))
for h in top[:20]: print(h["score"], h["book"], h["from"], "–", h["to"], h["features"])
