"""A reproducible lexical retrieval baseline with a small reference corpus."""

from dataclasses import dataclass
from math import log
import re


@dataclass(frozen=True)
class Passage:
    key: str
    text: str
    source: str


# Original paraphrases of the linked Python 3 documentation.
CORPUS = (
    Passage("LIST-SORT", "list.sort sorts a list in place and returns None. The sort is stable.",
            "https://docs.python.org/3/library/stdtypes.html#list.sort"),
    Passage("SORTED", "sorted takes an iterable and returns a new sorted list. The sort is stable.",
            "https://docs.python.org/3/library/functions.html#sorted"),
    Passage("COPY", "A shallow copy constructs a new container and retains references to its contents.",
            "https://docs.python.org/3/library/copy.html"),
    Passage("DEFAULT", "A default parameter value is evaluated at function definition time; mutable defaults may be shared across calls.",
            "https://docs.python.org/3/tutorial/controlflow.html#default-argument-values"),
    Passage("BISECT", "bisect_left finds the insertion point before existing entries equal to the value.",
            "https://docs.python.org/3/library/bisect.html#bisect.bisect_left"),
)

CHECKS = (
    ("Which function sorts a list in place?", {"LIST-SORT"}),
    ("What does sorted return for an iterable?", {"SORTED"}),
    ("What references does a shallow copy preserve?", {"COPY"}),
    ("When is the default parameter value evaluated?", {"DEFAULT"}),
    ("Which API finds the insertion point before equal entries?", {"BISECT"}),
    ("Quand la valeur par défaut est-elle évaluée ?", {"DEFAULT"}),
    ("Quel est le tarif de réinscription ?", set()),
)


def terms(text):
    return set(re.findall(r"[\w.]+", text.casefold()))


def rank(query, k):
    query_terms = terms(query)
    documents = [(passage, terms(passage.text)) for passage in CORPUS]
    weights = {
        term: log((len(documents) + 1) / (1 + sum(term in words for _, words in documents)))
        for term in query_terms
    }
    ranked = []
    for passage, words in documents:
        score = sum(weights[term] for term in sorted(query_terms & words))
        if score > 0:
            ranked.append((score, passage))
    ranked.sort(key=lambda result: (-result[0], result[1].key))
    return [passage for _, passage in ranked[:k]]


if __name__ == "__main__":
    hit_count = answerable = 0
    for query, expected in CHECKS:
        ids = [passage.key for passage in rank(query, k=2)]
        print(f"{query} -> {ids or 'no result'}")
        if expected:
            answerable += 1
            hit_count += bool(expected.intersection(ids))
    print(f"hit@2 = {hit_count}/{answerable}")
    assert rank("première année informatique", k=2) == []
    assert rank("bisect_left", k=1)[0].key == "BISECT"
