lexmatch 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. lexmatch-0.1.0/LICENSE +21 -0
  2. lexmatch-0.1.0/PKG-INFO +132 -0
  3. lexmatch-0.1.0/README.md +105 -0
  4. lexmatch-0.1.0/pyproject.toml +128 -0
  5. lexmatch-0.1.0/pyproject.toml.orig +87 -0
  6. lexmatch-0.1.0/src/lexmatch/__init__.py +100 -0
  7. lexmatch-0.1.0/src/lexmatch/composer.py +190 -0
  8. lexmatch-0.1.0/src/lexmatch/constants.py +124 -0
  9. lexmatch-0.1.0/src/lexmatch/contractions.py +100 -0
  10. lexmatch-0.1.0/src/lexmatch/exceptions/__init__.py +19 -0
  11. lexmatch-0.1.0/src/lexmatch/exceptions/base.py +11 -0
  12. lexmatch-0.1.0/src/lexmatch/exceptions/entry.py +20 -0
  13. lexmatch-0.1.0/src/lexmatch/exceptions/parsing.py +14 -0
  14. lexmatch-0.1.0/src/lexmatch/exceptions/smalltalk.py +17 -0
  15. lexmatch-0.1.0/src/lexmatch/exceptions/synonyms.py +16 -0
  16. lexmatch-0.1.0/src/lexmatch/exceptions/tuning.py +18 -0
  17. lexmatch-0.1.0/src/lexmatch/index.py +948 -0
  18. lexmatch-0.1.0/src/lexmatch/models/__init__.py +15 -0
  19. lexmatch-0.1.0/src/lexmatch/models/corrections.py +152 -0
  20. lexmatch-0.1.0/src/lexmatch/models/entry.py +73 -0
  21. lexmatch-0.1.0/src/lexmatch/models/outcome.py +46 -0
  22. lexmatch-0.1.0/src/lexmatch/models/reply.py +60 -0
  23. lexmatch-0.1.0/src/lexmatch/models/synonyms.py +137 -0
  24. lexmatch-0.1.0/src/lexmatch/models/tuning.py +229 -0
  25. lexmatch-0.1.0/src/lexmatch/morphology.py +116 -0
  26. lexmatch-0.1.0/src/lexmatch/negation.py +95 -0
  27. lexmatch-0.1.0/src/lexmatch/parsers/__init__.py +20 -0
  28. lexmatch-0.1.0/src/lexmatch/parsers/csv_parser.py +110 -0
  29. lexmatch-0.1.0/src/lexmatch/parsers/json_parser.py +127 -0
  30. lexmatch-0.1.0/src/lexmatch/parsers/markdown_parser.py +159 -0
  31. lexmatch-0.1.0/src/lexmatch/py.typed +0 -0
  32. lexmatch-0.1.0/src/lexmatch/questions.py +172 -0
  33. lexmatch-0.1.0/src/lexmatch/smalltalk.py +157 -0
  34. lexmatch-0.1.0/src/lexmatch/stemming.py +347 -0
  35. lexmatch-0.1.0/src/lexmatch/stopwords.py +188 -0
  36. lexmatch-0.1.0/src/lexmatch/text.py +14 -0
  37. lexmatch-0.1.0/src/lexmatch/utils/__init__.py +13 -0
  38. lexmatch-0.1.0/src/lexmatch/utils/text.py +97 -0
lexmatch-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 janbalitaan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,132 @@
1
+ Metadata-Version: 2.4
2
+ Name: lexmatch
3
+ Version: 0.1.0
4
+ Summary: Dependency-free BM25 question matching with phrase scoring, for when embeddings are more machinery than you need
5
+ Keywords: bm25,search,retrieval,faq,matching,nlp,tfidf
6
+ Author: janbalitaan
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Text Processing :: Indexing
19
+ Classifier: Typing :: Typed
20
+ Requires-Python: >=3.10
21
+ Project-URL: Homepage, https://github.com/janbalitaan/lexmatch
22
+ Project-URL: Documentation, https://github.com/janbalitaan/lexmatch#readme
23
+ Project-URL: Source, https://github.com/janbalitaan/lexmatch
24
+ Project-URL: Changelog, https://github.com/janbalitaan/lexmatch/blob/main/CHANGELOG.md
25
+ Project-URL: Issues, https://github.com/janbalitaan/lexmatch/issues
26
+ Description-Content-Type: text/markdown
27
+
28
+ # lexmatch
29
+
30
+ BM25 question matching with phrase scoring, and no dependencies.
31
+
32
+ [![PyPI](https://img.shields.io/pypi/v/lexmatch)](https://pypi.org/project/lexmatch/)
33
+ [![Python](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-blue)](https://github.com/janbalitaan/lexmatch/blob/main/pyproject.toml)
34
+ [![CI](https://github.com/janbalitaan/lexmatch/actions/workflows/ci.yml/badge.svg)](https://github.com/janbalitaan/lexmatch/actions/workflows/ci.yml)
35
+ [![License](https://img.shields.io/badge/license-MIT-green)](https://github.com/janbalitaan/lexmatch/blob/main/LICENSE)
36
+
37
+ ```python
38
+ from lexmatch import Entry, Index
39
+
40
+ index = Index([
41
+ Entry("What do you do?", "I build backend services.",
42
+ aliases=["what is your job", "what is your role"]),
43
+ Entry("Are you open to remote work?", "Yes, and to relocation.",
44
+ aliases=["remote", "relocate", "would you move"]),
45
+ ])
46
+
47
+ entry, score = index.answer("what is your role")
48
+ # -> (Entry("What do you do?", ...), 1.0)
49
+
50
+ index.answer("what is the capital of France")
51
+ # -> (None, 0.0) below the threshold, so it refuses
52
+ ```
53
+
54
+ ## When this beats embeddings
55
+
56
+ For a few dozen to a few thousand short question/answer pairs where the
57
+ answers are written by hand, a vector database is more machinery than the
58
+ problem deserves — a model to build the embeddings, a store to query them, and
59
+ a deployment that depends on both.
60
+
61
+ Lexical scoring needs none of that, runs in microseconds, and has a property
62
+ embeddings lack: **you can predict what it will match**, because the math is
63
+ legible. For an FAQ or a help desk, being able to explain why a question
64
+ matched is often worth more than a few points of recall.
65
+
66
+ Use embeddings when you need paraphrase matching across a large, unseen
67
+ corpus. Use this when you wrote the corpus yourself.
68
+
69
+ ## Two things it adds to plain BM25
70
+
71
+ **Phrase scoring.** BM25 cannot rank a query made entirely of stopwords —
72
+ `"what do you do?"` — because those words appear in every entry and carry no
73
+ IDF. So phrasing is scored separately, with one guard: a phrase match only
74
+ counts when the substantive words overlap. Without that, `"what is 2+2"`
75
+ scores 0.57 against `"what is your job"` on `"what is"` alone.
76
+
77
+ **A refusal threshold.** Below `DEFAULT_THRESHOLD` (0.45), `answer()` returns
78
+ `None`. For question answering this matters more than ranking — an off-topic
79
+ question scores 0.2–0.4 from shared filler words, and a confident wrong answer
80
+ is worse than no answer. Scores are scaled to 0–1, so a threshold means the
81
+ same thing whatever the corpus size.
82
+
83
+ ## Aliases do the heavy lifting
84
+
85
+ ```python
86
+ Entry("Are you open to remote work?", "Yes.",
87
+ aliases=["remote", "relocate", "would you move", "hybrid"])
88
+ ```
89
+
90
+ Listing how people actually ask is the cheapest accuracy you can buy. Aliases
91
+ are weighted as heavily as the question itself.
92
+
93
+ ## Tuning
94
+
95
+ | Setting | Default | Effect |
96
+ | --- | --- | --- |
97
+ | `threshold` | `0.45` | Higher refuses more. |
98
+ | `TERM_FREQUENCY_SATURATION` | `1.4` | Above this, repeating a term stops helping. |
99
+ | `LENGTH_PENALTY` | `0.72` | How much long entries are discounted. |
100
+
101
+ ## Documentation
102
+
103
+ | Page | |
104
+ | --- | --- |
105
+ | [Getting started](https://github.com/janbalitaan/lexmatch/blob/main/docs/getting-started.md) | An index, a query, an answer |
106
+ | [How scoring works](https://github.com/janbalitaan/lexmatch/blob/main/docs/scoring.md) | BM25, phrase overlap, and why both |
107
+ | [Refusing to answer](https://github.com/janbalitaan/lexmatch/blob/main/docs/refusing.md) | The threshold, and why a wrong answer is worse |
108
+ | [Replying like a chatbot](https://github.com/janbalitaan/lexmatch/blob/main/docs/replying.md) | Ambiguity, negation, relevance-ranked suggestions |
109
+ | [Composing a reply](https://github.com/janbalitaan/lexmatch/blob/main/docs/composing.md) | One sentence out of several answers |
110
+ | [Aliases](https://github.com/janbalitaan/lexmatch/blob/main/docs/aliases.md) | Where most of the accuracy comes from |
111
+ | [Synonyms](https://github.com/janbalitaan/lexmatch/blob/main/docs/synonyms.md) | A word that means the same thing index-wide |
112
+ | [Compound questions](https://github.com/janbalitaan/lexmatch/blob/main/docs/compound-questions.md) | A message that asks more than one thing |
113
+ | [Typos](https://github.com/janbalitaan/lexmatch/blob/main/docs/typos.md) | Reading a word somebody mistyped |
114
+ | [Small talk](https://github.com/janbalitaan/lexmatch/blob/main/docs/small-talk.md) | Hello, thanks, who are you |
115
+ | [Tuning](https://github.com/janbalitaan/lexmatch/blob/main/docs/tuning.md) | Every number, and when to move it |
116
+ | [API reference](https://github.com/janbalitaan/lexmatch/blob/main/docs/api.md) | Everything importable |
117
+ | [Limitations](https://github.com/janbalitaan/lexmatch/blob/main/docs/limitations.md) | What to use instead, and when |
118
+
119
+ Runnable [examples](https://github.com/janbalitaan/lexmatch/blob/main/examples) too.
120
+
121
+ ## Install
122
+
123
+ ```bash
124
+ pip install lexmatch
125
+ ```
126
+
127
+ Python 3.10+. No dependencies, so it runs in a serverless function, in CI, or
128
+ on a Raspberry Pi with no install step.
129
+
130
+ ## License
131
+
132
+ MIT
@@ -0,0 +1,105 @@
1
+ # lexmatch
2
+
3
+ BM25 question matching with phrase scoring, and no dependencies.
4
+
5
+ [![PyPI](https://img.shields.io/pypi/v/lexmatch)](https://pypi.org/project/lexmatch/)
6
+ [![Python](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12%20%7C%203.13-blue)](https://github.com/janbalitaan/lexmatch/blob/main/pyproject.toml)
7
+ [![CI](https://github.com/janbalitaan/lexmatch/actions/workflows/ci.yml/badge.svg)](https://github.com/janbalitaan/lexmatch/actions/workflows/ci.yml)
8
+ [![License](https://img.shields.io/badge/license-MIT-green)](https://github.com/janbalitaan/lexmatch/blob/main/LICENSE)
9
+
10
+ ```python
11
+ from lexmatch import Entry, Index
12
+
13
+ index = Index([
14
+ Entry("What do you do?", "I build backend services.",
15
+ aliases=["what is your job", "what is your role"]),
16
+ Entry("Are you open to remote work?", "Yes, and to relocation.",
17
+ aliases=["remote", "relocate", "would you move"]),
18
+ ])
19
+
20
+ entry, score = index.answer("what is your role")
21
+ # -> (Entry("What do you do?", ...), 1.0)
22
+
23
+ index.answer("what is the capital of France")
24
+ # -> (None, 0.0) below the threshold, so it refuses
25
+ ```
26
+
27
+ ## When this beats embeddings
28
+
29
+ For a few dozen to a few thousand short question/answer pairs where the
30
+ answers are written by hand, a vector database is more machinery than the
31
+ problem deserves — a model to build the embeddings, a store to query them, and
32
+ a deployment that depends on both.
33
+
34
+ Lexical scoring needs none of that, runs in microseconds, and has a property
35
+ embeddings lack: **you can predict what it will match**, because the math is
36
+ legible. For an FAQ or a help desk, being able to explain why a question
37
+ matched is often worth more than a few points of recall.
38
+
39
+ Use embeddings when you need paraphrase matching across a large, unseen
40
+ corpus. Use this when you wrote the corpus yourself.
41
+
42
+ ## Two things it adds to plain BM25
43
+
44
+ **Phrase scoring.** BM25 cannot rank a query made entirely of stopwords —
45
+ `"what do you do?"` — because those words appear in every entry and carry no
46
+ IDF. So phrasing is scored separately, with one guard: a phrase match only
47
+ counts when the substantive words overlap. Without that, `"what is 2+2"`
48
+ scores 0.57 against `"what is your job"` on `"what is"` alone.
49
+
50
+ **A refusal threshold.** Below `DEFAULT_THRESHOLD` (0.45), `answer()` returns
51
+ `None`. For question answering this matters more than ranking — an off-topic
52
+ question scores 0.2–0.4 from shared filler words, and a confident wrong answer
53
+ is worse than no answer. Scores are scaled to 0–1, so a threshold means the
54
+ same thing whatever the corpus size.
55
+
56
+ ## Aliases do the heavy lifting
57
+
58
+ ```python
59
+ Entry("Are you open to remote work?", "Yes.",
60
+ aliases=["remote", "relocate", "would you move", "hybrid"])
61
+ ```
62
+
63
+ Listing how people actually ask is the cheapest accuracy you can buy. Aliases
64
+ are weighted as heavily as the question itself.
65
+
66
+ ## Tuning
67
+
68
+ | Setting | Default | Effect |
69
+ | --- | --- | --- |
70
+ | `threshold` | `0.45` | Higher refuses more. |
71
+ | `TERM_FREQUENCY_SATURATION` | `1.4` | Above this, repeating a term stops helping. |
72
+ | `LENGTH_PENALTY` | `0.72` | How much long entries are discounted. |
73
+
74
+ ## Documentation
75
+
76
+ | Page | |
77
+ | --- | --- |
78
+ | [Getting started](https://github.com/janbalitaan/lexmatch/blob/main/docs/getting-started.md) | An index, a query, an answer |
79
+ | [How scoring works](https://github.com/janbalitaan/lexmatch/blob/main/docs/scoring.md) | BM25, phrase overlap, and why both |
80
+ | [Refusing to answer](https://github.com/janbalitaan/lexmatch/blob/main/docs/refusing.md) | The threshold, and why a wrong answer is worse |
81
+ | [Replying like a chatbot](https://github.com/janbalitaan/lexmatch/blob/main/docs/replying.md) | Ambiguity, negation, relevance-ranked suggestions |
82
+ | [Composing a reply](https://github.com/janbalitaan/lexmatch/blob/main/docs/composing.md) | One sentence out of several answers |
83
+ | [Aliases](https://github.com/janbalitaan/lexmatch/blob/main/docs/aliases.md) | Where most of the accuracy comes from |
84
+ | [Synonyms](https://github.com/janbalitaan/lexmatch/blob/main/docs/synonyms.md) | A word that means the same thing index-wide |
85
+ | [Compound questions](https://github.com/janbalitaan/lexmatch/blob/main/docs/compound-questions.md) | A message that asks more than one thing |
86
+ | [Typos](https://github.com/janbalitaan/lexmatch/blob/main/docs/typos.md) | Reading a word somebody mistyped |
87
+ | [Small talk](https://github.com/janbalitaan/lexmatch/blob/main/docs/small-talk.md) | Hello, thanks, who are you |
88
+ | [Tuning](https://github.com/janbalitaan/lexmatch/blob/main/docs/tuning.md) | Every number, and when to move it |
89
+ | [API reference](https://github.com/janbalitaan/lexmatch/blob/main/docs/api.md) | Everything importable |
90
+ | [Limitations](https://github.com/janbalitaan/lexmatch/blob/main/docs/limitations.md) | What to use instead, and when |
91
+
92
+ Runnable [examples](https://github.com/janbalitaan/lexmatch/blob/main/examples) too.
93
+
94
+ ## Install
95
+
96
+ ```bash
97
+ pip install lexmatch
98
+ ```
99
+
100
+ Python 3.10+. No dependencies, so it runs in a serverless function, in CI, or
101
+ on a Raspberry Pi with no install step.
102
+
103
+ ## License
104
+
105
+ MIT
@@ -0,0 +1,128 @@
1
+ [project]
2
+ name = "lexmatch"
3
+ version = "0.1.0"
4
+ description = "Dependency-free BM25 question matching with phrase scoring, for when embeddings are more machinery than you need"
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ license = "MIT"
8
+ license-files = ["LICENSE"]
9
+ keywords = [
10
+ "bm25",
11
+ "search",
12
+ "retrieval",
13
+ "faq",
14
+ "matching",
15
+ "nlp",
16
+ "tfidf",
17
+ ]
18
+ classifiers = [
19
+ "Development Status :: 3 - Alpha",
20
+ "Intended Audience :: Developers",
21
+ "Operating System :: OS Independent",
22
+ "License :: OSI Approved :: MIT License",
23
+ "Programming Language :: Python :: 3",
24
+ "Programming Language :: Python :: 3.10",
25
+ "Programming Language :: Python :: 3.11",
26
+ "Programming Language :: Python :: 3.12",
27
+ "Programming Language :: Python :: 3.13",
28
+ "Topic :: Text Processing :: Indexing",
29
+ "Typing :: Typed",
30
+ ]
31
+ dependencies = []
32
+
33
+ [[project.authors]]
34
+ name = "janbalitaan"
35
+
36
+ [project.urls]
37
+ Homepage = "https://github.com/janbalitaan/lexmatch"
38
+ Documentation = "https://github.com/janbalitaan/lexmatch#readme"
39
+ Source = "https://github.com/janbalitaan/lexmatch"
40
+ Changelog = "https://github.com/janbalitaan/lexmatch/blob/main/CHANGELOG.md"
41
+ Issues = "https://github.com/janbalitaan/lexmatch/issues"
42
+
43
+ [build-system]
44
+ requires = ["uv_build>=0.5"]
45
+ build-backend = "uv_build"
46
+
47
+ [tool.uv.build-backend]
48
+ module-root = "src"
49
+
50
+ [tool.ruff]
51
+ line-length = 96
52
+ target-version = "py310"
53
+ src = [
54
+ "src",
55
+ "tests",
56
+ ]
57
+
58
+ [tool.ruff.lint]
59
+ select = [
60
+ "E",
61
+ "W",
62
+ "F",
63
+ "I",
64
+ "N",
65
+ "UP",
66
+ "B",
67
+ "A",
68
+ "C4",
69
+ "SIM",
70
+ "TID",
71
+ "PTH",
72
+ "RET",
73
+ "ARG",
74
+ "PL",
75
+ "RUF",
76
+ "S",
77
+ "D",
78
+ ]
79
+ ignore = [
80
+ "D107",
81
+ "D203",
82
+ "D213",
83
+ ]
84
+
85
+ [tool.ruff.lint.per-file-ignores]
86
+ "src/lexmatch/index.py" = ["PLR0911"]
87
+ "src/lexmatch/composer.py" = ["PLR0911"]
88
+ "examples/*" = [
89
+ "S",
90
+ "D",
91
+ "T201",
92
+ "INP001",
93
+ "E402",
94
+ ]
95
+ "tests/*" = [
96
+ "S",
97
+ "PLR2004",
98
+ "ARG",
99
+ "D",
100
+ ]
101
+
102
+ [tool.ruff.lint.flake8-tidy-imports]
103
+ ban-relative-imports = "all"
104
+
105
+ [tool.ruff.lint.pydocstyle]
106
+ convention = "pep257"
107
+
108
+ [tool.ruff.lint.isort]
109
+ known-first-party = ["lexmatch"]
110
+ force-sort-within-sections = true
111
+
112
+ [tool.pytest.ini_options]
113
+ testpaths = ["tests"]
114
+ pythonpath = ["src"]
115
+ addopts = "-q --strict-markers"
116
+
117
+ [tool.coverage.run]
118
+ source = ["src/lexmatch"]
119
+ branch = true
120
+
121
+ [dependency-groups]
122
+ dev = [
123
+ "pytest>=8.3",
124
+ "pytest-cov>=6.0",
125
+ "complexipy>=8.1",
126
+ "ruff>=0.8",
127
+ "ty>=0.0.1a1",
128
+ ]
@@ -0,0 +1,87 @@
1
+ [project]
2
+ name = "lexmatch"
3
+ version = "0.1.0"
4
+ description = "Dependency-free BM25 question matching with phrase scoring, for when embeddings are more machinery than you need"
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ license = "MIT"
8
+ license-files = ["LICENSE"]
9
+ authors = [{ name = "janbalitaan" }]
10
+ keywords = ["bm25", "search", "retrieval", "faq", "matching", "nlp", "tfidf"]
11
+ classifiers = [
12
+ "Development Status :: 3 - Alpha",
13
+ "Intended Audience :: Developers",
14
+ "Operating System :: OS Independent",
15
+ "License :: OSI Approved :: MIT License",
16
+ "Programming Language :: Python :: 3",
17
+ "Programming Language :: Python :: 3.10",
18
+ "Programming Language :: Python :: 3.11",
19
+ "Programming Language :: Python :: 3.12",
20
+ "Programming Language :: Python :: 3.13",
21
+ "Topic :: Text Processing :: Indexing",
22
+ "Typing :: Typed",
23
+ ]
24
+ # No dependencies, and no model. That is the whole point: this runs in a
25
+ # serverless function, in CI, or on a Raspberry Pi with no install step.
26
+ dependencies = []
27
+
28
+ [project.urls]
29
+ Homepage = "https://github.com/janbalitaan/lexmatch"
30
+ Documentation = "https://github.com/janbalitaan/lexmatch#readme"
31
+ Source = "https://github.com/janbalitaan/lexmatch"
32
+ Changelog = "https://github.com/janbalitaan/lexmatch/blob/main/CHANGELOG.md"
33
+ Issues = "https://github.com/janbalitaan/lexmatch/issues"
34
+
35
+ [build-system]
36
+ requires = ["uv_build>=0.5"]
37
+ build-backend = "uv_build"
38
+
39
+ [tool.uv.build-backend]
40
+ module-root = "src"
41
+
42
+ [dependency-groups]
43
+ dev = ["pytest>=8.3", "pytest-cov>=6.0", "complexipy>=8.1", "ruff>=0.8", "ty>=0.0.1a1"]
44
+
45
+ [tool.ruff]
46
+ line-length = 96
47
+ target-version = "py310"
48
+ src = ["src", "tests"]
49
+
50
+ [tool.ruff.lint]
51
+ select = ["E", "W", "F", "I", "N", "UP", "B", "A", "C4", "SIM", "TID", "PTH",
52
+ "RET", "ARG", "PL", "RUF", "S", "D"]
53
+ ignore = ["D107", "D203", "D213"]
54
+
55
+ [tool.ruff.lint.per-file-ignores]
56
+ # `reply()` has one branch per way a query can fail, and each returns. An
57
+ # `if/elif` chain assigning to one variable would be the same decisions with
58
+ # a mutable in the middle, and the rule cannot tell a clean dispatch from a
59
+ # tangle.
60
+ "src/lexmatch/index.py" = ["PLR0911"]
61
+ # `compose_reply` has one branch per outcome and each returns a different
62
+ # sentence. An if/elif chain assigning to one variable would be the same
63
+ # decisions with a mutable in the middle.
64
+ "src/lexmatch/composer.py" = ["PLR0911"]
65
+ # Examples are illustrative scripts, with the same exemptions as the tests
66
+ # and for the same reason: they demonstrate rather than implement.
67
+ "examples/*" = ["S", "D", "T201", "INP001", "E402"]
68
+ "tests/*" = ["S", "PLR2004", "ARG", "D"]
69
+
70
+ [tool.ruff.lint.flake8-tidy-imports]
71
+ ban-relative-imports = "all"
72
+
73
+ [tool.ruff.lint.pydocstyle]
74
+ convention = "pep257"
75
+
76
+ [tool.ruff.lint.isort]
77
+ known-first-party = ["lexmatch"]
78
+ force-sort-within-sections = true
79
+
80
+ [tool.pytest.ini_options]
81
+ testpaths = ["tests"]
82
+ pythonpath = ["src"]
83
+ addopts = "-q --strict-markers"
84
+
85
+ [tool.coverage.run]
86
+ source = ["src/lexmatch"]
87
+ branch = true
@@ -0,0 +1,100 @@
1
+ """BM25 question matching with phrase scoring, and no dependencies.
2
+
3
+ Built for the case where a vector database is more machinery than the problem
4
+ deserves: a few dozen to a few thousand short question/answer pairs, where the
5
+ answers are written by hand and the matching has to be predictable.
6
+
7
+ from lexmatch import Entry, Index
8
+
9
+ index = Index([
10
+ Entry("What do you do?", "I build backend services.",
11
+ aliases=["what is your job", "what is your role"]),
12
+ Entry("Are you open to remote work?", "Yes, and to relocation."),
13
+ ])
14
+
15
+ entry, score = index.answer("what is your role")
16
+ # -> the first entry, 1.0
17
+
18
+ Two things it does that plain BM25 does not:
19
+
20
+ **Phrase scoring.** BM25 cannot rank a query made entirely of stopwords —
21
+ "what do you do?" — because those words appear in every entry and carry no
22
+ IDF. Phrasing is scored separately so such questions still match.
23
+
24
+ **A refusal threshold.** Below it, `answer()` returns None. For a question
25
+ answering system this matters more than ranking: a confident wrong answer is
26
+ worse than no answer, and an off-topic question scores 0.2-0.4 on shared
27
+ filler words alone.
28
+
29
+ Scores are scaled to 0..1, so a threshold means the same thing whatever the
30
+ corpus size.
31
+ """
32
+
33
+ from lexmatch.composer import Composer
34
+ from lexmatch.constants import (
35
+ DEFAULT_THRESHOLD,
36
+ LENGTH_PENALTY,
37
+ MIN_CONTENT_OVERLAP,
38
+ TERM_FREQUENCY_SATURATION,
39
+ )
40
+ from lexmatch.contractions import Contractions
41
+ from lexmatch.exceptions import (
42
+ EntryError,
43
+ LexmatchError,
44
+ ParseError,
45
+ SmallTalkError,
46
+ SynonymError,
47
+ TuningError,
48
+ )
49
+ from lexmatch.index import Index
50
+ from lexmatch.models import (
51
+ Corrections,
52
+ Entry,
53
+ Outcome,
54
+ Reply,
55
+ Synonyms,
56
+ Tuning,
57
+ )
58
+ from lexmatch.morphology import Morphology
59
+ from lexmatch.negation import Negation
60
+ from lexmatch.parsers import CsvParser, JsonParser, MarkdownParser
61
+ from lexmatch.questions import QuestionSplitter
62
+ from lexmatch.smalltalk import SmallTalk
63
+ from lexmatch.stemming import Stemmer
64
+ from lexmatch.stopwords import STOPWORDS
65
+ from lexmatch.text import normalize, query_terms
66
+
67
+ __all__ = [
68
+ "DEFAULT_THRESHOLD",
69
+ "LENGTH_PENALTY",
70
+ "MIN_CONTENT_OVERLAP",
71
+ "STOPWORDS",
72
+ "TERM_FREQUENCY_SATURATION",
73
+ "Composer",
74
+ "Contractions",
75
+ "Corrections",
76
+ "CsvParser",
77
+ "Entry",
78
+ "EntryError",
79
+ "Index",
80
+ "JsonParser",
81
+ "LexmatchError",
82
+ "MarkdownParser",
83
+ "Morphology",
84
+ "Negation",
85
+ "Outcome",
86
+ "ParseError",
87
+ "QuestionSplitter",
88
+ "Reply",
89
+ "SmallTalk",
90
+ "SmallTalkError",
91
+ "Stemmer",
92
+ "SynonymError",
93
+ "Synonyms",
94
+ "Tuning",
95
+ "TuningError",
96
+ "__version__",
97
+ "normalize",
98
+ "query_terms",
99
+ ]
100
+ __version__ = "0.1.0"