1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2020 Pierre Ratinaud
4 #modification pour python 3 : Laurent Mérat, 6x7 - mai 2020
7 #------------------------------------
8 # import des modules python
9 #------------------------------------
11 from shutil import copy
12 from time import time, sleep
13 from uuid import uuid4
16 #------------------------------------
17 # import des fichiers du projet
18 #------------------------------------
19 from chemins import PathOut
20 from functions import exec_rcode, check_Rresult, DoConf, ReadDicoAsDico, progressbar
21 from openanalyse import OpenAnalyse
22 from dialog import StatDialog
26 log = logging.getLogger('iramuteq.analyse')
32 def __init__(self, ira, corpus, parametres=None, dlg=False, lemdial=True):
38 self.parametres = parametres
39 self.lemdial = lemdial
41 self.keys = DoConf(self.ira.ConfigPath['key']).getoptions()
42 if not 'pathout' in self.parametres:
43 self.pathout = PathOut(corpus.parametres['originalpath'], analyse_type=parametres['type'], dirout=corpus.parametres['pathout'])
45 self.pathout = PathOut(filename=corpus.parametres['originalpath'], dirout=self.parametres['pathout'], analyse_type=self.parametres['type'])
46 self.parametres = self.lemparam()
47 if self.parametres is not None:
48 self.parametres = self.make_config(parametres)
49 if self.parametres is not None:
50 self.keys = DoConf(self.ira.ConfigPath['key']).getoptions()
52 gramact = [k for k in self.keys if self.keys[k] == 1]
53 gramsup = [k for k in self.keys if self.keys[k] == 2]
54 self.parametres['pathout'] = self.pathout.mkdirout()
55 self.pathout = PathOut(dirout=self.parametres['pathout'])
56 self.pathout.createdir(self.parametres['pathout'])
57 self.parametres['corpus'] = self.corpus.parametres['uuid']
58 self.parametres['uuid'] = str(uuid4())
59 self.parametres['name'] = os.path.split(self.parametres['pathout'])[1]
60 self.parametres['type'] = parametres['type']
61 #self.parametres['encoding'] = self.ira.syscoding
63 if not self.parametres.get('dictionary', False):
64 self.corpus.make_lems(lem=self.parametres['lem'])
66 print('read new dico')
67 dico = ReadDicoAsDico(self.parametres['dictionary'])
68 self.corpus.make_lems_from_dict(dico, dolem=self.parametres['lem'])
69 dictname = os.path.basename(self.parametres['dictionary'])
70 dictpath = os.path.join(self.pathout.dirout, dictname)
71 copy(self.parametres['dictionary'], dictpath)
72 self.parametres['dictionary'] = dictpath
73 self.corpus.parse_active(gramact, gramsup)
75 # self.dlg = progressbar(self.ira, dlg)
76 result_analyse = self.doanalyse()
77 if result_analyse is None:
78 self.time = time() - self.t1
79 minutes, seconds = divmod(self.time, 60)
80 hours, minutes = divmod(minutes, 60)
81 self.parametres['time'] = '%.0fh %.0fm %.0fs' % (hours, minutes, seconds)
82 self.parametres['ira'] = self.pathout['Analyse.ira']
83 DoConf().makeoptions([self.parametres['type']], [self.parametres], self.pathout['Analyse.ira'])
84 self.ira.history.add(self.parametres)
86 # if not isinstance(dlg, int) :
88 self.dlg = progressbar(self.ira, dlg)
89 OpenAnalyse(self.parent, self.parametres['ira'])
90 self.ira.tree.AddAnalyse(self.parametres)
101 #if isinstance(dlg, wx.ProgressDialog) :
105 def doanalyse(self) :
109 if self.dlg and self.lemdial:
110 dial = StatDialog(self.parent, self.keys)
111 dial.CenterOnParent()
112 dial.corpus = self.corpus
113 val = dial.ShowModal()
115 if dial.radio_lem.GetSelection() == 0 :
119 self.parametres['lem'] = lem
120 if dial.radio_dictchoice.GetSelection() == 1 :
121 self.parametres['dictionary'] = dial.dictpath.GetValue()
123 return self.parametres
128 return self.parametres
130 def make_config(self, config) :
131 if config is not None :
135 return self.preferences()
139 def readconfig(self, config) :
142 def preferences(self) :
143 return self.parametres
145 def printRscript(self) :
148 def doR(self, Rscript, wait=False, dlg=None, message='') :
149 log.info('R code... ')
150 # idéalement, la fonction prendrait en charge la création/destruction de sa propre fenêtre de progression
151 if isinstance(dlg, int):
152 dialProgression = progressbar(self, dlg)
154 dialProgression = dlg
155 pid = exec_rcode(self.ira.RPath, Rscript, wait=wait)
156 while pid.poll() is None :
158 dialProgression.Pulse(message)
162 if isinstance(dlg, int):
163 dialProgression.Destroy()
164 return check_Rresult(self.ira, pid)