Let's make the tests and demo paths indepedant
authorSimon Chabot <simon.chabot@logilab.fr>
Wed, 07 Nov 2012 17:46:11 +0100
changeset 86 ecf97bda4259
parent 85 ab75d8ffb260
child 87 efd007aa6096
Let's make the tests and demo paths indepedant
demo.py
test/test_alignment.py
--- a/demo.py	Wed Nov 07 17:24:42 2012 +0100
+++ b/demo.py	Wed Nov 07 17:46:11 2012 +0100
@@ -1,10 +1,14 @@
 #!/usr/bin/python
 #-*- coding:utf-8 -*-
 
+from os import path
+
 import alignment.distances as d
 import alignment.normalize as n
 from alignment.aligner import align, parsefile, findneighbours
 
+DEMODIR = path.dirname(__file__)
+
 def demo_0():
     # prixgoncourt is the list of Goncourt Prize, extracted
     # from wikipedia
@@ -18,8 +22,9 @@
 
     #We try to align Goncourt winers onto dbpedia results
 
-    alignset = parsefile('demo/prixgoncourt', indexes = [1, 1])
-    targetset = parsefile('demo/dbfrenchauthors', indexes = [0, 1], delimiter='#')
+    alignset = parsefile(path.join(DEMODIR, 'demo','prixgoncourt'), indexes = [1, 1])
+    targetset = parsefile(path.join(DEMODIR, 'demo', 'dbfrenchauthors'),
+                          indexes = [0, 1], delimiter='#')
 
     def removeparenthesis(string):
         if '(' in string:
@@ -31,7 +36,7 @@
               }
 
     dmatrix, hasmatched = align(alignset, targetset, [tr_name],
-                                0.4, 'demo/demo0_results')
+                                0.4, 'demo0_results')
 
     print dmatrix
 
@@ -44,8 +49,10 @@
     # position (longitude, latitude)
     # ``nbmax`` is the number of locations to load
 
-    targetset = parsefile('demo/FR.txt', indexes = [0, 1, (4, 5)], nbmax = 2000)
-    alignset = parsefile('demo/frenchbnf', indexes = [0, 2, (14, 12)], nbmax = 1000)
+    targetset = parsefile(path.join(DEMODIR, 'demo', 'FR.txt'), indexes = [0, 1, (4, 5)],
+                          nbmax = 2000)
+    alignset = parsefile(path.join(DEMODIR, 'demo', 'frenchbnf'),
+                         indexes = [0, 2, (14, 12)], nbmax = 1000)
 
 
     # Let's define the treatements to apply on the location's name
@@ -70,7 +77,7 @@
                                                     #   given by order.
                                 0.4,                # The maximal distance
                                                     #   threshold
-                                'demo/demo1_results')    # Filename of the output
+                                'demo1_results')    # Filename of the output
                                                     #   result file
     # the ``align()`` function return two items
     # 0. the computed distance matrix
@@ -79,8 +86,8 @@
     print dmatrix
 
 def demo_2():
-    targetset = parsefile('demo/FR.txt', indexes=[0, 1, (4, 5)])
-    alignset = parsefile('demo/frenchbnf', indexes=[0, 2, (14, 12)])
+    targetset = parsefile(path.join(DEMODIR, 'demo', 'FR.txt'), indexes=[0, 1, (4, 5)])
+    alignset = parsefile(path.join(DEMODIR, 'demo', 'frenchbnf'), indexes=[0, 2, (14, 12)])
 
     neighbors = findneighbours(alignset, targetset, indexes=(2, 2),
                                mode='kdtree', threshold=0.1)
@@ -103,8 +110,8 @@
               [targetset[i][:2] for i in nei], # The target dataset
               [tr_name],
               0.3,
-              'demo/demo2_results')  # Filename of the output
-                                     #   result file
+              'demo2_results')  # Filename of the output
+                                #   result file
 
 if __name__ == '__main__':
     print "Running demo_0"
--- a/test/test_alignment.py	Wed Nov 07 17:24:42 2012 +0100
+++ b/test/test_alignment.py	Wed Nov 07 17:46:11 2012 +0100
@@ -41,6 +41,7 @@
 
 import unittest2
 import random
+from os import path
 random.seed(42) ### Make sure tests are repeatable
 
 from alignment.distances import (levenshtein, soundex, soundexcode,   \
@@ -52,6 +53,8 @@
 from alignment.minhashing import Minlsh
 from alignment.aligner import parsefile
 
+TESTDIR = path.dirname(__file__)
+
 class DistancesTest(unittest2.TestCase):
     def test_levenshtein(self):
         self.assertEqual(levenshtein('niche', 'chiens'), 5)
@@ -148,7 +151,7 @@
 
 class NormalizerTestCase(unittest2.TestCase):
     def setUp(self):
-        self.lemmas = loadlemmas('data/french_lemmas.txt')
+        self.lemmas = loadlemmas(path.join(TESTDIR, 'data', 'french_lemmas.txt'))
 
     def test_unormalize(self):
         self.assertEqual(lunormalize(u'bépoèàÀêùï'),
@@ -232,14 +235,15 @@
                       "pour la santé, faîtes du sport"
                     ]
         minlsh = Minlsh()
-        lemmas = loadlemmas('data/french_lemmas.txt')
+        lemmas = loadlemmas(path.join(TESTDIR, 'data', 'french_lemmas.txt'))
         minlsh.train((simplify(s, lemmas) for s in sentences), 1, 200)
 
         self.assertEqual(minlsh.findsimilarsentences(0.65), set([(0, 1), (2, 4)]))
 
 class AlignerTestCase(unittest2.TestCase):
     def test_parser(self):
-        data = parsefile('data/file2parse', [0, (2, 3), 4, 1], delimiter=',')
+        data = parsefile(path.join(TESTDIR, 'data', 'file2parse'),
+                         [0, (2, 3), 4, 1], delimiter=',')
         self.assertEqual(data, [[1, (12, 19), 'apple', 'house'],
                                 [2, (21.9, 19), 'stramberry', 'horse'],
                                 [3, (23, 2.17), 'cherry', 'flower']])