Skip to main content

lexmatch

BM25 question matching with phrase scoring, and no dependencies.

PyPI Python CI License

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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lexmatch 0.1.1
File Size Uploaded Provenance
lexmatch-0.1.1.tar.gz 57.8 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for lexmatch 0.1.1
File Interpreter ABI Platform
lexmatch-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 127.7 kB

Release files / lexmatch-0.1.1.tar.gz

Download URL lexmatch-0.1.1.tar.gz
Size 57.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7cb68d08d6f41ee25a5b1eac5ad38782a34f50aa30cda48287e188672764f71e
BLAKE2b-256 checksum
How to use checksums
bdb6afe4056e4ebada9a3a0a0d31d063fcdbbf10eeafea26d49f6e6333daee79
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 log

Release files / lexmatch-0.1.1-py3-none-any.whl

Download URL lexmatch-0.1.1-py3-none-any.whl
Size 69.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
37a222c737497f50cb90ce7c02053808022783507779cd786a8a15e4bcac4a09
BLAKE2b-256 checksum
How to use checksums
4c655ef8570fed7e59594b4b51aec2f27e6bfb5d97fcbacc94bee9a3ccaff3e8
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 log

Security for lexmatch 0.1.1

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.

Select from 2 files

Attestation for lexmatch-0.1.1.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 log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page