"""Print every number the lesson quotes, from the toy in embed_search.py.

    uv run --no-project python show_numbers.py
"""
import embed_search as es

Q = es.QUERIES[0]
DOCS = es.DOCS


def vec(v):
    return "(" + ", ".join(f"{x:.3f}" for x in v) + ")"


def known(text):
    return [w for w in es.words(text) if w in es.WORD_VECTORS]


def raw_sum(text):
    return [sum(es.WORD_VECTORS[w][t] for w in known(text)) for t in range(len(es.TOPICS))]


print(f'1. Words only. Question: "{Q}"')
print("   content words:", ", ".join(es.content_words(Q)))
ranked = es.rank_keywords(Q)
for i, score in ranked[:3]:
    shared = ", ".join(es.shared_words(Q, DOCS[i])) or "none"
    print(f"   {score} shared  {DOCS[i]}  ({shared})")
print("   sentences sharing no word with the question:", sum(1 for _, s in ranked if s == 0), "of", len(DOCS))

print()
print("2. Meaning. Same question, closest 4 sentences (cosine):")
for i, score in es.rank_meaning(Q)[:4]:
    print(f"   {score:.3f}  {DOCS[i]}")

print()
print("3. One comparison by hand: the question and the refund sentence")
a, b = es.embed(Q), es.embed(DOCS[1])
print("   topics:", ", ".join(es.TOPICS))
print("   question words the toy knows:", ", ".join(known(Q)), "->", vec(a))
print("   sentence words the toy knows:", ", ".join(known(DOCS[1])), "->", vec(b))
print("   sentence before scaling:", vec(raw_sum(DOCS[1])), f"length {es.length(raw_sum(DOCS[1])):.3f}")
print(f"   dot = {es.dot(a, b):.3f}, lengths = {es.length(a):.1f} and {es.length(b):.1f}, cosine = {es.cosine(a, b):.3f}")

print()
print("4. Length does not change the cosine")
short_list, long_list = (1.0, 0.0, 0.0, 0.0), (5.0, 0.0, 0.0, 0.0)
print(f"   dot = {es.dot(short_list, long_list):.1f}, cosine = {es.cosine(short_list, long_list):.1f}")
print(f"   sharing nothing: cosine = {es.cosine(short_list, (0.0, 1.0, 0.0, 0.0)):.1f}")

print()
print("5. Every question: best sentence by words, best by meaning")
for q in es.QUERIES:
    kw_i, kw_s = es.rank_keywords(q)[0]
    me_i, me_s = es.rank_meaning(q)[0]
    kw = f"{kw_s} shared: {DOCS[kw_i]}" if kw_s else "no shared word"
    me = f"{me_s:.3f}: {DOCS[me_i]}" if me_s else "0.000, nothing to rank"
    print(f"   {q}")
    print(f"      words   {kw}")
    print(f"      meaning {me}")

print()
print("6. A question with no word the toy knows")
unknown = es.embed("Code E4410")
print("   vector:", vec(unknown), " cosines:", sorted({round(es.cosine(unknown, es.embed(d)), 3) for d in DOCS}))

print()
print("7. How many multiplications one question costs (documents x list length)")
for n_docs, dims in ((len(DOCS), 4), (1_000, 768), (1_000_000, 768)):
    print(f"   {n_docs:>9,} x {dims:>3} = {es.scan_cost(n_docs, dims):>11,}")

print()
print("8. Looking only in the closest cell(s). Question:", es.QUERIES[-1])
vectors = [es.embed(d) for d in DOCS]
q_vec = es.embed(es.QUERIES[-1])
exact = es.nearest(q_vec, vectors, 1)[0]
print(f"   all {len(DOCS)}: best is {DOCS[exact[0]]} at {exact[1]:.3f}")
for probes in (1, 2):
    best, count = es.cell_search(q_vec, vectors, probes)
    print(f"   {probes} cell{'s' if probes > 1 else ' '}: compared {count}, best is {DOCS[best]} at {es.cosine(q_vec, vectors[best]):.3f}")
