BM25Plus#

class scikitplot.rank_bm25.BM25Plus(corpus, tokenizer=None, k1=1.5, b=0.75, delta=1, doc_ids=None, workers=None, backend='auto')[source]#
RECIPE_ID: str = 'bm25plus-delta/2'#

The scoring formula this class implements. Subclasses override it; the class name is not the recipe, because “BM25Okapi” implies a standard the implementation makes specific choices within.

property analyzer: Analyzer | None#

The declared analyzer, or None when tokens were supplied directly.

property backend: str#

"numpy", "numba" or "reference".

Notes

User. Every backend returns the same values, bit for bit; only the time differs. That is why the backend is not part of identity.

Type:

The backend this index scores with

candidate_count(query)[source]#

Number of documents that contain at least one query term.

Parameters:
querysequence of str

Query tokens.

Returns:
int

Documents the sparse path will score.

Notes

User. This is what a query actually costs. get_scores returns one value per document in the corpus whatever the query, which is a diagnostic rather than a retrieval path.

property doc_freqs#

Each document’s term counts, in row order.

Notes

Developer. An index loaded from a bm25-index/2 generation does not hold these dicts: they are rebuilt from the persisted arrays the first time a path that needs them runs (the reference backend, get_batch_scores), and never otherwise.

Type:

List of dict

property doc_ids#

Row identities, in row order.

Type:

Tuple of str or None

get_batch_scores(query, doc_ids)[source]#

Score the documents at the row positions doc_ids against query.

Parameters:
querysequence of str

Query tokens.

doc_idsiterable of int

Row positions (not stable identities) to score: a list, a tuple, an array or any iterable, read once.

Returns:
list of float

One score per requested position, in the order given.

Raises:
TypeError

If query is a string or holds a non-string token. This path used to skip the check, so a bare string was scored one character at a time – the defect the check exists to prevent.

get_scores(query)[source]#

Score every document in the corpus against query.

Parameters:
querysequence of str

Query tokens.

Returns:
numpy.ndarray

One score per document, in row order.

Raises:
TypeError

If query is a string or holds a non-string token.

Notes

Developer. Every scoring path – this one, get_batch_scores and the sparse path behind get_top_ids – evaluates the one recipe formula, _term_contribution, term by term in query order, so all three produce the same floating-point values.

get_top_ids(query, n=5)[source]#

Return the n best (doc_id, score) pairs.

Parameters:
querysequence of str

Query tokens.

nint, optional

Maximum number of hits.

Returns:
list of tuple

(doc_id, score) in descending score, ties in row order.

Raises:
ValueError

If the index holds no identities, or n is not a valid count.

Notes

User. This is the binding to prefer. get_top_n maps row offsets into a sequence you pass at query time, which is only correct while that sequence is in the same order as the corpus the index was built from – a precondition nothing can check for you.

get_top_n(query, documents, n=5)[source]#
property identity: str#

recipe, analyzer and parameters.

Notes

User. Two indexes sharing this value were built the same way. It does not cover the documents: that is what the corpus records, and folding it in here would make a re-tokenisation and a new document look like the same kind of change.

Type:

Digest of what produced this index

classmethod load(directory, *, mmap_mode=None)[source]#

Load a published index, or a generation directory directly.

Parameters:
directorystr or path-like

Index root, or a generation directory.

mmap_mode{None, “r”}, optional

"r" memory-maps the arrays of a bm25-index/2 generation read-only instead of reading them into memory.

Returns:
BM25

The reloaded index, ready to query.

Raises:
TypeError

If called on BM25 itself, which has no formula.

ValueError

If the root has no readable pointer (a root with no pointer is refused rather than guessed at); the generation was written under another schema; its recipe is not this class’s recipe (another scorer, or an earlier version of this one’s formula); a parameter is missing, unknown or outside its domain; or the recorded identity does not match the recipe, analyzer and parameters the generation declares.

Notes

User. Load with the class that built the index. An index written by an earlier version of the formula (an earlier recipe version) is refused, not rescored with the current one: rebuild it from the corpus.

Developer. Every check runs before the engine is rebuilt, so a wrong artifact fails here with a message naming the cause, never later inside the formula. identity is recomputed from what the generation declares and must equal the recorded value, so edited metadata is refused; the arrays are covered separately by their SHA-256 digests.

property parameters: dict#

The scoring parameters that belong to this index’s identity.

prepare()[source]#

Build the scoring engine now, and compile the compiled backend if used.

Returns:
BM25

self, so BM25Okapi(corpus).prepare() reads naturally.

Notes

User. Optional. Without it the first query pays both costs. Call it at service start-up, before declaring the service ready, so the first request is not the slow one – compiling the Numba kernel takes seconds once per process.

property recipe: Recipe#

The named, versioned scoring formula this index used.

save(directory)[source]#

Publish this index as a generation behind a pointer.

Parameters:
directorystr or path-like

Index root. Created if absent.

Returns:
pathlib.Path

The generation directory written.

Raises:
OSError

If the generation could not be written. Nothing is published in that case, and any previously published generation is untouched.

Notes

User. A saved index reloads without the corpus: the statistics are what get published, not the documents.

Developer. Same shape as the corpus artifact and the Annoy bundle: candidate, verify by reloading, then move the pointer. The three carry their own implementation rather than a shared one, which is the cost of each submodule standing alone; the shape is deliberately identical so a reader of one recognises the others.