lexmatch
BM25 question matching with phrase scoring, and no dependencies.
from lexmatch import Entry, Index
index = Index([
Entry("What do you do?", "I build backend services.",
aliases=["what is your job", "what is your role"]),
Entry("Are you open to remote work?", "Yes, and to relocation.",
aliases=["remote", "relocate", "would you move"]),
])
entry, score = index.answer("what is your role")
# -> (Entry("What do you do?", ...), 1.0)
index.answer("what is the capital of France")
# -> (None, 0.0) below the threshold, so it refuses
When this beats embeddings
For a few dozen to a few thousand short question/answer pairs where the answers are written by hand, a vector database is more machinery than the problem deserves — a model to build the embeddings, a store to query them, and a deployment that depends on both.
Lexical scoring needs none of that, runs in microseconds, and has a property embeddings lack: you can predict what it will match, because the math is legible. For an FAQ or a help desk, being able to explain why a question matched is often worth more than a few points of recall.
Use embeddings when you need paraphrase matching across a large, unseen corpus. Use this when you wrote the corpus yourself.
Two things it adds to plain BM25
Phrase scoring. BM25 cannot rank a query made entirely of stopwords —
"what do you do?" — because those words appear in every entry and carry no
IDF. So phrasing is scored separately, with one guard: a phrase match only
counts when the substantive words overlap. Without that, "what is 2+2"
scores 0.57 against "what is your job" on "what is" alone.
A refusal threshold. Below DEFAULT_THRESHOLD (0.45), answer() returns
None. For question answering this matters more than ranking — an off-topic
question scores 0.2–0.4 from shared filler words, and a confident wrong answer
is worse than no answer. Scores are scaled to 0–1, so a threshold means the
same thing whatever the corpus size.
Aliases do the heavy lifting
Entry("Are you open to remote work?", "Yes.",
aliases=["remote", "relocate", "would you move", "hybrid"])
Listing how people actually ask is the cheapest accuracy you can buy. Aliases are weighted as heavily as the question itself.
Tuning
| Setting | Default | Effect |
|---|---|---|
threshold |
0.45 |
Higher refuses more. |
TERM_FREQUENCY_SATURATION |
1.4 |
Above this, repeating a term stops helping. |
LENGTH_PENALTY |
0.72 |
How much long entries are discounted. |
Documentation
| Page | |
|---|---|
| Getting started | An index, a query, an answer |
| How scoring works | BM25, phrase overlap, and why both |
| Refusing to answer | The threshold, and why a wrong answer is worse |
| Replying like a chatbot | Ambiguity, negation, relevance-ranked suggestions |
| Composing a reply | One sentence out of several answers |
| Aliases | Where most of the accuracy comes from |
| Synonyms | A word that means the same thing index-wide |
| Compound questions | A message that asks more than one thing |
| Typos | Reading a word somebody mistyped |
| Small talk | Hello, thanks, who are you |
| Tuning | Every number, and when to move it |
| API reference | Everything importable |
| Limitations | What to use instead, and when |
Runnable examples too.
Install
pip install lexmatch
Python 3.10+. No dependencies, so it runs in a serverless function, in CI, or on a Raspberry Pi with no install step.
License
MIT
Metadata
Release files for lexmatch 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| lexmatch-0.1.2.tar.gz | 58.4 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lexmatch-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 128.9 kB
Release files / lexmatch-0.1.2.tar.gz
| Download URL | lexmatch-0.1.2.tar.gz |
|---|---|
| Size | 58.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f55b449f41003091c3e2ecb07b9f904c33cd03edb3c39c5ab1b19e359b2c5673
|
|
BLAKE2b-256 checksum How to use checksums |
98cad1d1d1899c6a04dda9f2cd6f278be5cccd28641399944fb726b4507365d7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logRelease files / lexmatch-0.1.2-py3-none-any.whl
| Download URL | lexmatch-0.1.2-py3-none-any.whl |
|---|---|
| Size | 70.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
74cb2b2eba1668b11551077fc717f769a417a5794ea2dcc7113d3b9b6ffcae24
|
|
BLAKE2b-256 checksum How to use checksums |
e21c3906333ed0165767036feeb2d9a5c88e75a2d39fc613fbebb489ca990683
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logSecurity for lexmatch 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for lexmatch-0.1.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.