7bbd053b2b28ab2f35a2509430642caea8ee7a68
[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             print(self.keys)
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
62             self.t1 = time()
63             if not self.parametres.get('dictionary', False):
64                 self.corpus.make_lems(lem=self.parametres['lem'])
65             else:
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)
74 #            if dlg:
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)
85                 if dlg :
86 #                    if not isinstance(dlg, int) :
87 #                        dlg.Destroy()
88                     self.dlg = progressbar(self.ira, dlg)
89                     OpenAnalyse(self.parent, self.parametres['ira'])
90                     self.ira.tree.AddAnalyse(self.parametres)
91                     self.val = 5100
92                     self.dlg.Destroy()
93             else :
94                 self.val = False
95                 if dlg :
96                     try :
97                         self.dlg.Destroy()
98                     except :
99                         pass
100         else :
101             #if isinstance(dlg, wx.ProgressDialog) :
102             #    self.dlg.Destroy()
103             self.val = False
104
105     def doanalyse(self) :
106         pass
107
108     def lemparam(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()
114             if val == 5100 :
115                 if dial.radio_lem.GetSelection() == 0 :
116                     lem = 1
117                 else :
118                     lem = 0
119                 self.parametres['lem'] = lem
120                 if dial.radio_dictchoice.GetSelection() == 1 :
121                     self.parametres['dictionary'] = dial.dictpath.GetValue()
122                 dial.Destroy()
123                 return self.parametres
124             else :
125                 dial.Destroy()
126                 return None
127         else :
128             return self.parametres
129
130     def make_config(self, config) :
131         if config is not None :
132             if not self.dlg :
133                 return config
134             else :
135                 return self.preferences()
136         else :
137             return None
138
139     def readconfig(self, config) :
140         return config
141
142     def preferences(self) :
143         return self.parametres
144
145     def printRscript(self) :
146         pass
147
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)
153         else:
154             dialProgression = dlg
155         pid = exec_rcode(self.ira.RPath, Rscript, wait=wait)
156         while pid.poll() is None :
157             if dlg :
158                 dialProgression.Pulse(message)
159                 sleep(0.2)
160             else :
161                 sleep(0.2)
162         if isinstance(dlg, int):
163             dialProgression.Destroy()
164         return check_Rresult(self.ira, pid)