PY-87
Add Sparse Score Contributions
Task
Write add_sparse_score_contributions(documents, contributions). documents
is an ordered list of unique document IDs. contributions is a mapping from a
term name to a mapping of document ID to one finite numeric contribution. A
term mapping contains only documents that received a contribution; a document
may be absent from every term. The insertion order of the outer mapping is the
term order used in the trace. Do not construct a dense document-by-term array.
For each document, add all supplied contributions. Retain the evidence in a
trace whose document entries list {"term": term, "value": value} records in
outer-term insertion order. Include a record even when its supplied value is
zero. Documents with no contributions have total 0.0 and an empty trace.
Return a dictionary with exactly these keys:
"totals": a document-to-floatmapping in the declared document order;"trace": a document-to-list mapping in the same order;"ranking": a list of{"document": document, "score": total}records, sorted by descending total, with equal totals kept in declared document order.
Raise ValueError("duplicate document: <id>") for a repeated declared ID,
ValueError("unknown document: <id>") for a contribution keyed by an ID not
in documents, and ValueError("non-finite contribution: <term>.<id>") for a
non-finite value. Keep both input structures unchanged.
Example
For documents ['d1','d2','d3'] and
the totals are d1: 2.0, d2: 0.2, and d3: 1.7; the ranking is
d1, d3, d2. The trace for d1 keeps title before body.
Your implementation
Do not import a search library, print, or ask for input. Ordinary dictionaries and lists are sufficient.