semhash 0.3.0__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.
- {semhash-0.3.0 → semhash-0.3.1}/PKG-INFO +14 -8
- {semhash-0.3.0 → semhash-0.3.1}/README.md +13 -7
- semhash-0.3.1/assets/images/semhash_logo_v2.png +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/semhash/datamodels.py +62 -3
- {semhash-0.3.0 → semhash-0.3.1}/semhash/records.py +0 -7
- {semhash-0.3.0 → semhash-0.3.1}/semhash/semhash.py +7 -5
- {semhash-0.3.0 → semhash-0.3.1}/semhash/utils.py +6 -0
- {semhash-0.3.0 → semhash-0.3.1}/semhash/version.py +1 -1
- {semhash-0.3.0 → semhash-0.3.1}/semhash.egg-info/PKG-INFO +14 -8
- {semhash-0.3.0 → semhash-0.3.1}/semhash.egg-info/SOURCES.txt +1 -0
- semhash-0.3.1/tests/test_datamodels.py +221 -0
- semhash-0.3.0/tests/test_datamodels.py +0 -118
- {semhash-0.3.0 → semhash-0.3.1}/.github/workflows/ci.yaml +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/.gitignore +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/.pre-commit-config.yaml +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/LICENSE +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/Makefile +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/assets/images/semhash_logo.png +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/benchmarks/__init__.py +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/benchmarks/data.py +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/benchmarks/results/train_benchmark_results.json +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/benchmarks/results/train_test_benchmark_results.json +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/benchmarks/run_benchmarks.py +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/pyproject.toml +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/semhash/__init__.py +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/semhash/index.py +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/semhash.egg-info/dependency_links.txt +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/semhash.egg-info/requires.txt +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/semhash.egg-info/top_level.txt +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/setup.cfg +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/tests/conftest.py +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/tests/data/test_model/README.md +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/tests/data/test_model/config.json +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/tests/data/test_model/model.safetensors +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/tests/data/test_model/modules.json +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/tests/data/test_model/tokenizer.json +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/tests/test_semhash.py +0 -0
- {semhash-0.3.0 → semhash-0.3.1}/uv.lock +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: semhash
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.1
|
|
4
4
|
Summary: Fast Semantic Text Deduplication & Filtering
|
|
5
5
|
Author-email: Thomas van Dongen <thomas123@live.nl>, Stéphan Tulkens <stephantul@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -56,21 +56,23 @@ Requires-Dist: ruff; extra == "dev"
|
|
|
56
56
|
|
|
57
57
|
|
|
58
58
|
|
|
59
|
-
<
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
</div>
|
|
59
|
+
<h2 align="center">
|
|
60
|
+
<img width="30%" alt="SemHash logo" src="assets/images/semhash_logo_v2.png"><br/>
|
|
61
|
+
Fast Semantic Text Deduplication & Filtering
|
|
62
|
+
</h2>
|
|
63
|
+
|
|
65
64
|
|
|
66
65
|
<div align="center">
|
|
67
66
|
|
|
68
|
-
|
|
67
|
+
|
|
69
68
|
|
|
70
69
|
|
|
71
70
|
<h2>
|
|
72
71
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/v/semhash?color=%23007ec6&label=pypi%20package" alt="Package version"></a>
|
|
73
72
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/pyversions/semhash" alt="Supported Python versions"></a>
|
|
73
|
+
<a href="https://pepy.tech/project/semhash">
|
|
74
|
+
<img src="https://static.pepy.tech/badge/semhash" alt="Downloads">
|
|
75
|
+
</a>
|
|
74
76
|
<a href="https://app.codecov.io/gh/MinishLab/semhash">
|
|
75
77
|
<img src="https://codecov.io/gh/MinishLab/semhash/graph/badge.svg?token=YPOD6HD0MG" alt="Codecov">
|
|
76
78
|
</a>
|
|
@@ -318,6 +320,10 @@ least_similar = deduplication_result.get_least_similar_from_duplicates()
|
|
|
318
320
|
|
|
319
321
|
# Rethreshold the duplicates. This allows you to instantly rethreshold the duplicates with a new threshold without having to re-deduplicate the texts.
|
|
320
322
|
deduplication_result.rethreshold(0.95)
|
|
323
|
+
|
|
324
|
+
# View selected records along with their duplicates.
|
|
325
|
+
# This is the opposite of the `filtered` attribute, which shows for every duplicate the record that caused it.
|
|
326
|
+
deduplication_result.selected_with_duplicates
|
|
321
327
|
```
|
|
322
328
|
|
|
323
329
|
</details>
|
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
<
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
</div>
|
|
3
|
+
<h2 align="center">
|
|
4
|
+
<img width="30%" alt="SemHash logo" src="assets/images/semhash_logo_v2.png"><br/>
|
|
5
|
+
Fast Semantic Text Deduplication & Filtering
|
|
6
|
+
</h2>
|
|
7
|
+
|
|
9
8
|
|
|
10
9
|
<div align="center">
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
|
|
13
12
|
|
|
14
13
|
|
|
15
14
|
<h2>
|
|
16
15
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/v/semhash?color=%23007ec6&label=pypi%20package" alt="Package version"></a>
|
|
17
16
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/pyversions/semhash" alt="Supported Python versions"></a>
|
|
17
|
+
<a href="https://pepy.tech/project/semhash">
|
|
18
|
+
<img src="https://static.pepy.tech/badge/semhash" alt="Downloads">
|
|
19
|
+
</a>
|
|
18
20
|
<a href="https://app.codecov.io/gh/MinishLab/semhash">
|
|
19
21
|
<img src="https://codecov.io/gh/MinishLab/semhash/graph/badge.svg?token=YPOD6HD0MG" alt="Codecov">
|
|
20
22
|
</a>
|
|
@@ -262,6 +264,10 @@ least_similar = deduplication_result.get_least_similar_from_duplicates()
|
|
|
262
264
|
|
|
263
265
|
# Rethreshold the duplicates. This allows you to instantly rethreshold the duplicates with a new threshold without having to re-deduplicate the texts.
|
|
264
266
|
deduplication_result.rethreshold(0.95)
|
|
267
|
+
|
|
268
|
+
# View selected records along with their duplicates.
|
|
269
|
+
# This is the opposite of the `filtered` attribute, which shows for every duplicate the record that caused it.
|
|
270
|
+
deduplication_result.selected_with_duplicates
|
|
265
271
|
```
|
|
266
272
|
|
|
267
273
|
</details>
|
|
Binary file
|
|
@@ -1,8 +1,17 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
import warnings
|
|
4
|
+
from collections import defaultdict
|
|
2
5
|
from dataclasses import dataclass, field
|
|
3
|
-
from typing import Generic, TypeVar
|
|
6
|
+
from typing import Any, Generic, Hashable, Sequence, TypeVar
|
|
7
|
+
|
|
8
|
+
from frozendict import frozendict
|
|
9
|
+
from typing_extensions import TypeAlias
|
|
10
|
+
|
|
11
|
+
from semhash.utils import to_frozendict
|
|
4
12
|
|
|
5
|
-
Record = TypeVar("Record", str, dict[str,
|
|
13
|
+
Record = TypeVar("Record", str, dict[str, Any])
|
|
14
|
+
DuplicateList: TypeAlias = list[tuple[Record, float]]
|
|
6
15
|
|
|
7
16
|
|
|
8
17
|
@dataclass
|
|
@@ -20,13 +29,29 @@ class DuplicateRecord(Generic[Record]):
|
|
|
20
29
|
|
|
21
30
|
record: Record
|
|
22
31
|
exact: bool
|
|
23
|
-
duplicates:
|
|
32
|
+
duplicates: DuplicateList = field(default_factory=list)
|
|
24
33
|
|
|
25
34
|
def _rethreshold(self, threshold: float) -> None:
|
|
26
35
|
"""Rethreshold the duplicates."""
|
|
27
36
|
self.duplicates = [(d, score) for d, score in self.duplicates if score >= threshold]
|
|
28
37
|
|
|
29
38
|
|
|
39
|
+
@dataclass
|
|
40
|
+
class SelectedWithDuplicates(Generic[Record]):
|
|
41
|
+
"""
|
|
42
|
+
A record that has been selected along with its duplicates.
|
|
43
|
+
|
|
44
|
+
Attributes
|
|
45
|
+
----------
|
|
46
|
+
record: The original record being selected.
|
|
47
|
+
duplicates: List of tuples consisting of duplicate records and their associated scores.
|
|
48
|
+
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
record: Record
|
|
52
|
+
duplicates: DuplicateList = field(default_factory=list)
|
|
53
|
+
|
|
54
|
+
|
|
30
55
|
@dataclass
|
|
31
56
|
class DeduplicationResult(Generic[Record]):
|
|
32
57
|
"""
|
|
@@ -37,6 +62,7 @@ class DeduplicationResult(Generic[Record]):
|
|
|
37
62
|
selected: List of deduplicated records after removing duplicates.
|
|
38
63
|
filtered: List of DuplicateRecord objects containing details about duplicates of an original record.
|
|
39
64
|
threshold: The similarity threshold used for deduplication.
|
|
65
|
+
columns: Columns used for deduplication.
|
|
40
66
|
deduplicated: Deprecated, use selected instead.
|
|
41
67
|
duplicates: Deprecated, use filtered instead.
|
|
42
68
|
|
|
@@ -45,6 +71,7 @@ class DeduplicationResult(Generic[Record]):
|
|
|
45
71
|
selected: list[Record] = field(default_factory=list)
|
|
46
72
|
filtered: list[DuplicateRecord] = field(default_factory=list)
|
|
47
73
|
threshold: float = field(default=0.9)
|
|
74
|
+
columns: Sequence[str] | None = field(default=None)
|
|
48
75
|
deduplicated: list[Record] = field(default_factory=list) # Deprecated
|
|
49
76
|
duplicates: list[DuplicateRecord] = field(default_factory=list) # Deprecated
|
|
50
77
|
|
|
@@ -102,6 +129,38 @@ class DeduplicationResult(Generic[Record]):
|
|
|
102
129
|
self.selected.append(dup.record)
|
|
103
130
|
self.threshold = threshold
|
|
104
131
|
|
|
132
|
+
@property
|
|
133
|
+
def selected_with_duplicates(self) -> list[SelectedWithDuplicates[Record]]:
|
|
134
|
+
"""
|
|
135
|
+
For every kept record, return the duplicates that were removed along with their similarity scores.
|
|
136
|
+
|
|
137
|
+
:return: A list of tuples where each tuple contains a kept record
|
|
138
|
+
and a list of its duplicates with their similarity scores.
|
|
139
|
+
"""
|
|
140
|
+
|
|
141
|
+
def _to_hashable(record: Record) -> frozendict[str, str] | str:
|
|
142
|
+
"""Convert a record to a hashable representation."""
|
|
143
|
+
if isinstance(record, dict) and self.columns is not None:
|
|
144
|
+
# Convert dict to frozendict for immutability and hashability
|
|
145
|
+
return to_frozendict(record, set(self.columns))
|
|
146
|
+
return str(record)
|
|
147
|
+
|
|
148
|
+
# Build a mapping from original-record to [(duplicate, score), …]
|
|
149
|
+
buckets: defaultdict[Hashable, DuplicateList] = defaultdict(list)
|
|
150
|
+
for duplicate_record in self.filtered:
|
|
151
|
+
for original_record, score in duplicate_record.duplicates:
|
|
152
|
+
buckets[_to_hashable(original_record)].append((duplicate_record.record, float(score)))
|
|
153
|
+
|
|
154
|
+
result: list[SelectedWithDuplicates[Record]] = []
|
|
155
|
+
for selected in self.selected:
|
|
156
|
+
# Get the list of duplicates for the selected record
|
|
157
|
+
raw_list = buckets.get(_to_hashable(selected), [])
|
|
158
|
+
# Ensure we don't have duplicates in the list
|
|
159
|
+
deduped = {_to_hashable(rec): (rec, score) for rec, score in raw_list}
|
|
160
|
+
result.append(SelectedWithDuplicates(record=selected, duplicates=list(deduped.values())))
|
|
161
|
+
|
|
162
|
+
return result
|
|
163
|
+
|
|
105
164
|
|
|
106
165
|
@dataclass
|
|
107
166
|
class FilterResult(Generic[Record]):
|
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
from typing import Sequence
|
|
2
2
|
|
|
3
|
-
from frozendict import frozendict
|
|
4
|
-
|
|
5
3
|
from semhash.datamodels import DeduplicationResult, DuplicateRecord
|
|
6
4
|
|
|
7
5
|
|
|
8
|
-
def to_frozendict(record: dict[str, str], columns: set[str]) -> frozendict[str, str]:
|
|
9
|
-
"""Convert a record to a frozendict."""
|
|
10
|
-
return frozendict({k: record.get(k, "") for k in columns})
|
|
11
|
-
|
|
12
|
-
|
|
13
6
|
def dict_to_string(record: dict[str, str], columns: Sequence[str]) -> str:
|
|
14
7
|
r"""
|
|
15
8
|
Turn a record into a single string.
|
|
@@ -11,8 +11,8 @@ from vicinity import Backend
|
|
|
11
11
|
|
|
12
12
|
from semhash.datamodels import DeduplicationResult, DuplicateRecord, FilterResult, Record
|
|
13
13
|
from semhash.index import Index
|
|
14
|
-
from semhash.records import add_scores_to_records, map_deduplication_result_to_strings
|
|
15
|
-
from semhash.utils import Encoder, compute_candidate_limit
|
|
14
|
+
from semhash.records import add_scores_to_records, map_deduplication_result_to_strings
|
|
15
|
+
from semhash.utils import Encoder, compute_candidate_limit, to_frozendict
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
class SemHash(Generic[Record]):
|
|
@@ -190,7 +190,9 @@ class SemHash(Generic[Record]):
|
|
|
190
190
|
|
|
191
191
|
# If no records are left after removing exact duplicates, return early
|
|
192
192
|
if not dict_records:
|
|
193
|
-
return DeduplicationResult(
|
|
193
|
+
return DeduplicationResult(
|
|
194
|
+
deduplicated=[], duplicates=duplicate_records, threshold=threshold, columns=self.columns
|
|
195
|
+
)
|
|
194
196
|
|
|
195
197
|
# Compute embeddings for the new records
|
|
196
198
|
embeddings = self._featurize(records=dict_records, columns=self.columns, model=self.model)
|
|
@@ -212,7 +214,7 @@ class SemHash(Generic[Record]):
|
|
|
212
214
|
)
|
|
213
215
|
|
|
214
216
|
result = DeduplicationResult(
|
|
215
|
-
deduplicated=deduplicated_records, duplicates=duplicate_records, threshold=threshold
|
|
217
|
+
deduplicated=deduplicated_records, duplicates=duplicate_records, threshold=threshold, columns=self.columns
|
|
216
218
|
)
|
|
217
219
|
|
|
218
220
|
if self._was_string:
|
|
@@ -281,7 +283,7 @@ class SemHash(Generic[Record]):
|
|
|
281
283
|
seen_items.update(frozen_items)
|
|
282
284
|
|
|
283
285
|
result = DeduplicationResult(
|
|
284
|
-
deduplicated=deduplicated_records, duplicates=duplicate_records, threshold=threshold
|
|
286
|
+
deduplicated=deduplicated_records, duplicates=duplicate_records, threshold=threshold, columns=self.columns
|
|
285
287
|
)
|
|
286
288
|
|
|
287
289
|
if self._was_string:
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from typing import Any, Protocol, Sequence, Union
|
|
2
2
|
|
|
3
3
|
import numpy as np
|
|
4
|
+
from frozendict import frozendict
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
class Encoder(Protocol):
|
|
@@ -21,6 +22,11 @@ class Encoder(Protocol):
|
|
|
21
22
|
... # pragma: no cover
|
|
22
23
|
|
|
23
24
|
|
|
25
|
+
def to_frozendict(record: dict[str, str], columns: set[str]) -> frozendict[str, str]:
|
|
26
|
+
"""Convert a record to a frozendict."""
|
|
27
|
+
return frozendict({k: record.get(k, "") for k in columns})
|
|
28
|
+
|
|
29
|
+
|
|
24
30
|
def compute_candidate_limit(
|
|
25
31
|
total: int,
|
|
26
32
|
selection_size: int,
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
__version_triple__ = (0, 3,
|
|
1
|
+
__version_triple__ = (0, 3, 1)
|
|
2
2
|
__version__ = ".".join(map(str, __version_triple__))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: semhash
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.1
|
|
4
4
|
Summary: Fast Semantic Text Deduplication & Filtering
|
|
5
5
|
Author-email: Thomas van Dongen <thomas123@live.nl>, Stéphan Tulkens <stephantul@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -56,21 +56,23 @@ Requires-Dist: ruff; extra == "dev"
|
|
|
56
56
|
|
|
57
57
|
|
|
58
58
|
|
|
59
|
-
<
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
</div>
|
|
59
|
+
<h2 align="center">
|
|
60
|
+
<img width="30%" alt="SemHash logo" src="assets/images/semhash_logo_v2.png"><br/>
|
|
61
|
+
Fast Semantic Text Deduplication & Filtering
|
|
62
|
+
</h2>
|
|
63
|
+
|
|
65
64
|
|
|
66
65
|
<div align="center">
|
|
67
66
|
|
|
68
|
-
|
|
67
|
+
|
|
69
68
|
|
|
70
69
|
|
|
71
70
|
<h2>
|
|
72
71
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/v/semhash?color=%23007ec6&label=pypi%20package" alt="Package version"></a>
|
|
73
72
|
<a href="https://pypi.org/project/semhash/"><img src="https://img.shields.io/pypi/pyversions/semhash" alt="Supported Python versions"></a>
|
|
73
|
+
<a href="https://pepy.tech/project/semhash">
|
|
74
|
+
<img src="https://static.pepy.tech/badge/semhash" alt="Downloads">
|
|
75
|
+
</a>
|
|
74
76
|
<a href="https://app.codecov.io/gh/MinishLab/semhash">
|
|
75
77
|
<img src="https://codecov.io/gh/MinishLab/semhash/graph/badge.svg?token=YPOD6HD0MG" alt="Codecov">
|
|
76
78
|
</a>
|
|
@@ -318,6 +320,10 @@ least_similar = deduplication_result.get_least_similar_from_duplicates()
|
|
|
318
320
|
|
|
319
321
|
# Rethreshold the duplicates. This allows you to instantly rethreshold the duplicates with a new threshold without having to re-deduplicate the texts.
|
|
320
322
|
deduplication_result.rethreshold(0.95)
|
|
323
|
+
|
|
324
|
+
# View selected records along with their duplicates.
|
|
325
|
+
# This is the opposite of the `filtered` attribute, which shows for every duplicate the record that caused it.
|
|
326
|
+
deduplication_result.selected_with_duplicates
|
|
321
327
|
```
|
|
322
328
|
|
|
323
329
|
</details>
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
import semhash
|
|
4
|
+
import semhash.version
|
|
5
|
+
from semhash.datamodels import DeduplicationResult, DuplicateRecord, SelectedWithDuplicates
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_deduplication_scoring() -> None:
|
|
9
|
+
"""Test the deduplication scoring."""
|
|
10
|
+
d = DeduplicationResult(
|
|
11
|
+
["a", "b", "c"],
|
|
12
|
+
[DuplicateRecord("a", False, [("b", 0.9)]), DuplicateRecord("b", False, [("c", 0.8)])],
|
|
13
|
+
0.8,
|
|
14
|
+
)
|
|
15
|
+
assert d.duplicate_ratio == 0.4
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_deduplication_scoring_exact() -> None:
|
|
19
|
+
"""Test the deduplication scoring."""
|
|
20
|
+
d = DeduplicationResult(
|
|
21
|
+
["a", "b", "c"],
|
|
22
|
+
[DuplicateRecord("a", True, [("b", 0.9)]), DuplicateRecord("b", False, [("c", 0.8)])],
|
|
23
|
+
0.8,
|
|
24
|
+
)
|
|
25
|
+
assert d.exact_duplicate_ratio == 0.2
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_deduplication_scoring_exact_empty() -> None:
|
|
29
|
+
"""Test the deduplication scoring."""
|
|
30
|
+
d = DeduplicationResult([], [], 0.8, columns=["text"])
|
|
31
|
+
assert d.exact_duplicate_ratio == 0.0
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_deduplication_scoring_empty() -> None:
|
|
35
|
+
"""Test the deduplication scoring."""
|
|
36
|
+
d = DeduplicationResult([], [], 0.8, columns=["text"])
|
|
37
|
+
assert d.duplicate_ratio == 0.0
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_rethreshold() -> None:
|
|
41
|
+
"""Test rethresholding the duplicates."""
|
|
42
|
+
d = DuplicateRecord("a", False, [("b", 0.9), ("c", 0.8)])
|
|
43
|
+
d._rethreshold(0.85)
|
|
44
|
+
assert d.duplicates == [("b", 0.9)]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_rethreshold_empty() -> None:
|
|
48
|
+
"""Test rethresholding the duplicates."""
|
|
49
|
+
d = DuplicateRecord("a", False, [])
|
|
50
|
+
d._rethreshold(0.85)
|
|
51
|
+
assert d.duplicates == []
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_get_least_similar_from_duplicates() -> None:
|
|
55
|
+
"""Test getting the least similar duplicates."""
|
|
56
|
+
d = DeduplicationResult(
|
|
57
|
+
["a", "b", "c"],
|
|
58
|
+
[DuplicateRecord("a", False, [("b", 0.9), ("c", 0.7)]), DuplicateRecord("b", False, [("c", 0.8)])],
|
|
59
|
+
0.8,
|
|
60
|
+
)
|
|
61
|
+
result = d.get_least_similar_from_duplicates(1)
|
|
62
|
+
assert result == [("a", "c", 0.7)]
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_get_least_similar_from_duplicates_empty() -> None:
|
|
66
|
+
"""Test getting the least similar duplicates."""
|
|
67
|
+
d = DeduplicationResult([], [], 0.8, columns=["text"])
|
|
68
|
+
assert d.get_least_similar_from_duplicates(1) == []
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_rethreshold_deduplication_result() -> None:
|
|
72
|
+
"""Test rethresholding the duplicates."""
|
|
73
|
+
d = DeduplicationResult(
|
|
74
|
+
["a", "b", "c"],
|
|
75
|
+
[
|
|
76
|
+
DuplicateRecord("d", False, [("x", 0.9), ("y", 0.8)]),
|
|
77
|
+
DuplicateRecord("e", False, [("z", 0.8)]),
|
|
78
|
+
],
|
|
79
|
+
0.8,
|
|
80
|
+
)
|
|
81
|
+
d.rethreshold(0.85)
|
|
82
|
+
assert d.filtered == [DuplicateRecord("d", False, [("x", 0.9)])]
|
|
83
|
+
assert d.selected == ["a", "b", "c", "e"]
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_rethreshold_exception() -> None:
|
|
87
|
+
"""Test rethresholding throws an exception."""
|
|
88
|
+
d = DeduplicationResult(
|
|
89
|
+
["a", "b", "c"],
|
|
90
|
+
[
|
|
91
|
+
DuplicateRecord("d", False, [("x", 0.9), ("y", 0.8)]),
|
|
92
|
+
DuplicateRecord("e", False, [("z", 0.8)]),
|
|
93
|
+
],
|
|
94
|
+
0.7,
|
|
95
|
+
)
|
|
96
|
+
with pytest.raises(ValueError):
|
|
97
|
+
d.rethreshold(0.6)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_deprecation_deduplicated_duplicates() -> None:
|
|
101
|
+
"""Test deprecation warnings for deduplicated and duplicates fields."""
|
|
102
|
+
if semhash.version.__version__ < "0.4.0":
|
|
103
|
+
with pytest.warns(DeprecationWarning):
|
|
104
|
+
d = DeduplicationResult(
|
|
105
|
+
deduplicated=["a", "b", "c"],
|
|
106
|
+
duplicates=[
|
|
107
|
+
DuplicateRecord("d", False, [("x", 0.9), ("y", 0.8)]),
|
|
108
|
+
DuplicateRecord("e", False, [("z", 0.8)]),
|
|
109
|
+
],
|
|
110
|
+
threshold=0.8,
|
|
111
|
+
)
|
|
112
|
+
else:
|
|
113
|
+
raise ValueError("deprecate `deduplicated` and `duplicates` fields in `DeduplicationResult`")
|
|
114
|
+
assert d.selected == ["a", "b", "c"]
|
|
115
|
+
assert d.filtered == [
|
|
116
|
+
DuplicateRecord("d", False, [("x", 0.9), ("y", 0.8)]),
|
|
117
|
+
DuplicateRecord("e", False, [("z", 0.8)]),
|
|
118
|
+
]
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def test_selected_with_duplicates_strings() -> None:
|
|
122
|
+
"""Test selected_with_duplicates for strings."""
|
|
123
|
+
d = DeduplicationResult(
|
|
124
|
+
selected=["original"],
|
|
125
|
+
filtered=[
|
|
126
|
+
DuplicateRecord("duplicate_1", False, [("original", 0.9)]),
|
|
127
|
+
DuplicateRecord("duplicate_2", False, [("original", 0.8)]),
|
|
128
|
+
],
|
|
129
|
+
threshold=0.8,
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
expected = [
|
|
133
|
+
SelectedWithDuplicates(
|
|
134
|
+
record="original",
|
|
135
|
+
duplicates=[("duplicate_1", 0.9), ("duplicate_2", 0.8)],
|
|
136
|
+
)
|
|
137
|
+
]
|
|
138
|
+
assert d.selected_with_duplicates == expected
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def test_selected_with_duplicates_dicts() -> None:
|
|
142
|
+
"""Test selected_with_duplicates for dicts."""
|
|
143
|
+
selected = {"id": 0, "text": "hello"}
|
|
144
|
+
d = DeduplicationResult(
|
|
145
|
+
selected=[selected],
|
|
146
|
+
filtered=[
|
|
147
|
+
DuplicateRecord({"id": 1, "text": "hello"}, True, [(selected, 1.0)]),
|
|
148
|
+
DuplicateRecord({"id": 2, "text": "helllo"}, False, [(selected, 0.1)]),
|
|
149
|
+
],
|
|
150
|
+
threshold=0.8,
|
|
151
|
+
columns=["text"],
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
items = d.selected_with_duplicates
|
|
155
|
+
assert len(items) == 1
|
|
156
|
+
kept = items[0].record
|
|
157
|
+
dups = items[0].duplicates
|
|
158
|
+
assert kept == selected
|
|
159
|
+
assert {r["id"] for r, _ in dups} == {1, 2}
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_selected_with_duplicates_multi_column() -> None:
|
|
163
|
+
"""Test selected_with_duplicates for multi-columns."""
|
|
164
|
+
selected = {"text": "hello", "text2": "world"}
|
|
165
|
+
d = DeduplicationResult(
|
|
166
|
+
selected=[selected],
|
|
167
|
+
filtered=[
|
|
168
|
+
DuplicateRecord({"text": "hello", "text2": "world"}, True, [(selected, 1.0)]),
|
|
169
|
+
DuplicateRecord({"text": "helllo", "text2": "world"}, False, [(selected, 0.1)]),
|
|
170
|
+
],
|
|
171
|
+
threshold=0.8,
|
|
172
|
+
columns=["text", "text2"],
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
items = d.selected_with_duplicates
|
|
176
|
+
assert len(items) == 1
|
|
177
|
+
kept = items[0].record
|
|
178
|
+
assert kept == selected
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def test_selected_with_duplicates_unhashable_values() -> None:
|
|
182
|
+
"""Test selected_with_duplicates with unhashable values in records."""
|
|
183
|
+
selected = {"text": "hello", "a": [1, 2, 3]} # list -> unhashable value
|
|
184
|
+
filtered = {"text": "hello", "a": [1, 2, 3], "flag": True}
|
|
185
|
+
|
|
186
|
+
d = DeduplicationResult(
|
|
187
|
+
selected=[selected],
|
|
188
|
+
filtered=[DuplicateRecord(filtered, exact=False, duplicates=[(selected, 1.0)])],
|
|
189
|
+
threshold=0.8,
|
|
190
|
+
columns=["text"],
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
items = d.selected_with_duplicates
|
|
194
|
+
assert items == [SelectedWithDuplicates(record=selected, duplicates=[(filtered, 1.0)])]
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def test_selected_with_duplicates_removes_internal_duplicates() -> None:
|
|
198
|
+
"""Test that selected_with_duplicates removes internal duplicates that have the same hash."""
|
|
199
|
+
selected = {"id": 0, "text": "hello"}
|
|
200
|
+
filtered = {"id": 1, "text": "hello"}
|
|
201
|
+
|
|
202
|
+
d = DeduplicationResult(
|
|
203
|
+
selected=[selected],
|
|
204
|
+
filtered=[
|
|
205
|
+
DuplicateRecord(filtered, exact=False, duplicates=[(selected, 0.95)]),
|
|
206
|
+
DuplicateRecord(filtered, exact=False, duplicates=[(selected, 0.90)]),
|
|
207
|
+
],
|
|
208
|
+
threshold=0.8,
|
|
209
|
+
columns=["text"],
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
items = d.selected_with_duplicates
|
|
213
|
+
assert len(items) == 1
|
|
214
|
+
|
|
215
|
+
selected_record = items[0].record
|
|
216
|
+
duplicate_list = items[0].duplicates
|
|
217
|
+
# Should keep the kept record unchanged
|
|
218
|
+
assert selected_record == selected
|
|
219
|
+
# The duplicate row must appear only once
|
|
220
|
+
assert len(duplicate_list) == 1
|
|
221
|
+
assert duplicate_list[0][0] == filtered
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import pytest
|
|
2
|
-
|
|
3
|
-
import semhash
|
|
4
|
-
import semhash.version
|
|
5
|
-
from semhash.datamodels import DeduplicationResult, DuplicateRecord
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def test_deduplication_scoring() -> None:
|
|
9
|
-
"""Test the deduplication scoring."""
|
|
10
|
-
d = DeduplicationResult(
|
|
11
|
-
["a", "b", "c"],
|
|
12
|
-
[DuplicateRecord("a", False, [("b", 0.9)]), DuplicateRecord("b", False, [("c", 0.8)])],
|
|
13
|
-
0.8,
|
|
14
|
-
)
|
|
15
|
-
assert d.duplicate_ratio == 0.4
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def test_deduplication_scoring_exact() -> None:
|
|
19
|
-
"""Test the deduplication scoring."""
|
|
20
|
-
d = DeduplicationResult(
|
|
21
|
-
["a", "b", "c"],
|
|
22
|
-
[DuplicateRecord("a", True, [("b", 0.9)]), DuplicateRecord("b", False, [("c", 0.8)])],
|
|
23
|
-
0.8,
|
|
24
|
-
)
|
|
25
|
-
assert d.exact_duplicate_ratio == 0.2
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def test_deduplication_scoring_exact_empty() -> None:
|
|
29
|
-
"""Test the deduplication scoring."""
|
|
30
|
-
d = DeduplicationResult([], [], 0.8)
|
|
31
|
-
assert d.exact_duplicate_ratio == 0.0
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
def test_deduplication_scoring_empty() -> None:
|
|
35
|
-
"""Test the deduplication scoring."""
|
|
36
|
-
d = DeduplicationResult([], [], 0.8)
|
|
37
|
-
assert d.duplicate_ratio == 0.0
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def test_rethreshold() -> None:
|
|
41
|
-
"""Test rethresholding the duplicates."""
|
|
42
|
-
d = DuplicateRecord("a", False, [("b", 0.9), ("c", 0.8)])
|
|
43
|
-
d._rethreshold(0.85)
|
|
44
|
-
assert d.duplicates == [("b", 0.9)]
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
def test_rethreshold_empty() -> None:
|
|
48
|
-
"""Test rethresholding the duplicates."""
|
|
49
|
-
d = DuplicateRecord("a", False, [])
|
|
50
|
-
d._rethreshold(0.85)
|
|
51
|
-
assert d.duplicates == []
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
def test_get_least_similar_from_duplicates() -> None:
|
|
55
|
-
"""Test getting the least similar duplicates."""
|
|
56
|
-
d = DeduplicationResult(
|
|
57
|
-
["a", "b", "c"],
|
|
58
|
-
[DuplicateRecord("a", False, [("b", 0.9), ("c", 0.7)]), DuplicateRecord("b", False, [("c", 0.8)])],
|
|
59
|
-
0.8,
|
|
60
|
-
)
|
|
61
|
-
result = d.get_least_similar_from_duplicates(1)
|
|
62
|
-
assert result == [("a", "c", 0.7)]
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
def test_get_least_similar_from_duplicates_empty() -> None:
|
|
66
|
-
"""Test getting the least similar duplicates."""
|
|
67
|
-
d = DeduplicationResult([], [], 0.8)
|
|
68
|
-
assert d.get_least_similar_from_duplicates(1) == []
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
def test_rethreshold_deduplication_result() -> None:
|
|
72
|
-
"""Test rethresholding the duplicates."""
|
|
73
|
-
d = DeduplicationResult(
|
|
74
|
-
["a", "b", "c"],
|
|
75
|
-
[
|
|
76
|
-
DuplicateRecord("d", False, [("x", 0.9), ("y", 0.8)]),
|
|
77
|
-
DuplicateRecord("e", False, [("z", 0.8)]),
|
|
78
|
-
],
|
|
79
|
-
0.8,
|
|
80
|
-
)
|
|
81
|
-
d.rethreshold(0.85)
|
|
82
|
-
assert d.filtered == [DuplicateRecord("d", False, [("x", 0.9)])]
|
|
83
|
-
assert d.selected == ["a", "b", "c", "e"]
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
def test_rethreshold_exception() -> None:
|
|
87
|
-
"""Test rethresholding throws an exception."""
|
|
88
|
-
d = DeduplicationResult(
|
|
89
|
-
["a", "b", "c"],
|
|
90
|
-
[
|
|
91
|
-
DuplicateRecord("d", False, [("x", 0.9), ("y", 0.8)]),
|
|
92
|
-
DuplicateRecord("e", False, [("z", 0.8)]),
|
|
93
|
-
],
|
|
94
|
-
0.7,
|
|
95
|
-
)
|
|
96
|
-
with pytest.raises(ValueError):
|
|
97
|
-
d.rethreshold(0.6)
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
def test_deprecation_deduplicated_duplicates() -> None:
|
|
101
|
-
"""Test deprecation warnings for deduplicated and duplicates fields."""
|
|
102
|
-
if semhash.version.__version__ < "0.4.0":
|
|
103
|
-
with pytest.warns(DeprecationWarning):
|
|
104
|
-
d = DeduplicationResult(
|
|
105
|
-
deduplicated=["a", "b", "c"],
|
|
106
|
-
duplicates=[
|
|
107
|
-
DuplicateRecord("d", False, [("x", 0.9), ("y", 0.8)]),
|
|
108
|
-
DuplicateRecord("e", False, [("z", 0.8)]),
|
|
109
|
-
],
|
|
110
|
-
threshold=0.8,
|
|
111
|
-
)
|
|
112
|
-
else:
|
|
113
|
-
raise ValueError("deprecate `deduplicated` and `duplicates` fields in `DeduplicationResult`")
|
|
114
|
-
assert d.selected == ["a", "b", "c"]
|
|
115
|
-
assert d.filtered == [
|
|
116
|
-
DuplicateRecord("d", False, [("x", 0.9), ("y", 0.8)]),
|
|
117
|
-
DuplicateRecord("e", False, [("z", 0.8)]),
|
|
118
|
-
]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|