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()
51 gramact = [k for k in self.keys if self.keys[k] == 1]
52 gramsup = [k for k in self.keys if self.keys[k] == 2]
53 self.parametres['pathout'] = self.pathout.mkdirout()
54 self.pathout = PathOut(dirout=self.parametres['pathout'])
55 self.pathout.createdir(self.parametres['pathout'])
56 self.parametres['corpus'] = self.corpus.parametres['uuid']
57 self.parametres['uuid'] = str(uuid4())
58 self.parametres['name'] = os.path.split(self.parametres['pathout'])[1]
59 self.parametres['type'] = parametres['type']
60 #self.parametres['encoding'] = self.ira.syscoding
62 if not self.parametres.get('dictionary', False):
63 self.corpus.make_lems(lem=self.parametres['lem'])
65 print('read new dico')
66 dico = ReadDicoAsDico(self.parametres['dictionary'])
67 self.corpus.make_lems_from_dict(dico, dolem=self.parametres['lem'])
68 dictname = os.path.basename(self.parametres['dictionary'])
69 dictpath = os.path.join(self.pathout.dirout, dictname)
70 copy(self.parametres['dictionary'], dictpath)
71 self.parametres['dictionary'] = dictpath
72 self.corpus.parse_active(gramact, gramsup)
74 # self.dlg = progressbar(self.ira, dlg)
75 result_analyse = self.doanalyse()
76 if result_analyse is None:
77 self.time = time() - self.t1
78 minutes, seconds = divmod(self.time, 60)
79 hours, minutes = divmod(minutes, 60)
80 self.parametres['time'] = '%.0fh %.0fm %.0fs' % (hours, minutes, seconds)
81 self.parametres['ira'] = self.pathout['Analyse.ira']
82 DoConf().makeoptions([self.parametres['type']], [self.parametres], self.pathout['Analyse.ira'])
83 self.ira.history.add(self.parametres)
85 # if not isinstance(dlg, int) :
87 self.dlg = progressbar(self.ira, dlg)
88 OpenAnalyse(self.parent, self.parametres['ira'])
89 self.ira.tree.AddAnalyse(self.parametres)
100 #if isinstance(dlg, wx.ProgressDialog) :
104 def doanalyse(self) :
108 if self.dlg and self.lemdial:
109 dial = StatDialog(self.parent, self.keys)
110 dial.CenterOnParent()
111 dial.corpus = self.corpus
112 val = dial.ShowModal()
114 if dial.radio_lem.GetSelection() == 0 :
118 self.parametres['lem'] = lem
119 if dial.radio_dictchoice.GetSelection() == 1 :
120 self.parametres['dictionary'] = dial.dictpath.GetValue()
122 return self.parametres
127 return self.parametres
129 def make_config(self, config) :
130 if config is not None :
134 return self.preferences()
138 def readconfig(self, config) :
141 def preferences(self) :
142 return self.parametres
144 def printRscript(self) :
147 def doR(self, Rscript, wait=False, dlg=None, message='') :
148 log.info('R code... ')
149 # idéalement, la fonction prendrait en charge la création/destruction de sa propre fenêtre de progression
150 if isinstance(dlg, int):
151 dialProgression = progressbar(self, dlg)
153 dialProgression = dlg
154 pid = exec_rcode(self.ira.RPath, Rscript, wait=wait)
155 while pid.poll() is None :
157 dialProgression.Pulse(message)
161 if isinstance(dlg, int):
162 dialProgression.Destroy()
163 return check_Rresult(self.ira, pid)