"""Word-level matching with Levenshtein ratio instead of JW."""

from rapidfuzz import fuzz
from harness import evaluate


def word_score(query, candidate):
    q_words = query.lower().split()
    c_words = candidate.lower().split()

    if not q_words or not c_words:
        return 0.0

    total = 0.0
    for qw in q_words:
        best = 0.0
        for cw in c_words:
            s = fuzz.ratio(qw, cw) / 100.0
            if s > best:
                best = s
        total += best

    return total / len(q_words)


if __name__ == "__main__":
    evaluate(word_score, name="09b - Word-level Levenshtein (avg best match)")
