"""Print every number the lesson quotes. Run it from this folder: python3 show_numbers.py"""
from prompts import PROMPTS
from toy_gen import END, generate, lines_of, load_corpus, positions_read, reply_of, tokenize, train

lines = lines_of(load_corpus())
model = train(lines)
tea = PROMPTS[0]["prompt"]


def counts(p, n=3):
    return ", ".join(f"{t} {c}" for t, c in p["ranked"][:n])


print("== The corpus (corpus.txt)")
print(f"  {len(lines)} lines, {sum(len(tokenize(l)) + 1 for l in lines)} tokens counting one end mark per line, "
      f"{len({t for l in lines for t in tokenize(l)})} different tokens besides the end mark")
for item in PROMPTS:
    print(f"  {item['prompt']!r} starts {sum(l.startswith(item['prompt']) for l in lines)} lines")
print(f"  the 3-token table has {len(model[3])} contexts, the 2-token table {len(model[2])}, the 1-token table {len(model[1])}")

print("\n== Greedy replies (every pass takes the likeliest token)")
for item in PROMPTS:
    passes = generate(model, item["prompt"], 40)
    prompt_tokens, written = len(tokenize(item["prompt"])), len(passes) - 1
    split = sum(1 for p in passes if p["ranked"][0][1] < p["total"])
    print(f"  {item['name']}: {prompt_tokens} prompt tokens, {written} reply tokens + end mark = {len(passes)} passes; "
          f"{split} passes had more than one possible token")
    print(f"    {reply_of(passes)}")
    print(f"    positions read: {positions_read(prompt_tokens, len(passes), False)} without the cache, "
          f"{positions_read(prompt_tokens, len(passes), True)} with it")

print("\n== Tea, every pass")
for i, p in enumerate(generate(model, tea, 40), 1):
    print(f"  {i:2d} looked at {' '.join(p['looked_at']):<22} counts {counts(p)} of {p['total']}  adds {p['token']}")

print("\n== Tea, the first three passes by hand")
for i, p in enumerate(generate(model, tea, 3), 1):
    shares = ", ".join(f"{t} {c}/{p['total']} = {c / p['total']:.0%}" for t, c in p["ranked"])
    print(f"  pass {i}: text ends '{' '.join(p['before'][-3:])}', looked at '{' '.join(p['looked_at'])}': {shares}; adds {p['token']}")

print("\n== Cap")
print(f"  cap 10 on tea stops after {len(generate(model, tea, 10))} passes; the reply is {reply_of(generate(model, tea, 10))!r}")

print("\n== Seeded picks (tea)")
for seed in (1, 2, 3, 7):
    passes = generate(model, tea, 40, seed)
    print(f"  seed {seed}: {len(passes)} passes: {reply_of(passes)}")

print("\n== Passes with and without the cache, tea, by reply length")
for n in (1, 2, 10, 31):
    print(f"  {n:2d} passes: {positions_read(6, n, False)} positions without, {positions_read(6, n, True)} with")
