Practice in Lab: Project: rank an authorized corpus with real BM25 statistics →
Project: rank an authorized corpus with real BM25 statistics
Permission filtering after top-k can leak document existence and let inaccessible material distort ranking. This project builds a real BM25 scorer over the caller's authorized corpus, including document frequencies and average length. Hidden documents must not change either returned identities or their scores. Its tokenizer is intentionally small and declared; it is not multilingual linguistic analysis. The exercise runs retrieval, not an LLM, and authorization comes from trusted caller context rather than from instructions found inside document text.
import math
n, document_frequency = 1, 1
idf = math.log(1+(n-document_frequency+.5)/(document_frequency+.5))
print(round(idf, 6))Review the design
Review tenant/ACL provenance, corpus-statistic scope, tokenizer changes, ranking evaluation and tie stability. Production indexes also need versioned updates, revocation handling and access-aware caches.
Your task
Documents have tenant,id,roles,text; (tenant,id) must be unique. Admit only the requested tenant and public documents (roles empty) or any role intersection. Tokenize query and documents using lowercase ASCII [a-z0-9]+; deduplicate query terms. Compute all corpus statistics after permission filtering. BM25 uses k1=1.2,b=.75, idf=log(1+(N-df+.5)/(df+.5)), term contribution=idf*tf*2.2/(tf+1.2*(.25+.75*length/average_length)). Return positive-score {id,score} rows sorted by descending score then id, cut at integer k>=0. Empty/no-match/empty-length corpora return [].
The explanation opens here once every check passes.