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