semhash 0.2.1__tar.gz → 0.3.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.
Files changed (45) hide show
  1. {semhash-0.2.1 → semhash-0.3.1}/.gitignore +2 -0
  2. {semhash-0.2.1 → semhash-0.3.1}/PKG-INFO +84 -35
  3. {semhash-0.2.1 → semhash-0.3.1}/README.md +81 -31
  4. semhash-0.3.1/assets/images/semhash_logo_v2.png +0 -0
  5. {semhash-0.2.1 → semhash-0.3.1}/benchmarks/run_benchmarks.py +2 -2
  6. {semhash-0.2.1 → semhash-0.3.1}/pyproject.toml +2 -1
  7. semhash-0.3.1/semhash/datamodels.py +194 -0
  8. {semhash-0.2.1 → semhash-0.3.1}/semhash/index.py +20 -0
  9. {semhash-0.2.1 → semhash-0.3.1}/semhash/records.py +2 -9
  10. semhash-0.3.1/semhash/semhash.py +586 -0
  11. semhash-0.3.1/semhash/utils.py +55 -0
  12. {semhash-0.2.1 → semhash-0.3.1}/semhash/version.py +1 -1
  13. {semhash-0.2.1 → semhash-0.3.1}/semhash.egg-info/PKG-INFO +84 -35
  14. {semhash-0.2.1 → semhash-0.3.1}/semhash.egg-info/SOURCES.txt +2 -0
  15. semhash-0.3.1/tests/conftest.py +57 -0
  16. semhash-0.3.1/tests/data/test_model/README.md +97 -0
  17. semhash-0.3.1/tests/data/test_model/config.json +12 -0
  18. semhash-0.3.1/tests/data/test_model/model.safetensors +0 -0
  19. semhash-0.3.1/tests/data/test_model/modules.json +14 -0
  20. {semhash-0.2.1 → semhash-0.3.1}/tests/data/test_model/tokenizer.json +2 -2
  21. semhash-0.3.1/tests/test_datamodels.py +221 -0
  22. {semhash-0.2.1 → semhash-0.3.1}/tests/test_semhash.py +89 -9
  23. {semhash-0.2.1 → semhash-0.3.1}/uv.lock +5 -4
  24. semhash-0.2.1/semhash/datamodels.py +0 -80
  25. semhash-0.2.1/semhash/semhash.py +0 -296
  26. semhash-0.2.1/semhash/utils.py +0 -21
  27. semhash-0.2.1/tests/conftest.py +0 -14
  28. semhash-0.2.1/tests/data/test_model/README.md +0 -78
  29. semhash-0.2.1/tests/data/test_model/config.json +0 -1
  30. semhash-0.2.1/tests/data/test_model/model.safetensors +0 -0
  31. semhash-0.2.1/tests/test_datamodels.py +0 -95
  32. {semhash-0.2.1 → semhash-0.3.1}/.github/workflows/ci.yaml +0 -0
  33. {semhash-0.2.1 → semhash-0.3.1}/.pre-commit-config.yaml +0 -0
  34. {semhash-0.2.1 → semhash-0.3.1}/LICENSE +0 -0
  35. {semhash-0.2.1 → semhash-0.3.1}/Makefile +0 -0
  36. {semhash-0.2.1 → semhash-0.3.1}/assets/images/semhash_logo.png +0 -0
  37. {semhash-0.2.1 → semhash-0.3.1}/benchmarks/__init__.py +0 -0
  38. {semhash-0.2.1 → semhash-0.3.1}/benchmarks/data.py +0 -0
  39. {semhash-0.2.1 → semhash-0.3.1}/benchmarks/results/train_benchmark_results.json +0 -0
  40. {semhash-0.2.1 → semhash-0.3.1}/benchmarks/results/train_test_benchmark_results.json +0 -0
  41. {semhash-0.2.1 → semhash-0.3.1}/semhash/__init__.py +0 -0
  42. {semhash-0.2.1 → semhash-0.3.1}/semhash.egg-info/dependency_links.txt +0 -0
  43. {semhash-0.2.1 → semhash-0.3.1}/semhash.egg-info/requires.txt +0 -0
  44. {semhash-0.2.1 → semhash-0.3.1}/semhash.egg-info/top_level.txt +0 -0
  45. {semhash-0.2.1 → semhash-0.3.1}/setup.cfg +0 -0
@@ -163,3 +163,5 @@ cython_debug/
163
163
  # and can be added to the global gitignore or merged into this file. For a more nuclear
164
164
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
165
165
  #.idea/
166
+
167
+ .DS_Store
@@ -1,7 +1,7 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: semhash
3
- Version: 0.2.1
4
- Summary: Fast Semantic Text Deduplication
3
+ Version: 0.3.1
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
@@ -57,21 +56,23 @@ Requires-Dist: ruff; extra == "dev"
57
56
 
58
57
 
59
58
 
60
- <div align="center">
61
- <picture>
62
- <img width="30%" alt="SemHash logo" src="assets/images/semhash_logo.png">
63
- </picture>
64
- </a>
65
- </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
+
66
64
 
67
65
  <div align="center">
68
66
 
69
- # Fast Semantic Text Deduplication
67
+
70
68
 
71
69
 
72
70
  <h2>
73
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>
74
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>
75
76
  <a href="https://app.codecov.io/gh/MinishLab/semhash">
76
77
  <img src="https://codecov.io/gh/MinishLab/semhash/graph/badge.svg?token=YPOD6HD0MG" alt="Codecov">
77
78
  </a>
@@ -93,9 +94,9 @@ Requires-Dist: ruff; extra == "dev"
93
94
  </div>
94
95
 
95
96
 
96
- 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).
97
+ 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).
97
98
 
98
- 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.
99
+ 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.
99
100
 
100
101
  ## Quickstart
101
102
 
@@ -104,7 +105,7 @@ Install the package with:
104
105
  pip install semhash
105
106
  ```
106
107
 
107
- Deduplicate a single dataset with the following code (note: the examples assume you have `datasets` installed, which you can install with `pip install datasets`):
108
+ 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`):
108
109
 
109
110
  ```python
110
111
  from datasets import load_dataset
@@ -117,10 +118,16 @@ texts = load_dataset("ag_news", split="train")["text"]
117
118
  semhash = SemHash.from_records(records=texts)
118
119
 
119
120
  # Deduplicate the texts
120
- deduplicated_texts = semhash.self_deduplicate().deduplicated
121
+ deduplicated_texts = semhash.self_deduplicate().selected
122
+
123
+ # Filter outliers
124
+ filtered_texts = semhash.self_filter_outliers().selected
125
+
126
+ # Find representative texts
127
+ representative_texts = semhash.self_find_representative().selected
121
128
  ```
122
129
 
123
- Or, deduplicate across two datasets with the following code (e.g., eliminating train/test leakage):
130
+ Or, deduplicate across two datasets, filter outliers, and find representative samples with the following code (e.g., eliminating train/test leakage):
124
131
 
125
132
  ```python
126
133
  from datasets import load_dataset
@@ -134,10 +141,21 @@ test_texts = load_dataset("ag_news", split="test")["text"]
134
141
  semhash = SemHash.from_records(records=train_texts)
135
142
 
136
143
  # Deduplicate the test data against the training data, optionally with a specific threshold
137
- deduplicated_test_texts = semhash.deduplicate(records=test_texts, threshold=0.9).deduplicated
144
+ deduplicated_test_texts = semhash.deduplicate(records=test_texts, threshold=0.9).selected
145
+
146
+ # Filter outliers from the test data against the training data,
147
+ # optionally with a specific percentage
148
+ filtered_test_texts = semhash.filter_outliers(records=test_texts, outlier_percentage=0.1).selected
149
+
150
+ # Find representative texts in the test data against the training data,
151
+ # optionally with a specific selection size
152
+ representative_test_texts = semhash.find_representative(
153
+ records=test_texts, selection_size=10).selected
154
+
155
+
138
156
  ```
139
157
 
140
- Or, deduplicate multi-column datasets with the following code (e.g., deduplicating a QA dataset):
158
+ Or, deduplicate multi-column dataset, filter outliers, and find representative samples with the following code (e.g., deduplicating a QA dataset):
141
159
 
142
160
  ```python
143
161
  from datasets import load_dataset
@@ -153,28 +171,37 @@ records = [dict(row) for row in dataset]
153
171
  semhash = SemHash.from_records(records=records, columns=["question", "context"])
154
172
 
155
173
  # Deduplicate the records
156
- deduplicated_records = semhash.self_deduplicate().deduplicated
174
+ deduplicated_records = semhash.self_deduplicate().selected
175
+
176
+ # Filter outliers from the records
177
+ filtered_texts = semhash.self_filter_outliers().selected
178
+
179
+ # Find representative texts in the records
180
+ representative_texts = semhash.self_find_representative().selected
157
181
  ```
158
182
 
159
183
  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.
160
184
 
185
+ 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.
186
+
187
+ 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`)
161
188
  ## Main Features
162
189
 
163
190
  - **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.
164
- - **Scalable**: SemHash can deduplicate large datasets with millions of records thanks to the ANN backends in Vicinity.
165
- - **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).
191
+ - **Scalable**: SemHash can deduplicate & filter large datasets with millions of records thanks to the ANN backends in Vicinity.
192
+ - **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).
166
193
  - **Lightweight**: SemHash is a lightweight package with minimal dependencies, making it easy to install and use.
167
194
  - **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.
168
195
 
169
196
  ## Usage
170
197
 
171
- 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`.
198
+ 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`.
172
199
 
173
200
  <details>
174
- <summary> Deduplicate a single dataset </summary>
201
+ <summary> Deduplicate, filter outliers, and find representative samples on a single dataset </summary>
175
202
  <br>
176
203
 
177
- 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)):
204
+ 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)):
178
205
 
179
206
  ```python
180
207
  from datasets import load_dataset
@@ -187,15 +214,21 @@ texts = load_dataset("ag_news", split="train")["text"]
187
214
  semhash = SemHash.from_records(records=texts)
188
215
 
189
216
  # Deduplicate the texts
190
- deduplicated_texts = semhash.self_deduplicate()
217
+ deduplicated_texts = semhash.self_deduplicate().selected
218
+
219
+ # Filter outliers
220
+ filtered_texts = semhash.self_filter_outliers().selected
221
+
222
+ # Find representative texts
223
+ representative_texts = semhash.self_find_representative().selected
191
224
  ```
192
225
  </details>
193
226
 
194
227
  <details>
195
- <summary> Deduplicate across two datasets </summary>
228
+ <summary> Deduplicate, filter outliers, and find representative samples across two datasets </summary>
196
229
  <br>
197
230
 
198
- 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)):
231
+ 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)):
199
232
 
200
233
  ```python
201
234
  from datasets import load_dataset
@@ -212,16 +245,22 @@ test_texts = load_dataset("ag_news", split="test")["text"]
212
245
  semhash = SemHash.from_records(records=train_texts)
213
246
 
214
247
  # Deduplicate the test data against the training data
215
- deduplicated_test_texts = semhash.deduplicate(records=test_texts)
248
+ deduplicated_test_texts = semhash.deduplicate(records=test_texts).selected
249
+
250
+ # Filter outliers from the test data
251
+ filtered_test_texts = semhash.filter_outliers(records=test_texts).selected
252
+
253
+ # Find representative texts in the test data
254
+ representative_test_texts = semhash.find_representative(records=test_texts).selected
216
255
  ```
217
256
 
218
257
  </details>
219
258
 
220
259
  <details>
221
- <summary> Deduplicate multi-column datasets </summary>
260
+ <summary> Deduplicate, filter outliers, and find representative samples on multi-column datasets </summary>
222
261
  <br>
223
262
 
224
- 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):
263
+ 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):
225
264
 
226
265
  ```python
227
266
  from datasets import load_dataset
@@ -237,7 +276,13 @@ records = [dict(row) for row in dataset]
237
276
  semhash = SemHash.from_records(records=records, columns=["question", "context"])
238
277
 
239
278
  # Deduplicate the records
240
- deduplicated_records = semhash.self_deduplicate().deduplicated
279
+ deduplicated_records = semhash.self_deduplicate().selected
280
+
281
+ # Filter outliers from the records
282
+ filtered_records = semhash.self_filter_outliers().selected
283
+
284
+ # Find representative samples in the records
285
+ representative_records = semhash.self_find_representative().selected
241
286
  ```
242
287
 
243
288
  </details>
@@ -262,9 +307,9 @@ semhash = SemHash.from_records(records=texts)
262
307
  deduplication_result = semhash.self_deduplicate()
263
308
 
264
309
  # Check the deduplicated texts
265
- deduplication_result.deduplicated
310
+ deduplication_result.selected
266
311
  # Check the duplicates
267
- deduplication_result.duplicates
312
+ deduplication_result.filtered
268
313
  # See what percentage of the texts were duplicates
269
314
  deduplication_result.duplicate_ratio
270
315
  # See what percentage of the texts were exact duplicates
@@ -275,6 +320,10 @@ least_similar = deduplication_result.get_least_similar_from_duplicates()
275
320
 
276
321
  # Rethreshold the duplicates. This allows you to instantly rethreshold the duplicates with a new threshold without having to re-deduplicate the texts.
277
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
278
327
  ```
279
328
 
280
329
  </details>
@@ -346,7 +395,7 @@ dataframe = dataframe.to_dict(orient="records")
346
395
  semhash = SemHash.from_records(records=dataframe, columns=["text"])
347
396
 
348
397
  # Deduplicate the texts
349
- deduplicated_records = semhash.self_deduplicate().deduplicated
398
+ deduplicated_records = semhash.self_deduplicate().selected
350
399
 
351
400
  # Convert the deduplicated records back to a pandas dataframe
352
401
  deduplicated_dataframe = pd.DataFrame(deduplicated_records)
@@ -436,7 +485,7 @@ If you use SemHash in your research, please cite the following:
436
485
  ```bibtex
437
486
  @software{minishlab2025semhash,
438
487
  author = {Thomas van Dongen and Stephan Tulkens},
439
- title = {SemHash: Fast Semantic Text Deduplication},
488
+ title = {SemHash: Fast Semantic Text Deduplication & Filtering},
440
489
  year = {2025},
441
490
  url = {https://github.com/MinishLab/semhash}
442
491
  }
@@ -1,20 +1,22 @@
1
1
 
2
2
 
3
- <div align="center">
4
- <picture>
5
- <img width="30%" alt="SemHash logo" src="assets/images/semhash_logo.png">
6
- </picture>
7
- </a>
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
- # Fast Semantic Text Deduplication
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>
@@ -36,9 +38,9 @@
36
38
  </div>
37
39
 
38
40
 
39
- 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).
41
+ 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).
40
42
 
41
- 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.
43
+ 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.
42
44
 
43
45
  ## Quickstart
44
46
 
@@ -47,7 +49,7 @@ Install the package with:
47
49
  pip install semhash
48
50
  ```
49
51
 
50
- Deduplicate a single dataset with the following code (note: the examples assume you have `datasets` installed, which you can install with `pip install datasets`):
52
+ 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`):
51
53
 
52
54
  ```python
53
55
  from datasets import load_dataset
@@ -60,10 +62,16 @@ texts = load_dataset("ag_news", split="train")["text"]
60
62
  semhash = SemHash.from_records(records=texts)
61
63
 
62
64
  # Deduplicate the texts
63
- deduplicated_texts = semhash.self_deduplicate().deduplicated
65
+ deduplicated_texts = semhash.self_deduplicate().selected
66
+
67
+ # Filter outliers
68
+ filtered_texts = semhash.self_filter_outliers().selected
69
+
70
+ # Find representative texts
71
+ representative_texts = semhash.self_find_representative().selected
64
72
  ```
65
73
 
66
- Or, deduplicate across two datasets with the following code (e.g., eliminating train/test leakage):
74
+ Or, deduplicate across two datasets, filter outliers, and find representative samples with the following code (e.g., eliminating train/test leakage):
67
75
 
68
76
  ```python
69
77
  from datasets import load_dataset
@@ -77,10 +85,21 @@ test_texts = load_dataset("ag_news", split="test")["text"]
77
85
  semhash = SemHash.from_records(records=train_texts)
78
86
 
79
87
  # Deduplicate the test data against the training data, optionally with a specific threshold
80
- deduplicated_test_texts = semhash.deduplicate(records=test_texts, threshold=0.9).deduplicated
88
+ deduplicated_test_texts = semhash.deduplicate(records=test_texts, threshold=0.9).selected
89
+
90
+ # Filter outliers from the test data against the training data,
91
+ # optionally with a specific percentage
92
+ filtered_test_texts = semhash.filter_outliers(records=test_texts, outlier_percentage=0.1).selected
93
+
94
+ # Find representative texts in the test data against the training data,
95
+ # optionally with a specific selection size
96
+ representative_test_texts = semhash.find_representative(
97
+ records=test_texts, selection_size=10).selected
98
+
99
+
81
100
  ```
82
101
 
83
- Or, deduplicate multi-column datasets with the following code (e.g., deduplicating a QA dataset):
102
+ Or, deduplicate multi-column dataset, filter outliers, and find representative samples with the following code (e.g., deduplicating a QA dataset):
84
103
 
85
104
  ```python
86
105
  from datasets import load_dataset
@@ -96,28 +115,37 @@ records = [dict(row) for row in dataset]
96
115
  semhash = SemHash.from_records(records=records, columns=["question", "context"])
97
116
 
98
117
  # Deduplicate the records
99
- deduplicated_records = semhash.self_deduplicate().deduplicated
118
+ deduplicated_records = semhash.self_deduplicate().selected
119
+
120
+ # Filter outliers from the records
121
+ filtered_texts = semhash.self_filter_outliers().selected
122
+
123
+ # Find representative texts in the records
124
+ representative_texts = semhash.self_find_representative().selected
100
125
  ```
101
126
 
102
127
  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.
103
128
 
129
+ 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.
130
+
131
+ 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`)
104
132
  ## Main Features
105
133
 
106
134
  - **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.
107
- - **Scalable**: SemHash can deduplicate large datasets with millions of records thanks to the ANN backends in Vicinity.
108
- - **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).
135
+ - **Scalable**: SemHash can deduplicate & filter large datasets with millions of records thanks to the ANN backends in Vicinity.
136
+ - **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).
109
137
  - **Lightweight**: SemHash is a lightweight package with minimal dependencies, making it easy to install and use.
110
138
  - **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.
111
139
 
112
140
  ## Usage
113
141
 
114
- 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`.
142
+ 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`.
115
143
 
116
144
  <details>
117
- <summary> Deduplicate a single dataset </summary>
145
+ <summary> Deduplicate, filter outliers, and find representative samples on a single dataset </summary>
118
146
  <br>
119
147
 
120
- 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)):
148
+ 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)):
121
149
 
122
150
  ```python
123
151
  from datasets import load_dataset
@@ -130,15 +158,21 @@ texts = load_dataset("ag_news", split="train")["text"]
130
158
  semhash = SemHash.from_records(records=texts)
131
159
 
132
160
  # Deduplicate the texts
133
- deduplicated_texts = semhash.self_deduplicate()
161
+ deduplicated_texts = semhash.self_deduplicate().selected
162
+
163
+ # Filter outliers
164
+ filtered_texts = semhash.self_filter_outliers().selected
165
+
166
+ # Find representative texts
167
+ representative_texts = semhash.self_find_representative().selected
134
168
  ```
135
169
  </details>
136
170
 
137
171
  <details>
138
- <summary> Deduplicate across two datasets </summary>
172
+ <summary> Deduplicate, filter outliers, and find representative samples across two datasets </summary>
139
173
  <br>
140
174
 
141
- 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)):
175
+ 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)):
142
176
 
143
177
  ```python
144
178
  from datasets import load_dataset
@@ -155,16 +189,22 @@ test_texts = load_dataset("ag_news", split="test")["text"]
155
189
  semhash = SemHash.from_records(records=train_texts)
156
190
 
157
191
  # Deduplicate the test data against the training data
158
- deduplicated_test_texts = semhash.deduplicate(records=test_texts)
192
+ deduplicated_test_texts = semhash.deduplicate(records=test_texts).selected
193
+
194
+ # Filter outliers from the test data
195
+ filtered_test_texts = semhash.filter_outliers(records=test_texts).selected
196
+
197
+ # Find representative texts in the test data
198
+ representative_test_texts = semhash.find_representative(records=test_texts).selected
159
199
  ```
160
200
 
161
201
  </details>
162
202
 
163
203
  <details>
164
- <summary> Deduplicate multi-column datasets </summary>
204
+ <summary> Deduplicate, filter outliers, and find representative samples on multi-column datasets </summary>
165
205
  <br>
166
206
 
167
- 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):
207
+ 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):
168
208
 
169
209
  ```python
170
210
  from datasets import load_dataset
@@ -180,7 +220,13 @@ records = [dict(row) for row in dataset]
180
220
  semhash = SemHash.from_records(records=records, columns=["question", "context"])
181
221
 
182
222
  # Deduplicate the records
183
- deduplicated_records = semhash.self_deduplicate().deduplicated
223
+ deduplicated_records = semhash.self_deduplicate().selected
224
+
225
+ # Filter outliers from the records
226
+ filtered_records = semhash.self_filter_outliers().selected
227
+
228
+ # Find representative samples in the records
229
+ representative_records = semhash.self_find_representative().selected
184
230
  ```
185
231
 
186
232
  </details>
@@ -205,9 +251,9 @@ semhash = SemHash.from_records(records=texts)
205
251
  deduplication_result = semhash.self_deduplicate()
206
252
 
207
253
  # Check the deduplicated texts
208
- deduplication_result.deduplicated
254
+ deduplication_result.selected
209
255
  # Check the duplicates
210
- deduplication_result.duplicates
256
+ deduplication_result.filtered
211
257
  # See what percentage of the texts were duplicates
212
258
  deduplication_result.duplicate_ratio
213
259
  # See what percentage of the texts were exact duplicates
@@ -218,6 +264,10 @@ least_similar = deduplication_result.get_least_similar_from_duplicates()
218
264
 
219
265
  # Rethreshold the duplicates. This allows you to instantly rethreshold the duplicates with a new threshold without having to re-deduplicate the texts.
220
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
221
271
  ```
222
272
 
223
273
  </details>
@@ -289,7 +339,7 @@ dataframe = dataframe.to_dict(orient="records")
289
339
  semhash = SemHash.from_records(records=dataframe, columns=["text"])
290
340
 
291
341
  # Deduplicate the texts
292
- deduplicated_records = semhash.self_deduplicate().deduplicated
342
+ deduplicated_records = semhash.self_deduplicate().selected
293
343
 
294
344
  # Convert the deduplicated records back to a pandas dataframe
295
345
  deduplicated_dataframe = pd.DataFrame(deduplicated_records)
@@ -379,7 +429,7 @@ If you use SemHash in your research, please cite the following:
379
429
  ```bibtex
380
430
  @software{minishlab2025semhash,
381
431
  author = {Thomas van Dongen and Stephan Tulkens},
382
- title = {SemHash: Fast Semantic Text Deduplication},
432
+ title = {SemHash: Fast Semantic Text Deduplication & Filtering},
383
433
  year = {2025},
384
434
  url = {https://github.com/MinishLab/semhash}
385
435
  }
@@ -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.deduplicated)
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.deduplicated)
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