Matheel 0.2.0__tar.gz → 0.2.1__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.
- matheel-0.2.1/PKG-INFO +193 -0
- matheel-0.2.1/README.md +160 -0
- {matheel-0.2.0 → matheel-0.2.1}/matheel/__init__.py +9 -3
- matheel-0.2.1/matheel/chunking.py +316 -0
- {matheel-0.2.0 → matheel-0.2.1}/matheel/cli.py +24 -5
- {matheel-0.2.0 → matheel-0.2.1}/matheel/comparison_suite.py +2 -2
- matheel-0.2.1/matheel/feature_weights.py +101 -0
- matheel-0.2.1/matheel/model_routing.py +132 -0
- {matheel-0.2.0 → matheel-0.2.1}/matheel/similarity.py +172 -94
- matheel-0.2.1/matheel/vectors.py +281 -0
- matheel-0.2.1/matheel.egg-info/PKG-INFO +193 -0
- {matheel-0.2.0 → matheel-0.2.1}/matheel.egg-info/SOURCES.txt +5 -0
- {matheel-0.2.0 → matheel-0.2.1}/matheel.egg-info/requires.txt +12 -1
- {matheel-0.2.0 → matheel-0.2.1}/pyproject.toml +16 -3
- matheel-0.2.1/tests/test_chunking.py +72 -0
- {matheel-0.2.0 → matheel-0.2.1}/tests/test_cli.py +15 -4
- matheel-0.2.1/tests/test_feature_weights.py +16 -0
- matheel-0.2.1/tests/test_model_routing.py +65 -0
- matheel-0.2.1/tests/test_real_models.py +121 -0
- {matheel-0.2.0 → matheel-0.2.1}/tests/test_similarity.py +120 -13
- {matheel-0.2.0 → matheel-0.2.1}/tests/test_vectors.py +2 -2
- matheel-0.2.0/PKG-INFO +0 -215
- matheel-0.2.0/README.md +0 -189
- matheel-0.2.0/matheel/chunking.py +0 -93
- matheel-0.2.0/matheel/vectors.py +0 -101
- matheel-0.2.0/matheel.egg-info/PKG-INFO +0 -215
- matheel-0.2.0/tests/test_chunking.py +0 -20
- {matheel-0.2.0 → matheel-0.2.1}/LICENSE +0 -0
- {matheel-0.2.0 → matheel-0.2.1}/matheel/code_metrics.py +0 -0
- {matheel-0.2.0 → matheel-0.2.1}/matheel/preprocessing.py +0 -0
- {matheel-0.2.0 → matheel-0.2.1}/matheel.egg-info/dependency_links.txt +0 -0
- {matheel-0.2.0 → matheel-0.2.1}/matheel.egg-info/entry_points.txt +0 -0
- {matheel-0.2.0 → matheel-0.2.1}/matheel.egg-info/top_level.txt +0 -0
- {matheel-0.2.0 → matheel-0.2.1}/setup.cfg +0 -0
- {matheel-0.2.0 → matheel-0.2.1}/tests/test_code_metrics.py +0 -0
- {matheel-0.2.0 → matheel-0.2.1}/tests/test_comparison_suite.py +0 -0
- {matheel-0.2.0 → matheel-0.2.1}/tests/test_preprocessing.py +0 -0
matheel-0.2.1/PKG-INFO
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: matheel
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: Matheel: A CLI and Python package for source-code similarity detection.
|
|
5
|
+
Author: Fahad Ebrahim
|
|
6
|
+
License-Expression: CC-BY-NC-4.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/FahadEbrahim/matheel
|
|
8
|
+
Project-URL: Source, https://github.com/FahadEbrahim/matheel
|
|
9
|
+
Requires-Python: <3.13,>=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: click
|
|
13
|
+
Requires-Dist: pandas
|
|
14
|
+
Requires-Dist: numpy
|
|
15
|
+
Requires-Dist: rapidfuzz
|
|
16
|
+
Requires-Dist: sentence-transformers
|
|
17
|
+
Requires-Dist: sentencepiece
|
|
18
|
+
Provides-Extra: chunking
|
|
19
|
+
Requires-Dist: chonkie; extra == "chunking"
|
|
20
|
+
Provides-Extra: chunking-code
|
|
21
|
+
Requires-Dist: chonkie[code]; extra == "chunking-code"
|
|
22
|
+
Provides-Extra: metrics
|
|
23
|
+
Provides-Extra: model2vec
|
|
24
|
+
Requires-Dist: model2vec; extra == "model2vec"
|
|
25
|
+
Provides-Extra: pylate
|
|
26
|
+
Requires-Dist: pylate; extra == "pylate"
|
|
27
|
+
Provides-Extra: gradio
|
|
28
|
+
Requires-Dist: gradio<6,>=5; extra == "gradio"
|
|
29
|
+
Requires-Dist: gradio-huggingfacehub-search==0.0.8; extra == "gradio"
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: pytest<9,>=8; extra == "dev"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# Matheel
|
|
35
|
+
|
|
36
|
+
Matheel is a simple, function-based Python package and CLI for source-code similarity. It combines semantic embeddings, lexical similarity, chunking, preprocessing, and code-aware metrics without forcing a class-heavy API.
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
Use Python `3.10` to `3.12`.
|
|
41
|
+
|
|
42
|
+
Base install:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install matheel
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Optional extras:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install "matheel[metrics]"
|
|
52
|
+
pip install "matheel[chunking]"
|
|
53
|
+
pip install "matheel[model2vec]"
|
|
54
|
+
pip install "matheel[pylate]"
|
|
55
|
+
pip install "matheel[dev]"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Supported Languages
|
|
59
|
+
|
|
60
|
+
- Chunking is language-agnostic by default because it can split any text.
|
|
61
|
+
- `CodeBLEU`-style metrics are intentionally scoped to `Java`, `Python`, `C`, and `C++`.
|
|
62
|
+
- Generic preprocessing works across languages, but code-aware metrics are most defensible in that four-language scope.
|
|
63
|
+
|
|
64
|
+
## Supported Methods
|
|
65
|
+
|
|
66
|
+
Similarity features:
|
|
67
|
+
|
|
68
|
+
- `semantic`
|
|
69
|
+
- `levenshtein`
|
|
70
|
+
- `jaro_winkler`
|
|
71
|
+
- `code_metric`
|
|
72
|
+
|
|
73
|
+
Code metrics:
|
|
74
|
+
|
|
75
|
+
- `codebleu`
|
|
76
|
+
- `codebleu_ngram`
|
|
77
|
+
- `codebleu_weighted_ngram`
|
|
78
|
+
- `codebleu_syntax`
|
|
79
|
+
- `codebleu_dataflow`
|
|
80
|
+
- `crystalbleu`
|
|
81
|
+
|
|
82
|
+
Chunking methods:
|
|
83
|
+
|
|
84
|
+
- Built-in: `none`, `lines`, `tokens`, `characters`
|
|
85
|
+
- Chonkie-backed when installed: `code`, `codechunker`, `chonkie_code`, `chonkie_token`, `chonkie_word`, `chonkie_sentence`, `chonkie_recursive`
|
|
86
|
+
|
|
87
|
+
Vector backends:
|
|
88
|
+
|
|
89
|
+
- `auto`
|
|
90
|
+
- `sentence_transformers`
|
|
91
|
+
- `model2vec`
|
|
92
|
+
- `pylate`
|
|
93
|
+
- `static_hash`
|
|
94
|
+
|
|
95
|
+
`auto` inspects Hugging Face model metadata and routes to the correct backend when the model exposes a known library.
|
|
96
|
+
|
|
97
|
+
## CLI
|
|
98
|
+
|
|
99
|
+
Compare a directory or ZIP archive:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
matheel compare codes/ \
|
|
103
|
+
--model sentence-transformers/all-MiniLM-L6-v2 \
|
|
104
|
+
--vector-backend auto \
|
|
105
|
+
--feature-weight semantic=0.6 \
|
|
106
|
+
--feature-weight levenshtein=0.2 \
|
|
107
|
+
--feature-weight jaro_winkler=0.1 \
|
|
108
|
+
--feature-weight code_metric=0.1 \
|
|
109
|
+
--preprocess-mode basic \
|
|
110
|
+
--chunking-method code \
|
|
111
|
+
--chunk-language python \
|
|
112
|
+
--chunker-option include_line_numbers=true \
|
|
113
|
+
--code-metric codebleu \
|
|
114
|
+
--code-language python \
|
|
115
|
+
--threshold 0.5 \
|
|
116
|
+
--num 25
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Run multiple configurations:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
matheel compare-suite codes/ runs.json \
|
|
123
|
+
--summary-out results/summary.csv \
|
|
124
|
+
--details-dir results/runs
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Python API
|
|
128
|
+
|
|
129
|
+
Pairwise scoring:
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
from matheel.similarity import calculate_similarity
|
|
133
|
+
|
|
134
|
+
score = calculate_similarity(
|
|
135
|
+
"def add(a, b):\n return a + b\n",
|
|
136
|
+
"def add(x, y):\n return x + y\n",
|
|
137
|
+
0.7,
|
|
138
|
+
0.2,
|
|
139
|
+
0.1,
|
|
140
|
+
"sentence-transformers/all-MiniLM-L6-v2",
|
|
141
|
+
vector_backend="auto",
|
|
142
|
+
preprocess_mode="basic",
|
|
143
|
+
chunking_method="code",
|
|
144
|
+
chunk_language="python",
|
|
145
|
+
code_metric="codebleu",
|
|
146
|
+
code_language="python",
|
|
147
|
+
feature_weights={"semantic": 0.5, "code_metric": 0.5},
|
|
148
|
+
)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Directory or ZIP ranking:
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
from matheel.similarity import get_sim_list
|
|
155
|
+
|
|
156
|
+
results = get_sim_list(
|
|
157
|
+
"sample_codes",
|
|
158
|
+
0.7,
|
|
159
|
+
0.2,
|
|
160
|
+
0.1,
|
|
161
|
+
"sentence-transformers/all-MiniLM-L6-v2",
|
|
162
|
+
0.4,
|
|
163
|
+
50,
|
|
164
|
+
vector_backend="auto",
|
|
165
|
+
chunking_method="chonkie_token",
|
|
166
|
+
chunk_size=120,
|
|
167
|
+
chunk_overlap=20,
|
|
168
|
+
feature_weights="semantic=0.7,levenshtein=0.15,jaro_winkler=0.15",
|
|
169
|
+
)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Hugging Face Routing
|
|
173
|
+
|
|
174
|
+
Matheel can inspect Hugging Face model metadata and route automatically:
|
|
175
|
+
|
|
176
|
+
- `sentence-transformers` models go to the Sentence Transformers path
|
|
177
|
+
- `model2vec` models go to the model2vec static path
|
|
178
|
+
- `PyLate` models go to the multivector late-interaction path
|
|
179
|
+
|
|
180
|
+
If metadata is unavailable, Matheel falls back to simple name and tag heuristics, then defaults to the Sentence Transformers path.
|
|
181
|
+
|
|
182
|
+
## Docs and Examples
|
|
183
|
+
|
|
184
|
+
- Usage guide: [docs/usage.md](docs/usage.md)
|
|
185
|
+
- Example run configs: [examples/runs/basic_runs.json](examples/runs/basic_runs.json)
|
|
186
|
+
|
|
187
|
+
## Gradio
|
|
188
|
+
|
|
189
|
+
The Gradio demo stays in `gradio_app/`. The core package and CLI can read either a ZIP archive or a directory. The Gradio upload flow remains ZIP-based.
|
|
190
|
+
|
|
191
|
+
## License
|
|
192
|
+
|
|
193
|
+
This project is licensed under the [Creative Commons Attribution-NonCommercial 4.0 International License](https://creativecommons.org/licenses/by-nc/4.0/).
|
matheel-0.2.1/README.md
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Matheel
|
|
2
|
+
|
|
3
|
+
Matheel is a simple, function-based Python package and CLI for source-code similarity. It combines semantic embeddings, lexical similarity, chunking, preprocessing, and code-aware metrics without forcing a class-heavy API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Use Python `3.10` to `3.12`.
|
|
8
|
+
|
|
9
|
+
Base install:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install matheel
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Optional extras:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install "matheel[metrics]"
|
|
19
|
+
pip install "matheel[chunking]"
|
|
20
|
+
pip install "matheel[model2vec]"
|
|
21
|
+
pip install "matheel[pylate]"
|
|
22
|
+
pip install "matheel[dev]"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Supported Languages
|
|
26
|
+
|
|
27
|
+
- Chunking is language-agnostic by default because it can split any text.
|
|
28
|
+
- `CodeBLEU`-style metrics are intentionally scoped to `Java`, `Python`, `C`, and `C++`.
|
|
29
|
+
- Generic preprocessing works across languages, but code-aware metrics are most defensible in that four-language scope.
|
|
30
|
+
|
|
31
|
+
## Supported Methods
|
|
32
|
+
|
|
33
|
+
Similarity features:
|
|
34
|
+
|
|
35
|
+
- `semantic`
|
|
36
|
+
- `levenshtein`
|
|
37
|
+
- `jaro_winkler`
|
|
38
|
+
- `code_metric`
|
|
39
|
+
|
|
40
|
+
Code metrics:
|
|
41
|
+
|
|
42
|
+
- `codebleu`
|
|
43
|
+
- `codebleu_ngram`
|
|
44
|
+
- `codebleu_weighted_ngram`
|
|
45
|
+
- `codebleu_syntax`
|
|
46
|
+
- `codebleu_dataflow`
|
|
47
|
+
- `crystalbleu`
|
|
48
|
+
|
|
49
|
+
Chunking methods:
|
|
50
|
+
|
|
51
|
+
- Built-in: `none`, `lines`, `tokens`, `characters`
|
|
52
|
+
- Chonkie-backed when installed: `code`, `codechunker`, `chonkie_code`, `chonkie_token`, `chonkie_word`, `chonkie_sentence`, `chonkie_recursive`
|
|
53
|
+
|
|
54
|
+
Vector backends:
|
|
55
|
+
|
|
56
|
+
- `auto`
|
|
57
|
+
- `sentence_transformers`
|
|
58
|
+
- `model2vec`
|
|
59
|
+
- `pylate`
|
|
60
|
+
- `static_hash`
|
|
61
|
+
|
|
62
|
+
`auto` inspects Hugging Face model metadata and routes to the correct backend when the model exposes a known library.
|
|
63
|
+
|
|
64
|
+
## CLI
|
|
65
|
+
|
|
66
|
+
Compare a directory or ZIP archive:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
matheel compare codes/ \
|
|
70
|
+
--model sentence-transformers/all-MiniLM-L6-v2 \
|
|
71
|
+
--vector-backend auto \
|
|
72
|
+
--feature-weight semantic=0.6 \
|
|
73
|
+
--feature-weight levenshtein=0.2 \
|
|
74
|
+
--feature-weight jaro_winkler=0.1 \
|
|
75
|
+
--feature-weight code_metric=0.1 \
|
|
76
|
+
--preprocess-mode basic \
|
|
77
|
+
--chunking-method code \
|
|
78
|
+
--chunk-language python \
|
|
79
|
+
--chunker-option include_line_numbers=true \
|
|
80
|
+
--code-metric codebleu \
|
|
81
|
+
--code-language python \
|
|
82
|
+
--threshold 0.5 \
|
|
83
|
+
--num 25
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Run multiple configurations:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
matheel compare-suite codes/ runs.json \
|
|
90
|
+
--summary-out results/summary.csv \
|
|
91
|
+
--details-dir results/runs
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Python API
|
|
95
|
+
|
|
96
|
+
Pairwise scoring:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from matheel.similarity import calculate_similarity
|
|
100
|
+
|
|
101
|
+
score = calculate_similarity(
|
|
102
|
+
"def add(a, b):\n return a + b\n",
|
|
103
|
+
"def add(x, y):\n return x + y\n",
|
|
104
|
+
0.7,
|
|
105
|
+
0.2,
|
|
106
|
+
0.1,
|
|
107
|
+
"sentence-transformers/all-MiniLM-L6-v2",
|
|
108
|
+
vector_backend="auto",
|
|
109
|
+
preprocess_mode="basic",
|
|
110
|
+
chunking_method="code",
|
|
111
|
+
chunk_language="python",
|
|
112
|
+
code_metric="codebleu",
|
|
113
|
+
code_language="python",
|
|
114
|
+
feature_weights={"semantic": 0.5, "code_metric": 0.5},
|
|
115
|
+
)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Directory or ZIP ranking:
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from matheel.similarity import get_sim_list
|
|
122
|
+
|
|
123
|
+
results = get_sim_list(
|
|
124
|
+
"sample_codes",
|
|
125
|
+
0.7,
|
|
126
|
+
0.2,
|
|
127
|
+
0.1,
|
|
128
|
+
"sentence-transformers/all-MiniLM-L6-v2",
|
|
129
|
+
0.4,
|
|
130
|
+
50,
|
|
131
|
+
vector_backend="auto",
|
|
132
|
+
chunking_method="chonkie_token",
|
|
133
|
+
chunk_size=120,
|
|
134
|
+
chunk_overlap=20,
|
|
135
|
+
feature_weights="semantic=0.7,levenshtein=0.15,jaro_winkler=0.15",
|
|
136
|
+
)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Hugging Face Routing
|
|
140
|
+
|
|
141
|
+
Matheel can inspect Hugging Face model metadata and route automatically:
|
|
142
|
+
|
|
143
|
+
- `sentence-transformers` models go to the Sentence Transformers path
|
|
144
|
+
- `model2vec` models go to the model2vec static path
|
|
145
|
+
- `PyLate` models go to the multivector late-interaction path
|
|
146
|
+
|
|
147
|
+
If metadata is unavailable, Matheel falls back to simple name and tag heuristics, then defaults to the Sentence Transformers path.
|
|
148
|
+
|
|
149
|
+
## Docs and Examples
|
|
150
|
+
|
|
151
|
+
- Usage guide: [docs/usage.md](docs/usage.md)
|
|
152
|
+
- Example run configs: [examples/runs/basic_runs.json](examples/runs/basic_runs.json)
|
|
153
|
+
|
|
154
|
+
## Gradio
|
|
155
|
+
|
|
156
|
+
The Gradio demo stays in `gradio_app/`. The core package and CLI can read either a ZIP archive or a directory. The Gradio upload flow remains ZIP-based.
|
|
157
|
+
|
|
158
|
+
## License
|
|
159
|
+
|
|
160
|
+
This project is licensed under the [Creative Commons Attribution-NonCommercial 4.0 International License](https://creativecommons.org/licenses/by-nc/4.0/).
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from .chunking import chunk_text
|
|
1
|
+
from .chunking import chunk_text, chunker_parameter_names
|
|
2
2
|
from .comparison_suite import load_run_configs, parse_run_configs, run_comparison_suite
|
|
3
3
|
from .code_metrics import (
|
|
4
4
|
available_code_metric_languages,
|
|
@@ -6,6 +6,8 @@ from .code_metrics import (
|
|
|
6
6
|
codebleu_components,
|
|
7
7
|
score_code_metric_pair,
|
|
8
8
|
)
|
|
9
|
+
from .feature_weights import available_default_features, normalize_feature_weights
|
|
10
|
+
from .model_routing import infer_model_backend, infer_model_capabilities, available_vector_backends
|
|
9
11
|
from .preprocessing import preprocess_code
|
|
10
12
|
from .similarity import (
|
|
11
13
|
available_runtime_devices,
|
|
@@ -13,21 +15,25 @@ from .similarity import (
|
|
|
13
15
|
detect_default_device,
|
|
14
16
|
get_sim_list,
|
|
15
17
|
)
|
|
16
|
-
from .vectors import available_vector_backends
|
|
17
18
|
|
|
18
19
|
__all__ = [
|
|
19
20
|
"available_code_metrics",
|
|
20
21
|
"available_code_metric_languages",
|
|
22
|
+
"available_default_features",
|
|
21
23
|
"available_runtime_devices",
|
|
24
|
+
"available_vector_backends",
|
|
22
25
|
"calculate_similarity",
|
|
23
26
|
"chunk_text",
|
|
27
|
+
"chunker_parameter_names",
|
|
24
28
|
"codebleu_components",
|
|
25
29
|
"get_sim_list",
|
|
30
|
+
"infer_model_backend",
|
|
31
|
+
"infer_model_capabilities",
|
|
26
32
|
"load_run_configs",
|
|
33
|
+
"normalize_feature_weights",
|
|
27
34
|
"parse_run_configs",
|
|
28
35
|
"preprocess_code",
|
|
29
36
|
"run_comparison_suite",
|
|
30
37
|
"score_code_metric_pair",
|
|
31
38
|
"detect_default_device",
|
|
32
|
-
"available_vector_backends",
|
|
33
39
|
]
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
_BASE_METHODS = ("none", "lines", "tokens", "characters")
|
|
5
|
+
_CHONKIE_METHODS = (
|
|
6
|
+
"code",
|
|
7
|
+
"codechunker",
|
|
8
|
+
"chonkie_code",
|
|
9
|
+
"chonkie_token",
|
|
10
|
+
"chonkie_sentence",
|
|
11
|
+
"chonkie_recursive",
|
|
12
|
+
"chonkie_fast",
|
|
13
|
+
)
|
|
14
|
+
_CHONKIE_CLASS_NAMES = {
|
|
15
|
+
"code": "CodeChunker",
|
|
16
|
+
"codechunker": "CodeChunker",
|
|
17
|
+
"chonkie_code": "CodeChunker",
|
|
18
|
+
"chonkie_token": "TokenChunker",
|
|
19
|
+
"chonkie_sentence": "SentenceChunker",
|
|
20
|
+
"chonkie_recursive": "RecursiveChunker",
|
|
21
|
+
"chonkie_fast": "FastChunker",
|
|
22
|
+
}
|
|
23
|
+
_CHONKIE_FALLBACK_METHODS = {
|
|
24
|
+
"code": "lines",
|
|
25
|
+
"codechunker": "lines",
|
|
26
|
+
"chonkie_code": "lines",
|
|
27
|
+
"chonkie_token": "tokens",
|
|
28
|
+
"chonkie_sentence": "lines",
|
|
29
|
+
"chonkie_recursive": "characters",
|
|
30
|
+
"chonkie_fast": "lines",
|
|
31
|
+
}
|
|
32
|
+
_CHONKIE_PARAMETER_NAMES = {
|
|
33
|
+
"code": ("chunk_size", "language", "include_nodes", "tokenizer"),
|
|
34
|
+
"codechunker": ("chunk_size", "language", "include_nodes", "tokenizer"),
|
|
35
|
+
"chonkie_code": ("chunk_size", "language", "include_nodes", "tokenizer"),
|
|
36
|
+
"chonkie_token": ("chunk_size", "chunk_overlap", "tokenizer"),
|
|
37
|
+
"chonkie_sentence": (
|
|
38
|
+
"chunk_size",
|
|
39
|
+
"chunk_overlap",
|
|
40
|
+
"min_sentences_per_chunk",
|
|
41
|
+
"min_characters_per_sentence",
|
|
42
|
+
"approximate",
|
|
43
|
+
"delim",
|
|
44
|
+
"include_delim",
|
|
45
|
+
"tokenizer",
|
|
46
|
+
),
|
|
47
|
+
"chonkie_recursive": ("chunk_size", "min_characters_per_chunk", "tokenizer"),
|
|
48
|
+
"chonkie_fast": ("chunk_size", "delimiters", "pattern", "prefix", "consecutive", "forward_fallback"),
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def available_chunking_methods():
|
|
53
|
+
return _BASE_METHODS + _CHONKIE_METHODS
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def chunker_parameter_names(method):
|
|
57
|
+
selected_method = (method or "").strip().lower()
|
|
58
|
+
if selected_method in ("none", "document", "lines", "tokens", "characters"):
|
|
59
|
+
return ()
|
|
60
|
+
return _CHONKIE_PARAMETER_NAMES.get(selected_method, ())
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def available_chunk_aggregations():
|
|
64
|
+
return ("mean", "max", "first")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _normalize_chunk_settings(chunk_size, chunk_overlap):
|
|
68
|
+
size = int(chunk_size or 0)
|
|
69
|
+
overlap = int(chunk_overlap or 0)
|
|
70
|
+
if size <= 0:
|
|
71
|
+
size = 1
|
|
72
|
+
if overlap < 0:
|
|
73
|
+
overlap = 0
|
|
74
|
+
if overlap >= size:
|
|
75
|
+
overlap = size - 1
|
|
76
|
+
return size, overlap
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _window_chunks(items, chunk_size, chunk_overlap, separator):
|
|
80
|
+
if not items:
|
|
81
|
+
return []
|
|
82
|
+
|
|
83
|
+
size, overlap = _normalize_chunk_settings(chunk_size, chunk_overlap)
|
|
84
|
+
step = max(1, size - overlap)
|
|
85
|
+
chunks = []
|
|
86
|
+
start = 0
|
|
87
|
+
|
|
88
|
+
while start < len(items):
|
|
89
|
+
current = items[start : start + size]
|
|
90
|
+
if not current:
|
|
91
|
+
break
|
|
92
|
+
value = separator.join(current).strip()
|
|
93
|
+
if value:
|
|
94
|
+
chunks.append(value)
|
|
95
|
+
start += step
|
|
96
|
+
|
|
97
|
+
return chunks
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def chunk_by_lines(text, chunk_size=20, chunk_overlap=0):
|
|
101
|
+
lines = (text or "").splitlines()
|
|
102
|
+
if not lines:
|
|
103
|
+
return [""]
|
|
104
|
+
return _window_chunks(lines, chunk_size, chunk_overlap, "\n")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def chunk_by_tokens(text, chunk_size=200, chunk_overlap=0):
|
|
108
|
+
tokens = (text or "").split()
|
|
109
|
+
if not tokens:
|
|
110
|
+
return [""]
|
|
111
|
+
return _window_chunks(tokens, chunk_size, chunk_overlap, " ")
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def chunk_by_characters(text, chunk_size=1000, chunk_overlap=0):
|
|
115
|
+
value = text or ""
|
|
116
|
+
if not value:
|
|
117
|
+
return [""]
|
|
118
|
+
|
|
119
|
+
size, overlap = _normalize_chunk_settings(chunk_size, chunk_overlap)
|
|
120
|
+
step = max(1, size - overlap)
|
|
121
|
+
chunks = []
|
|
122
|
+
start = 0
|
|
123
|
+
|
|
124
|
+
while start < len(value):
|
|
125
|
+
current = value[start : start + size]
|
|
126
|
+
if not current:
|
|
127
|
+
break
|
|
128
|
+
chunks.append(current)
|
|
129
|
+
start += step
|
|
130
|
+
|
|
131
|
+
return chunks
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def parse_chunker_options(chunker_options):
|
|
135
|
+
if chunker_options is None:
|
|
136
|
+
return {}
|
|
137
|
+
if isinstance(chunker_options, dict):
|
|
138
|
+
return dict(chunker_options)
|
|
139
|
+
if isinstance(chunker_options, str):
|
|
140
|
+
parts = [part.strip() for part in chunker_options.split(",") if part.strip()]
|
|
141
|
+
return parse_chunker_options(parts)
|
|
142
|
+
|
|
143
|
+
parsed = {}
|
|
144
|
+
for item in chunker_options:
|
|
145
|
+
if isinstance(item, str):
|
|
146
|
+
if "=" not in item:
|
|
147
|
+
raise ValueError(f"Chunker options must look like name=value. Got: {item}")
|
|
148
|
+
key, value = item.split("=", 1)
|
|
149
|
+
parsed[key.strip()] = _coerce_chunker_option_value(value.strip())
|
|
150
|
+
continue
|
|
151
|
+
if isinstance(item, (tuple, list)) and len(item) == 2:
|
|
152
|
+
parsed[str(item[0]).strip()] = _coerce_chunker_option_value(item[1])
|
|
153
|
+
continue
|
|
154
|
+
raise ValueError(f"Unsupported chunker option entry: {item!r}")
|
|
155
|
+
return parsed
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _coerce_chunker_option_value(value):
|
|
159
|
+
if isinstance(value, bool):
|
|
160
|
+
return value
|
|
161
|
+
text = str(value).strip()
|
|
162
|
+
if text.lower() in ("true", "false"):
|
|
163
|
+
return text.lower() == "true"
|
|
164
|
+
try:
|
|
165
|
+
if "." in text:
|
|
166
|
+
return float(text)
|
|
167
|
+
return int(text)
|
|
168
|
+
except ValueError:
|
|
169
|
+
return text
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _load_chonkie_class(class_name):
|
|
173
|
+
try:
|
|
174
|
+
import chonkie
|
|
175
|
+
except ImportError: # pragma: no cover - optional dependency
|
|
176
|
+
return None
|
|
177
|
+
|
|
178
|
+
direct = getattr(chonkie, class_name, None)
|
|
179
|
+
if direct is not None:
|
|
180
|
+
return direct
|
|
181
|
+
|
|
182
|
+
for attr_name in ("chunkers", "chunker"):
|
|
183
|
+
namespace = getattr(chonkie, attr_name, None)
|
|
184
|
+
if namespace is None:
|
|
185
|
+
continue
|
|
186
|
+
candidate = getattr(namespace, class_name, None)
|
|
187
|
+
if candidate is not None:
|
|
188
|
+
return candidate
|
|
189
|
+
return None
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def _filter_callable_kwargs(callable_obj, values):
|
|
193
|
+
try:
|
|
194
|
+
parameters = inspect.signature(callable_obj).parameters
|
|
195
|
+
except (TypeError, ValueError):
|
|
196
|
+
return dict(values)
|
|
197
|
+
return {
|
|
198
|
+
key: value
|
|
199
|
+
for key, value in values.items()
|
|
200
|
+
if key in parameters
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _normalize_chunk_output_items(items):
|
|
205
|
+
chunks = []
|
|
206
|
+
for item in items:
|
|
207
|
+
if isinstance(item, str):
|
|
208
|
+
value = item
|
|
209
|
+
else:
|
|
210
|
+
value = getattr(item, "text", None)
|
|
211
|
+
if value is None:
|
|
212
|
+
value = getattr(item, "content", None)
|
|
213
|
+
if value is None:
|
|
214
|
+
value = getattr(item, "chunk", None)
|
|
215
|
+
if value is None:
|
|
216
|
+
value = str(item)
|
|
217
|
+
value = str(value or "").strip()
|
|
218
|
+
if value:
|
|
219
|
+
chunks.append(value)
|
|
220
|
+
return chunks or [""]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _call_chunker(chunker, text):
|
|
224
|
+
if hasattr(chunker, "chunk"):
|
|
225
|
+
return chunker.chunk(text)
|
|
226
|
+
if hasattr(chunker, "split_text"):
|
|
227
|
+
return chunker.split_text(text)
|
|
228
|
+
if callable(chunker):
|
|
229
|
+
return chunker(text)
|
|
230
|
+
raise ValueError("Unsupported Chonkie chunker instance.")
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _build_chonkie_chunker(method, chunk_size, chunk_overlap, chunk_language="text", chunker_options=None):
|
|
234
|
+
class_name = _CHONKIE_CLASS_NAMES.get(method)
|
|
235
|
+
if class_name is None:
|
|
236
|
+
return None
|
|
237
|
+
chunker_class = _load_chonkie_class(class_name)
|
|
238
|
+
if chunker_class is None:
|
|
239
|
+
return None
|
|
240
|
+
|
|
241
|
+
options = {
|
|
242
|
+
"chunk_size": int(chunk_size or 0),
|
|
243
|
+
"chunk_overlap": int(chunk_overlap or 0),
|
|
244
|
+
"language": chunk_language or "text",
|
|
245
|
+
}
|
|
246
|
+
options.update(parse_chunker_options(chunker_options))
|
|
247
|
+
filtered = _filter_callable_kwargs(chunker_class, options)
|
|
248
|
+
try:
|
|
249
|
+
return chunker_class(**filtered)
|
|
250
|
+
except Exception:
|
|
251
|
+
return None
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _chunk_with_chonkie(text, method, chunk_size, chunk_overlap, max_chunks=0, chunk_language="text", chunker_options=None):
|
|
255
|
+
chunker = _build_chonkie_chunker(
|
|
256
|
+
method,
|
|
257
|
+
chunk_size=chunk_size,
|
|
258
|
+
chunk_overlap=chunk_overlap,
|
|
259
|
+
chunk_language=chunk_language,
|
|
260
|
+
chunker_options=chunker_options,
|
|
261
|
+
)
|
|
262
|
+
if chunker is None:
|
|
263
|
+
fallback_method = _CHONKIE_FALLBACK_METHODS.get(method, "tokens")
|
|
264
|
+
return chunk_text(
|
|
265
|
+
text,
|
|
266
|
+
method=fallback_method,
|
|
267
|
+
chunk_size=chunk_size,
|
|
268
|
+
chunk_overlap=chunk_overlap,
|
|
269
|
+
max_chunks=max_chunks,
|
|
270
|
+
chunk_language=chunk_language,
|
|
271
|
+
chunker_options=chunker_options,
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
chunks = _normalize_chunk_output_items(_call_chunker(chunker, text or ""))
|
|
275
|
+
max_chunk_count = int(max_chunks or 0)
|
|
276
|
+
if max_chunk_count > 0:
|
|
277
|
+
return chunks[:max_chunk_count]
|
|
278
|
+
return chunks
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def chunk_text(
|
|
282
|
+
text,
|
|
283
|
+
method="none",
|
|
284
|
+
chunk_size=200,
|
|
285
|
+
chunk_overlap=0,
|
|
286
|
+
max_chunks=0,
|
|
287
|
+
chunk_language="text",
|
|
288
|
+
chunker_options=None,
|
|
289
|
+
):
|
|
290
|
+
selected_method = (method or "none").strip().lower()
|
|
291
|
+
|
|
292
|
+
if selected_method in ("none", "document"):
|
|
293
|
+
chunks = [text or ""]
|
|
294
|
+
elif selected_method == "lines":
|
|
295
|
+
chunks = chunk_by_lines(text, chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
|
296
|
+
elif selected_method == "tokens":
|
|
297
|
+
chunks = chunk_by_tokens(text, chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
|
298
|
+
elif selected_method == "characters":
|
|
299
|
+
chunks = chunk_by_characters(text, chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
|
300
|
+
elif selected_method in _CHONKIE_METHODS:
|
|
301
|
+
return _chunk_with_chonkie(
|
|
302
|
+
text,
|
|
303
|
+
method=selected_method,
|
|
304
|
+
chunk_size=chunk_size,
|
|
305
|
+
chunk_overlap=chunk_overlap,
|
|
306
|
+
max_chunks=max_chunks,
|
|
307
|
+
chunk_language=chunk_language,
|
|
308
|
+
chunker_options=chunker_options,
|
|
309
|
+
)
|
|
310
|
+
else:
|
|
311
|
+
raise ValueError(f"Unsupported chunking method: {method}")
|
|
312
|
+
|
|
313
|
+
max_chunk_count = int(max_chunks or 0)
|
|
314
|
+
if max_chunk_count > 0:
|
|
315
|
+
return chunks[:max_chunk_count]
|
|
316
|
+
return chunks
|