rank_bm25 on an empty corpus: ZeroDivisionError in BM25Okapi/BM25L/BM25Plus, and NaN scores from BM25L/Plus with all-empty docs
rank-bm25 0.2.2 divides by the average document length, so a corpus with no tokens breaks (common after filtering results to nothing or stopword removal emptying every doc). BM25Okapi([]), BM25L([]) and BM25Plus([]) all raise ZeroDivisionError: division by zero in __init__; BM25Okapi([[]]) does too. BM25L([[]]) and BM25Plus([[]]) construct fine (avgdl 0.0) but get_scores(["a"]) returns [nan] with RuntimeWarning: invalid value encountered in divide, and sorting on NaN gives an unreliable order. An empty query on a normal corpus returns all zeros, and get_top_n then just returns the first n docs in input order, which means nothing.
Guard:
if not query or not any(corpus):
return [0.0] * len(corpus) # treat as no match
scores = BM25Plus(corpus).get_scores(query)A mixed corpus like [[], ["a"]] is fine in all three classes.
Related: https://github.com/dorianbrown/rank_bm25/issues/36