[distance] Set the jaccard distance of empty sets to 1.
authorSimon Chabot <simon.chabot@logilab.fr>
Mon, 03 Jun 2019 15:32:07 +0200
changeset 555 575293b56d73
parent 554 c47252b44079
child 556 40ba8510e0dc
[distance] Set the jaccard distance of empty sets to 1. If two sets are empty, one can't compute the distance between the two. Let's return 1.
nazca/utils/distances.py
--- a/nazca/utils/distances.py	Mon Jun 03 14:49:36 2019 +0200
+++ b/nazca/utils/distances.py	Mon Jun 03 15:32:07 2019 +0200
@@ -251,8 +251,14 @@
 
         J(A, B) = (A \cap B)/(A \cup B)
         d(A, B) = 1 - J(A, B)
+
+        special case: if A and B are emtpy, return 1.
     """
-    return 1.0 - 1.0 * len(seta.intersection(setb)) / len(seta.union(setb))
+    try:
+        return 1.0 - 1.0 * len(seta.intersection(setb)) / len(seta.union(setb))
+    except ZeroDivisionError:
+        return 1.0
+
 
 
 def difflib_match(stra, strb):