semhash 0.3.0__tar.gz → 0.3.2__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.0 → semhash-0.3.2}/.gitignore +0 -2
- {semhash-0.3.0 → semhash-0.3.2}/PKG-INFO +45 -10
- {semhash-0.3.0 → semhash-0.3.2}/README.md +43 -8
- semhash-0.3.2/assets/images/semhash_logo_v2.png +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/pyproject.toml +1 -1
- {semhash-0.3.0 → semhash-0.3.2}/semhash/datamodels.py +62 -3
- {semhash-0.3.0 → semhash-0.3.2}/semhash/index.py +8 -2
- {semhash-0.3.0 → semhash-0.3.2}/semhash/records.py +0 -7
- {semhash-0.3.0 → semhash-0.3.2}/semhash/semhash.py +14 -7
- {semhash-0.3.0 → semhash-0.3.2}/semhash/utils.py +6 -0
- {semhash-0.3.0 → semhash-0.3.2}/semhash/version.py +1 -1
- {semhash-0.3.0 → semhash-0.3.2}/semhash.egg-info/PKG-INFO +45 -10
- {semhash-0.3.0 → semhash-0.3.2}/semhash.egg-info/SOURCES.txt +1 -0
- {semhash-0.3.0 → semhash-0.3.2}/semhash.egg-info/requires.txt +1 -1
- semhash-0.3.2/tests/test_datamodels.py +221 -0
- {semhash-0.3.0 → semhash-0.3.2}/uv.lock +812 -811
- semhash-0.3.0/tests/test_datamodels.py +0 -118
- {semhash-0.3.0 → semhash-0.3.2}/.github/workflows/ci.yaml +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/.pre-commit-config.yaml +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/LICENSE +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/Makefile +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/assets/images/semhash_logo.png +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/benchmarks/__init__.py +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/benchmarks/data.py +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/benchmarks/results/train_benchmark_results.json +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/benchmarks/results/train_test_benchmark_results.json +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/benchmarks/run_benchmarks.py +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/semhash/__init__.py +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/semhash.egg-info/dependency_links.txt +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/semhash.egg-info/top_level.txt +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/setup.cfg +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/tests/conftest.py +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/tests/data/test_model/README.md +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/tests/data/test_model/config.json +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/tests/data/test_model/model.safetensors +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/tests/data/test_model/modules.json +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/tests/data/test_model/tokenizer.json +0 -0
- {semhash-0.3.0 → semhash-0.3.2}/tests/test_semhash.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: semhash
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.2
|
|
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"
|
|
@@ -56,21 +56,23 @@ Requires-Dist: ruff; extra == "dev"
|
|
|
56
56
|
|
|
57
57
|
|
|
58
58
|
|
|
59
|
-
<
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
</div>
|
|
59
|
+
<h2 align="center">
|
|
60
|
+
<img width="30%" alt="SemHash logo" src="assets/images/semhash_logo_v2.png"><br/>
|
|
61
|
+
Fast Semantic Text Deduplication & Filtering
|
|
62
|
+
</h2>
|
|
63
|
+
|
|
65
64
|
|
|
66
65
|
<div align="center">
|
|
67
66
|
|
|
68
|
-
|
|
67
|
+
|
|
69
68
|
|
|
70
69
|
|
|
71
70
|
<h2>
|
|
72
71
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/v/semhash?color=%23007ec6&label=pypi%20package" alt="Package version"></a>
|
|
73
72
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/pyversions/semhash" alt="Supported Python versions"></a>
|
|
73
|
+
<a href="https://pepy.tech/project/semhash">
|
|
74
|
+
<img src="https://static.pepy.tech/badge/semhash" alt="Downloads">
|
|
75
|
+
</a>
|
|
74
76
|
<a href="https://app.codecov.io/gh/MinishLab/semhash">
|
|
75
77
|
<img src="https://codecov.io/gh/MinishLab/semhash/graph/badge.svg?token=YPOD6HD0MG" alt="Codecov">
|
|
76
78
|
</a>
|
|
@@ -318,6 +320,10 @@ least_similar = deduplication_result.get_least_similar_from_duplicates()
|
|
|
318
320
|
|
|
319
321
|
# Rethreshold the duplicates. This allows you to instantly rethreshold the duplicates with a new threshold without having to re-deduplicate the texts.
|
|
320
322
|
deduplication_result.rethreshold(0.95)
|
|
323
|
+
|
|
324
|
+
# View selected records along with their duplicates.
|
|
325
|
+
# This is the opposite of the `filtered` attribute, which shows for every duplicate the record that caused it.
|
|
326
|
+
deduplication_result.selected_with_duplicates
|
|
321
327
|
```
|
|
322
328
|
|
|
323
329
|
</details>
|
|
@@ -368,6 +374,35 @@ deduplicated_texts = semhash.self_deduplicate()
|
|
|
368
374
|
|
|
369
375
|
</details>
|
|
370
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
|
+
|
|
371
406
|
<details>
|
|
372
407
|
<summary> Using Pandas DataFrames </summary>
|
|
373
408
|
<br>
|
|
@@ -467,7 +502,7 @@ To run the benchmarks yourself, you can use the following command (assuming you
|
|
|
467
502
|
```bash
|
|
468
503
|
python -m benchmarks.run_benchmarks
|
|
469
504
|
```
|
|
470
|
-
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.
|
|
471
506
|
|
|
472
507
|
## License
|
|
473
508
|
|
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
<
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
</div>
|
|
3
|
+
<h2 align="center">
|
|
4
|
+
<img width="30%" alt="SemHash logo" src="assets/images/semhash_logo_v2.png"><br/>
|
|
5
|
+
Fast Semantic Text Deduplication & Filtering
|
|
6
|
+
</h2>
|
|
7
|
+
|
|
9
8
|
|
|
10
9
|
<div align="center">
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
|
|
13
12
|
|
|
14
13
|
|
|
15
14
|
<h2>
|
|
16
15
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/v/semhash?color=%23007ec6&label=pypi%20package" alt="Package version"></a>
|
|
17
16
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/pyversions/semhash" alt="Supported Python versions"></a>
|
|
17
|
+
<a href="https://pepy.tech/project/semhash">
|
|
18
|
+
<img src="https://static.pepy.tech/badge/semhash" alt="Downloads">
|
|
19
|
+
</a>
|
|
18
20
|
<a href="https://app.codecov.io/gh/MinishLab/semhash">
|
|
19
21
|
<img src="https://codecov.io/gh/MinishLab/semhash/graph/badge.svg?token=YPOD6HD0MG" alt="Codecov">
|
|
20
22
|
</a>
|
|
@@ -262,6 +264,10 @@ least_similar = deduplication_result.get_least_similar_from_duplicates()
|
|
|
262
264
|
|
|
263
265
|
# Rethreshold the duplicates. This allows you to instantly rethreshold the duplicates with a new threshold without having to re-deduplicate the texts.
|
|
264
266
|
deduplication_result.rethreshold(0.95)
|
|
267
|
+
|
|
268
|
+
# View selected records along with their duplicates.
|
|
269
|
+
# This is the opposite of the `filtered` attribute, which shows for every duplicate the record that caused it.
|
|
270
|
+
deduplication_result.selected_with_duplicates
|
|
265
271
|
```
|
|
266
272
|
|
|
267
273
|
</details>
|
|
@@ -312,6 +318,35 @@ deduplicated_texts = semhash.self_deduplicate()
|
|
|
312
318
|
|
|
313
319
|
</details>
|
|
314
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
|
+
|
|
315
350
|
<details>
|
|
316
351
|
<summary> Using Pandas DataFrames </summary>
|
|
317
352
|
<br>
|
|
@@ -411,7 +446,7 @@ To run the benchmarks yourself, you can use the following command (assuming you
|
|
|
411
446
|
```bash
|
|
412
447
|
python -m benchmarks.run_benchmarks
|
|
413
448
|
```
|
|
414
|
-
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.
|
|
415
450
|
|
|
416
451
|
## License
|
|
417
452
|
|
|
Binary file
|
|
@@ -1,8 +1,17 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
import warnings
|
|
4
|
+
from collections import defaultdict
|
|
2
5
|
from dataclasses import dataclass, field
|
|
3
|
-
from typing import Generic, TypeVar
|
|
6
|
+
from typing import Any, Generic, Hashable, Sequence, TypeVar
|
|
7
|
+
|
|
8
|
+
from frozendict import frozendict
|
|
9
|
+
from typing_extensions import TypeAlias
|
|
10
|
+
|
|
11
|
+
from semhash.utils import to_frozendict
|
|
4
12
|
|
|
5
|
-
Record = TypeVar("Record", str, dict[str,
|
|
13
|
+
Record = TypeVar("Record", str, dict[str, Any])
|
|
14
|
+
DuplicateList: TypeAlias = list[tuple[Record, float]]
|
|
6
15
|
|
|
7
16
|
|
|
8
17
|
@dataclass
|
|
@@ -20,13 +29,29 @@ class DuplicateRecord(Generic[Record]):
|
|
|
20
29
|
|
|
21
30
|
record: Record
|
|
22
31
|
exact: bool
|
|
23
|
-
duplicates:
|
|
32
|
+
duplicates: DuplicateList = field(default_factory=list)
|
|
24
33
|
|
|
25
34
|
def _rethreshold(self, threshold: float) -> None:
|
|
26
35
|
"""Rethreshold the duplicates."""
|
|
27
36
|
self.duplicates = [(d, score) for d, score in self.duplicates if score >= threshold]
|
|
28
37
|
|
|
29
38
|
|
|
39
|
+
@dataclass
|
|
40
|
+
class SelectedWithDuplicates(Generic[Record]):
|
|
41
|
+
"""
|
|
42
|
+
A record that has been selected along with its duplicates.
|
|
43
|
+
|
|
44
|
+
Attributes
|
|
45
|
+
----------
|
|
46
|
+
record: The original record being selected.
|
|
47
|
+
duplicates: List of tuples consisting of duplicate records and their associated scores.
|
|
48
|
+
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
record: Record
|
|
52
|
+
duplicates: DuplicateList = field(default_factory=list)
|
|
53
|
+
|
|
54
|
+
|
|
30
55
|
@dataclass
|
|
31
56
|
class DeduplicationResult(Generic[Record]):
|
|
32
57
|
"""
|
|
@@ -37,6 +62,7 @@ class DeduplicationResult(Generic[Record]):
|
|
|
37
62
|
selected: List of deduplicated records after removing duplicates.
|
|
38
63
|
filtered: List of DuplicateRecord objects containing details about duplicates of an original record.
|
|
39
64
|
threshold: The similarity threshold used for deduplication.
|
|
65
|
+
columns: Columns used for deduplication.
|
|
40
66
|
deduplicated: Deprecated, use selected instead.
|
|
41
67
|
duplicates: Deprecated, use filtered instead.
|
|
42
68
|
|
|
@@ -45,6 +71,7 @@ class DeduplicationResult(Generic[Record]):
|
|
|
45
71
|
selected: list[Record] = field(default_factory=list)
|
|
46
72
|
filtered: list[DuplicateRecord] = field(default_factory=list)
|
|
47
73
|
threshold: float = field(default=0.9)
|
|
74
|
+
columns: Sequence[str] | None = field(default=None)
|
|
48
75
|
deduplicated: list[Record] = field(default_factory=list) # Deprecated
|
|
49
76
|
duplicates: list[DuplicateRecord] = field(default_factory=list) # Deprecated
|
|
50
77
|
|
|
@@ -102,6 +129,38 @@ class DeduplicationResult(Generic[Record]):
|
|
|
102
129
|
self.selected.append(dup.record)
|
|
103
130
|
self.threshold = threshold
|
|
104
131
|
|
|
132
|
+
@property
|
|
133
|
+
def selected_with_duplicates(self) -> list[SelectedWithDuplicates[Record]]:
|
|
134
|
+
"""
|
|
135
|
+
For every kept record, return the duplicates that were removed along with their similarity scores.
|
|
136
|
+
|
|
137
|
+
:return: A list of tuples where each tuple contains a kept record
|
|
138
|
+
and a list of its duplicates with their similarity scores.
|
|
139
|
+
"""
|
|
140
|
+
|
|
141
|
+
def _to_hashable(record: Record) -> frozendict[str, str] | str:
|
|
142
|
+
"""Convert a record to a hashable representation."""
|
|
143
|
+
if isinstance(record, dict) and self.columns is not None:
|
|
144
|
+
# Convert dict to frozendict for immutability and hashability
|
|
145
|
+
return to_frozendict(record, set(self.columns))
|
|
146
|
+
return str(record)
|
|
147
|
+
|
|
148
|
+
# Build a mapping from original-record to [(duplicate, score), …]
|
|
149
|
+
buckets: defaultdict[Hashable, DuplicateList] = defaultdict(list)
|
|
150
|
+
for duplicate_record in self.filtered:
|
|
151
|
+
for original_record, score in duplicate_record.duplicates:
|
|
152
|
+
buckets[_to_hashable(original_record)].append((duplicate_record.record, float(score)))
|
|
153
|
+
|
|
154
|
+
result: list[SelectedWithDuplicates[Record]] = []
|
|
155
|
+
for selected in self.selected:
|
|
156
|
+
# Get the list of duplicates for the selected record
|
|
157
|
+
raw_list = buckets.get(_to_hashable(selected), [])
|
|
158
|
+
# Ensure we don't have duplicates in the list
|
|
159
|
+
deduped = {_to_hashable(rec): (rec, score) for rec, score in raw_list}
|
|
160
|
+
result.append(SelectedWithDuplicates(record=selected, duplicates=list(deduped.values())))
|
|
161
|
+
|
|
162
|
+
return result
|
|
163
|
+
|
|
105
164
|
|
|
106
165
|
@dataclass
|
|
107
166
|
class FilterResult(Generic[Record]):
|
|
@@ -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
|
|
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
from typing import Sequence
|
|
2
2
|
|
|
3
|
-
from frozendict import frozendict
|
|
4
|
-
|
|
5
3
|
from semhash.datamodels import DeduplicationResult, DuplicateRecord
|
|
6
4
|
|
|
7
5
|
|
|
8
|
-
def to_frozendict(record: dict[str, str], columns: set[str]) -> frozendict[str, str]:
|
|
9
|
-
"""Convert a record to a frozendict."""
|
|
10
|
-
return frozendict({k: record.get(k, "") for k in columns})
|
|
11
|
-
|
|
12
|
-
|
|
13
6
|
def dict_to_string(record: dict[str, str], columns: Sequence[str]) -> str:
|
|
14
7
|
r"""
|
|
15
8
|
Turn a record into a single string.
|
|
@@ -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
|
|
@@ -11,8 +11,8 @@ from vicinity import Backend
|
|
|
11
11
|
|
|
12
12
|
from semhash.datamodels import DeduplicationResult, DuplicateRecord, FilterResult, Record
|
|
13
13
|
from semhash.index import Index
|
|
14
|
-
from semhash.records import add_scores_to_records, map_deduplication_result_to_strings
|
|
15
|
-
from semhash.utils import Encoder, compute_candidate_limit
|
|
14
|
+
from semhash.records import add_scores_to_records, map_deduplication_result_to_strings
|
|
15
|
+
from semhash.utils import Encoder, compute_candidate_limit, to_frozendict
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
class SemHash(Generic[Record]):
|
|
@@ -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)
|
|
@@ -190,7 +195,9 @@ class SemHash(Generic[Record]):
|
|
|
190
195
|
|
|
191
196
|
# If no records are left after removing exact duplicates, return early
|
|
192
197
|
if not dict_records:
|
|
193
|
-
return DeduplicationResult(
|
|
198
|
+
return DeduplicationResult(
|
|
199
|
+
deduplicated=[], duplicates=duplicate_records, threshold=threshold, columns=self.columns
|
|
200
|
+
)
|
|
194
201
|
|
|
195
202
|
# Compute embeddings for the new records
|
|
196
203
|
embeddings = self._featurize(records=dict_records, columns=self.columns, model=self.model)
|
|
@@ -212,7 +219,7 @@ class SemHash(Generic[Record]):
|
|
|
212
219
|
)
|
|
213
220
|
|
|
214
221
|
result = DeduplicationResult(
|
|
215
|
-
deduplicated=deduplicated_records, duplicates=duplicate_records, threshold=threshold
|
|
222
|
+
deduplicated=deduplicated_records, duplicates=duplicate_records, threshold=threshold, columns=self.columns
|
|
216
223
|
)
|
|
217
224
|
|
|
218
225
|
if self._was_string:
|
|
@@ -281,7 +288,7 @@ class SemHash(Generic[Record]):
|
|
|
281
288
|
seen_items.update(frozen_items)
|
|
282
289
|
|
|
283
290
|
result = DeduplicationResult(
|
|
284
|
-
deduplicated=deduplicated_records, duplicates=duplicate_records, threshold=threshold
|
|
291
|
+
deduplicated=deduplicated_records, duplicates=duplicate_records, threshold=threshold, columns=self.columns
|
|
285
292
|
)
|
|
286
293
|
|
|
287
294
|
if self._was_string:
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from typing import Any, Protocol, Sequence, Union
|
|
2
2
|
|
|
3
3
|
import numpy as np
|
|
4
|
+
from frozendict import frozendict
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
class Encoder(Protocol):
|
|
@@ -21,6 +22,11 @@ class Encoder(Protocol):
|
|
|
21
22
|
... # pragma: no cover
|
|
22
23
|
|
|
23
24
|
|
|
25
|
+
def to_frozendict(record: dict[str, str], columns: set[str]) -> frozendict[str, str]:
|
|
26
|
+
"""Convert a record to a frozendict."""
|
|
27
|
+
return frozendict({k: record.get(k, "") for k in columns})
|
|
28
|
+
|
|
29
|
+
|
|
24
30
|
def compute_candidate_limit(
|
|
25
31
|
total: int,
|
|
26
32
|
selection_size: int,
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
__version_triple__ = (0, 3,
|
|
1
|
+
__version_triple__ = (0, 3, 2)
|
|
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.2
|
|
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"
|
|
@@ -56,21 +56,23 @@ Requires-Dist: ruff; extra == "dev"
|
|
|
56
56
|
|
|
57
57
|
|
|
58
58
|
|
|
59
|
-
<
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
</div>
|
|
59
|
+
<h2 align="center">
|
|
60
|
+
<img width="30%" alt="SemHash logo" src="assets/images/semhash_logo_v2.png"><br/>
|
|
61
|
+
Fast Semantic Text Deduplication & Filtering
|
|
62
|
+
</h2>
|
|
63
|
+
|
|
65
64
|
|
|
66
65
|
<div align="center">
|
|
67
66
|
|
|
68
|
-
|
|
67
|
+
|
|
69
68
|
|
|
70
69
|
|
|
71
70
|
<h2>
|
|
72
71
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/v/semhash?color=%23007ec6&label=pypi%20package" alt="Package version"></a>
|
|
73
72
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/pyversions/semhash" alt="Supported Python versions"></a>
|
|
73
|
+
<a href="https://pepy.tech/project/semhash">
|
|
74
|
+
<img src="https://static.pepy.tech/badge/semhash" alt="Downloads">
|
|
75
|
+
</a>
|
|
74
76
|
<a href="https://app.codecov.io/gh/MinishLab/semhash">
|
|
75
77
|
<img src="https://codecov.io/gh/MinishLab/semhash/graph/badge.svg?token=YPOD6HD0MG" alt="Codecov">
|
|
76
78
|
</a>
|
|
@@ -318,6 +320,10 @@ least_similar = deduplication_result.get_least_similar_from_duplicates()
|
|
|
318
320
|
|
|
319
321
|
# Rethreshold the duplicates. This allows you to instantly rethreshold the duplicates with a new threshold without having to re-deduplicate the texts.
|
|
320
322
|
deduplication_result.rethreshold(0.95)
|
|
323
|
+
|
|
324
|
+
# View selected records along with their duplicates.
|
|
325
|
+
# This is the opposite of the `filtered` attribute, which shows for every duplicate the record that caused it.
|
|
326
|
+
deduplication_result.selected_with_duplicates
|
|
321
327
|
```
|
|
322
328
|
|
|
323
329
|
</details>
|
|
@@ -368,6 +374,35 @@ deduplicated_texts = semhash.self_deduplicate()
|
|
|
368
374
|
|
|
369
375
|
</details>
|
|
370
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
|
+
|
|
371
406
|
<details>
|
|
372
407
|
<summary> Using Pandas DataFrames </summary>
|
|
373
408
|
<br>
|
|
@@ -467,7 +502,7 @@ To run the benchmarks yourself, you can use the following command (assuming you
|
|
|
467
502
|
```bash
|
|
468
503
|
python -m benchmarks.run_benchmarks
|
|
469
504
|
```
|
|
470
|
-
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.
|
|
471
506
|
|
|
472
507
|
## License
|
|
473
508
|
|