14
14
"""
15
15
16
16
17
- def jaccard_similariy (setA , setB , alternativeUnion = False ):
17
+ def jaccard_similarity (setA , setB , alternativeUnion = False ):
18
18
"""
19
19
Finds the jaccard similarity between two sets.
20
20
Essentially, its intersection over union.
@@ -35,21 +35,24 @@ def jaccard_similariy(setA, setB, alternativeUnion=False):
35
35
Examples:
36
36
>>> setA = {'a', 'b', 'c', 'd', 'e'}
37
37
>>> setB = {'c', 'd', 'e', 'f', 'h', 'i'}
38
- >>> jaccard_similariy (setA,setB)
38
+ >>> jaccard_similarity (setA,setB)
39
39
0.375
40
40
41
- >>> jaccard_similariy (setA,setA)
41
+ >>> jaccard_similarity (setA,setA)
42
42
1.0
43
43
44
- >>> jaccard_similariy (setA,setA,True)
44
+ >>> jaccard_similarity (setA,setA,True)
45
45
0.5
46
46
47
47
>>> setA = ['a', 'b', 'c', 'd', 'e']
48
48
>>> setB = ('c', 'd', 'e', 'f', 'h', 'i')
49
- >>> jaccard_similariy (setA,setB)
49
+ >>> jaccard_similarity (setA,setB)
50
50
0.375
51
51
"""
52
52
53
+ if len (setA ) == 0 or len (setB ) == 0 :
54
+ raise ValueError ("Input must not be empty" )
55
+
53
56
if isinstance (setA , set ) and isinstance (setB , set ):
54
57
55
58
intersection = len (setA .intersection (setB ))
@@ -67,14 +70,14 @@ def jaccard_similariy(setA, setB, alternativeUnion=False):
67
70
68
71
if alternativeUnion :
69
72
union = len (setA ) + len (setB )
73
+ return len (intersection ) / union
70
74
else :
71
75
union = setA + [element for element in setB if element not in setA ]
72
-
73
- return len (intersection ) / len (union )
76
+ return len (intersection ) / len (union )
74
77
75
78
76
79
if __name__ == "__main__" :
77
80
78
81
setA = {"a" , "b" , "c" , "d" , "e" }
79
82
setB = {"c" , "d" , "e" , "f" , "h" , "i" }
80
- print (jaccard_similariy (setA , setB ))
83
+ print (jaccard_similarity (setA , setB ))
0 commit comments