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