#!/usr/bin/env python3
"""What each corpus file was actually looking for, and how a passage is attributed to one.

Imported by both analyse.py and build_page.py so the published labels and the ones I read while
deciding what to publish cannot drift apart. (Two hand-written copies of one rule drift, and the
drift lands on the branch the check doesn't reach.)

Descriptions are taken from the query families in ~/work/pangram/fetch*.py, not from memory.
"""

LABELS = {
    "corpus.jsonl":   "Self-study textbooks, study guides, ESL instructional materials",
    "corpus2.jsonl":  "Programmed instruction, technical/maintenance manuals, translated textbooks",
    "corpus3.jsonl":  "Beginner computer tutorials — Teach Yourself, For Dummies, step-by-step",
    "corpus4.jsonl":  "US military training series — NEETS, rate training, correspondence courses",
    "corpus5.jsonl":  "Management, self-improvement, popular science",
    "corpus6.jsonl":  "Nursing, counselling, social work and education textbooks",
    "corpus7.jsonl":  "Self-help and advisory writing — how-to, career, study skills",
    "corpus8.jsonl":  "Composition and rhetoric textbooks, reference and encyclopedia entries",
    "corpus9.jsonl":  "Mid-1990s popular introductions to the internet and computers",
    "corpus10.jsonl": "Translated / non-native-English textbooks — Mir, Tata McGraw, Progress",
}


def order(fname):
    """corpus.jsonl, corpus2.jsonl, ... corpus10.jsonl in the order they were fetched.

    Sorting these as strings puts corpus10 second, which is not an order anyone means. It also
    silently changes which family a multi-family passage gets attributed to.
    """
    stem = fname.replace("corpus", "").replace(".jsonl", "")
    return int(stem) if stem else 1


def family(row):
    """Attribute a passage to one query family: the earliest one that found it.

    Roughly a third of passages were returned by more than one family — the queries overlap by
    design. Attribution is therefore arbitrary, but it is deterministic and it is stated: first
    finder wins. Rates per family are 'passages this family found first', not 'passages this
    family could find'.
    """
    return LABELS.get(min(row["files"], key=order), "unlabelled")
