-
Notifications
You must be signed in to change notification settings - Fork 2
/
dedup.py
executable file
·293 lines (263 loc) · 8.3 KB
/
dedup.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
#!/usr/bin/env python3
import csv
import sys
import json
from math import inf
from operator import itemgetter
from unicodedata import normalize
from itertools import combinations
from cityhash import CityHash32, CityHash64, CityHash128
from tqdm import tqdm
tqdm.monitor_interval = 0
HASHSIZE = {
CityHash32: 32,
CityHash64: 64,
CityHash128: 128,
}
HASHES = {bits: hashf for (hashf, bits) in HASHSIZE.items()}
def ngrams(iterable, n=1):
"""Generate ngrams from an iterable
l = range(5)
list(l) -> [0, 1, 2, 3, 4, 5]
list(ngrams(l, n=1)) -> [(0,), (1,), (2,), (3,), (4,)]
list(ngrams(l, n=2)) -> [(0, 1), (1, 2), (2, 3), (3, 4)]
list(ngrams(l, n=3)) -> [(0, 1, 2), (1, 2, 3), (2, 3, 4)]
"""
return zip(*(iterable[i:] for i in range(n)))
def rotate(n, rotations=1, width=32):
"""Bitwise rotate an int.
bin(rotate(1, rotations=0)) -> '0b1'
bin(rotate(1, rotations=1)) -> '0b10000000000000000000000000000000'
bin(rotate(1, rotations=2)) -> '0b1000000000000000000000000000000'
bin(rotate(1, rotations=32)) -> '0b1'
bin(rotate(1, rotations=31)) -> '0b10'
bin(rotate(1, rotations=-1)) -> '0b10'
bin(rotate(1, rotations=1, width=8)) -> '0b10000000'
bin(rotate(1, rotations=8, width=8)) -> '0b1'
"""
width = max(n.bit_length(), width)
rotations %= width
if rotations < 1:
return n
mask = 2 ** width - 1
n &= mask
return (n >> rotations) | ((n << (width - rotations) & mask))
def simhash(text, n=2, hashf=CityHash32):
"""Simhash implementation using an underlying, fast string-hash: cityhash"""
lsh = [0] * HASHSIZE[hashf]
if not text:
return 0
for ngram in ngrams(text, n=n):
hash_ = hashf(''.join(ngram))
for i, _ in enumerate(lsh):
if hash_ & (1 << i):
lsh[i] += 1
else:
lsh[i] -= 1
return sum(int(b > 0) << i for (i, b) in enumerate(reversed(lsh)))
class Text(object):
"""A class modeling a text document that can be compared for equality
to other Text instances using simhash (a locality sensitive hash or
LSH) based on character n-grams.
"""
def __init__(
self,
filename,
n=2,
hashf=CityHash128,
normalization_form=None
):
self.filename = filename
self.n = n
self.hashf = hashf
self.normalization_form = normalization_form
self.lsh = simhash(self.load(), n=self.n, hashf=self.hashf)
def load(self):
with open(self.filename, mode='r') as f:
data = f.read()
if self.normalization_form:
data = normalize(self.normalization_form, data)
self.size = len(data)
return data
def __hash__(self):
return hash(self.filename)
def __repr__(self):
return (
f'{self.__class__.__name__}('
f'{self.filename!r}, n={self.n}, hashf={self.hashf.__name__})'
)
class ADM(Text):
"""A class modeling a Basis Technology Annotatded Data Model (ADM)
that can be compared for equality to other Text instances using simhash
based on character n-grams."""
def load(self):
with open(self.filename, mode='r') as f:
obj = json.loads(f.read())
data = obj['data']
self.size = len(data)
return data
def document_sorter(document):
"""A sorting key function that sorts documents by length (descending)
and filename (ascending)
"""
return -document.size, document.filename
def simdiff(a, b, bits=128):
"""Compute the bitwise difference between two simhashes"""
if bits < 1:
raise ValueError(f'bits must be >= 1 (bits={bits})')
xor = a ^ b
difference = sum(((xor & (1 << i)) > 0) for i in range(bits))
return difference
def pairs(documents, hashf=CityHash128, window=2):
"""Generate duplicate candidate pairs and their minimum bitwise difference
scores.
The return type is ((a, b), c) where:
a: a document instance that has a .lsh attribute
b: a document instance that has a .lsh attribute
c: the minimum difference score between all rotations of the LSHs of
a and b
"""
d = {}
bits = HASHSIZE[hashf]
for i in range(bits):
def lsh(document):
return rotate(document.lsh, i, width=bits)
for ng in ngrams(
sorted(documents, key=lsh),
n=window
):
for a, b in combinations(ng, 2):
a, b = sorted((a, b), key=document_sorter)
d[(a, b)] = simdiff(lsh(a), lsh(b), bits=bits)
yield from sorted(d.items(), key=itemgetter(1))
def load(
filenames,
doctype=Text,
n=2,
hashf=CityHash128,
normalization_form=None
):
"""Generate doctype instances per filename with a progress bar"""
with tqdm(
filenames,
unit='files',
dynamic_ncols=True
) as progress:
for filename in progress:
yield doctype(
filename,
n=n,
hashf=hashf,
normalization_form=normalization_form
)
DOCTYPES = {
'.txt': Text,
'.adm.json': ADM
}
def main(
filenames,
doctype=Text,
n=2,
hashf=CityHash128,
normalization_form=None,
threshold=inf,
window=10
):
documents = load(
filenames,
doctype=doctype,
n=n,
hashf=hashf,
normalization_form=normalization_form
)
writer = csv.writer(sys.stdout, dialect=csv.excel_tab)
for (a, b), score in pairs(documents, hashf=hashf, window=window):
if score <= threshold:
writer.writerow((a.filename, b.filename, score))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description=__doc__
)
parser.add_argument(
'filenames',
type=argparse.FileType('r'),
help=(
'file listing a set of filenames to check for duplicates; '
'one filename per line (use "-" to read filenames as lines from '
'stdin)'
)
)
parser.add_argument(
'-n',
'--n-gram-size',
type=int,
default=2,
help='size of character n-grams'
)
parser.add_argument(
'-b',
'--bits',
type=int,
default=128,
choices=sorted(HASHES),
help='hash size (in bits)'
)
parser.add_argument(
'-r',
'--threshold',
type=float,
default=max(HASHES),
help=(
'minimum bitwise difference threshold for considering two LSHs '
'equivalent (lower thresholds are more strict; higher thresholds '
'are more lenient; thresholds must be between 0 and -b/--bits)'
)
)
parser.add_argument(
'-f',
'--normalization-form',
default='NFKD',
choices=['NFC', 'NFKC', 'NFD', 'NFKD'],
help=(
'Unicode normalization option; refer to unicodedata.normalize for '
'explanation of options'
)
)
parser.add_argument(
'-t',
'--doc-type',
choices=sorted(DOCTYPES),
default='.txt',
help='the type of documents to compare'
)
parser.add_argument(
'-w', '--window',
type=int,
default=2,
help='window size to check for candidates in the sorted list of pairs',
)
args = parser.parse_args()
if not (args.threshold <= args.bits):
print(
(
'[INVALID DIFFERENCE THRESHOLD] '
f'-r/--threshold={args.threshold} must be '
f'less than -b/--bits={args.bits}'
),
file=sys.stderr
)
sys.exit(1)
filenames = args.filenames.read().splitlines()
if args.filenames.name != '<stdin>':
args.filenames.close()
main(
filenames,
doctype=DOCTYPES[args.doc_type],
n=args.n_gram_size,
hashf=HASHES[args.bits],
normalization_form=args.normalization_form,
threshold=args.threshold,
window=args.window
)