[distances] Add an euclidean distance function between two numbers (closes #128982)
authorSimon Chabot <simon.chabot@logilab.fr>
Wed, 17 Oct 2012 16:47:07 +0200
changeset 14 e02ce920aff6
parent 13 2f19c16c3610
child 15 3940c3c651a3
[distances] Add an euclidean distance function between two numbers (closes #128982)
distances.py
test/test_alignment.py
--- a/distances.py	Wed Oct 17 15:31:11 2012 +0200
+++ b/distances.py	Wed Oct 17 16:47:07 2012 +0200
@@ -135,3 +135,6 @@
     if granularity.lower() == 'months':
         return abs(diff.days / 30.5)
     return abs(diff.days)
+
+def euclidean(a, b):
+    return abs(a - b)
--- a/test/test_alignment.py	Wed Oct 17 15:31:11 2012 +0200
+++ b/test/test_alignment.py	Wed Oct 17 16:47:07 2012 +0200
@@ -39,7 +39,7 @@
 
 from cubicweb.devtools import testlib
 from cubes.alignment.distances import (levenshtein, soundex, soundexcode, \
-                                       jaccard, temporal)
+                                       jaccard, temporal, euclidean)
 
 class DistancesTest(testlib.CubicWebTC):
     def test_levenshtein(self):
@@ -97,6 +97,10 @@
         self.assertAlmostEqual(temporal('13 march', '13 may', 'months',
                                         'english'), 2.0, 1)
 
+    def test_euclidean(self):
+        self.assertEqual(euclidean(10, 11), 1)
+        self.assertEqual(euclidean(-10, 11), 21)
+
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main
     unittest_main()