"""Teaching models, not an Elasticsearch or time-series engine test."""
import re
from collections import defaultdict


def analyze(value):
    return re.findall(r"[a-z0-9]+", value.lower())


documents = {1: "Go SQL database", 2: "Go search engine", 3: "SQL search index"}
postings = defaultdict(set)
for document_id, body in documents.items():
    for token in analyze(body):
        postings[token].add(document_id)


def match_all(query):
    tokens = analyze(query)
    if not tokens:
        return []
    return sorted(set.intersection(*(postings[token] for token in tokens)))


print("postings:", {token: sorted(postings[token]) for token in ("go", "sql", "search")})
print("raw Go:", sorted(postings["Go"]))
print("analyzed Go!:", match_all("Go!"))
print("Go AND SQL:", match_all("Go SQL"))
assert match_all("Go SQL") == [1]
assert match_all("absent") == []
assert match_all("") == []

buckets = [[0] * 9, [100]]
means = [sum(bucket) / len(bucket) for bucket in buckets]
mean_of_means = sum(means) / len(means)
weighted = sum(sum(bucket) for bucket in buckets) / sum(map(len, buckets))
print(f"mean-of-means={mean_of_means:.1f} sample-weighted={weighted:.1f}")
assert mean_of_means == 50 and weighted == 10
