Implement BM25 ranking function for e-commerce product search relevance scoring. Use this skill when the user needs to build a text-based product search engine, improve search result relevance, or replace basic TF-IDF with a more robust ranking function — even if they say 'product search ranking', 'search relevance', or 'BM25 implementation'.
npx skills add https://github.com/asgard-ai-platform/skills --skill algo-ecom-bm25
BM25 (Best Matching 25) is an improved TF-IDF ranking function that adds term frequency saturation and document length normalization. Score = Σ IDF(t) × (TF(t,d) × (k₁+1)) / (TF(t,d) + k₁ × (1 - b + b × |d|/avgdl)). Standard parameters: k₁=1.2, b=0.75. The backbone of most text search engines (Elasticsearch, Solr).
Trigger conditions:
When NOT to use:
IRON LAW: BM25 Has Two Critical Parameters — k₁ and b
k₁ controls term frequency saturation: higher k₁ = more weight to
repeated terms. k₁=0 ignores TF entirely (boolean).
b controls document length normalization: b=1 fully normalizes by
length, b=0 ignores length. Default k₁=1.2, b=0.75 works for most
cases but MUST be tuned for your specific corpus.
Tokenize each document to lowercase word tokens. Remove stop words before
counting — the bundled script drops a standard English stop list (`the, a, an,
and, or, but, of, in, on, at, to, for, with, by, from, as, is, are, was, were,
be, been, being`). Then build an inverted index: term → list of (document, term
frequency). Compute: document lengths (post stop-word removal), average document
length, document frequency per term.
> ⚠️ Stop-word removal affects |d| and avgdl: because stop words are
> dropped before length is measured, hand-computing BM25 without removing them
> will give the wrong length normalization and scores will be off by 3–5%.
> If you're reproducing BM25 by hand to compare against the script, apply the
> same stop list first — or just run the script.
Gate: Index built, statistics computed, corpus non-empty.
For query Q with terms t₁...tₙ against document d:
> ⚠️ IDF variant lock-in: BM25 has several IDF formulations in the wild
> (Robertson-Sparck Jones, classic Okapi, Lucene's smoothed +1, BM25+, BM25L).
> This skill — and the bundled script — uses the Lucene-style smoothed variant
> shown above (log((N - df + 0.5) / (df + 0.5) + 1)), which never returns negative
> IDF for very common terms. If you compare scores against another engine
> (Elasticsearch, Solr, Whoosh), they may differ by ~3–5% even on identical inputs.
> Do not "correct" the script unless you intend to change the variant globally.
Spot-check: query "red shoes" should rank documents containing both "red" and "shoes" higher than documents with only one term. Shorter product titles with both terms should rank above long descriptions with sparse mentions.
Gate: Relevance spot-check passes on 10+ test queries.
Return ranked results with scores.
{
"results": [{"doc_id": "SKU-123", "score": 12.5, "title": "Red Running Shoes"}],
"metadata": {"query": "red shoes", "hits": 85, "k1": 1.2, "b": 0.75, "avg_doc_length": 45}
}
Input: Query "wireless earbuds", corpus of 1000 product listings
Expected: Products with "wireless earbuds" in title rank highest; "wireless headphones" ranks lower (no "earbuds" term).
| Input | Expected | Why |
|-------|----------|-----|
| Single-word query | IDF-dominated ranking | Only one term's IDF differentiates |
| Very common term ("the") | Near-zero IDF, low impact | IDF suppresses common terms |
| Document with 100 repetitions | Saturated TF, not 100x score | k₁ caps the benefit of repetition |
| Script | Description | Usage |
|--------|-------------|-------|
| scripts/bm25.py | Score documents against a query using BM25 ranking function | python scripts/bm25.py --help |
Run python scripts/bm25.py --verify to execute built-in sanity tests.
references/bm25f.mdreferences/parameter-tuning.mdIntegration with protocols.io API for managing scientific protocols. This skill should be used when working with protocols.io to search, create, update, or publish protocols; manage protocol steps and materials; handle discussions and comments; organize workspaces; upload and manage files; or integrate protocols.io functionality into workflows. Applicable for protocol discovery, collaborative protocol development, experiment tracking, lab protocol management, and scientific documentation.
Analyzes job descriptions and generates tailored resumes that highlight relevant experience, skills, and achievements to maximize interview chances
Generate Excalidraw diagrams from natural language descriptions. Use when asked to "create a diagram", "make a flowchart", "visualize a process", "draw a system architecture", "create a mind map", or "generate an Excalidraw file". Supports flowcharts, relationship diagrams, mind maps, and system architecture diagrams. Outputs .excalidraw JSON files that can be opened directly in Excalidraw.
Build and distribute Expo development clients locally or via TestFlight
Use when you have a written implementation plan to execute in a separate session with review checkpoints
Data structure for annotated matrices in single-cell analysis. Use when working with .h5ad files or integrating with the scverse ecosystem. This is the data format skill—for analysis workflows use scanpy; for probabilistic models use scvi-tools; for population-scale queries use cellxgene-census.
Benchling R&D platform integration. Access registry (DNA, proteins), inventory, ELN entries, workflows via API, build Benchling Apps, query Data Warehouse, for lab data management automation.
Comprehensive molecular biology toolkit. Use for sequence manipulation, file parsing (FASTA/GenBank/PDB), phylogenetics, and programmatic NCBI/PubMed access (Bio.Entrez). Best for batch processing, custom bioinformatics pipelines, BLAST automation. For quick lookups use gget; for multi-service integration use bioservices.
Take asgard-ai-platform/algo-ecom-bm25 from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.