multisplit
[iramuteq] / analysetxt.py
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
5 #License: GNU/GPL
6
7 #------------------------------------
8 # import des modules python
9 #------------------------------------
10 import logging
11 from shutil import copy
12 from time import time, sleep
13 from uuid import uuid4
14 import os
15
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
23
24
25
26 log = logging.getLogger('iramuteq.analyse')
27
28
29
30 class AnalyseText :
31
32     def __init__(self, ira, corpus, parametres=None, dlg=False, lemdial=True):
33         self.corpus = corpus
34         self.ira = ira
35         self.parent = ira
36         self.dlg = dlg
37         self.dialok = True
38         self.parametres = parametres
39         self.lemdial = lemdial
40         self.val = False
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'])
44         else:
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
61             self.t1 = time()
62             if not self.parametres.get('dictionary', False):
63                 self.corpus.make_lems(lem=self.parametres['lem'])
64             else:
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)
73 #            if dlg:
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)
84                 if dlg :
85 #                    if not isinstance(dlg, int) :
86 #                        dlg.Destroy()
87                     self.dlg = progressbar(self.ira, dlg)
88                     OpenAnalyse(self.parent, self.parametres['ira'])
89                     self.ira.tree.AddAnalyse(self.parametres)
90                     self.val = 5100
91                     self.dlg.Destroy()
92             else :
93                 self.val = False
94                 if dlg :
95                     try :
96                         self.dlg.Destroy()
97                     except :
98                         pass
99         else :
100             #if isinstance(dlg, wx.ProgressDialog) :
101             #    self.dlg.Destroy()
102             self.val = False
103
104     def doanalyse(self) :
105         pass
106
107     def lemparam(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()
113             if val == 5100 :
114                 if dial.radio_lem.GetSelection() == 0 :
115                     lem = 1
116                 else :
117                     lem = 0
118                 self.parametres['lem'] = lem
119                 if dial.radio_dictchoice.GetSelection() == 1 :
120                     self.parametres['dictionary'] = dial.dictpath.GetValue()
121                 dial.Destroy()
122                 return self.parametres
123             else :
124                 dial.Destroy()
125                 return None
126         else :
127             return self.parametres
128
129     def make_config(self, config) :
130         if config is not None :
131             if not self.dlg :
132                 return config
133             else :
134                 return self.preferences()
135         else :
136             return None
137
138     def readconfig(self, config) :
139         return config
140
141     def preferences(self) :
142         return self.parametres
143
144     def printRscript(self) :
145         pass
146
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)
152         else:
153             dialProgression = dlg
154         pid = exec_rcode(self.ira.RPath, Rscript, wait=wait)
155         while pid.poll() is None :
156             if dlg :
157                 dialProgression.Pulse(message)
158                 sleep(0.2)
159             else :
160                 sleep(0.2)
161         if isinstance(dlg, int):
162             dialProgression.Destroy()
163         return check_Rresult(self.ira, pid)