/
metrics.py
494 lines (350 loc) · 14 KB
/
metrics.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
'''
Created on Mar 1, 2016
@author: Yury Zhauniarovich
'''
import math
class Block():
name = "Block"
def compare(self, list1, list2):
if not list1 and not list2:
return 1.0
if not list1 or not list2:
return 0.0
return 1.0 - self.distance(list1, list2) / (len(list1) + len(list2))
def distance(self, list1, list2):
dict1 = dict()
for k in list1:
dict1[k] = dict1.get(k, 0) + 1
dict2 = dict()
for k in list2:
dict2[k] = dict2.get(k, 0) + 1
distance = 0.0
elements_union = set(dict1.keys()).union(set(dict2.keys()))
for elem in elements_union:
distance += abs(dict1.get(elem, 0) - dict2.get(elem, 0))
return float(distance)
def get_name(self):
return self.name
################################################################################
class Cosine():
name = "Cosine"
def compare(self, list1, list2):
if not list1 and not list2:
return 1.0
if not list1 or not list2:
return 0.0
dict1 = dict()
for k in list1:
dict1[k] = dict1.get(k, 0) + 1
dict2 = dict()
for k in list2:
dict2[k] = dict2.get(k, 0) + 1
dot_product = 0.0
magnitude1 = 0.0
magnitude2 = 0.0
elements_union = set(dict1.keys()).union(set(dict2.keys()))
for elem in elements_union:
count1 = dict1.get(elem, 0)
count2 = dict2.get(elem, 0)
dot_product += count1*count2
magnitude1 += count1*count1
magnitude2 += count2*count2
return dot_product / (math.sqrt(magnitude1) * math.sqrt(magnitude2))
def distance(self, list1, list2):
return (1.0 - self.compare(list1, list2))
def get_name(self):
return self.name
################################################################################
class Dice():
name = "Dice"
def compare(self, list1, list2):
if not list1 and not list2:
return 1.0
if not list1 or not list2:
return 0.0
return ((2.0 * len(set(list1).intersection(list2))) / (len(list1) + len(list2)))
def distance(self, list1, list2):
return (1.0 - self.compare(list1, list2))
def get_name(self):
return self.name
################################################################################
class Euclidian():
name = "Euclidian"
def compare(self, list1, list2):
if not list1 and not list2:
return 1.0
max_distance = math.sqrt((len(list1)*len(list1) + len(list2)*len(list2)))
return (1.0 - (self.distance(list1, list2) / max_distance))
def distance(self, list1, list2):
dict1 = dict()
for k in list1:
dict1[k] = dict1.get(k, 0) + 1
dict2 = dict()
for k in list2:
dict2[k] = dict2.get(k, 0) + 1
distance = 0.0
elements_union = set(dict1.keys()).union(set(dict2.keys()))
for elem in elements_union:
count1 = dict1.get(elem, 0)
count2 = dict2.get(elem, 0)
distance += ((count1 - count2)*(count1 - count2))
return math.sqrt(distance)
def get_name(self):
return self.name
################################################################################
class GeneralizedJaccard():
name = "GeneralizedJaccard"
def compare(self, list1, list2):
if not list1 and not list2:
return 1.0
if not list1 or not list2:
return 0.0
intersection = len(set(list1).intersection(set(list2)))
return float(intersection) / (len(list1) + len(list2) - intersection)
def distance(self, list1, list2):
return (1.0 - self.compare(list1, list2))
def get_name(self):
return self.name
################################################################################
class GeneralizedOverlap():
name = "GeneralizedOverlap"
def compare(self, list1, list2):
if not list1 and not list2:
return 1.0
if not list1 or not list2:
return 0.0
intersection = len(set(list1).intersection(set(list2)))
return float(intersection) / min(len(list1), len(list2))
def distance(self, list1, list2):
raise NotApplicableException("Generalized Overlap cannot be used as a distance metric!")
def get_name(self):
return self.name
################################################################################
class Jaccard():
name = "Jaccard"
def compare(self, list1, list2):
if not list1 and not list2:
return 1.0
if not list1 or not list2:
return 0.0
set1 = set(list1)
set2 = set(list2)
intersection = len(set1.intersection(set2))
return float(intersection) / (len(set1) + len(set2) - intersection)
def distance(self, list1, list2):
return (1.0 - self.compare(list1, list2))
def get_name(self):
return self.name
################################################################################
class Overlap():
name = "Overlap"
def compare(self, list1, list2):
if not list1 and not list2:
return 1.0
if not list1 or not list2:
return 0.0
set1 = set(list1)
set2 = set(list2)
intersection = len(set1.intersection(set2))
return float(intersection) / min(len(set1), len(set2))
def distance(self, list1, list2):
return (1.0 - self.compare(list1, list2))
def get_name(self):
return self.name
################################################################################
class SimonWhite():
name = "SimonWhite"
def compare(self, list1, list2):
if not list1 and not list2:
return 1.0
if not list1 or not list2:
return 0.0
intersection = len(set(list1).intersection(set(list2)))
return (2.0 * intersection) / (len(list1) + len(list2))
def distance(self, list1, list2):
return (1.0 - self.compare(list1, list2))
def get_name(self):
return self.name
################################################################################
class Tanimoto():
name = "Tanimoto"
def compare(self, list1, list2):
if not list1 and not list2:
return 1.0
if not list1 or not list2:
return 0.0
set1 = set(list1)
set2 = set(list2)
intersection = len(set1.intersection(set2))
return float(intersection) / math.sqrt(len(set1) * len(set2))
def distance(self, list1, list2):
return (1.0 - self.compare(list1, list2))
def get_name(self):
return self.name
################################################################################
class NotApplicableException(Exception):
pass
#******************************************************************************#
def block_test1():
list1 = "aaa bbb ccc ddd".split(' ')
list2 = "aaa bbb ccc eee".split(' ')
block_distance = Block()
bd = block_distance.compare(list1, list2)
assert bd == 0.75, "Error in block_test1. Actual value: %f" % bd
def block_test2():
list1 = "aaa bbb".split(' ')
list2 = "aaa aaa".split(' ')
block_distance = Block()
bd = block_distance.compare(list1, list2)
assert bd == 0.5, "Error in block_test2. Actual value: %f" % bd
#******************************************************************************#
def cosine_test1():
list1 = "aaa bbb ccc ddd".split(' ')
list2 = "aaa bbb ccc eee".split(' ')
cosine_similarity = Cosine()
cs = cosine_similarity.compare(list1, list2)
assert cs == 0.75, "Error in cosine_test1. Actual value: %f" % cs
def cosine_test2():
list1 = "a b c d".split(' ')
list2 = "a b e f".split(' ')
cosine_similarity = Cosine()
cs = cosine_similarity.compare(list1, list2)
assert cs == 0.5, "Error in cosine_test2. Actual value: %f" % cs
#******************************************************************************#
def dice_test1():
list1 = "aaa bbb ccc ddd".split(' ')
list2 = "aaa bbb ccc eee".split(' ')
dice_similarity = Dice()
ds = dice_similarity.compare(list1, list2)
assert ds == 0.75, "Error in dice_test1. Actual value: %f" % ds
def dice_test2():
list1 = "a b c d".split(' ')
list2 = "a b c e".split(' ')
dice_similarity = Dice()
ds = dice_similarity.compare(list1, list2)
assert ds == 0.75, "Error in dice_test2. Actual value: %f" % ds
#******************************************************************************#
def euclidian_test1():
list1 = "aaa bbb ccc ddd".split(' ')
list2 = "aaa bbb ccc eee".split(' ')
euclidian_similarity = Euclidian()
es = euclidian_similarity.compare(list1, list2)
assert es == 0.75, "Error in test1. Actual value: %f" % es
def euclidian_test2():
list1 = "a b c d".split(' ')
list2 = "a b c e".split(' ')
euclidian_similarity = Euclidian()
es = euclidian_similarity.compare(list1, list2)
assert es == 0.75, "Error in test2. Actual value: %f" % es
#******************************************************************************#
def generalized_jaccard_test1():
list1 = "aaa bbb ccc ddd".split(' ')
list2 = "aaa bbb ccc eee".split(' ')
generalized_jaccard_similarity = GeneralizedJaccard()
res = generalized_jaccard_similarity.compare(list1, list2)
assert res == 0.6, "Error in test1. Actual value: %f" % res
def generalized_jaccard_test2():
list1 = "a b c".split(' ')
list2 = "a b c e f g".split(' ')
generalized_jaccard_similarity = GeneralizedJaccard()
res = generalized_jaccard_similarity.compare(list1, list2)
assert res == 0.5, "Error in test2. Actual value: %f" % res
#******************************************************************************#
def generalized_overlap_test1():
list1 = "aaa bbb ccc ddd".split(' ')
list2 = "aaa bbb ccc eee".split(' ')
generalized_overlap_similarity = GeneralizedOverlap()
res = generalized_overlap_similarity.compare(list1, list2)
assert res == 0.75, "Error in test1. Actual value: %f" % res
def generalized_overlap_test2():
list1 = "a b b c c".split(' ')
list2 = "a b c e f g".split(' ')
generalized_overlap_similarity = GeneralizedOverlap()
res = generalized_overlap_similarity.compare(list1, list2)
assert res == 0.6, "Error in test2. Actual value: %f" % res
#******************************************************************************#
def jaccard_test1():
list1 = "aaa bbb ccc ddd".split(' ')
list2 = "aaa bbb ccc eee".split(' ')
jaccard_similarity = Jaccard()
res = jaccard_similarity.compare(list1, list2)
assert res == 0.6, "Error in test1. Actual value: %f" % res
def jaccard_test2():
list1 = "a b b c c".split(' ')
list2 = "a b c e f g".split(' ')
accard_similarity = Jaccard()
res = accard_similarity.compare(list1, list2)
assert res == 0.5, "Error in test2. Actual value: %f" % res
#******************************************************************************#
def overlap_test1():
list1 = "aaa bbb ccc ddd".split(' ')
list2 = "aaa bbb ccc eee".split(' ')
overlap_similarity = Overlap()
res = overlap_similarity.compare(list1, list2)
assert res == 0.75, "Error in test1. Actual value: %f" % res
def overlap_test2():
list1 = "a b b c c".split(' ')
list2 = "a b c e f g".split(' ')
overlap_similarity = Overlap()
res = overlap_similarity.compare(list1, list2)
assert res == 1.0, "Error in test2. Actual value: %f" % res
#******************************************************************************#
def simon_white_test1():
list1 = "aaa bbb ccc ddd".split(' ')
list2 = "aaa bbb ccc eee".split(' ')
simon_white_similarity = SimonWhite()
res = simon_white_similarity.compare(list1, list2)
assert res == 0.75, "Error in test1. Actual value: %f" % res
def simon_white_test2():
list1 = "a b c d".split(' ')
list2 = "a b c e".split(' ')
simon_white_similarity = SimonWhite()
res = simon_white_similarity.compare(list1, list2)
assert res == 0.75, "Error in test2. Actual value: %f" % res
#******************************************************************************#
def tanimoto_test1():
list1 = "aaa bbb ccc ddd".split(' ')
list2 = "aaa bbb ccc eee".split(' ')
tanimoto_similarity = Tanimoto()
res = tanimoto_similarity.compare(list1, list2)
assert res == 0.75, "Error in test1. Actual value: %f" % res
def tanimoto_test2():
list1 = "aaa bbb ccc ddd aaa bbb ccc ddd".split(' ')
list2 = "aaa bbb ccc eee".split(' ')
tanimoto_similarity = Tanimoto()
res = tanimoto_similarity.compare(list1, list2)
assert res == 0.75, "Error in test2. Actual value: %f" % res
#******************************************************************************#
def test():
#BlockTests
block_test1()
block_test2()
#CosineTests
cosine_test1()
cosine_test2()
#DiceTests
dice_test1()
dice_test2()
#EuclidianTests
euclidian_test1()
euclidian_test2()
#GeneralizedJaccardTests
generalized_jaccard_test1()
generalized_jaccard_test2()
#GeneralizedOverlapTests
generalized_overlap_test1()
generalized_overlap_test2()
#JaccardTests
jaccard_test1()
jaccard_test2()
#OverlapTests
overlap_test1()
overlap_test2()
#SimonWhiteTests
simon_white_test1()
simon_white_test2()
#TanimotoTests
tanimoto_test1()
tanimoto_test2()
if __name__ == '__main__':
test()