# dorianbrown/rank_bm25 architecture

> Architecture diagram of the GitHub repository dorianbrown/rank_bm25, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dorianbrown/rank_bm25
- Repository: https://github.com/dorianbrown/rank_bm25

## Overview

Rank-BM25 is a small in-process Python retrieval library. A caller supplies tokenized documents (or a tokenizer), the BM25 base class builds corpus statistics and delegates inverse-document-frequency calculation to a selected variant; queries are scored across the corpus or a document subset, and top-N results map scores back to caller-provided documents. The README also documents Okapi, BM25L and BM25+; preprocessing remains caller-owned. The single implementation file was excerpted, so no separate runtime modules or external services are represented.

Main entry points: [`rank_bm25.py`](https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py) (BM25 base)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_indexing["Corpus indexing"]
  node_base["BM25 base<br/>[rank_bm25.py]"]
  node_tokenizer["Corpus tokenizer<br/>[rank_bm25.py]"]
  node_statistics[("Corpus statistics<br/>[rank_bm25.py]")]
end

subgraph group_ranking["BM25 ranking"]
  node_okapi["BM25Okapi<br/>[rank_bm25.py]"]
  node_b25l["BM25L<br/>[rank_bm25.py]"]
  node_plus["BM25Plus<br/>[rank_bm25.py]"]
  node_scores["Document scores<br/>[rank_bm25.py]"]
  node_results["Top documents<br/>[rank_bm25.py]"]
end

node_caller(("Library caller"))
node_corpus["Tokenized corpus"]
node_query["Tokenized query"]

node_caller -->|"provides"| node_corpus
node_caller -->|"provides"| node_query
node_corpus -->|"initializes"| node_base
node_base -->|"tokenizes via"| node_tokenizer
node_base -->|"builds"| node_statistics
node_base -->|"delegates IDF"| node_okapi
node_base -->|"delegates IDF"| node_b25l
node_base -->|"delegates IDF"| node_plus
node_query -->|"scores with"| node_okapi
node_query -->|"scores with"| node_b25l
node_query -->|"scores with"| node_plus
node_okapi -->|"returns"| node_scores
node_b25l -->|"returns"| node_scores
node_plus -->|"returns"| node_scores
node_okapi -->|"reads"| node_statistics
node_b25l -->|"reads"| node_statistics
node_plus -->|"reads"| node_statistics
node_scores -->|"selects top"| node_results
node_caller -->|"receives"| node_results

click node_base "https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py"
click node_tokenizer "https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py"
click node_statistics "https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py"
click node_okapi "https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py"
click node_b25l "https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py"
click node_plus "https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py"
click node_scores "https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py"
click node_results "https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_base,node_tokenizer,node_statistics toneBlue
class node_okapi,node_b25l,node_plus,node_scores,node_results toneAmber
class node_caller,node_corpus,node_query toneIndigo
```

## Components

### Corpus indexing

- [BM25 base](https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py): `rank_bm25.py`
- [Corpus tokenizer](https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py): `rank_bm25.py`
- [Corpus statistics](https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py): `rank_bm25.py`

### BM25 ranking

- [BM25Okapi](https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py): `rank_bm25.py`
- [BM25L](https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py): `rank_bm25.py`
- [BM25Plus](https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py): `rank_bm25.py`
- [Document scores](https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py): `rank_bm25.py`
- [Top documents](https://github.com/dorianbrown/rank_bm25/blob/master/rank_bm25.py): `rank_bm25.py`

### Other components

- **Library caller**
- **Tokenized corpus**
- **Tokenized query**

## Connections

- Library caller → Tokenized corpus: provides (evidence: `README.md`)
- Library caller → Tokenized query: provides (evidence: `README.md`)
- Tokenized corpus → BM25 base: initializes (evidence: `rank_bm25.py`)
- BM25 base → Corpus tokenizer: tokenizes via (evidence: `rank_bm25.py`)
- BM25 base → Corpus statistics: builds (evidence: `rank_bm25.py`)
- BM25 base → BM25Okapi: delegates IDF (evidence: `rank_bm25.py`)
- BM25 base → BM25L: delegates IDF (evidence: `rank_bm25.py`)
- BM25 base → BM25Plus: delegates IDF (evidence: `rank_bm25.py`)
- Tokenized query → BM25Okapi: scores with (evidence: `README.md`)
- Tokenized query → BM25L: scores with (evidence: `README.md`)
- Tokenized query → BM25Plus: scores with (evidence: `README.md`)
- BM25Okapi → Document scores: returns (evidence: `rank_bm25.py`)
- BM25L → Document scores: returns (evidence: `rank_bm25.py`)
- BM25Plus → Document scores: returns (evidence: `rank_bm25.py`)
- BM25Okapi → Corpus statistics: reads (evidence: `rank_bm25.py`)
- BM25L → Corpus statistics: reads (evidence: `rank_bm25.py`)
- BM25Plus → Corpus statistics: reads (evidence: `rank_bm25.py`)
- Document scores → Top documents: selects top (evidence: `rank_bm25.py`)
- Library caller → Top documents: receives (evidence: `README.md`)
