1 # -*- coding: utf-8 -*-
2 # Author: Pierre Ratinaud
4 # copyright : 2012-2013 (c) Pierre Ratinaud
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
13 from openanalyse import OpenAnalyse
14 from dialog import StatDialog
16 log = logging.getLogger('iramuteq.analyse')
19 def __init__(self, ira, corpus, parametres=None, dlg=False, lemdial=True) :
25 self.parametres = parametres
26 self.lemdial = lemdial
28 self.keys = DoConf(self.ira.ConfigPath['key']).getoptions()
29 if not 'pathout' in self.parametres :
30 self.pathout = PathOut(corpus.parametres['originalpath'], analyse_type=parametres['type'], dirout=corpus.parametres['pathout'])
32 self.pathout = PathOut(filename=corpus.parametres['originalpath'], dirout=self.parametres['pathout'], analyse_type=self.parametres['type'])
33 self.parametres = self.lemparam()
34 if self.parametres is not None :
35 self.parametres = self.make_config(parametres)
36 log.info(self.pathout.dirout)
37 if self.parametres is not None :
38 self.keys = DoConf(self.ira.ConfigPath['key']).getoptions()
39 gramact = [k for k in self.keys if self.keys[k] == 1]
40 gramsup = [k for k in self.keys if self.keys[k] == 2]
41 self.parametres['pathout'] = self.pathout.mkdirout()
42 self.pathout = PathOut(dirout=self.parametres['pathout'])
43 self.pathout.createdir(self.parametres['pathout'])
44 self.parametres['corpus'] = self.corpus.parametres['uuid']
45 self.parametres['uuid'] = str(uuid4())
46 self.parametres['name'] = os.path.split(self.parametres['pathout'])[1]
47 self.parametres['type'] = parametres['type']
48 self.parametres['encoding'] = self.ira.syscoding
50 if not self.parametres.get('dictionary', False) :
51 self.corpus.make_lems(lem=self.parametres['lem'])
53 dico = ReadDicoAsDico(self.parametres['dictionary'])
54 self.corpus.make_lems_from_dict(dico, dolem=self.parametres['lem'])
55 dictname = os.path.basename(self.parametres['dictionary'])
56 dictpath = os.path.join(self.pathout.dirout, dictname)
57 copy(self.parametres['dictionary'], dictpath)
58 self.parametres['dictionary'] = dictpath
59 self.corpus.parse_active(gramact, gramsup)
61 self.dlg = progressbar(self.ira, dlg)
62 result_analyse = self.doanalyse()
63 if result_analyse is None :
64 self.time = time() - self.t1
65 minutes, seconds = divmod(self.time, 60)
66 hours, minutes = divmod(minutes, 60)
67 self.parametres['time'] = '%.0fh %.0fm %.0fs' % (hours, minutes, seconds)
68 self.parametres['ira'] = self.pathout['Analyse.ira']
69 DoConf().makeoptions([self.parametres['type']], [self.parametres], self.pathout['Analyse.ira'])
70 self.ira.history.add(self.parametres)
72 if not isinstance(dlg, int) :
75 OpenAnalyse(self.parent, self.parametres['ira'])
76 self.ira.tree.AddAnalyse(self.parametres)
83 #if isinstance(dlg, wx.ProgressDialog) :
91 if self.dlg and self.lemdial:
92 dial = StatDialog(self.parent, self.keys)
94 val = dial.ShowModal()
96 if dial.radio_lem.GetSelection() == 0 :
100 self.parametres['lem'] = lem
101 if dial.radio_dictchoice.GetSelection() == 1 :
102 self.parametres['dictionary'] = dial.dictpath.GetValue()
104 return self.parametres
109 return self.parametres
111 def make_config(self, config) :
112 if config is not None :
116 return self.preferences()
120 def readconfig(self, config) :
123 def preferences(self) :
124 return self.parametres
126 def printRscript(self) :
129 def doR(self, Rscript, wait=False, dlg=None, message='') :
130 log.info('R code...')
131 pid = exec_rcode(self.ira.RPath, Rscript, wait=wait)
132 while pid.poll() is None :
134 self.dlg.Pulse(message)
138 return check_Rresult(self.ira, pid)
145 # keys = {'art_def' : 2,
174 # gramact = [k for k in keys if keys[k] == 1]
175 # gramsup = [k for k in keys if keys[k] == 2]