correction labbé
[iramuteq] / analysetxt.py
1 # -*- coding: utf-8 -*-
2 # Author: Pierre Ratinaud
3 # lisence : GNU GPL
4 # copyright : 2012-2013 (c) Pierre Ratinaud
5
6 import logging
7 from chemins import PathOut
8 from functions import exec_rcode, check_Rresult, DoConf, ReadDicoAsDico, progressbar
9 from shutil import copy
10 from time import time, sleep
11 from uuid import uuid4
12 import os
13 from openanalyse import OpenAnalyse
14 from dialog import StatDialog
15
16 log = logging.getLogger('iramuteq.analyse')
17
18 class AnalyseText :
19     def __init__(self, ira, corpus, parametres=None, dlg=False, lemdial=True) :
20         self.corpus = corpus
21         self.ira = ira
22         self.parent = ira
23         self.dlg = dlg
24         self.dialok = True
25         self.parametres = parametres
26         #print(parametres)
27         self.lemdial = lemdial
28         self.val = False
29         self.keys = DoConf(self.ira.ConfigPath['key']).getoptions()
30         if not 'pathout' in self.parametres :
31             self.pathout = PathOut(corpus.parametres['originalpath'], analyse_type=parametres['type'], dirout=corpus.parametres['pathout'])
32         else :
33             self.pathout = PathOut(filename=corpus.parametres['originalpath'], dirout=self.parametres['pathout'], analyse_type=self.parametres['type'])
34         self.parametres = self.lemparam()
35         if self.parametres is not None :
36             self.parametres = self.make_config(parametres)
37         #print 'paramtre aprs make_config', self.parametres
38         log.info(self.pathout.dirout)
39         if self.parametres is not None :
40             self.keys = DoConf(self.ira.ConfigPath['key']).getoptions()
41             gramact = [k for k in self.keys if self.keys[k] == 1]
42             gramsup = [k for k in self.keys if self.keys[k] == 2]
43             self.parametres['pathout'] = self.pathout.mkdirout()
44             self.pathout = PathOut(dirout=self.parametres['pathout'])
45             self.pathout.createdir(self.parametres['pathout'])
46             self.parametres['corpus'] = self.corpus.parametres['uuid']
47             self.parametres['uuid'] = str(uuid4())
48             self.parametres['name'] = os.path.split(self.parametres['pathout'])[1]
49             self.parametres['type'] = parametres['type']
50             self.parametres['encoding'] = self.ira.syscoding
51             self.t1 = time()
52             if not self.parametres.get('dictionary', False) :
53                 print self.parametres
54                 self.corpus.make_lems(lem=self.parametres['lem'])
55             else :
56                 print 'read new dico'
57                 dico = ReadDicoAsDico(self.parametres['dictionary'])
58                 self.corpus.make_lems_from_dict(dico, dolem=self.parametres['lem'])
59                 dictname = os.path.basename(self.parametres['dictionary'])
60                 dictpath = os.path.join(self.pathout.dirout, dictname)
61                 copy(self.parametres['dictionary'], dictpath)
62                 self.parametres['dictionary'] = dictpath
63             self.corpus.parse_active(gramact, gramsup)
64             if dlg :
65                 self.dlg = progressbar(self.ira, dlg)
66             result_analyse = self.doanalyse()
67             if result_analyse is None :
68                 self.time = time() - self.t1
69                 minutes, seconds = divmod(self.time, 60)
70                 hours, minutes = divmod(minutes, 60)
71                 self.parametres['time'] = '%.0fh %.0fm %.0fs' % (hours, minutes, seconds)
72                 self.parametres['ira'] = self.pathout['Analyse.ira']
73                 DoConf().makeoptions([self.parametres['type']], [self.parametres], self.pathout['Analyse.ira'])
74                 self.ira.history.add(self.parametres)
75                 if dlg :
76                     if not isinstance(dlg, int) :
77                         dlg.Destroy()
78                     self.dlg.Destroy()
79                     OpenAnalyse(self.parent, self.parametres['ira'])
80                     self.ira.tree.AddAnalyse(self.parametres)
81                     self.val = 5100
82             else :
83                 self.val = False
84                 if dlg :
85                     try :
86                         self.dlg.Destroy()
87                     except :
88                         pass
89         else :
90             #if isinstance(dlg, wx.ProgressDialog) :
91             #    self.dlg.Destroy()
92             self.val = False
93
94     def doanalyse(self) :
95         pass
96
97     def lemparam(self) :
98         if self.dlg and self.lemdial:
99             dial = StatDialog(self.parent, self.keys)
100             dial.CenterOnParent()
101             dial.corpus = self.corpus
102             val = dial.ShowModal()
103             if val == 5100 :
104                 if dial.radio_lem.GetSelection() == 0 :
105                     lem = 1
106                 else :
107                     lem = 0
108                 self.parametres['lem'] = lem
109                 if dial.radio_dictchoice.GetSelection() == 1 :
110                     self.parametres['dictionary'] = dial.dictpath.GetValue()
111                 dial.Destroy()
112                 return self.parametres
113             else :
114                 dial.Destroy()
115                 return None
116         else :
117             return self.parametres
118
119     def make_config(self, config) :
120         if config is not None :
121             if not self.dlg :
122                 return config
123             else :
124                 return self.preferences()
125         else :
126             return None
127
128     def readconfig(self, config) :
129         return config
130
131     def preferences(self) :
132         return self.parametres
133
134     def printRscript(self) :
135         pass
136
137     def doR(self, Rscript, wait=False, dlg=None, message='') :
138         log.info('R code...')
139         pid = exec_rcode(self.ira.RPath, Rscript, wait=wait)
140         while pid.poll() is None :
141             if dlg :
142                 self.dlg.Pulse(message)
143                 sleep(0.2)
144             else :
145                 sleep(0.2)
146         return check_Rresult(self.ira, pid)