-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathdocument_minhash_deduplicator.py
340 lines (293 loc) · 12.6 KB
/
document_minhash_deduplicator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# Some code here has been modified from:
# https://github.com/bigcode-project/bigcode-dataset/blob/main/near_deduplication/minhash_deduplication.py
# --------------------------------------------------------
import hashlib
import struct
from collections import defaultdict
from typing import Optional
import numpy as np
import regex
from loguru import logger
from pydantic import Field, PositiveInt
from tqdm import tqdm
from typing_extensions import Annotated
from data_juicer.utils.constant import HashKeys
from data_juicer.utils.lazy_loader import LazyLoader
from data_juicer.utils.model_utils import prepare_sentencepiece_model
from ..base_op import OPERATORS, Deduplicator
from ..common.helper_func import UnionFind, split_on_whitespace
integrate = LazyLoader('integrate', 'scipy.integrate')
OP_NAME = 'document_minhash_deduplicator'
MERSENNE_PRIME = np.uint64((1 << 61) - 1)
MAX_HASH = np.uint64((1 << 32) - 1)
def sha1_hash32(data):
"""
Directly taken from datasketch package to avoid dependency.
Parameters
----------
data : bytes
Returns
-------
int
"""
return struct.unpack('<I', hashlib.sha1(data).digest()[:4])[0]
def optimal_param(
threshold: float,
num_perm: int,
false_positive_weight: float = 0.5,
false_negative_weight: float = 0.5,
):
"""
Compute the optimal `MinHashLSH` parameter that minimizes the weighted sum
of probabilities of false positive and false negative, taken from
datasketch.
:param threshold: float. The threshold for similarity
:param num_perm: int. The number of permutations
:param false_positive_weight: float. The weight of false positive
:param false_negative_weight: float. The weight of false negative
:return: Tuple[int, int]. The optimal `b` and `r` parameters. The number of
bands, and the number of rows per band respectively
"""
def false_positive_probability(th: float, band: int, rows: int):
"""Source: `datasketch.lsh`"""
def proba(s):
return 1 - (1 - s**float(rows))**float(band)
a, _ = integrate.quad(proba, 0.0, th)
return a
def false_negative_probability(th: float, band: int, rows: int):
"""Source: `datasketch.lsh`"""
def proba(s):
return 1 - (1 - (1 - s**float(rows))**float(band))
a, _ = integrate.quad(proba, th, 1.0)
return a
# object: minimize the weighted FP and FN ratio
min_error = float('inf')
opt = (0, 0)
for b in range(1, num_perm + 1):
max_r = int(num_perm / b)
for r in range(1, max_r + 1):
fp = false_positive_probability(threshold, b, r)
fn = false_negative_probability(threshold, b, r)
error = fp * false_positive_weight + fn * false_negative_weight
if error < min_error:
min_error = error
opt = (b, r)
return opt
@OPERATORS.register_module(OP_NAME)
class DocumentMinhashDeduplicator(Deduplicator):
"""
Deduplicator to deduplicate samples at document-level using MinHashLSH.
Different from simhash, minhash is stored as bytes, so they won't be
kept in the final dataset.
"""
def __init__(
self,
tokenization: str = 'space',
window_size: PositiveInt = 5,
lowercase: bool = True,
ignore_pattern: Optional[str] = None,
num_permutations: PositiveInt = 256,
jaccard_threshold: Annotated[float, Field(ge=0, le=1)] = 0.7,
num_bands: Optional[PositiveInt] = None,
num_rows_per_band: Optional[PositiveInt] = None,
tokenizer_model: Optional[str] = None,
*args,
**kwargs,
):
"""
Initialization method.
:param tokenization: tokenization method for sample texts. It
should be one of [space, punctuation, character,
sentencepiece]. For English-like languages, we recommend
to use 'space', for Chinese-like languages, we recommend
to use 'character', and for multiple languages, we recommend
to use 'sentencepiece'. If using 'sentencepiece', please
provided the model path in the 'tokenizer_model' field.
:param window_size: window size of shingling
:param lowercase: whether to convert text to lower case first
:param ignore_pattern: whether to ignore sub-strings with
specific pattern when computing minhash
:param num_permutations: number of permutations in minhash
computing
:param jaccard_threshold: the min jaccard similarity threshold
in near-duplicate detection. When the jaccard similarity of
two sample texts is >= this threshold, they are regarded as
similar samples and this op will only keep one of them after
deduplication
:param num_bands: number of bands in LSH. Default it's None, and
it will be determined by an optimal params computation
algorithm by minimize the weighted sum of probs of False
Positives and False Negatives
:param num_rows_per_band: number of rows in each band in LSH.
Default it's None, and it will be determined by an optimal
params computation algorithm
:param tokenizer_model: path for the sentencepiece model, used for
sentencepiece tokenization.
"""
super().__init__(*args, **kwargs)
# about minhash computation
self.tokenization = tokenization
self.window_size = window_size
self.lowercase = lowercase
self.ignore_pattern = ignore_pattern
if self.ignore_pattern:
self.ignore_pattern = regex.compile(self.ignore_pattern)
# check parameters
if self.ignore_pattern and self.tokenization == 'punctuation':
logger.warning('Be careful that tokenization with punctuations '
'won\'t work if the ignore pattern includes '
'punctuations.')
self.punctuation_pattern = regex.compile(r'\p{P}')
if self.tokenization == 'sentencepiece':
if tokenizer_model is None:
raise ValueError("To use 'sentencepiece' tokenization, "
"'tokenizer_model' is required.")
self.tokenizer = prepare_sentencepiece_model(tokenizer_model)
else:
self.tokenizer = None
# about deduplication
self.num_permutation = num_permutations
self.jaccard_threshold = jaccard_threshold
self.num_bands = num_bands
self.num_rows_per_band = num_rows_per_band
# initialize deduplication parameters
# check number of bands and rows
if self.num_bands is None or self.num_rows_per_band is None:
self.num_bands, self.num_rows_per_band = optimal_param(
self.jaccard_threshold,
self.num_permutation,
)
# compute hash ranges and create hash tables
self.hash_ranges = [(i * self.num_rows_per_band,
(i + 1) * self.num_rows_per_band)
for i in range(self.num_bands)]
self.hash_tables = [defaultdict(set) for _ in range(self.num_bands)]
# generate permutations
gen = np.random.RandomState(seed=42)
self.perm_a, self.perm_b = np.array(
[(
gen.randint(1, MERSENNE_PRIME, dtype=np.uint64),
gen.randint(0, MERSENNE_PRIME, dtype=np.uint64),
) for _ in range(self.num_permutation)],
dtype=np.uint64,
).T
def compute_hash(self, sample):
"""
Compute minhash values for the sample.
:param sample: input sample
:return: sample with minhash value.
"""
# check if it's computed already
if HashKeys.minhash in sample:
return sample
text = sample[self.text_key]
if self.lowercase:
text = text.lower()
if self.ignore_pattern:
text = self.ignore_pattern.sub('', text)
# get tokens for different tokenization method
tokens = set()
if self.tokenization == 'character':
tokens = {
str.encode(text[i:i + self.window_size])
for i in range(len(text) - self.window_size)
}
elif self.tokenization == 'punctuation':
tokens = self.punctuation_pattern.split(text)
tokens = {
str.encode(' '.join(tokens[i:i + self.window_size]))
for i in range(len(tokens) - self.window_size)
}
elif self.tokenization == 'space':
tokens = split_on_whitespace(text)
tokens = {
str.encode(' '.join(tokens[i:i + self.window_size]))
for i in range(len(tokens) - self.window_size)
}
elif self.tokenization == 'sentencepiece':
tokens = self.tokenizer.encode(text, out_type=str)
tokens = {
str.encode(''.join(tokens[i:i + self.window_size]))
for i in range(len(tokens) - self.window_size)
}
else:
raise NotImplementedError(
f'Unimplemented tokenization method [{self.tokenization}]')
# compute minhash value
hv = np.array([sha1_hash32(token) for token in tokens],
dtype=np.uint64)
phv = np.bitwise_and(
((hv * np.tile(self.perm_a,
(len(hv), 1)).T).T + self.perm_b) % MERSENNE_PRIME,
MAX_HASH)
hash_values = np.vstack([
phv,
np.ones(self.num_permutation, dtype=np.uint64) * MAX_HASH
]).min(axis=0)
sample[HashKeys.minhash] = [
bytes(hash_values[start:end].byteswap().data)
for start, end in self.hash_ranges
]
return sample
def process(self, dataset, show_num=0):
"""
For doc-level, dataset --> dataset.
:param dataset: input dataset
:param show_num: number of traced samples used when tracer is
open.
:return: deduplicated dataset and the sampled duplicate pairs.
"""
# no need to deduplicate because too few samples
if len(dataset) <= 1:
return dataset, {}
minhashes = dataset[HashKeys.minhash]
# remove bytes minhash column otherwise unexpected error would occur
# when exporting the processed dataset
dataset = dataset.remove_columns([HashKeys.minhash])
# make clusters -- construct the minhash lookup tables of seg to ids
logger.info(f'Start clustering for {len(dataset)} samples...')
batch_size = 10000
for i in tqdm(range(0, len(minhashes), batch_size),
dynamic_ncols=True,
desc='Iterating MinHashes of samples...'):
batch = minhashes[i:i + batch_size]
for idx, hs in enumerate(batch):
for h, hashtable in zip(hs, self.hash_tables):
hashtable[h].add(idx + i)
# using UnionFind set to union samples within the same clusters
union_find = UnionFind()
for table in tqdm(self.hash_tables,
dynamic_ncols=True,
desc='Clustering'):
for cluster in table.values():
if len(cluster) <= 1:
continue
idx = min(cluster)
for x in cluster:
union_find.union(x, idx)
logger.info(f'There are {len(set(union_find.parent.values()))} '
f'clusters that includes multiple near-duplicate samples.')
# record the duplicate sample pairs
dup_pairs = {}
if show_num > 0:
for i in range(len(dataset)):
cluster_idx = union_find.find(i)
if cluster_idx not in dup_pairs and cluster_idx != i:
dup_pairs[cluster_idx] = [
dataset[cluster_idx],
dataset[i],
]
if len(dup_pairs) >= show_num:
break
# filtering -- only keep those samples whose parent index is itself,
# including:
# 1. samples that form a cluster by themselves
# 2. the first sample in a cluster that includes multiple samples
def _filter_minhash_dup_helper(sample, index):
return union_find.find(index) == index
dataset = dataset.filter(
_filter_minhash_dup_helper,
with_indices=True,
)
logger.info(f'Keep {len(dataset)} samples after MinHash dedup.')
return dataset, dup_pairs