semhash 0.3.1__tar.gz → 0.3.3__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.
- {semhash-0.3.1 → semhash-0.3.3}/.gitignore +0 -2
- semhash-0.3.3/CITATION.cff +27 -0
- {semhash-0.3.1 → semhash-0.3.3}/PKG-INFO +39 -7
- {semhash-0.3.1 → semhash-0.3.3}/README.md +37 -5
- {semhash-0.3.1 → semhash-0.3.3}/pyproject.toml +1 -1
- {semhash-0.3.1 → semhash-0.3.3}/semhash/datamodels.py +10 -1
- {semhash-0.3.1 → semhash-0.3.3}/semhash/index.py +8 -2
- {semhash-0.3.1 → semhash-0.3.3}/semhash/semhash.py +7 -2
- {semhash-0.3.1 → semhash-0.3.3}/semhash/version.py +1 -1
- {semhash-0.3.1 → semhash-0.3.3}/semhash.egg-info/PKG-INFO +39 -7
- {semhash-0.3.1 → semhash-0.3.3}/semhash.egg-info/SOURCES.txt +1 -0
- {semhash-0.3.1 → semhash-0.3.3}/semhash.egg-info/requires.txt +1 -1
- {semhash-0.3.1 → semhash-0.3.3}/uv.lock +812 -811
- {semhash-0.3.1 → semhash-0.3.3}/.github/workflows/ci.yaml +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/.pre-commit-config.yaml +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/LICENSE +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/Makefile +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/assets/images/semhash_logo.png +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/assets/images/semhash_logo_v2.png +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/benchmarks/__init__.py +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/benchmarks/data.py +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/benchmarks/results/train_benchmark_results.json +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/benchmarks/results/train_test_benchmark_results.json +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/benchmarks/run_benchmarks.py +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/semhash/__init__.py +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/semhash/records.py +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/semhash/utils.py +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/semhash.egg-info/dependency_links.txt +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/semhash.egg-info/top_level.txt +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/setup.cfg +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/tests/conftest.py +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/tests/data/test_model/README.md +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/tests/data/test_model/config.json +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/tests/data/test_model/model.safetensors +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/tests/data/test_model/modules.json +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/tests/data/test_model/tokenizer.json +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/tests/test_datamodels.py +0 -0
- {semhash-0.3.1 → semhash-0.3.3}/tests/test_semhash.py +0 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
message: "If you use SemHash in your research, please cite it as below."
|
|
3
|
+
title: "SemHash: Fast Semantic Text Deduplication & Filtering"
|
|
4
|
+
authors:
|
|
5
|
+
- family-names: "van Dongen"
|
|
6
|
+
given-names: "Thomas"
|
|
7
|
+
- family-names: "Tulkens"
|
|
8
|
+
given-names: "Stephan"
|
|
9
|
+
doi: 10.5281/zenodo.17265942
|
|
10
|
+
license: MIT
|
|
11
|
+
url: "https://github.com/MinishLab/semhash"
|
|
12
|
+
repository-code: "https://github.com/MinishLab/semhash"
|
|
13
|
+
date-released: "2025-01-05"
|
|
14
|
+
|
|
15
|
+
preferred-citation:
|
|
16
|
+
type: software
|
|
17
|
+
title: "SemHash: Fast Semantic Text Deduplication & Filtering"
|
|
18
|
+
authors:
|
|
19
|
+
- family-names: "van Dongen"
|
|
20
|
+
given-names: "Thomas"
|
|
21
|
+
- family-names: "Tulkens"
|
|
22
|
+
given-names: "Stephan"
|
|
23
|
+
year: 2025
|
|
24
|
+
publisher: Zenodo
|
|
25
|
+
doi: 10.5281/zenodo.17265942
|
|
26
|
+
url: "https://github.com/MinishLab/semhash"
|
|
27
|
+
license: MIT
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: semhash
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.3
|
|
4
4
|
Summary: Fast Semantic Text Deduplication & Filtering
|
|
5
5
|
Author-email: Thomas van Dongen <thomas123@live.nl>, Stéphan Tulkens <stephantul@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -43,7 +43,7 @@ Classifier: Natural Language :: English
|
|
|
43
43
|
Requires-Python: >=3.9
|
|
44
44
|
Description-Content-Type: text/markdown
|
|
45
45
|
Requires-Dist: model2vec>=0.3.4
|
|
46
|
-
Requires-Dist: vicinity[usearch]>=0.4.
|
|
46
|
+
Requires-Dist: vicinity[usearch]>=0.4.3
|
|
47
47
|
Requires-Dist: frozendict
|
|
48
48
|
Provides-Extra: dev
|
|
49
49
|
Requires-Dist: black; extra == "dev"
|
|
@@ -374,6 +374,35 @@ deduplicated_texts = semhash.self_deduplicate()
|
|
|
374
374
|
|
|
375
375
|
</details>
|
|
376
376
|
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
<details>
|
|
381
|
+
<summary> Using custom ANN backends </summary>
|
|
382
|
+
<br>
|
|
383
|
+
|
|
384
|
+
The following code snippet shows how to use a custom ANN backend and custom args with SemHash:
|
|
385
|
+
|
|
386
|
+
```python
|
|
387
|
+
from datasets import load_dataset
|
|
388
|
+
from semhash import SemHash
|
|
389
|
+
from vicinity import Backend
|
|
390
|
+
|
|
391
|
+
# Load a dataset to deduplicate
|
|
392
|
+
texts = load_dataset("ag_news", split="train")["text"]
|
|
393
|
+
|
|
394
|
+
# Initialize a SemHash with the model and custom ann backend and custom args
|
|
395
|
+
semhash = SemHash.from_records(records=texts, ann_backend=Backend.FAISS, nlist=50)
|
|
396
|
+
|
|
397
|
+
# Deduplicate the texts
|
|
398
|
+
deduplicated_texts = semhash.self_deduplicate()
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
For the full list of supported ANN backends and args, see the [Vicinity docs](https://github.com/MinishLab/vicinity/tree/main?tab=readme-ov-file#supported-backends).
|
|
402
|
+
|
|
403
|
+
</details>
|
|
404
|
+
|
|
405
|
+
|
|
377
406
|
<details>
|
|
378
407
|
<summary> Using Pandas DataFrames </summary>
|
|
379
408
|
<br>
|
|
@@ -473,7 +502,7 @@ To run the benchmarks yourself, you can use the following command (assuming you
|
|
|
473
502
|
```bash
|
|
474
503
|
python -m benchmarks.run_benchmarks
|
|
475
504
|
```
|
|
476
|
-
Optionally, the datasets can be updated in the [
|
|
505
|
+
Optionally, the datasets can be updated in the [data.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/data.py) file.
|
|
477
506
|
|
|
478
507
|
## License
|
|
479
508
|
|
|
@@ -484,9 +513,12 @@ MIT
|
|
|
484
513
|
If you use SemHash in your research, please cite the following:
|
|
485
514
|
```bibtex
|
|
486
515
|
@software{minishlab2025semhash,
|
|
487
|
-
author
|
|
488
|
-
title
|
|
489
|
-
year
|
|
490
|
-
|
|
516
|
+
author = {{van Dongen}, Thomas and Stephan Tulkens},
|
|
517
|
+
title = {SemHash: Fast Semantic Text Deduplication \& Filtering},
|
|
518
|
+
year = {2025},
|
|
519
|
+
publisher = {Zenodo},
|
|
520
|
+
doi = {10.5281/zenodo.17265942},
|
|
521
|
+
url = {https://github.com/MinishLab/semhash},
|
|
522
|
+
license = {MIT}
|
|
491
523
|
}
|
|
492
524
|
```
|
|
@@ -318,6 +318,35 @@ deduplicated_texts = semhash.self_deduplicate()
|
|
|
318
318
|
|
|
319
319
|
</details>
|
|
320
320
|
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
<details>
|
|
325
|
+
<summary> Using custom ANN backends </summary>
|
|
326
|
+
<br>
|
|
327
|
+
|
|
328
|
+
The following code snippet shows how to use a custom ANN backend and custom args with SemHash:
|
|
329
|
+
|
|
330
|
+
```python
|
|
331
|
+
from datasets import load_dataset
|
|
332
|
+
from semhash import SemHash
|
|
333
|
+
from vicinity import Backend
|
|
334
|
+
|
|
335
|
+
# Load a dataset to deduplicate
|
|
336
|
+
texts = load_dataset("ag_news", split="train")["text"]
|
|
337
|
+
|
|
338
|
+
# Initialize a SemHash with the model and custom ann backend and custom args
|
|
339
|
+
semhash = SemHash.from_records(records=texts, ann_backend=Backend.FAISS, nlist=50)
|
|
340
|
+
|
|
341
|
+
# Deduplicate the texts
|
|
342
|
+
deduplicated_texts = semhash.self_deduplicate()
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
For the full list of supported ANN backends and args, see the [Vicinity docs](https://github.com/MinishLab/vicinity/tree/main?tab=readme-ov-file#supported-backends).
|
|
346
|
+
|
|
347
|
+
</details>
|
|
348
|
+
|
|
349
|
+
|
|
321
350
|
<details>
|
|
322
351
|
<summary> Using Pandas DataFrames </summary>
|
|
323
352
|
<br>
|
|
@@ -417,7 +446,7 @@ To run the benchmarks yourself, you can use the following command (assuming you
|
|
|
417
446
|
```bash
|
|
418
447
|
python -m benchmarks.run_benchmarks
|
|
419
448
|
```
|
|
420
|
-
Optionally, the datasets can be updated in the [
|
|
449
|
+
Optionally, the datasets can be updated in the [data.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/data.py) file.
|
|
421
450
|
|
|
422
451
|
## License
|
|
423
452
|
|
|
@@ -428,9 +457,12 @@ MIT
|
|
|
428
457
|
If you use SemHash in your research, please cite the following:
|
|
429
458
|
```bibtex
|
|
430
459
|
@software{minishlab2025semhash,
|
|
431
|
-
author
|
|
432
|
-
title
|
|
433
|
-
year
|
|
434
|
-
|
|
460
|
+
author = {{van Dongen}, Thomas and Stephan Tulkens},
|
|
461
|
+
title = {SemHash: Fast Semantic Text Deduplication \& Filtering},
|
|
462
|
+
year = {2025},
|
|
463
|
+
publisher = {Zenodo},
|
|
464
|
+
doi = {10.5281/zenodo.17265942},
|
|
465
|
+
url = {https://github.com/MinishLab/semhash},
|
|
466
|
+
license = {MIT}
|
|
435
467
|
}
|
|
436
468
|
```
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import json
|
|
3
4
|
import warnings
|
|
4
5
|
from collections import defaultdict
|
|
5
6
|
from dataclasses import dataclass, field
|
|
@@ -156,7 +157,15 @@ class DeduplicationResult(Generic[Record]):
|
|
|
156
157
|
# Get the list of duplicates for the selected record
|
|
157
158
|
raw_list = buckets.get(_to_hashable(selected), [])
|
|
158
159
|
# Ensure we don't have duplicates in the list
|
|
159
|
-
|
|
160
|
+
# Use full-record canonical JSON for dicts so that unhashable values are handled correctly
|
|
161
|
+
deduped = {
|
|
162
|
+
(
|
|
163
|
+
json.dumps(rec, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
|
|
164
|
+
if isinstance(rec, dict)
|
|
165
|
+
else rec
|
|
166
|
+
): (rec, score)
|
|
167
|
+
for rec, score in raw_list
|
|
168
|
+
}
|
|
160
169
|
result.append(SelectedWithDuplicates(record=selected, duplicates=list(deduped.values())))
|
|
161
170
|
|
|
162
171
|
return result
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
3
5
|
import numpy as np
|
|
4
6
|
from vicinity import Backend
|
|
5
7
|
from vicinity.backends import AbstractBackend, get_backend_class
|
|
@@ -27,17 +29,21 @@ class Index:
|
|
|
27
29
|
self.vectors = vectors
|
|
28
30
|
|
|
29
31
|
@classmethod
|
|
30
|
-
def from_vectors_and_items(
|
|
32
|
+
def from_vectors_and_items(
|
|
33
|
+
cls, vectors: np.ndarray, items: list[DictItem], backend_type: Backend | str, **kwargs: Any
|
|
34
|
+
) -> Index:
|
|
31
35
|
"""
|
|
32
36
|
Load the index from vectors and items.
|
|
33
37
|
|
|
34
38
|
:param vectors: The vectors of the items.
|
|
35
39
|
:param items: The items in the index.
|
|
36
40
|
:param backend_type: The type of backend to use.
|
|
41
|
+
:param **kwargs: Additional arguments to pass to the backend.
|
|
37
42
|
:return: The index.
|
|
38
43
|
"""
|
|
39
44
|
backend_class = get_backend_class(backend_type)
|
|
40
|
-
|
|
45
|
+
arguments = backend_class.argument_class(**kwargs)
|
|
46
|
+
backend = backend_class.from_vectors(vectors, **arguments.dict())
|
|
41
47
|
|
|
42
48
|
return cls(vectors, items, backend)
|
|
43
49
|
|
|
@@ -2,7 +2,7 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
from collections import defaultdict
|
|
4
4
|
from math import ceil
|
|
5
|
-
from typing import Generic, Literal, Sequence
|
|
5
|
+
from typing import Any, Generic, Literal, Sequence
|
|
6
6
|
|
|
7
7
|
import numpy as np
|
|
8
8
|
from frozendict import frozendict
|
|
@@ -102,6 +102,8 @@ class SemHash(Generic[Record]):
|
|
|
102
102
|
columns: Sequence[str] | None = None,
|
|
103
103
|
use_ann: bool = True,
|
|
104
104
|
model: Encoder | None = None,
|
|
105
|
+
ann_backend: Backend | str = Backend.USEARCH,
|
|
106
|
+
**kwargs: Any,
|
|
105
107
|
) -> SemHash:
|
|
106
108
|
"""
|
|
107
109
|
Initialize a SemHash instance from records.
|
|
@@ -112,6 +114,8 @@ class SemHash(Generic[Record]):
|
|
|
112
114
|
:param columns: Columns to featurize if records are dictionaries.
|
|
113
115
|
:param use_ann: Whether to use approximate nearest neighbors (True) or basic search (False). Default is True.
|
|
114
116
|
:param model: (Optional) An Encoder model. If None, the default model is used (minishlab/potion-base-8M).
|
|
117
|
+
:param ann_backend: (Optional) The ANN backend to use if use_ann is True. Defaults to Backend.USEARCH.
|
|
118
|
+
:param **kwargs: Any additional keyword arguments to pass to the Vicinity index.
|
|
115
119
|
:return: A SemHash instance with a fitted vicinity index.
|
|
116
120
|
:raises ValueError: If columns are not provided for dictionary records.
|
|
117
121
|
"""
|
|
@@ -151,11 +155,12 @@ class SemHash(Generic[Record]):
|
|
|
151
155
|
embeddings = cls._featurize(deduplicated_records, columns, model)
|
|
152
156
|
|
|
153
157
|
# Build the Vicinity index
|
|
154
|
-
backend =
|
|
158
|
+
backend = ann_backend if use_ann else Backend.BASIC
|
|
155
159
|
index = Index.from_vectors_and_items(
|
|
156
160
|
vectors=embeddings,
|
|
157
161
|
items=items,
|
|
158
162
|
backend_type=backend,
|
|
163
|
+
**kwargs,
|
|
159
164
|
)
|
|
160
165
|
|
|
161
166
|
return cls(index=index, columns=columns, model=model, was_string=was_string)
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
__version_triple__ = (0, 3,
|
|
1
|
+
__version_triple__ = (0, 3, 3)
|
|
2
2
|
__version__ = ".".join(map(str, __version_triple__))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: semhash
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.3
|
|
4
4
|
Summary: Fast Semantic Text Deduplication & Filtering
|
|
5
5
|
Author-email: Thomas van Dongen <thomas123@live.nl>, Stéphan Tulkens <stephantul@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -43,7 +43,7 @@ Classifier: Natural Language :: English
|
|
|
43
43
|
Requires-Python: >=3.9
|
|
44
44
|
Description-Content-Type: text/markdown
|
|
45
45
|
Requires-Dist: model2vec>=0.3.4
|
|
46
|
-
Requires-Dist: vicinity[usearch]>=0.4.
|
|
46
|
+
Requires-Dist: vicinity[usearch]>=0.4.3
|
|
47
47
|
Requires-Dist: frozendict
|
|
48
48
|
Provides-Extra: dev
|
|
49
49
|
Requires-Dist: black; extra == "dev"
|
|
@@ -374,6 +374,35 @@ deduplicated_texts = semhash.self_deduplicate()
|
|
|
374
374
|
|
|
375
375
|
</details>
|
|
376
376
|
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
<details>
|
|
381
|
+
<summary> Using custom ANN backends </summary>
|
|
382
|
+
<br>
|
|
383
|
+
|
|
384
|
+
The following code snippet shows how to use a custom ANN backend and custom args with SemHash:
|
|
385
|
+
|
|
386
|
+
```python
|
|
387
|
+
from datasets import load_dataset
|
|
388
|
+
from semhash import SemHash
|
|
389
|
+
from vicinity import Backend
|
|
390
|
+
|
|
391
|
+
# Load a dataset to deduplicate
|
|
392
|
+
texts = load_dataset("ag_news", split="train")["text"]
|
|
393
|
+
|
|
394
|
+
# Initialize a SemHash with the model and custom ann backend and custom args
|
|
395
|
+
semhash = SemHash.from_records(records=texts, ann_backend=Backend.FAISS, nlist=50)
|
|
396
|
+
|
|
397
|
+
# Deduplicate the texts
|
|
398
|
+
deduplicated_texts = semhash.self_deduplicate()
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
For the full list of supported ANN backends and args, see the [Vicinity docs](https://github.com/MinishLab/vicinity/tree/main?tab=readme-ov-file#supported-backends).
|
|
402
|
+
|
|
403
|
+
</details>
|
|
404
|
+
|
|
405
|
+
|
|
377
406
|
<details>
|
|
378
407
|
<summary> Using Pandas DataFrames </summary>
|
|
379
408
|
<br>
|
|
@@ -473,7 +502,7 @@ To run the benchmarks yourself, you can use the following command (assuming you
|
|
|
473
502
|
```bash
|
|
474
503
|
python -m benchmarks.run_benchmarks
|
|
475
504
|
```
|
|
476
|
-
Optionally, the datasets can be updated in the [
|
|
505
|
+
Optionally, the datasets can be updated in the [data.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/data.py) file.
|
|
477
506
|
|
|
478
507
|
## License
|
|
479
508
|
|
|
@@ -484,9 +513,12 @@ MIT
|
|
|
484
513
|
If you use SemHash in your research, please cite the following:
|
|
485
514
|
```bibtex
|
|
486
515
|
@software{minishlab2025semhash,
|
|
487
|
-
author
|
|
488
|
-
title
|
|
489
|
-
year
|
|
490
|
-
|
|
516
|
+
author = {{van Dongen}, Thomas and Stephan Tulkens},
|
|
517
|
+
title = {SemHash: Fast Semantic Text Deduplication \& Filtering},
|
|
518
|
+
year = {2025},
|
|
519
|
+
publisher = {Zenodo},
|
|
520
|
+
doi = {10.5281/zenodo.17265942},
|
|
521
|
+
url = {https://github.com/MinishLab/semhash},
|
|
522
|
+
license = {MIT}
|
|
491
523
|
}
|
|
492
524
|
```
|