Package medite :: Package MediteAppli :: Package test :: Module test_alignement
[hide private]
[frames] | no frames]

Source Code for Module medite.MediteAppli.test.test_alignement

  1  # -*- coding: iso-8859-1 -*- 
  2  # Copyright 20003 - 2008: Julien Bourdaillet (julien.bourdaillet@lip6.fr), Jean-Gabriel Ganascia (jean-gabriel.ganascia@lip6.fr) 
  3  # This file is part of MEDITE. 
  4  # 
  5  #    MEDITE is free software; you can redistribute it and/or modify 
  6  #    it under the terms of the GNU General Public License as published by 
  7  #    the Free Software Foundation; either version 2 of the License, or 
  8  #    (at your option) any later version. 
  9  # 
 10  #    MEDITE is distributed in the hope that it will be useful, 
 11  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  #    GNU General Public License for more details. 
 14  # 
 15  #    You should have received a copy of the GNU General Public License 
 16  #    along with Foobar; if not, write to the Free Software 
 17  #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 18   
 19  import unittest, contract, sys, time, logging, os, ConfigParser  
 20  import MediteAppli.alignement 
 21  import test_data 
 22  import psyco 
 23  from Donnees.resultatAppli import * 
 24  # vérification des contrats 
 25  #contract.checkmod(MediteAppli.alignement) 
 26   
27 -class Test_AlignementCas1(unittest.TestCase):
28 - def _readFile(self,name):
29 """ Lit un fichier dans le dossier courant et renvoie une chaine """ 30 path=os.path.join(os.getcwd(),"MediteAppli","test",name) 31 f = open(path) 32 res = f.read() 33 f.close() 34 return res
35 - def err(self,text):
36 sys.stderr.write(str(time.clock())+" : "+text+"\n") 37 sys.stderr.flush()
38 - def setUp(self):
39 f = test_data.TestDataFactory() 40 self.td = f.getTestData(test_data.Chedid,"1")
41
43 self.align = MediteAppli.alignement.AlignDicho(self.td.gettexte1()+self.td.gettexte2()) 44 occs_deplaces,blocsCommuns = self.align.deplacements_pond(self.td.getL1(),self.td.getL2()) 45 self.__check_deplacements_pond(occs_deplaces,blocsCommuns)
46
48 self.align = MediteAppli.alignement.AlignAstar(self.td.gettexte1()+self.td.gettexte2()) 49 occs_deplaces,blocsCommuns = self.align.deplacements_pond(self.td.getL1(),self.td.getL2()) 50 self.__check_deplacements_pond(occs_deplaces,blocsCommuns)
51
53 self.align = MediteAppli.alignement.AlignAstar2(self.td.gettexte1()+self.td.gettexte2()) 54 occs_deplaces,blocsCommuns = self.align.deplacements_pond(self.td.getL1(),self.td.getL2()) 55 self.__check_deplacements_pond(occs_deplaces,blocsCommuns)
56
58 self.align = MediteAppli.alignement.AlignAstarRecur2(self.td.gettexte1()+self.td.gettexte2()) 59 occs_deplaces,blocsCommuns = self.align.deplacements_pond(self.td.getL1(),self.td.getL2()) 60 self.__check_deplacements_pond(occs_deplaces,blocsCommuns)
61
63 self.align = MediteAppli.alignement.AlignAstarRecur2(len(self.td.gettexte1()),long_min_pivots=5) 64 #occs_deplaces,blocsCommuns = self.align.deplacements_pond(self.td.getL1(),self.td.getL2()) 65 occs_deplaces,blocsCommuns = self.align.run(self.td.gettexte1(),self.td.gettexte2())
66 #self.__check_deplacements_pond(occs_deplaces,blocsCommuns) 67
68 - def test_preTraitDiffSymVect(self):
69 self.align = MediteAppli.alignement.AlignAstarRecur(self.td.gettexte1()+self.td.gettexte2()) 70 LH1 = eval(self._readFile('bovaryLH1.txt')) 71 LH2 = eval(self._readFile('bovaryLH2.txt')) 72 dicoPos = eval(self._readFile('bovaryDicoPos.txt')) 73 diffSym = self.align.preTraitDiffSymVect(LH1,LH2,dicoPos)
74 #trace('diffSym = self.align.preTraitDiffSymVect(LH1,LH2,dicoPos)',locals()) 75
77 self.align = MediteAppli.alignement.AlignAstarRecur3(self.td.gettexte1()+self.td.gettexte2(),len(self.td.gettexte1()),self.td.getp1()) 78 occs_deplaces,blocsCommuns,LUnique = self.align.deplacements_pond(self.td.getL1(),self.td.getL2()) 79 self.__check_deplacements_pond(occs_deplaces,blocsCommuns) 80 texte=self.td.gettexte1()+self.td.gettexte2() 81 for i in xrange(0,len(LUnique)): 82 j=self.__interInclus(LUnique[i],occs_deplaces) 83 self.assert_(j==-1,texte[LUnique[i][0]:LUnique[i][1]]+str(occs_deplaces[j])+str(LUnique[i])) 84 j=self.__interInclus(LUnique[i],blocsCommuns) 85 self.assert_(j==-1,texte[LUnique[i][0]:LUnique[i][1]]+str(blocsCommuns[j])+str(LUnique[i]))
86 87
89 self.align = MediteAppli.alignement.AlignAstarRecur4(self.td.gettexte1()+self.td.gettexte2(),len(self.td.gettexte1()),self.td.getp1()) 90 occs_deplaces,blocsCommuns,LUnique = self.align.deplacements_pond(self.td.getL1(),self.td.getL2()) 91 self.__check_deplacements_pond(occs_deplaces,blocsCommuns) 92 texte=self.td.gettexte1()+self.td.gettexte2() 93 for i in xrange(0,len(LUnique)): 94 j=self.__interInclus(LUnique[i],occs_deplaces) 95 self.assert_(j==-1,texte[LUnique[i][0]:LUnique[i][1]]+str(occs_deplaces[j])+str(LUnique[i])) 96 j=self.__interInclus(LUnique[i],blocsCommuns) 97 self.assert_(j==-1,texte[LUnique[i][0]:LUnique[i][1]]+str(blocsCommuns[j])+str(LUnique[i]))
98
100 self.align = MediteAppli.alignement.AlignAstarRecur5(self.td.gettexte1()+self.td.gettexte2(),len(self.td.gettexte1()),self.td.getp1()) 101 #trace('occs_deplaces,blocsCommuns = self.align.run(self.td.gettexte1(),self.td.gettexte2())',locals()) 102 occs_deplaces,blocsCommuns = self.align.run(self.td.gettexte1(),self.td.gettexte2()) 103 self.__check_deplacements_pond(occs_deplaces,blocsCommuns)
104 #texte=self.td.gettexte1()+self.td.gettexte2() 105 #for i in xrange(0,len(LUnique)): 106 # j=self.__interInclus(LUnique[i],occs_deplaces) 107 # self.assert_(j==-1,texte[LUnique[i][0]:LUnique[i][1]]+str(occs_deplaces[j])+str(LUnique[i])) 108 # j=self.__interInclus(LUnique[i],blocsCommuns) 109 # self.assert_(j==-1,texte[LUnique[i][0]:LUnique[i][1]]+str(blocsCommuns[j])+str(LUnique[i])) 110
112 self.align = MediteAppli.alignement.AlignAstarRecur(len(self.td.gettexte1()),5) 113 #trace('occs_deplaces,blocsCommuns = self.align.run(self.td.gettexte1(),self.td.gettexte2())',locals()) 114 occs_deplaces,blocsCommuns = self.align.run(self.td.gettexte1(),self.td.gettexte2()) 115 self.__check_deplacements_pond(occs_deplaces,blocsCommuns)
116
118 self.align = MediteAppli.alignement.AlignShapiraRecur(len(self.td.gettexte1()),5) 119 #trace('occs_deplaces,blocsCommuns = self.align.run(self.td.gettexte1(),self.td.gettexte2())',locals()) 120 occs_deplaces,blocsCommuns = self.align.run(self.td.gettexte1(),self.td.gettexte2()) 121 self.__check_deplacements_pond(occs_deplaces,blocsCommuns)
122
123 - def __check_deplacements_pond(self,occs_deplaces,blocsCommuns):
124 texte=self.td.gettexte1()+self.td.gettexte2() 125 self.assert_(isinstance(occs_deplaces,list)) 126 self.assert_(isinstance(blocsCommuns,list)) 127 #self.assertNotEqual(len(occs_deplaces),0) 128 self.assertNotEqual(len(blocsCommuns),0) 129 self.assert_(isinstance(blocsCommuns[0],list)) 130 self.assert_(isinstance(blocsCommuns[0][0],int)) 131 self.assert_(isinstance(blocsCommuns[0][1],int)) 132 for i in xrange(1,len(blocsCommuns)): 133 self.assert_(isinstance(blocsCommuns[i],list),str(type(blocsCommuns[i]))) 134 self.assert_(isinstance(blocsCommuns[i][0],int)) 135 self.assert_(isinstance(blocsCommuns[i][1],int)) 136 # les BC sont ordonnés 137 self.assert_(0<=blocsCommuns[i-1][1]<=blocsCommuns[i][0]<=len(texte)) 138 if len(occs_deplaces)>0: 139 self.assert_(isinstance(occs_deplaces[0],list)) 140 self.assert_(isinstance(occs_deplaces[0][0],int)) 141 self.assert_(isinstance(occs_deplaces[0][1],int)) 142 for i in xrange(1,len(occs_deplaces)): 143 self.assert_(isinstance(occs_deplaces[i],list)) 144 self.assert_(isinstance(occs_deplaces[i][0],int)) 145 self.assert_(isinstance(occs_deplaces[i][1],int)) 146 self.assert_(0<=occs_deplaces[i-1][1]<=occs_deplaces[i][0]<=len(texte), 147 str(occs_deplaces[i-1])+' '+str(occs_deplaces[i])) 148 # liste des intervalles entre les blocs communs 149 lDep=[(0,blocsCommuns[0][0])] 150 for i in xrange(1,len(blocsCommuns)): 151 lDep.append((blocsCommuns[i-1][1],blocsCommuns[i][0])) 152 lDep.append((blocsCommuns[-1][1],len(texte))) 153 for i in xrange(0,len(occs_deplaces)): 154 j=self.__interInclus(occs_deplaces[i],lDep) 155 self.assert_(j>=0,texte[occs_deplaces[i][0]:occs_deplaces[i][1]])
156
157 - def __interInclus(self,i1,i2): # i1 inclus dans un des inter de [i2] ?
158 for i in xrange(len(i2)): 159 if i1[0]>=i2[i][0] and i1[1]<=i2[i][1]: return i 160 return -1
161
162 -class Test_AlignementCas2(Test_AlignementCas1):
163 - def setUp(self):
166 -class Test_AlignementCas3(Test_AlignementCas1):
167 - def setUp(self):
170 -class Test_AlignementCas4(Test_AlignementCas1):
171 - def setUp(self):
174 -class Test_AlignementCas5(Test_AlignementCas1):
175 - def setUp(self):
178 -class Test_AlignementCharroiCas1(Test_AlignementCas1):
179 - def setUp(self):
180 f = test_data.TestDataFactory() 181 self.td = f.getTestData(test_data.Charroi,"1")
182 -class Test_AlignementBioCas1(Test_AlignementCas1):
183 - def setUp(self):
184 f = test_data.TestDataFactory() 185 self.td = f.getTestData(test_data.Bio,"1")
186 -class Test_AlignementCondorcetCas1(Test_AlignementCas1):
187 - def setUp(self):
188 f = test_data.TestDataFactory() 189 self.td = f.getTestData(test_data.Condorcet,"1")
190 -class Test_AlignementCondorcetCas2(Test_AlignementCas1):
191 - def setUp(self):
192 f = test_data.TestDataFactory() 193 self.td = f.getTestData(test_data.Condorcet,"2")
194 -class Test_AlignementTraductionCas1(Test_AlignementCas1):
195 - def setUp(self):
196 f = test_data.TestDataFactory() 197 self.td = f.getTestData(test_data.Traduction,"1")
199 config = ConfigParser.ConfigParser() 200 config.read(os.path.join(os.getcwd(),'MediteAppli','test','traduc.ini')) 201 L1 = eval(config.get('listes','L1')) 202 L2 = eval(config.get('listes','L2')) 203 logging.debug('len(L1)='+str(len(L1))+' / len(L2)='+str(len(L2))) 204 texte = self.td.gettexte1()+self.td.gettexte2() 205 self.align = MediteAppli.alignement.AlignAstarRecur5(texte,len(self.td.gettexte1()),self.td.getp1()) 206 logging.debug("debut _appelAstar") 207 LC1 = [] ; LC2 = [] ; Lcf1 = [] ; Lcf2 = [] ; LH1 = [] ; LH2 = [] 208 for bloc in L1: 209 cle = hash(texte[bloc[0]:bloc[1]]) 210 LC1.append(cle) 211 Lcf1.append((cle,bloc[1]-bloc[0])) 212 LH1.append((cle, bloc[0], bloc[1])) 213 #print LH1 214 logging.debug('init L1 done') 215 for bloc in L2: 216 cle = hash(texte[bloc[0]:bloc[1]]) 217 LC2.append(cle) 218 Lcf2.append((cle,bloc[1]-bloc[0])) 219 LH2.append((cle, bloc[0], bloc[1])) 220 dicoPos = {} 221 coutFixe = {} 222 dicoPos[1], coutFixe[1] = self.align.calcPosCoutFixe(Lcf1,texte) 223 dicoPos[2], coutFixe[2] = self.align.calcPosCoutFixe(Lcf2,texte) 224 for cle in dicoPos[1]: assert cle in dicoPos[2], str([texte[d:f] for d,f in L1])+' '+str([texte[d:f] for d,f in L2]) 225 for cle in dicoPos[2]: assert cle in dicoPos[1] 226 logging.debug("fin calcPosCoutFixe") 227 self.align.preTraitDiffSym = psyco.proxy(self.align.preTraitDiffSym) 228 diffSym = self.align.preTraitDiffSym(LH1,LH2,texte,dicoPos) # pré-calcul des différences symétriques 229 logging.debug('len(diffSym)='+str(len(diffSym))) 230 logging.debug("fin preTraitDiffSym")
231
232 -class Test_AlignementAlthusserCas1(Test_AlignementCas1):
233 - def setUp(self):
234 f = test_data.TestDataFactory() 235 self.td = f.getTestData(test_data.Althusser,"1")
236 -class Test_AlignementAlthusserCas2(Test_AlignementCas1):
237 - def setUp(self):
238 f = test_data.TestDataFactory() 239 self.td = f.getTestData(test_data.Althusser,"2")
240
241 -class Test_AlignementChedidCas1(Test_AlignementCas1):
242 - def setUp(self):
243 f = test_data.TestDataFactory() 244 self.td = f.getTestData(test_data.Chedid,"1")
245
246 -class Test_AlignementSuite(unittest.TestSuite):
247 - def __init__(self):
248 unittest.TestSuite.__init__(self,map(Test_Alignement,( 249 "test_deplacements_pond" 250 )))
251
252 -def suite():
253 suite1 = unittest.TestSuite() 254 #suite1.addTest(Test_AlignementCas1("test_deplacements_pond_Dicho")) 255 #suite1.addTest(Test_AlignementCas1("test_deplacements_pond_Astar2")) 256 #suite1.addTest(Test_AlignementCas1("test_deplacements_pond_AstarRecur")) 257 #suite1.addTest(Test_AlignementCas1("test_deplacements_pond_AstarRecur2")) 258 #suite1.addTest(Test_AlignementCas1("test_deplacements_pond_AstarRecur3")) 259 #suite1.addTest(Test_AlignementCas1("test_deplacements_pond_AstarRecur4")) 260 #suite1.addTest(Test_AlignementCas1("test_deplacements_pond_AstarRecur5")) 261 suite2 = unittest.TestSuite() 262 #suite2.addTest(Test_AlignementCas2("test_deplacements_pond_Dicho")) 263 #suite2.addTest(Test_AlignementCas2("test_deplacements_pond_Astar2")) 264 #suite2.addTest(Test_AlignementCas2("test_deplacements_pond_AstarRecur")) 265 suite2.addTest(Test_AlignementCas2("test_deplacements_pond_AstarRecur2")) 266 #suite2.addTest(Test_AlignementCas2("test_deplacements_pond_AstarRecur3")) 267 #suite2.addTest(Test_AlignementCas2("test_deplacements_pond_AstarRecur4")) 268 #suite2.addTest(Test_AlignementCas2("test_deplacements_pond_AstarRecur5")) 269 #suite2.addTest(Test_AlignementCas2("test_deplacements_pond_AstarRecurNew")) 270 #suite2.addTest(Test_AlignementChedidCas1("test_deplacements_pond_ShapiraRecur")) 271 #suite2.addTest(Test_AlignementChedidCas1("test_preTraitDiffSymVect")) 272 suite3 = unittest.TestSuite() 273 #suite3.addTest(Test_AlignementCas3("test_deplacements_pond_AstarRecur3")) 274 #suite3.addTest(Test_AlignementCas3("test_deplacements_pond_AstarRecur2")) 275 suite4 = unittest.TestSuite() 276 #suite4.addTest(Test_AlignementCas4("test_deplacements_pond_AstarRecur3")) 277 #suite4.addTest(Test_AlignementCas4("test_deplacements_pond_AstarRecur2")) 278 suite5 = unittest.TestSuite() 279 #suite5.addTest(Test_AlignementCas5("test_deplacements_pond_AstarRecur3")) 280 #suite5.addTest(Test_AlignementCas5("test_deplacements_pond_AstarRecur2")) 281 suite6 = unittest.TestSuite() 282 #suite6.addTest(Test_AlignementCharroiCas1("test_deplacements_pond_AstarRecur5")) 283 #suite6.addTest(Test_AlignementCharroiCas1("test_deplacements_pond_AstarRecur2")) 284 suite7 = unittest.TestSuite() 285 #suite7.addTest(Test_AlignementBioCas1("test_deplacements_pond_AstarRecur5")) 286 #suite7.addTest(Test_AlignementBioCas1("test_deplacements_pond_AstarRecur2")) 287 suite8 = unittest.TestSuite() 288 #suite8.addTest(Test_AlignementCondorcetCas1("test_deplacements_pond_AstarRecur5")) 289 #suite8.addTest(Test_AlignementCondorcetCas2("test_deplacements_pond_AstarRecur5")) 290 #suite8.addTest(Test_AlignementTraductionCas1("test_deplacements_pond_AstarRecur5")) 291 #suite8.addTest(Test_AlignementTraductionCas1("test_preTraitDiffSym_AstarRecur5")) 292 #suite8.addTest(Test_AlignementAlthusserCas1("test_deplacements_pond_AstarRecur5")) 293 #suite8.addTest(Test_AlignementAlthusserCas2("test_deplacements_pond_AstarRecur5")) 294 #suite8.addTest(Test_AlignementCas3("test_deplacements_pond_AstarRecur2")) 295 suite = unittest.TestSuite((suite1, suite2, suite3, suite4, suite5, suite6,suite7,suite8)) 296 return suite
297
298 -def trace(commande,dic_locals):
299 import hotshot,hotshot.stats,sys 300 #import profile,pstats,sys 301 #profile.runctx(commande,globals(), dic_locals,'c:\workspace\medite\statFile') 302 #s = pstats.Stats('c:\workspace\medite\statFile') 303 #s.sort_stats('time') 304 #s.print_stats() 305 #sys.stderr.flush() 306 #sys.stdout.flush() 307 prof = hotshot.Profile("c:\workspace\medite\statFile") 308 benchtime = prof.runctx(commande,globals(), dic_locals) 309 prof.close() 310 stats = hotshot.stats.load("c:\workspace\medite\statFile") 311 #stats.strip_dirs() 312 stats.sort_stats('cumulative', 'time', 'calls') 313 stats.print_stats()
314 315 if __name__ == '__main__': 316 logging.basicConfig(level=logging.DEBUG,#INFO, 317 format='%(asctime)s %(levelname)s %(message)s', 318 #datefmt='%H:%M:%S', 319 filename=os.path.join(os.getcwd(),'log.txt'), 320 filemode='w') 321 #console = logging.StreamHandler() 322 #console.setLevel(logging.INFO) 323 #logging.getLogger('').addHandler(console) 324 325 unittest.TextTestRunner(verbosity=2).run(suite()) 326 #unittest.TextTestRunner(verbosity=2).run(Test_MediteAppliSuite()) 327