première mise à jour pour python 3
[iramuteq] / autres / textdist.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2020 Pierre Ratinaud
4 #License: GNU/GPL
5
6 #------------------------------------
7 # import des modules python
8 #------------------------------------
9 import os
10 import sys
11 from time import time, sleep
12
13 #------------------------------------
14 # import des modules wx
15 #------------------------------------
16 import wx
17
18 #------------------------------------
19 # import des fichiers du projet
20 #------------------------------------
21 from chemins import ConstructPathOut, ConstructAfcUciPath, ChdTxtPathOut
22 from corpus import Corpus
23 from OptionAlceste import OptionPam
24 from analysetxt import AnalyseText
25 from configparser import *
26 from functions import print_liste, exec_rcode, CreateIraFile, progressbar, check_Rresult, BugDialog
27 from layout import PrintRapport
28 from PrintRScript import ReinertTxtProf, RPamTxt
29 from openanalyse import OpenAnalyse
30
31
32 class AnalysePam(AnalyseText) :
33
34 #    def __init__(self, parent, corpus, cmd = False):
35
36     def doanalyse(self) :
37         self.parametres['type'] = 'pamtxt'
38         self.pathout.basefiles(ChdTxtPathOut)
39         self.actives, lim = self.corpus.make_actives_nb(self.parametres['max_actives'], 1)
40         self.parametres['eff_min_forme'] = lim
41         self.parametres['nbactives'] = len(self.actives)
42         if self.parametres['classif_mode'] == 0 :
43             self.corpus.make_and_write_sparse_matrix_from_uces(self.actives, self.pathout['TableUc1'], self.pathout['listeuce1'])
44         elif self.parametres['classif_mode'] == 1 :
45             self.corpus.make_and_write_sparse_matrix_from_uci(self.actives, self.pathout['TableUc1'], self.pathout['listeuce1'])
46         RPamTxt(self.corpus, self.parent.RscriptsPath) 
47         #t1 = time()
48         #self.parent = parent
49         #self.corpus = corpus
50         #self.cmd = cmd
51         if not self.cmd :
52             self.dlg = progressbar(self, 9)
53         #else :
54         #    self.dlg = None
55         #ucis_txt, ucis_paras_txt = self.corpus.start_analyse(self.parent, dlg = self.dlg, cmd = self.cmd)
56         #self.corpus.make_len_uce(self.corpus.get_tot_occ_from_ucis_txt(ucis_txt))
57         #del ucis_txt
58         #if not self.cmd :
59         #    self.dlg.Update(5, '%i ucis - Construction des uces' % len(self.corpus.ucis))        
60         #if self.corpus.parametre['type'] == 0 :
61         #    self.corpus.make_ucis_paras_uces(ucis_paras_txt, make_uce = True)
62         #else :
63         #    self.corpus.make_ucis_paras_uces(ucis_paras_txt, make_uce = False)
64         #del ucis_paras_txt
65         #if not self.cmd :
66         #    self.dlg.Update(6, u'Dictionnaires')        
67         #uces, orderuces = self.corpus.make_forms_and_uces()
68         #self.corpus.ucenb = len(uces)
69         #self.corpus.make_lems(self.parent.lexique)
70         #self.corpus.min_eff_formes()
71         #self.corpus.make_var_actives() 
72         #self.corpus.make_var_supp()
73         #if not self.cmd :
74         #    self.dlg.Update(7, u'Creation des tableaux')
75         if self.corpus.parametre['type'] == 0:
76             tabuc1 = self.corpus.make_table_with_uce(orderuces)
77             uc1 = None
78             uces1 = [[uce, i] for i,uce in enumerate(orderuces)]
79             self.corpus.write_tab([['uce','uc']] + [[i, i] for i in range(0,len(uces))], self.corpus.dictpathout['listeuce1'])
80         else :
81             tabuc1 = self.corpus.make_table_with_uci()
82             uc1 = None
83             uces1 = [[uce, i] for i, uce in enumerate(orderuces)]
84             self.corpus.write_tab([['uce','uc']] + [[i, i] for i in range(0,len(orderuces))], self.corpus.dictpathout['listeuce1'])
85         self.corpus.write_tab(tabuc1,self.corpus.dictpathout['TableUc1'])
86         self.corpus.lenuc1 = len(tabuc1)
87         del tabuc1, uc1
88         RPamTxt(self, self.parent.RscriptsPath)
89         self.DoR(self.pathout['Rchdtxt'], dlg = self.dlg, message = 'R...')
90         #pid = exec_rcode(self.parent.RPath,self.pathout['Rchdtxt'], wait = False)
91         #while pid.poll() == None :
92         #    if not self.cmd :
93         #        self.dlg.Pulse(u'CHD...')
94         #        sleep(0.2)
95         #    else :
96         #        pass
97         #check_Rresult(self.parent, pid)
98         self.corpus.make_ucecl_from_R(self.pathout['uce'])
99         #ucecl0 = [cl for uce,cl in ucecl if cl != 0]
100         #clnb = len(list(set(ucecl0)))
101         #classes = [cl for uce, cl in ucecl]
102         #uces1 = [val for val, i in uces1] 
103         #self.corpus.make_lc(uces1, classes, clnb)
104         #self.corpus.build_profile(clnb, classes, self.corpus.actives, self.corpus.dictpathout['Contout']) 
105         self.corpus.make_and_write_profile(self.actives, self.corpus.lc, self.pathout['Contout'])
106         self.sup, lim = self.corpus.make_actives_nb(self.parametres['max_actives'], 2)
107         self.corpus.make_and_write_profile(self.sup, self.corpus.lc, self.pathout['ContSupOut'])
108         self.corpus.make_and_write_profile_et(self.corpus.lc, self.pathout['ContEtOut'])
109         self.clnb = len(self.corpus.lc)
110         self.parametres['clnb'] = self.clnb
111         #passives = [lem for lem in self.corpus.lems if lem not in self.corpus.actives]
112         #self.corpus.build_profile(clnb, classes, self.corpus.supp, self.corpus.dictpathout['ContSupOut'])
113         #self.corpus.make_etoiles(self.corpus.para_coords)
114         #self.corpus.build_profile_et(clnb, classes, uces1, self.corpus.dictpathout['ContEtOut'])
115         AlcesteTxtProf(self.pathout, self.parent.RscriptsPath, clnb, '0.9')
116         self.doR(self.pathout['RTxtProfGraph'], dlg = self.dlg, message = 'profils et A.F.C. ...')
117         #pid = exec_rcode(self.parent.RPath, self.corpus.dictpathout['RTxtProfGraph'], wait = False)
118         #while pid.poll() == None :
119         #    if not self.cmd :
120         #        self.dlg.Pulse(u'AFC...')
121         #        sleep(0.2)
122         #    else :
123         #        pass
124         #check_Rresult(self.parent, pid)
125         #temps = time() - t1
126         #self.corpus.minutes, self.corpus.seconds = divmod(temps, 60)
127         #self.corpus.hours, self.corpus.minutes = divmod(self.corpus.minutes, 60)
128         PrintRapport(self, self.corpus, self.parametres)
129         #CreateIraFile(self.corpus.dictpathout, clnb, os.path.basename(self.corpus.parametre['filename']))
130         self.corpus.save_corpus(self.corpus.dictpathout['db'])
131         afc_graph_list = [[os.path.basename(self.corpus.dictpathout['AFC2DL_OUT']), 'Variables actives - coordonnées - facteurs 1 / 2'],
132           [os.path.basename(self.corpus.dictpathout['AFC2DSL_OUT']), 'variables supplémentaires - coordonnées - facteurs 1 / 2'],
133           [os.path.basename(self.corpus.dictpathout['AFC2DEL_OUT']), 'Variables illustratives - Coordonnées - facteur 1 / 2'],
134           [os.path.basename(self.corpus.dictpathout['AFC2DCL_OUT']), 'Classes - Coordonnées - facteur 1 / 2']]
135         chd_graph_list = [[os.path.basename(self.corpus.dictpathout['arbre1']), 'résultats de la classification']]
136         print_liste(self.pathout['liste_graph_afc'],afc_graph_list)
137         print_liste(self.pathout['liste_graph_chd'],chd_graph_list)
138         #if not self.cmd :
139         #    self.dlg.Update(9, u'fin')
140         #    self.dlg.Destroy()
141         #print 'fini'
142
143
144 class PamTxt:
145
146     def __init__(self, parent, cmd = False):       
147         print('pam txt')
148         self.type = 'pamsimple'
149         self.parent = parent
150         self.ConfigPath = parent.ConfigPath
151         self.DictPath = parent.DictPath
152         self.pamconf = ConfigParser()
153         self.pamconf.read(self.ConfigPath['pam'])
154         self.KeyConf = ConfigParser()
155         self.KeyConf.read(self.ConfigPath['key'])
156         self.corpus = Corpus(parent)
157         try :
158             self.corpus.content = self.parent.content
159             self.corpus.parametre['encodage'] = parent.corpus_encodage
160             self.corpus.parametre['filename'] = parent.filename
161             self.corpus.parametre['lang'] = parent.corpus_lang
162             if not cmd :
163                 self.dial = OptionPam(self, parent)
164                 self.dial.CenterOnParent()
165                 self.val = self.dial.ShowModal()
166                 self.print_param()
167             else :
168                 self.val = wx.ID_OK
169             if self.val == wx.ID_OK :
170                 file = open(self.ConfigPath['key'], 'w')
171                 self.KeyConf.write(file)
172                 file.close()
173                 self.read_param()
174                 #self.corpus.parametre.update(param)
175                 self.corpus.supplementaires = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "2"]
176                 self.corpus.typeactive = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "1"]
177                 self.corpus.dictpathout =  ChdTxtPathOut(ConstructPathOut(self.corpus.parametre['filename'], 'PamSimple'))
178                 AnalysePam(parent, self.corpus ,cmd = cmd)
179         except AttributeError :
180             wx.MessageBox('Vous devez charger un corpus\nFichier -> Ouvrir un texte', 'Pas de corpus', wx.OK|wx.ICON_INFORMATION)
181             self.val = False
182
183     def print_param(self) :
184         self.pamconf = RawConfigParser()
185         self.pamconf.read(self.parent.ConfigPath['pam'])
186         vallem = self.dial.radio_1.GetSelection()
187         if vallem == 0 : vallem = True
188         else : vallem = False
189         expressions = self.dial.radio_exp.GetSelection()
190         if expressions == 0 : expressions = True
191         else: expressions = False
192         if self.dial.cltype[self.dial.radio_box_3.GetSelection()] == 'k-means (pam)' : cluster_type = 'pam'
193         else : cluster_type = 'fanny'
194         self.pamconf.set('pam', 'lem', vallem)
195         self.pamconf.set('pam', 'expressions', expressions)
196         self.pamconf.set('pam', 'type', self.dial.radio_box_classif.GetSelection())
197         self.pamconf.set('pam', 'method', self.dial.distance[self.dial.choice_1.GetSelection()])
198         self.pamconf.set('pam', 'cluster_type', cluster_type)
199         self.pamconf.set('pam', 'nbforme_uce', str(self.dial.spin_ctrl_3.GetValue()))
200         self.pamconf.set('pam', 'nbcl', str(self.dial.spin_ctrl_4.GetValue()))
201         self.pamconf.set('pam', 'expressions', expressions)
202         self.pamconf.set('pam', 'max_actives', str(self.dial.spin_max_actives.GetValue()))
203         with open(self.ConfigPath['pam'], 'w') as fileout :
204             self.pamconf.write(fileout)
205
206     def read_param(self) :
207         conf = RawConfigParser()
208         conf.read(self.parent.ConfigPath['pam'])
209         for option in conf.options('pam') :
210             if conf.get('pam', option).isdigit() :
211                 self.corpus.parametre[option] = int(conf.get('pam', option))
212             else :
213                 self.corpus.parametre[option] = conf.get('pam', option)
214         list_bool = ['lem', 'expressions']
215         for val in list_bool :
216             if self.corpus.parametre[val] == 'True' :
217                 self.corpus.parametre[val] = True
218             else : 
219                 self.corpus.parametre[val] = False
220         if self.corpus.parametre['type'] == 0 :
221             self.corpus.parametre['classif_mode'] = 1
222         else :
223             self.corpus.parametre['classif_mode'] = 2