semhash 0.2.0__tar.gz → 0.3.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.
- {semhash-0.2.0 → semhash-0.3.0}/.gitignore +2 -0
- {semhash-0.2.0 → semhash-0.3.0}/PKG-INFO +130 -33
- {semhash-0.2.0 → semhash-0.3.0}/README.md +127 -29
- {semhash-0.2.0 → semhash-0.3.0}/benchmarks/run_benchmarks.py +2 -2
- {semhash-0.2.0 → semhash-0.3.0}/pyproject.toml +2 -1
- semhash-0.3.0/semhash/datamodels.py +135 -0
- {semhash-0.2.0 → semhash-0.3.0}/semhash/index.py +20 -4
- {semhash-0.2.0 → semhash-0.3.0}/semhash/records.py +7 -2
- semhash-0.3.0/semhash/semhash.py +584 -0
- semhash-0.3.0/semhash/utils.py +49 -0
- {semhash-0.2.0 → semhash-0.3.0}/semhash/version.py +1 -1
- {semhash-0.2.0 → semhash-0.3.0}/semhash.egg-info/PKG-INFO +130 -33
- {semhash-0.2.0 → semhash-0.3.0}/semhash.egg-info/SOURCES.txt +1 -0
- semhash-0.3.0/tests/conftest.py +57 -0
- semhash-0.3.0/tests/data/test_model/README.md +97 -0
- semhash-0.3.0/tests/data/test_model/config.json +12 -0
- semhash-0.3.0/tests/data/test_model/model.safetensors +0 -0
- semhash-0.3.0/tests/data/test_model/modules.json +14 -0
- {semhash-0.2.0 → semhash-0.3.0}/tests/data/test_model/tokenizer.json +2 -2
- {semhash-0.2.0 → semhash-0.3.0}/tests/test_datamodels.py +25 -2
- {semhash-0.2.0 → semhash-0.3.0}/tests/test_semhash.py +89 -9
- {semhash-0.2.0 → semhash-0.3.0}/uv.lock +3 -2
- semhash-0.2.0/semhash/datamodels.py +0 -80
- semhash-0.2.0/semhash/semhash.py +0 -277
- semhash-0.2.0/semhash/utils.py +0 -21
- semhash-0.2.0/tests/conftest.py +0 -14
- semhash-0.2.0/tests/data/test_model/README.md +0 -78
- semhash-0.2.0/tests/data/test_model/config.json +0 -1
- semhash-0.2.0/tests/data/test_model/model.safetensors +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/.github/workflows/ci.yaml +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/.pre-commit-config.yaml +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/LICENSE +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/Makefile +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/assets/images/semhash_logo.png +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/benchmarks/__init__.py +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/benchmarks/data.py +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/benchmarks/results/train_benchmark_results.json +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/benchmarks/results/train_test_benchmark_results.json +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/semhash/__init__.py +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/semhash.egg-info/dependency_links.txt +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/semhash.egg-info/requires.txt +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/semhash.egg-info/top_level.txt +0 -0
- {semhash-0.2.0 → semhash-0.3.0}/setup.cfg +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: semhash
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary: Fast Semantic Text Deduplication
|
|
3
|
+
Version: 0.3.0
|
|
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
|
|
7
7
|
|
|
@@ -42,7 +42,6 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
42
42
|
Classifier: Natural Language :: English
|
|
43
43
|
Requires-Python: >=3.9
|
|
44
44
|
Description-Content-Type: text/markdown
|
|
45
|
-
License-File: LICENSE
|
|
46
45
|
Requires-Dist: model2vec>=0.3.4
|
|
47
46
|
Requires-Dist: vicinity[usearch]>=0.4.0
|
|
48
47
|
Requires-Dist: frozendict
|
|
@@ -66,17 +65,23 @@ Requires-Dist: ruff; extra == "dev"
|
|
|
66
65
|
|
|
67
66
|
<div align="center">
|
|
68
67
|
|
|
69
|
-
# Fast Semantic Text Deduplication
|
|
68
|
+
# Fast Semantic Text Deduplication & Filtering
|
|
70
69
|
|
|
71
70
|
|
|
72
|
-
|
|
71
|
+
<h2>
|
|
73
72
|
<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>
|
|
74
73
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/pyversions/semhash" alt="Supported Python versions"></a>
|
|
75
74
|
<a href="https://app.codecov.io/gh/MinishLab/semhash">
|
|
76
|
-
|
|
75
|
+
<img src="https://codecov.io/gh/MinishLab/semhash/graph/badge.svg?token=YPOD6HD0MG" alt="Codecov">
|
|
77
76
|
</a>
|
|
78
|
-
<a href="https://
|
|
79
|
-
|
|
77
|
+
<a href="https://discord.gg/4BDPR5nmtK">
|
|
78
|
+
<img src="https://img.shields.io/badge/Join-Discord-5865F2?logo=discord&logoColor=white" alt="Join Discord">
|
|
79
|
+
</a>
|
|
80
|
+
<a href="https://github.com/MinishLab/semhash/blob/main/LICENSE">
|
|
81
|
+
<img src="https://img.shields.io/badge/license-MIT-green" alt="License - MIT">
|
|
82
|
+
</a>
|
|
83
|
+
</h2>
|
|
84
|
+
|
|
80
85
|
|
|
81
86
|
|
|
82
87
|
[Quickstart](#quickstart) •
|
|
@@ -87,9 +92,9 @@ Requires-Dist: ruff; extra == "dev"
|
|
|
87
92
|
</div>
|
|
88
93
|
|
|
89
94
|
|
|
90
|
-
SemHash is a lightweight and flexible tool for deduplicating datasets using semantic similarity. It combines fast embedding generation from [Model2Vec](https://github.com/MinishLab/model2vec) with efficient ANN-based similarity search through [Vicinity](https://github.com/MinishLab/vicinity).
|
|
95
|
+
SemHash is a lightweight and flexible tool for deduplicating datasets, filtering outliers, and finding representative samples using semantic similarity. It combines fast embedding generation from [Model2Vec](https://github.com/MinishLab/model2vec) with efficient ANN-based similarity search through [Vicinity](https://github.com/MinishLab/vicinity).
|
|
91
96
|
|
|
92
|
-
SemHash supports both single-dataset deduplication (e.g., cleaning up a train set) and multi-dataset deduplication (e.g., ensuring no overlap between a test set and a train set). It works with simple datasets, such as text lists, and more complex ones, like multi-column QA datasets. Additionally, it includes functions to inspect deduplication results, making it easier to understand and refine your data cleaning process.
|
|
97
|
+
SemHash supports both single-dataset deduplication & filtering (e.g., cleaning up a train set by removing duplicates and outliers) and multi-dataset deduplication & filtering (e.g., ensuring no overlap between a test set and a train set). It works with simple datasets, such as text lists, and more complex ones, like multi-column QA datasets. Additionally, it includes functions to inspect deduplication results, making it easier to understand and refine your data cleaning process.
|
|
93
98
|
|
|
94
99
|
## Quickstart
|
|
95
100
|
|
|
@@ -98,7 +103,7 @@ Install the package with:
|
|
|
98
103
|
pip install semhash
|
|
99
104
|
```
|
|
100
105
|
|
|
101
|
-
Deduplicate a single dataset with the following code (note: the examples assume you have `datasets` installed, which you can install with `pip install datasets`):
|
|
106
|
+
Deduplicate a single dataset, filter outliers, and find representative samples with the following code (note: the examples assume you have `datasets` installed, which you can install with `pip install datasets`):
|
|
102
107
|
|
|
103
108
|
```python
|
|
104
109
|
from datasets import load_dataset
|
|
@@ -111,10 +116,16 @@ texts = load_dataset("ag_news", split="train")["text"]
|
|
|
111
116
|
semhash = SemHash.from_records(records=texts)
|
|
112
117
|
|
|
113
118
|
# Deduplicate the texts
|
|
114
|
-
deduplicated_texts = semhash.self_deduplicate().
|
|
119
|
+
deduplicated_texts = semhash.self_deduplicate().selected
|
|
120
|
+
|
|
121
|
+
# Filter outliers
|
|
122
|
+
filtered_texts = semhash.self_filter_outliers().selected
|
|
123
|
+
|
|
124
|
+
# Find representative texts
|
|
125
|
+
representative_texts = semhash.self_find_representative().selected
|
|
115
126
|
```
|
|
116
127
|
|
|
117
|
-
Or, deduplicate across two datasets with the following code (e.g., eliminating train/test leakage):
|
|
128
|
+
Or, deduplicate across two datasets, filter outliers, and find representative samples with the following code (e.g., eliminating train/test leakage):
|
|
118
129
|
|
|
119
130
|
```python
|
|
120
131
|
from datasets import load_dataset
|
|
@@ -127,11 +138,22 @@ test_texts = load_dataset("ag_news", split="test")["text"]
|
|
|
127
138
|
# Initialize a SemHash instance with the training data
|
|
128
139
|
semhash = SemHash.from_records(records=train_texts)
|
|
129
140
|
|
|
130
|
-
# Deduplicate the test data against the training data
|
|
131
|
-
deduplicated_test_texts = semhash.deduplicate(records=test_texts).
|
|
141
|
+
# Deduplicate the test data against the training data, optionally with a specific threshold
|
|
142
|
+
deduplicated_test_texts = semhash.deduplicate(records=test_texts, threshold=0.9).selected
|
|
143
|
+
|
|
144
|
+
# Filter outliers from the test data against the training data,
|
|
145
|
+
# optionally with a specific percentage
|
|
146
|
+
filtered_test_texts = semhash.filter_outliers(records=test_texts, outlier_percentage=0.1).selected
|
|
147
|
+
|
|
148
|
+
# Find representative texts in the test data against the training data,
|
|
149
|
+
# optionally with a specific selection size
|
|
150
|
+
representative_test_texts = semhash.find_representative(
|
|
151
|
+
records=test_texts, selection_size=10).selected
|
|
152
|
+
|
|
153
|
+
|
|
132
154
|
```
|
|
133
155
|
|
|
134
|
-
Or, deduplicate multi-column
|
|
156
|
+
Or, deduplicate multi-column dataset, filter outliers, and find representative samples with the following code (e.g., deduplicating a QA dataset):
|
|
135
157
|
|
|
136
158
|
```python
|
|
137
159
|
from datasets import load_dataset
|
|
@@ -147,27 +169,37 @@ records = [dict(row) for row in dataset]
|
|
|
147
169
|
semhash = SemHash.from_records(records=records, columns=["question", "context"])
|
|
148
170
|
|
|
149
171
|
# Deduplicate the records
|
|
150
|
-
deduplicated_records = semhash.self_deduplicate().
|
|
172
|
+
deduplicated_records = semhash.self_deduplicate().selected
|
|
173
|
+
|
|
174
|
+
# Filter outliers from the records
|
|
175
|
+
filtered_texts = semhash.self_filter_outliers().selected
|
|
176
|
+
|
|
177
|
+
# Find representative texts in the records
|
|
178
|
+
representative_texts = semhash.self_find_representative().selected
|
|
151
179
|
```
|
|
152
180
|
|
|
153
|
-
The `deduplicate` and `self_deduplicate` functions return a [DeduplicationResult](https://github.com/MinishLab/semhash/blob/
|
|
181
|
+
The `deduplicate` and `self_deduplicate` functions return a [DeduplicationResult](https://github.com/MinishLab/semhash/blob/main/semhash/datamodels.py#L30). This object stores the deduplicated corpus, a set of duplicate object (along with the objects that caused duplication), and several useful functions to further inspect the deduplication result. Examples of how these functions can be used can be found in the [usage](#usage) section.
|
|
182
|
+
|
|
183
|
+
The `filter_outliers`, `self_filter_outliers`, `find_representative`, and `self_find_representative` functions return a [FilterResult](https://github.com/MinishLab/semhash/blob/main/semhash/datamodels.py#106). This object stores the found outliers/representative samples.
|
|
154
184
|
|
|
185
|
+
For both the `DeduplicationResult` and `FilterResult` objects, you can easily view the filtered records with the `selected` attribute (e.g. to view outliers: `outliers = semhash.self_filter_outliers().filtered`)
|
|
155
186
|
## Main Features
|
|
156
187
|
|
|
157
188
|
- **Fast**: SemHash uses [model2vec](https://github.com/MinishLab/model2vec) to embed texts and [vicinity](https://github.com/MinishLab/vicinity) to perform similarity search, making it extremely fast.
|
|
158
|
-
- **Scalable**: SemHash can deduplicate large datasets with millions of records thanks to the ANN backends in Vicinity.
|
|
159
|
-
- **Flexible**: SemHash can be used to deduplicate a single dataset or across two datasets, and can also be used to deduplicate multi-column datasets (such as QA datasets).
|
|
189
|
+
- **Scalable**: SemHash can deduplicate & filter large datasets with millions of records thanks to the ANN backends in Vicinity.
|
|
190
|
+
- **Flexible**: SemHash can be used to deduplicate & filter a single dataset or across two datasets, and can also be used to deduplicate & filter multi-column datasets (such as QA datasets).
|
|
160
191
|
- **Lightweight**: SemHash is a lightweight package with minimal dependencies, making it easy to install and use.
|
|
192
|
+
- **Explainable**: Easily inspect the duplicates and what caused them with the `DeduplicationResult` object. You can also view the lowest similarity duplicates to find the right threshold for deduplication for your dataset.
|
|
161
193
|
|
|
162
194
|
## Usage
|
|
163
195
|
|
|
164
|
-
The following examples show the various ways you can use SemHash to deduplicate datasets. These examples assume you have the `datasets` library installed, which you can install with `pip install datasets`.
|
|
196
|
+
The following examples show the various ways you can use SemHash to deduplicate datasets, filter outliers, and find representative samples. These examples assume you have the `datasets` library installed, which you can install with `pip install datasets`.
|
|
165
197
|
|
|
166
198
|
<details>
|
|
167
|
-
<summary> Deduplicate a single dataset </summary>
|
|
199
|
+
<summary> Deduplicate, filter outliers, and find representative samples on a single dataset </summary>
|
|
168
200
|
<br>
|
|
169
201
|
|
|
170
|
-
The following code snippet shows how to deduplicate a single dataset using SemHash (in this example, the train split of the [AG News dataset](https://huggingface.co/datasets/fancyzhx/ag_news)):
|
|
202
|
+
The following code snippet shows how to deduplicate a single dataset, filter outliers, and find representative samples using SemHash (in this example, the train split of the [AG News dataset](https://huggingface.co/datasets/fancyzhx/ag_news)):
|
|
171
203
|
|
|
172
204
|
```python
|
|
173
205
|
from datasets import load_dataset
|
|
@@ -180,15 +212,21 @@ texts = load_dataset("ag_news", split="train")["text"]
|
|
|
180
212
|
semhash = SemHash.from_records(records=texts)
|
|
181
213
|
|
|
182
214
|
# Deduplicate the texts
|
|
183
|
-
deduplicated_texts = semhash.self_deduplicate()
|
|
215
|
+
deduplicated_texts = semhash.self_deduplicate().selected
|
|
216
|
+
|
|
217
|
+
# Filter outliers
|
|
218
|
+
filtered_texts = semhash.self_filter_outliers().selected
|
|
219
|
+
|
|
220
|
+
# Find representative texts
|
|
221
|
+
representative_texts = semhash.self_find_representative().selected
|
|
184
222
|
```
|
|
185
223
|
</details>
|
|
186
224
|
|
|
187
225
|
<details>
|
|
188
|
-
<summary> Deduplicate across two datasets </summary>
|
|
226
|
+
<summary> Deduplicate, filter outliers, and find representative samples across two datasets </summary>
|
|
189
227
|
<br>
|
|
190
228
|
|
|
191
|
-
The following code snippet shows how to deduplicate across two datasets using SemHash (in this example, the train/test split of the [AG News dataset](https://huggingface.co/datasets/fancyzhx/ag_news)):
|
|
229
|
+
The following code snippet shows how to deduplicate across two datasets, filter outliers, and find representative samples using SemHash (in this example, the train/test split of the [AG News dataset](https://huggingface.co/datasets/fancyzhx/ag_news)):
|
|
192
230
|
|
|
193
231
|
```python
|
|
194
232
|
from datasets import load_dataset
|
|
@@ -205,16 +243,22 @@ test_texts = load_dataset("ag_news", split="test")["text"]
|
|
|
205
243
|
semhash = SemHash.from_records(records=train_texts)
|
|
206
244
|
|
|
207
245
|
# Deduplicate the test data against the training data
|
|
208
|
-
deduplicated_test_texts = semhash.deduplicate(records=test_texts)
|
|
246
|
+
deduplicated_test_texts = semhash.deduplicate(records=test_texts).selected
|
|
247
|
+
|
|
248
|
+
# Filter outliers from the test data
|
|
249
|
+
filtered_test_texts = semhash.filter_outliers(records=test_texts).selected
|
|
250
|
+
|
|
251
|
+
# Find representative texts in the test data
|
|
252
|
+
representative_test_texts = semhash.find_representative(records=test_texts).selected
|
|
209
253
|
```
|
|
210
254
|
|
|
211
255
|
</details>
|
|
212
256
|
|
|
213
257
|
<details>
|
|
214
|
-
<summary> Deduplicate multi-column datasets </summary>
|
|
258
|
+
<summary> Deduplicate, filter outliers, and find representative samples on multi-column datasets </summary>
|
|
215
259
|
<br>
|
|
216
260
|
|
|
217
|
-
The following code snippet shows how to deduplicate multi-column datasets using SemHash (in this example, the train split of the QA dataset [SQuAD 2.0](https://huggingface.co/datasets/rajpurkar/squad_v2), which consists of questions, contexts, and answers):
|
|
261
|
+
The following code snippet shows how to deduplicate multi-column datasets, filter outliers, and find representative samples using SemHash (in this example, the train split of the QA dataset [SQuAD 2.0](https://huggingface.co/datasets/rajpurkar/squad_v2), which consists of questions, contexts, and answers):
|
|
218
262
|
|
|
219
263
|
```python
|
|
220
264
|
from datasets import load_dataset
|
|
@@ -230,7 +274,13 @@ records = [dict(row) for row in dataset]
|
|
|
230
274
|
semhash = SemHash.from_records(records=records, columns=["question", "context"])
|
|
231
275
|
|
|
232
276
|
# Deduplicate the records
|
|
233
|
-
deduplicated_records = semhash.self_deduplicate().
|
|
277
|
+
deduplicated_records = semhash.self_deduplicate().selected
|
|
278
|
+
|
|
279
|
+
# Filter outliers from the records
|
|
280
|
+
filtered_records = semhash.self_filter_outliers().selected
|
|
281
|
+
|
|
282
|
+
# Find representative samples in the records
|
|
283
|
+
representative_records = semhash.self_find_representative().selected
|
|
234
284
|
```
|
|
235
285
|
|
|
236
286
|
</details>
|
|
@@ -255,9 +305,9 @@ semhash = SemHash.from_records(records=texts)
|
|
|
255
305
|
deduplication_result = semhash.self_deduplicate()
|
|
256
306
|
|
|
257
307
|
# Check the deduplicated texts
|
|
258
|
-
deduplication_result.
|
|
308
|
+
deduplication_result.selected
|
|
259
309
|
# Check the duplicates
|
|
260
|
-
deduplication_result.
|
|
310
|
+
deduplication_result.filtered
|
|
261
311
|
# See what percentage of the texts were duplicates
|
|
262
312
|
deduplication_result.duplicate_ratio
|
|
263
313
|
# See what percentage of the texts were exact duplicates
|
|
@@ -318,12 +368,43 @@ deduplicated_texts = semhash.self_deduplicate()
|
|
|
318
368
|
|
|
319
369
|
</details>
|
|
320
370
|
|
|
371
|
+
<details>
|
|
372
|
+
<summary> Using Pandas DataFrames </summary>
|
|
373
|
+
<br>
|
|
374
|
+
|
|
375
|
+
You can easily use Pandas DataFrames with SemHash. The following code snippet shows how to deduplicate a Pandas DataFrame:
|
|
376
|
+
|
|
377
|
+
```python
|
|
378
|
+
import pandas as pd
|
|
379
|
+
from datasets import load_dataset
|
|
380
|
+
from semhash import SemHash
|
|
381
|
+
|
|
382
|
+
# Load a dataset as a pandas dataframe
|
|
383
|
+
dataframe = load_dataset("ag_news", split="train").to_pandas()
|
|
384
|
+
|
|
385
|
+
# Convert the dataframe to a list of dictionaries
|
|
386
|
+
dataframe = dataframe.to_dict(orient="records")
|
|
387
|
+
|
|
388
|
+
# Initialize a SemHash instance with the columns to deduplicate
|
|
389
|
+
semhash = SemHash.from_records(records=dataframe, columns=["text"])
|
|
390
|
+
|
|
391
|
+
# Deduplicate the texts
|
|
392
|
+
deduplicated_records = semhash.self_deduplicate().selected
|
|
393
|
+
|
|
394
|
+
# Convert the deduplicated records back to a pandas dataframe
|
|
395
|
+
deduplicated_dataframe = pd.DataFrame(deduplicated_records)
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
</details>
|
|
399
|
+
|
|
321
400
|
NOTE: By default, we use the ANN (approximate-nearest neighbors) backend for deduplication. We recommend keeping this since the recall for smaller datasets is ~100%, and it's needed for larger datasets (>1M samples) since these will take too long to deduplicate without ANN. If you want to use the flat/exact-matching backend, you can set `use_ann=False` in the SemHash constructor:
|
|
322
401
|
|
|
323
402
|
```python
|
|
324
403
|
semhash = SemHash.from_records(records=texts, use_ann=False)
|
|
325
404
|
```
|
|
326
405
|
|
|
406
|
+
|
|
407
|
+
|
|
327
408
|
## Benchmarks
|
|
328
409
|
|
|
329
410
|
We've benchmarked SemHash on a variety of datasets to measure the deduplication performance and speed. The benchmarks were run with the following setup:
|
|
@@ -387,3 +468,19 @@ To run the benchmarks yourself, you can use the following command (assuming you
|
|
|
387
468
|
python -m benchmarks.run_benchmarks
|
|
388
469
|
```
|
|
389
470
|
Optionally, the datasets can be updated in the [datasets.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/datasets.py) file.
|
|
471
|
+
|
|
472
|
+
## License
|
|
473
|
+
|
|
474
|
+
MIT
|
|
475
|
+
|
|
476
|
+
## Citing
|
|
477
|
+
|
|
478
|
+
If you use SemHash in your research, please cite the following:
|
|
479
|
+
```bibtex
|
|
480
|
+
@software{minishlab2025semhash,
|
|
481
|
+
author = {Thomas van Dongen and Stephan Tulkens},
|
|
482
|
+
title = {SemHash: Fast Semantic Text Deduplication & Filtering},
|
|
483
|
+
year = {2025},
|
|
484
|
+
url = {https://github.com/MinishLab/semhash}
|
|
485
|
+
}
|
|
486
|
+
```
|
|
@@ -9,17 +9,23 @@
|
|
|
9
9
|
|
|
10
10
|
<div align="center">
|
|
11
11
|
|
|
12
|
-
# Fast Semantic Text Deduplication
|
|
12
|
+
# Fast Semantic Text Deduplication & Filtering
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
<h2>
|
|
16
16
|
<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
17
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/pyversions/semhash" alt="Supported Python versions"></a>
|
|
18
18
|
<a href="https://app.codecov.io/gh/MinishLab/semhash">
|
|
19
|
-
|
|
19
|
+
<img src="https://codecov.io/gh/MinishLab/semhash/graph/badge.svg?token=YPOD6HD0MG" alt="Codecov">
|
|
20
20
|
</a>
|
|
21
|
-
<a href="https://
|
|
22
|
-
|
|
21
|
+
<a href="https://discord.gg/4BDPR5nmtK">
|
|
22
|
+
<img src="https://img.shields.io/badge/Join-Discord-5865F2?logo=discord&logoColor=white" alt="Join Discord">
|
|
23
|
+
</a>
|
|
24
|
+
<a href="https://github.com/MinishLab/semhash/blob/main/LICENSE">
|
|
25
|
+
<img src="https://img.shields.io/badge/license-MIT-green" alt="License - MIT">
|
|
26
|
+
</a>
|
|
27
|
+
</h2>
|
|
28
|
+
|
|
23
29
|
|
|
24
30
|
|
|
25
31
|
[Quickstart](#quickstart) •
|
|
@@ -30,9 +36,9 @@
|
|
|
30
36
|
</div>
|
|
31
37
|
|
|
32
38
|
|
|
33
|
-
SemHash is a lightweight and flexible tool for deduplicating datasets using semantic similarity. It combines fast embedding generation from [Model2Vec](https://github.com/MinishLab/model2vec) with efficient ANN-based similarity search through [Vicinity](https://github.com/MinishLab/vicinity).
|
|
39
|
+
SemHash is a lightweight and flexible tool for deduplicating datasets, filtering outliers, and finding representative samples using semantic similarity. It combines fast embedding generation from [Model2Vec](https://github.com/MinishLab/model2vec) with efficient ANN-based similarity search through [Vicinity](https://github.com/MinishLab/vicinity).
|
|
34
40
|
|
|
35
|
-
SemHash supports both single-dataset deduplication (e.g., cleaning up a train set) and multi-dataset deduplication (e.g., ensuring no overlap between a test set and a train set). It works with simple datasets, such as text lists, and more complex ones, like multi-column QA datasets. Additionally, it includes functions to inspect deduplication results, making it easier to understand and refine your data cleaning process.
|
|
41
|
+
SemHash supports both single-dataset deduplication & filtering (e.g., cleaning up a train set by removing duplicates and outliers) and multi-dataset deduplication & filtering (e.g., ensuring no overlap between a test set and a train set). It works with simple datasets, such as text lists, and more complex ones, like multi-column QA datasets. Additionally, it includes functions to inspect deduplication results, making it easier to understand and refine your data cleaning process.
|
|
36
42
|
|
|
37
43
|
## Quickstart
|
|
38
44
|
|
|
@@ -41,7 +47,7 @@ Install the package with:
|
|
|
41
47
|
pip install semhash
|
|
42
48
|
```
|
|
43
49
|
|
|
44
|
-
Deduplicate a single dataset with the following code (note: the examples assume you have `datasets` installed, which you can install with `pip install datasets`):
|
|
50
|
+
Deduplicate a single dataset, filter outliers, and find representative samples with the following code (note: the examples assume you have `datasets` installed, which you can install with `pip install datasets`):
|
|
45
51
|
|
|
46
52
|
```python
|
|
47
53
|
from datasets import load_dataset
|
|
@@ -54,10 +60,16 @@ texts = load_dataset("ag_news", split="train")["text"]
|
|
|
54
60
|
semhash = SemHash.from_records(records=texts)
|
|
55
61
|
|
|
56
62
|
# Deduplicate the texts
|
|
57
|
-
deduplicated_texts = semhash.self_deduplicate().
|
|
63
|
+
deduplicated_texts = semhash.self_deduplicate().selected
|
|
64
|
+
|
|
65
|
+
# Filter outliers
|
|
66
|
+
filtered_texts = semhash.self_filter_outliers().selected
|
|
67
|
+
|
|
68
|
+
# Find representative texts
|
|
69
|
+
representative_texts = semhash.self_find_representative().selected
|
|
58
70
|
```
|
|
59
71
|
|
|
60
|
-
Or, deduplicate across two datasets with the following code (e.g., eliminating train/test leakage):
|
|
72
|
+
Or, deduplicate across two datasets, filter outliers, and find representative samples with the following code (e.g., eliminating train/test leakage):
|
|
61
73
|
|
|
62
74
|
```python
|
|
63
75
|
from datasets import load_dataset
|
|
@@ -70,11 +82,22 @@ test_texts = load_dataset("ag_news", split="test")["text"]
|
|
|
70
82
|
# Initialize a SemHash instance with the training data
|
|
71
83
|
semhash = SemHash.from_records(records=train_texts)
|
|
72
84
|
|
|
73
|
-
# Deduplicate the test data against the training data
|
|
74
|
-
deduplicated_test_texts = semhash.deduplicate(records=test_texts).
|
|
85
|
+
# Deduplicate the test data against the training data, optionally with a specific threshold
|
|
86
|
+
deduplicated_test_texts = semhash.deduplicate(records=test_texts, threshold=0.9).selected
|
|
87
|
+
|
|
88
|
+
# Filter outliers from the test data against the training data,
|
|
89
|
+
# optionally with a specific percentage
|
|
90
|
+
filtered_test_texts = semhash.filter_outliers(records=test_texts, outlier_percentage=0.1).selected
|
|
91
|
+
|
|
92
|
+
# Find representative texts in the test data against the training data,
|
|
93
|
+
# optionally with a specific selection size
|
|
94
|
+
representative_test_texts = semhash.find_representative(
|
|
95
|
+
records=test_texts, selection_size=10).selected
|
|
96
|
+
|
|
97
|
+
|
|
75
98
|
```
|
|
76
99
|
|
|
77
|
-
Or, deduplicate multi-column
|
|
100
|
+
Or, deduplicate multi-column dataset, filter outliers, and find representative samples with the following code (e.g., deduplicating a QA dataset):
|
|
78
101
|
|
|
79
102
|
```python
|
|
80
103
|
from datasets import load_dataset
|
|
@@ -90,27 +113,37 @@ records = [dict(row) for row in dataset]
|
|
|
90
113
|
semhash = SemHash.from_records(records=records, columns=["question", "context"])
|
|
91
114
|
|
|
92
115
|
# Deduplicate the records
|
|
93
|
-
deduplicated_records = semhash.self_deduplicate().
|
|
116
|
+
deduplicated_records = semhash.self_deduplicate().selected
|
|
117
|
+
|
|
118
|
+
# Filter outliers from the records
|
|
119
|
+
filtered_texts = semhash.self_filter_outliers().selected
|
|
120
|
+
|
|
121
|
+
# Find representative texts in the records
|
|
122
|
+
representative_texts = semhash.self_find_representative().selected
|
|
94
123
|
```
|
|
95
124
|
|
|
96
|
-
The `deduplicate` and `self_deduplicate` functions return a [DeduplicationResult](https://github.com/MinishLab/semhash/blob/
|
|
125
|
+
The `deduplicate` and `self_deduplicate` functions return a [DeduplicationResult](https://github.com/MinishLab/semhash/blob/main/semhash/datamodels.py#L30). This object stores the deduplicated corpus, a set of duplicate object (along with the objects that caused duplication), and several useful functions to further inspect the deduplication result. Examples of how these functions can be used can be found in the [usage](#usage) section.
|
|
126
|
+
|
|
127
|
+
The `filter_outliers`, `self_filter_outliers`, `find_representative`, and `self_find_representative` functions return a [FilterResult](https://github.com/MinishLab/semhash/blob/main/semhash/datamodels.py#106). This object stores the found outliers/representative samples.
|
|
97
128
|
|
|
129
|
+
For both the `DeduplicationResult` and `FilterResult` objects, you can easily view the filtered records with the `selected` attribute (e.g. to view outliers: `outliers = semhash.self_filter_outliers().filtered`)
|
|
98
130
|
## Main Features
|
|
99
131
|
|
|
100
132
|
- **Fast**: SemHash uses [model2vec](https://github.com/MinishLab/model2vec) to embed texts and [vicinity](https://github.com/MinishLab/vicinity) to perform similarity search, making it extremely fast.
|
|
101
|
-
- **Scalable**: SemHash can deduplicate large datasets with millions of records thanks to the ANN backends in Vicinity.
|
|
102
|
-
- **Flexible**: SemHash can be used to deduplicate a single dataset or across two datasets, and can also be used to deduplicate multi-column datasets (such as QA datasets).
|
|
133
|
+
- **Scalable**: SemHash can deduplicate & filter large datasets with millions of records thanks to the ANN backends in Vicinity.
|
|
134
|
+
- **Flexible**: SemHash can be used to deduplicate & filter a single dataset or across two datasets, and can also be used to deduplicate & filter multi-column datasets (such as QA datasets).
|
|
103
135
|
- **Lightweight**: SemHash is a lightweight package with minimal dependencies, making it easy to install and use.
|
|
136
|
+
- **Explainable**: Easily inspect the duplicates and what caused them with the `DeduplicationResult` object. You can also view the lowest similarity duplicates to find the right threshold for deduplication for your dataset.
|
|
104
137
|
|
|
105
138
|
## Usage
|
|
106
139
|
|
|
107
|
-
The following examples show the various ways you can use SemHash to deduplicate datasets. These examples assume you have the `datasets` library installed, which you can install with `pip install datasets`.
|
|
140
|
+
The following examples show the various ways you can use SemHash to deduplicate datasets, filter outliers, and find representative samples. These examples assume you have the `datasets` library installed, which you can install with `pip install datasets`.
|
|
108
141
|
|
|
109
142
|
<details>
|
|
110
|
-
<summary> Deduplicate a single dataset </summary>
|
|
143
|
+
<summary> Deduplicate, filter outliers, and find representative samples on a single dataset </summary>
|
|
111
144
|
<br>
|
|
112
145
|
|
|
113
|
-
The following code snippet shows how to deduplicate a single dataset using SemHash (in this example, the train split of the [AG News dataset](https://huggingface.co/datasets/fancyzhx/ag_news)):
|
|
146
|
+
The following code snippet shows how to deduplicate a single dataset, filter outliers, and find representative samples using SemHash (in this example, the train split of the [AG News dataset](https://huggingface.co/datasets/fancyzhx/ag_news)):
|
|
114
147
|
|
|
115
148
|
```python
|
|
116
149
|
from datasets import load_dataset
|
|
@@ -123,15 +156,21 @@ texts = load_dataset("ag_news", split="train")["text"]
|
|
|
123
156
|
semhash = SemHash.from_records(records=texts)
|
|
124
157
|
|
|
125
158
|
# Deduplicate the texts
|
|
126
|
-
deduplicated_texts = semhash.self_deduplicate()
|
|
159
|
+
deduplicated_texts = semhash.self_deduplicate().selected
|
|
160
|
+
|
|
161
|
+
# Filter outliers
|
|
162
|
+
filtered_texts = semhash.self_filter_outliers().selected
|
|
163
|
+
|
|
164
|
+
# Find representative texts
|
|
165
|
+
representative_texts = semhash.self_find_representative().selected
|
|
127
166
|
```
|
|
128
167
|
</details>
|
|
129
168
|
|
|
130
169
|
<details>
|
|
131
|
-
<summary> Deduplicate across two datasets </summary>
|
|
170
|
+
<summary> Deduplicate, filter outliers, and find representative samples across two datasets </summary>
|
|
132
171
|
<br>
|
|
133
172
|
|
|
134
|
-
The following code snippet shows how to deduplicate across two datasets using SemHash (in this example, the train/test split of the [AG News dataset](https://huggingface.co/datasets/fancyzhx/ag_news)):
|
|
173
|
+
The following code snippet shows how to deduplicate across two datasets, filter outliers, and find representative samples using SemHash (in this example, the train/test split of the [AG News dataset](https://huggingface.co/datasets/fancyzhx/ag_news)):
|
|
135
174
|
|
|
136
175
|
```python
|
|
137
176
|
from datasets import load_dataset
|
|
@@ -148,16 +187,22 @@ test_texts = load_dataset("ag_news", split="test")["text"]
|
|
|
148
187
|
semhash = SemHash.from_records(records=train_texts)
|
|
149
188
|
|
|
150
189
|
# Deduplicate the test data against the training data
|
|
151
|
-
deduplicated_test_texts = semhash.deduplicate(records=test_texts)
|
|
190
|
+
deduplicated_test_texts = semhash.deduplicate(records=test_texts).selected
|
|
191
|
+
|
|
192
|
+
# Filter outliers from the test data
|
|
193
|
+
filtered_test_texts = semhash.filter_outliers(records=test_texts).selected
|
|
194
|
+
|
|
195
|
+
# Find representative texts in the test data
|
|
196
|
+
representative_test_texts = semhash.find_representative(records=test_texts).selected
|
|
152
197
|
```
|
|
153
198
|
|
|
154
199
|
</details>
|
|
155
200
|
|
|
156
201
|
<details>
|
|
157
|
-
<summary> Deduplicate multi-column datasets </summary>
|
|
202
|
+
<summary> Deduplicate, filter outliers, and find representative samples on multi-column datasets </summary>
|
|
158
203
|
<br>
|
|
159
204
|
|
|
160
|
-
The following code snippet shows how to deduplicate multi-column datasets using SemHash (in this example, the train split of the QA dataset [SQuAD 2.0](https://huggingface.co/datasets/rajpurkar/squad_v2), which consists of questions, contexts, and answers):
|
|
205
|
+
The following code snippet shows how to deduplicate multi-column datasets, filter outliers, and find representative samples using SemHash (in this example, the train split of the QA dataset [SQuAD 2.0](https://huggingface.co/datasets/rajpurkar/squad_v2), which consists of questions, contexts, and answers):
|
|
161
206
|
|
|
162
207
|
```python
|
|
163
208
|
from datasets import load_dataset
|
|
@@ -173,7 +218,13 @@ records = [dict(row) for row in dataset]
|
|
|
173
218
|
semhash = SemHash.from_records(records=records, columns=["question", "context"])
|
|
174
219
|
|
|
175
220
|
# Deduplicate the records
|
|
176
|
-
deduplicated_records = semhash.self_deduplicate().
|
|
221
|
+
deduplicated_records = semhash.self_deduplicate().selected
|
|
222
|
+
|
|
223
|
+
# Filter outliers from the records
|
|
224
|
+
filtered_records = semhash.self_filter_outliers().selected
|
|
225
|
+
|
|
226
|
+
# Find representative samples in the records
|
|
227
|
+
representative_records = semhash.self_find_representative().selected
|
|
177
228
|
```
|
|
178
229
|
|
|
179
230
|
</details>
|
|
@@ -198,9 +249,9 @@ semhash = SemHash.from_records(records=texts)
|
|
|
198
249
|
deduplication_result = semhash.self_deduplicate()
|
|
199
250
|
|
|
200
251
|
# Check the deduplicated texts
|
|
201
|
-
deduplication_result.
|
|
252
|
+
deduplication_result.selected
|
|
202
253
|
# Check the duplicates
|
|
203
|
-
deduplication_result.
|
|
254
|
+
deduplication_result.filtered
|
|
204
255
|
# See what percentage of the texts were duplicates
|
|
205
256
|
deduplication_result.duplicate_ratio
|
|
206
257
|
# See what percentage of the texts were exact duplicates
|
|
@@ -261,12 +312,43 @@ deduplicated_texts = semhash.self_deduplicate()
|
|
|
261
312
|
|
|
262
313
|
</details>
|
|
263
314
|
|
|
315
|
+
<details>
|
|
316
|
+
<summary> Using Pandas DataFrames </summary>
|
|
317
|
+
<br>
|
|
318
|
+
|
|
319
|
+
You can easily use Pandas DataFrames with SemHash. The following code snippet shows how to deduplicate a Pandas DataFrame:
|
|
320
|
+
|
|
321
|
+
```python
|
|
322
|
+
import pandas as pd
|
|
323
|
+
from datasets import load_dataset
|
|
324
|
+
from semhash import SemHash
|
|
325
|
+
|
|
326
|
+
# Load a dataset as a pandas dataframe
|
|
327
|
+
dataframe = load_dataset("ag_news", split="train").to_pandas()
|
|
328
|
+
|
|
329
|
+
# Convert the dataframe to a list of dictionaries
|
|
330
|
+
dataframe = dataframe.to_dict(orient="records")
|
|
331
|
+
|
|
332
|
+
# Initialize a SemHash instance with the columns to deduplicate
|
|
333
|
+
semhash = SemHash.from_records(records=dataframe, columns=["text"])
|
|
334
|
+
|
|
335
|
+
# Deduplicate the texts
|
|
336
|
+
deduplicated_records = semhash.self_deduplicate().selected
|
|
337
|
+
|
|
338
|
+
# Convert the deduplicated records back to a pandas dataframe
|
|
339
|
+
deduplicated_dataframe = pd.DataFrame(deduplicated_records)
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
</details>
|
|
343
|
+
|
|
264
344
|
NOTE: By default, we use the ANN (approximate-nearest neighbors) backend for deduplication. We recommend keeping this since the recall for smaller datasets is ~100%, and it's needed for larger datasets (>1M samples) since these will take too long to deduplicate without ANN. If you want to use the flat/exact-matching backend, you can set `use_ann=False` in the SemHash constructor:
|
|
265
345
|
|
|
266
346
|
```python
|
|
267
347
|
semhash = SemHash.from_records(records=texts, use_ann=False)
|
|
268
348
|
```
|
|
269
349
|
|
|
350
|
+
|
|
351
|
+
|
|
270
352
|
## Benchmarks
|
|
271
353
|
|
|
272
354
|
We've benchmarked SemHash on a variety of datasets to measure the deduplication performance and speed. The benchmarks were run with the following setup:
|
|
@@ -330,3 +412,19 @@ To run the benchmarks yourself, you can use the following command (assuming you
|
|
|
330
412
|
python -m benchmarks.run_benchmarks
|
|
331
413
|
```
|
|
332
414
|
Optionally, the datasets can be updated in the [datasets.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/datasets.py) file.
|
|
415
|
+
|
|
416
|
+
## License
|
|
417
|
+
|
|
418
|
+
MIT
|
|
419
|
+
|
|
420
|
+
## Citing
|
|
421
|
+
|
|
422
|
+
If you use SemHash in your research, please cite the following:
|
|
423
|
+
```bibtex
|
|
424
|
+
@software{minishlab2025semhash,
|
|
425
|
+
author = {Thomas van Dongen and Stephan Tulkens},
|
|
426
|
+
title = {SemHash: Fast Semantic Text Deduplication & Filtering},
|
|
427
|
+
year = {2025},
|
|
428
|
+
url = {https://github.com/MinishLab/semhash}
|
|
429
|
+
}
|
|
430
|
+
```
|
|
@@ -55,7 +55,7 @@ def main() -> None: # noqa: C901
|
|
|
55
55
|
|
|
56
56
|
train_only_dedup_time = train_only_end - train_only_start
|
|
57
57
|
original_train_size = len(train_records)
|
|
58
|
-
dedup_train_size = len(deduplicated_train.
|
|
58
|
+
dedup_train_size = len(deduplicated_train.selected)
|
|
59
59
|
|
|
60
60
|
percent_removed_train = deduplicated_train.duplicate_ratio * 100
|
|
61
61
|
train_dedup_results.append(
|
|
@@ -88,7 +88,7 @@ def main() -> None: # noqa: C901
|
|
|
88
88
|
train_test_end = perf_counter()
|
|
89
89
|
train_test_dedup_time = train_test_end - train_test_start
|
|
90
90
|
original_test_size = len(test_records)
|
|
91
|
-
deduped_test_size = len(deduplicated_test.
|
|
91
|
+
deduped_test_size = len(deduplicated_test.selected)
|
|
92
92
|
percent_removed_test = deduplicated_test.duplicate_ratio * 100
|
|
93
93
|
|
|
94
94
|
train_test_dedup_results.append(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "semhash"
|
|
3
|
-
description = "Fast Semantic Text Deduplication"
|
|
3
|
+
description = "Fast Semantic Text Deduplication & Filtering"
|
|
4
4
|
authors = [{name = "Thomas van Dongen", email = "thomas123@live.nl"}, { name = "Stéphan Tulkens", email = "stephantul@gmail.com"}]
|
|
5
5
|
readme = { file = "README.md", content-type = "text/markdown" }
|
|
6
6
|
dynamic = ["version"]
|
|
@@ -92,6 +92,7 @@ ignore_missing_imports = true
|
|
|
92
92
|
|
|
93
93
|
[tool.setuptools]
|
|
94
94
|
packages = ["semhash"]
|
|
95
|
+
license-files = []
|
|
95
96
|
|
|
96
97
|
[tool.setuptools_scm]
|
|
97
98
|
# can be empty if no extra settings are needed, presence enables setuptools_scm
|