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)
73 OpenAnalyse(self.parent, self.parametres['ira'])
74 self.ira.tree.AddAnalyse(self.parametres)
89 if self.dlg and self.lemdial:
90 dial = StatDialog(self.parent, self.keys)
92 val = dial.ShowModal()
94 if dial.radio_lem.GetSelection() == 0 :
98 self.parametres['lem'] = lem
99 if dial.radio_dictchoice.GetSelection() == 1 :
100 self.parametres['dictionary'] = dial.dictpath.GetValue()
102 return self.parametres
107 return self.parametres
109 def make_config(self, config) :
110 if config is not None :
114 return self.preferences()
118 def readconfig(self, config) :
121 def preferences(self) :
122 return self.parametres
124 def printRscript(self) :
127 def doR(self, Rscript, wait=False, dlg=None, message='') :
128 log.info('R code...')
129 pid = exec_rcode(self.ira.RPath, Rscript, wait=wait)
130 while pid.poll() is None :
132 self.dlg.Pulse(message)
136 return check_Rresult(self.ira, pid)
143 # keys = {'art_def' : 2,
172 # gramact = [k for k in keys if keys[k] == 1]
173 # gramsup = [k for k in keys if keys[k] == 2]