semhash 0.1.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.1.0/.gitignore +165 -0
- semhash-0.1.0/.pre-commit-config.yaml +34 -0
- semhash-0.1.0/LICENSE +21 -0
- semhash-0.1.0/Makefile +15 -0
- semhash-0.1.0/PKG-INFO +277 -0
- semhash-0.1.0/README.md +221 -0
- semhash-0.1.0/benchmarks/__init__.py +0 -0
- semhash-0.1.0/benchmarks/datasets.py +43 -0
- semhash-0.1.0/benchmarks/results/train_benchmark_results.json +121 -0
- semhash-0.1.0/benchmarks/results/train_test_benchmark_results.json +138 -0
- semhash-0.1.0/benchmarks/run_benchmarks.py +159 -0
- semhash-0.1.0/pyproject.toml +99 -0
- semhash-0.1.0/semhash/__init__.py +3 -0
- semhash-0.1.0/semhash/semhash.py +185 -0
- semhash-0.1.0/semhash/utils.py +21 -0
- semhash-0.1.0/semhash/version.py +2 -0
- semhash-0.1.0/semhash.egg-info/PKG-INFO +277 -0
- semhash-0.1.0/semhash.egg-info/SOURCES.txt +27 -0
- semhash-0.1.0/semhash.egg-info/dependency_links.txt +1 -0
- semhash-0.1.0/semhash.egg-info/requires.txt +11 -0
- semhash-0.1.0/semhash.egg-info/top_level.txt +1 -0
- semhash-0.1.0/setup.cfg +4 -0
- semhash-0.1.0/tests/conftest.py +18 -0
- semhash-0.1.0/tests/data/test_model/README.md +78 -0
- semhash-0.1.0/tests/data/test_model/config.json +1 -0
- semhash-0.1.0/tests/data/test_model/model.safetensors +0 -0
- semhash-0.1.0/tests/data/test_model/tokenizer.json +29678 -0
- semhash-0.1.0/tests/test_semhash.py +132 -0
- semhash-0.1.0/uv.lock +1408 -0
semhash-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Custom
|
|
2
|
+
local/*
|
|
3
|
+
|
|
4
|
+
# Byte-compiled / optimized / DLL files
|
|
5
|
+
__pycache__/
|
|
6
|
+
*.py[cod]
|
|
7
|
+
*$py.class
|
|
8
|
+
|
|
9
|
+
# C extensions
|
|
10
|
+
*.so
|
|
11
|
+
|
|
12
|
+
# Distribution / packaging
|
|
13
|
+
.Python
|
|
14
|
+
build/
|
|
15
|
+
develop-eggs/
|
|
16
|
+
dist/
|
|
17
|
+
downloads/
|
|
18
|
+
eggs/
|
|
19
|
+
.eggs/
|
|
20
|
+
lib/
|
|
21
|
+
lib64/
|
|
22
|
+
parts/
|
|
23
|
+
sdist/
|
|
24
|
+
var/
|
|
25
|
+
wheels/
|
|
26
|
+
share/python-wheels/
|
|
27
|
+
*.egg-info/
|
|
28
|
+
.installed.cfg
|
|
29
|
+
*.egg
|
|
30
|
+
MANIFEST
|
|
31
|
+
|
|
32
|
+
# PyInstaller
|
|
33
|
+
# Usually these files are written by a python script from a template
|
|
34
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
35
|
+
*.manifest
|
|
36
|
+
*.spec
|
|
37
|
+
|
|
38
|
+
# Installer logs
|
|
39
|
+
pip-log.txt
|
|
40
|
+
pip-delete-this-directory.txt
|
|
41
|
+
|
|
42
|
+
# Unit test / coverage reports
|
|
43
|
+
htmlcov/
|
|
44
|
+
.tox/
|
|
45
|
+
.nox/
|
|
46
|
+
.coverage
|
|
47
|
+
.coverage.*
|
|
48
|
+
.cache
|
|
49
|
+
nosetests.xml
|
|
50
|
+
coverage.xml
|
|
51
|
+
*.cover
|
|
52
|
+
*.py,cover
|
|
53
|
+
.hypothesis/
|
|
54
|
+
.pytest_cache/
|
|
55
|
+
cover/
|
|
56
|
+
|
|
57
|
+
# Translations
|
|
58
|
+
*.mo
|
|
59
|
+
*.pot
|
|
60
|
+
|
|
61
|
+
# Django stuff:
|
|
62
|
+
*.log
|
|
63
|
+
local_settings.py
|
|
64
|
+
db.sqlite3
|
|
65
|
+
db.sqlite3-journal
|
|
66
|
+
|
|
67
|
+
# Flask stuff:
|
|
68
|
+
instance/
|
|
69
|
+
.webassets-cache
|
|
70
|
+
|
|
71
|
+
# Scrapy stuff:
|
|
72
|
+
.scrapy
|
|
73
|
+
|
|
74
|
+
# Sphinx documentation
|
|
75
|
+
docs/_build/
|
|
76
|
+
|
|
77
|
+
# PyBuilder
|
|
78
|
+
.pybuilder/
|
|
79
|
+
target/
|
|
80
|
+
|
|
81
|
+
# Jupyter Notebook
|
|
82
|
+
.ipynb_checkpoints
|
|
83
|
+
|
|
84
|
+
# IPython
|
|
85
|
+
profile_default/
|
|
86
|
+
ipython_config.py
|
|
87
|
+
|
|
88
|
+
# pyenv
|
|
89
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
90
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
91
|
+
# .python-version
|
|
92
|
+
|
|
93
|
+
# pipenv
|
|
94
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
95
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
96
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
97
|
+
# install all needed dependencies.
|
|
98
|
+
#Pipfile.lock
|
|
99
|
+
|
|
100
|
+
# poetry
|
|
101
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
102
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
103
|
+
# commonly ignored for libraries.
|
|
104
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
105
|
+
#poetry.lock
|
|
106
|
+
|
|
107
|
+
# pdm
|
|
108
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
109
|
+
#pdm.lock
|
|
110
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
111
|
+
# in version control.
|
|
112
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
113
|
+
.pdm.toml
|
|
114
|
+
.pdm-python
|
|
115
|
+
.pdm-build/
|
|
116
|
+
|
|
117
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
118
|
+
__pypackages__/
|
|
119
|
+
|
|
120
|
+
# Celery stuff
|
|
121
|
+
celerybeat-schedule
|
|
122
|
+
celerybeat.pid
|
|
123
|
+
|
|
124
|
+
# SageMath parsed files
|
|
125
|
+
*.sage.py
|
|
126
|
+
|
|
127
|
+
# Environments
|
|
128
|
+
.env
|
|
129
|
+
.venv
|
|
130
|
+
env/
|
|
131
|
+
venv/
|
|
132
|
+
ENV/
|
|
133
|
+
env.bak/
|
|
134
|
+
venv.bak/
|
|
135
|
+
|
|
136
|
+
# Spyder project settings
|
|
137
|
+
.spyderproject
|
|
138
|
+
.spyproject
|
|
139
|
+
|
|
140
|
+
# Rope project settings
|
|
141
|
+
.ropeproject
|
|
142
|
+
|
|
143
|
+
# mkdocs documentation
|
|
144
|
+
/site
|
|
145
|
+
|
|
146
|
+
# mypy
|
|
147
|
+
.mypy_cache/
|
|
148
|
+
.dmypy.json
|
|
149
|
+
dmypy.json
|
|
150
|
+
|
|
151
|
+
# Pyre type checker
|
|
152
|
+
.pyre/
|
|
153
|
+
|
|
154
|
+
# pytype static type analyzer
|
|
155
|
+
.pytype/
|
|
156
|
+
|
|
157
|
+
# Cython debug symbols
|
|
158
|
+
cython_debug/
|
|
159
|
+
|
|
160
|
+
# PyCharm
|
|
161
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
162
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
163
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
164
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
165
|
+
#.idea/
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# See https://pre-commit.com for more information
|
|
2
|
+
# See https://pre-commit.com/hooks.html for more hooks
|
|
3
|
+
repos:
|
|
4
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
5
|
+
rev: v4.4.0
|
|
6
|
+
hooks:
|
|
7
|
+
- id: check-ast
|
|
8
|
+
description: Simply check whether files parse as valid python.
|
|
9
|
+
- id: trailing-whitespace
|
|
10
|
+
description: Trims trailing whitespace
|
|
11
|
+
- id: end-of-file-fixer
|
|
12
|
+
description: Makes sure files end in a newline and only a newline.
|
|
13
|
+
- id: check-added-large-files
|
|
14
|
+
args: ['--maxkb=5000']
|
|
15
|
+
description: Prevent giant files from being committed.
|
|
16
|
+
- id: check-case-conflict
|
|
17
|
+
description: Check for files with names that would conflict on case-insensitive filesystems like MacOS/Windows.
|
|
18
|
+
- repo: https://github.com/jsh9/pydoclint
|
|
19
|
+
rev: 0.5.3
|
|
20
|
+
hooks:
|
|
21
|
+
- id: pydoclint
|
|
22
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
23
|
+
rev: v0.4.10
|
|
24
|
+
hooks:
|
|
25
|
+
- id: ruff
|
|
26
|
+
args: [ --fix ]
|
|
27
|
+
- id: ruff-format
|
|
28
|
+
- repo: local
|
|
29
|
+
hooks:
|
|
30
|
+
- id: mypy
|
|
31
|
+
name: mypy
|
|
32
|
+
entry: mypy
|
|
33
|
+
language: python
|
|
34
|
+
types: [python]
|
semhash-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 The Minish Lab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
semhash-0.1.0/Makefile
ADDED
semhash-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: semhash
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fast Semantic Text Deduplication
|
|
5
|
+
Author-email: Thomas van Dongen <thomas123@live.nl>, Stéphan Tulkens <stephantul@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2024 The Minish Lab
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/MinishLab
|
|
29
|
+
Project-URL: Bug Reports, https://github.com/MinishLab/semhash/issues
|
|
30
|
+
Project-URL: Source, https://github.com/MinishLab/semhash
|
|
31
|
+
Classifier: Development Status :: 4 - Beta
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: Intended Audience :: Science/Research
|
|
34
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
35
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
36
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
37
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
42
|
+
Classifier: Natural Language :: English
|
|
43
|
+
Requires-Python: >=3.9
|
|
44
|
+
Description-Content-Type: text/markdown
|
|
45
|
+
License-File: LICENSE
|
|
46
|
+
Requires-Dist: model2vec>=0.3.0
|
|
47
|
+
Requires-Dist: vicinity[usearch]>=0.3.4
|
|
48
|
+
Provides-Extra: dev
|
|
49
|
+
Requires-Dist: black; extra == "dev"
|
|
50
|
+
Requires-Dist: ipython; extra == "dev"
|
|
51
|
+
Requires-Dist: mypy; extra == "dev"
|
|
52
|
+
Requires-Dist: pre-commit; extra == "dev"
|
|
53
|
+
Requires-Dist: pytest; extra == "dev"
|
|
54
|
+
Requires-Dist: pytest-coverage; extra == "dev"
|
|
55
|
+
Requires-Dist: ruff; extra == "dev"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
<div align="center">
|
|
59
|
+
|
|
60
|
+
# SemHash: Fast Semantic Text Deduplication
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
[Quickstart](#quickstart) •
|
|
64
|
+
[Main Features](#main-features) •
|
|
65
|
+
[Usage](#usage)
|
|
66
|
+
[Benchmarks](#benchmarks)
|
|
67
|
+
|
|
68
|
+
</div>
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
SemHash is a technique to efficiently deduplicate datasets based on semantic similarity. It uses a combination of lightning-fast embeddings through [model2vec](https://github.com/MinishLab/model2vec) and ANN-based similarity search through [vicinity](https://github.com/MinishLab/vicinity).
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
## Quickstart
|
|
76
|
+
|
|
77
|
+
Install the package with:
|
|
78
|
+
```bash
|
|
79
|
+
pip install semhash
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Deduplicate a single dataset with the following code:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from model2vec import StaticModel
|
|
86
|
+
from semhash import SemHash
|
|
87
|
+
|
|
88
|
+
# Load an embedding model
|
|
89
|
+
model = StaticModel.from_pretrained("minishlab/potion-base-8M")
|
|
90
|
+
|
|
91
|
+
# Initialize a SemHash with the model
|
|
92
|
+
semhash = SemHash(model=model)
|
|
93
|
+
|
|
94
|
+
# Create some texts to deduplicate
|
|
95
|
+
texts = [
|
|
96
|
+
"It's dangerous to go alone!",
|
|
97
|
+
"It's dangerous to go alone!", # Exact duplicate
|
|
98
|
+
"It's not safe to go by yourself!", # Semantically similar
|
|
99
|
+
]
|
|
100
|
+
|
|
101
|
+
# Deduplicate the texts
|
|
102
|
+
deduplicated_texts = semhash.fit_deduplicate(records=texts, threshold=0.5)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
For more advanced usage, you can also deduplicate across multiple datasets, or deduplicate multi-column datasets. Examples are provided in the [usage](#usage) section.
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
## Main Features
|
|
109
|
+
|
|
110
|
+
- **Fast**: SemHash uses model2vec to embed texts and vicinity to perform similarity search, making it extremely fast.
|
|
111
|
+
- **Scalable**: SemHash can deduplicate large datasets with millions of records thanks to the ANN backends in Vicinity.
|
|
112
|
+
- **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).
|
|
113
|
+
- **Lightweight**: SemHash is a lightweight package with minimal dependencies, making it easy to install and use.
|
|
114
|
+
|
|
115
|
+
## Usage
|
|
116
|
+
|
|
117
|
+
<details>
|
|
118
|
+
<summary> Deduplicate a single dataset </summary>
|
|
119
|
+
<br>
|
|
120
|
+
|
|
121
|
+
The following code snippet shows how to deduplicate a single dataset using SemHash:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from model2vec import StaticModel
|
|
125
|
+
from semhash import SemHash
|
|
126
|
+
|
|
127
|
+
# Load an embedding model
|
|
128
|
+
model = StaticModel.from_pretrained("minishlab/potion-base-8M")
|
|
129
|
+
|
|
130
|
+
# Initialize a SemHash with the model
|
|
131
|
+
semhash = SemHash(model=model)
|
|
132
|
+
|
|
133
|
+
# Create some texts to deduplicate
|
|
134
|
+
texts = [
|
|
135
|
+
"It's dangerous to go alone!",
|
|
136
|
+
"It's dangerous to go alone!", # Exact duplicate
|
|
137
|
+
"It's not safe to go by yourself!", # Semantically similar
|
|
138
|
+
]
|
|
139
|
+
|
|
140
|
+
# Deduplicate the texts
|
|
141
|
+
deduplicated_texts = semhash.fit_deduplicate(records=texts, threshold=0.5)
|
|
142
|
+
```
|
|
143
|
+
</details>
|
|
144
|
+
|
|
145
|
+
<details>
|
|
146
|
+
<summary> Deduplicate across two datasets </summary>
|
|
147
|
+
<br>
|
|
148
|
+
|
|
149
|
+
The following code snippet shows how to deduplicate across two datasets using SemHash (in this example, a training and test dataset):
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
from model2vec import StaticModel
|
|
153
|
+
from semhash import SemHash
|
|
154
|
+
|
|
155
|
+
# Load an embedding model
|
|
156
|
+
model = StaticModel.from_pretrained("minishlab/potion-base-8M")
|
|
157
|
+
|
|
158
|
+
# Initialize a SemHash with the model
|
|
159
|
+
semhash = SemHash(model=model)
|
|
160
|
+
|
|
161
|
+
# Create some texts to deduplicate
|
|
162
|
+
train = [
|
|
163
|
+
"It's dangerous to go alone!",
|
|
164
|
+
"It's a secret to everybody.",
|
|
165
|
+
"Ganondorf has invaded Hyrule!",
|
|
166
|
+
]
|
|
167
|
+
test = [
|
|
168
|
+
"It's dangerous to go alone!", # Exact duplicate
|
|
169
|
+
"It's not safe to go by yourself!", # Semantically similar
|
|
170
|
+
"The master sword seals the darkness",
|
|
171
|
+
]
|
|
172
|
+
|
|
173
|
+
# Fit on the training data
|
|
174
|
+
semhash.fit(records=train)
|
|
175
|
+
# Deduplicate the test data against the training data
|
|
176
|
+
deduplicated_texts = semhash.deduplicate(records=test, threshold=0.5)
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
</details>
|
|
180
|
+
|
|
181
|
+
<details>
|
|
182
|
+
<summary> Deduplicate multi-column datasets </summary>
|
|
183
|
+
<br>
|
|
184
|
+
|
|
185
|
+
The following code snippet shows how to deduplicate multi-column datasets using SemHash (in this example, a QA dataset with questions, contexts, and answers):
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
from model2vec import StaticModel
|
|
189
|
+
from semhash import SemHash
|
|
190
|
+
|
|
191
|
+
# Load an embedding model
|
|
192
|
+
model = StaticModel.from_pretrained("minishlab/potion-base-8M")
|
|
193
|
+
|
|
194
|
+
# Initialize a SemHash with the model and columns to deduplicate
|
|
195
|
+
semhash = SemHash(model=model, columns=["question", "context", "answer"])
|
|
196
|
+
|
|
197
|
+
# Create some texts to deduplicate
|
|
198
|
+
records = [
|
|
199
|
+
{"question": "What is the hero's name?", "context": "The hero is Link", "answer": "Link"},
|
|
200
|
+
{"question": "What is the hero's name?", "context": "The hero is Link", "answer": "Link"}, # Exact duplicate
|
|
201
|
+
{
|
|
202
|
+
"question": "Who is the protagonist?",
|
|
203
|
+
"context": "In this story, Link is the hero",
|
|
204
|
+
"answer": "Link",
|
|
205
|
+
}, # Semantically similar
|
|
206
|
+
{"question": "Who is the princess?", "context": "The princess is Zelda", "answer": "Zelda"},
|
|
207
|
+
]
|
|
208
|
+
|
|
209
|
+
# Deduplicate the records
|
|
210
|
+
deduplicated_records = semhash.fit_deduplicate(records=records, threshold=0.5)
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
</details>
|
|
214
|
+
|
|
215
|
+
## Benchmarks
|
|
216
|
+
|
|
217
|
+
We've benchmarked SemHash on a variety of datasets to measure the deduplication performance and speed. The benchmarks were run with the following setup:
|
|
218
|
+
- The benchmarks were all run on CPU
|
|
219
|
+
- The benchmarks were all run with `ann=True`
|
|
220
|
+
- The timings include the encoding time, index building time, and deduplication time.
|
|
221
|
+
|
|
222
|
+
### Train Deduplication Benchmark
|
|
223
|
+
|
|
224
|
+
| Dataset | Original Train Size | Deduplicated Train Size | % Removed | Deduplication Time (s) |
|
|
225
|
+
| --- | --- | --- | --- | --- |
|
|
226
|
+
| bbc | 1225 | 1144 | 6.61 | 0.23 |
|
|
227
|
+
| senteval_cr | 3012 | 2990 | 0.73 | 0.13 |
|
|
228
|
+
| tweet_sentiment_extraction | 27481 | 26695 | 2.86 | 1.64 |
|
|
229
|
+
| emotion | 16000 | 15696 | 1.90 | 0.66 |
|
|
230
|
+
| amazon_counterfactual | 5000 | 4992 | 0.16 | 0.31 |
|
|
231
|
+
| ag_news | 120000 | 106921 | 10.90 | 4.21 |
|
|
232
|
+
| enron_spam | 31716 | 20539 | 35.24 | 1.57 |
|
|
233
|
+
| subj | 8000 | 7990 | 0.12 | 0.57 |
|
|
234
|
+
| sst5 | 8544 | 8526 | 0.21 | 0.55 |
|
|
235
|
+
| 20_newgroups | 11314 | 10685 | 5.56 | 0.69 |
|
|
236
|
+
| hatespeech_offensive | 22783 | 22090 | 3.04 | 0.84 |
|
|
237
|
+
| ade | 17637 | 15718 | 10.88 | 0.68 |
|
|
238
|
+
| imdb | 25000 | 24832 | 0.67 | 1.65 |
|
|
239
|
+
| massive_scenario | 11514 | 9366 | 18.66 | 0.43 |
|
|
240
|
+
| student | 117519 | 63865 | 45.66 | 4.18 |
|
|
241
|
+
| squad_v2 | 130319 | 115546 | 11.34 | 11.20 |
|
|
242
|
+
| wikitext | 1801350 | 884586 | 50.89 | 56.11 |
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
### Train/Test Deduplication Benchmark
|
|
246
|
+
|
|
247
|
+
| Dataset | Train Size | Test Size | Deduplicated Test Size | % Removed | Deduplication Time (s) |
|
|
248
|
+
| --- | --- | --- | --- | --- | --- |
|
|
249
|
+
| bbc | 1225 | 1000 | 875 | 12.50 | 0.39 |
|
|
250
|
+
| senteval_cr | 3012 | 753 | 750 | 0.40 | 0.11 |
|
|
251
|
+
| tweet_sentiment_extraction | 27481 | 3534 | 3411 | 3.48 | 0.86 |
|
|
252
|
+
| emotion | 16000 | 2000 | 1926 | 3.70 | 0.56 |
|
|
253
|
+
| amazon_counterfactual | 5000 | 5000 | 4990 | 0.20 | 0.48 |
|
|
254
|
+
| ag_news | 120000 | 7600 | 6197 | 18.46 | 3.38 |
|
|
255
|
+
| enron_spam | 31716 | 2000 | 1063 | 46.85 | 1.98 |
|
|
256
|
+
| subj | 8000 | 2000 | 1999 | 0.05 | 0.58 |
|
|
257
|
+
| sst5 | 8544 | 2210 | 2205 | 0.23 | 0.56 |
|
|
258
|
+
| 20_newgroups | 11314 | 7532 | 7310 | 2.95 | 2.26 |
|
|
259
|
+
| hatespeech_offensive | 22783 | 2000 | 1926 | 3.70 | 0.72 |
|
|
260
|
+
| ade | 17637 | 5879 | 4954 | 15.73 | 0.82 |
|
|
261
|
+
| imdb | 25000 | 25000 | 24805 | 0.78 | 2.65 |
|
|
262
|
+
| massive_scenario | 11514 | 2974 | 2188 | 26.43 | 0.43 |
|
|
263
|
+
| student | 117519 | 5000 | 2395 | 52.10 | 3.02 |
|
|
264
|
+
| squad_v2 | 130319 | 11873 | 11869 | 0.03 | 9.11 |
|
|
265
|
+
| wikitext | 1801350 | 4358 | 3610 | 17.16 | 36.10 |
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
As can be seen, SemHash is extremely fast, and scales to large datasets with millions of records. There are some notable examples of train/test leakage, such as `enron_spam` and `student`, where the test dataset contains a significant amount of semantic overlap with the training dataset.
|
|
269
|
+
|
|
270
|
+
### Reproducing the Benchmarks
|
|
271
|
+
|
|
272
|
+
To run the benchmarks yourself, you can use the following command:
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
python -m benchmarks.run_benchmarks
|
|
276
|
+
```
|
|
277
|
+
Optionally, the datasets can be updated in the [datasets.py](https://github.com/MinishLab/semhash/blob/main/benchmarks/datasets.py) file.
|