...
[iramuteq] / textsimi.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2011 Pierre Ratinaud
4 #Lisense: GNU/GPL
5
6 from chemins import ffr, simipath
7 #from corpus import Corpus
8 import os
9 from analysetxt import AnalyseText
10 from ConfigParser import RawConfigParser
11 from guifunct import getPage, getCorpus
12 from dialog import StatDialog
13 from guifunct import SelectColumn, PrepSimi
14 from functions import indices_simi, progressbar, treat_var_mod, read_list_file, print_liste
15 from tableau import Tableau
16 from tabsimi import DoSimi
17 from PrintRScript import PrintSimiScript
18 import wx
19 from copy import copy
20
21 import logging
22
23 logger = logging.getLogger('iramuteq.textsimi')
24
25 class SimiTxt(AnalyseText): 
26     def doanalyse(self) :
27         self.parametres['type'] = 'simitxt'
28         self.pathout.basefiles(simipath)
29         self.indices = indices_simi
30         self.makesimiparam()
31         #FIXME
32         self.actives = self.corpus.make_actives_limit(3)
33         dictcol = dict([[i, [act, self.corpus.getlemeff(act)]] for i, act in enumerate(self.actives)]) 
34         SelectColumn(self.ira, dictcol, self.actives, self.pathout['selected.csv'], dlg = self.dlg)
35         self.makefiles()
36         if self.dlg :
37             prep = PrepSimi(self.ira, self.parametres, indices_simi)
38             self.parametres = prep.parametres
39         script = PrintSimiScript(self)
40         script.make_script()
41         self.doR(script.scriptout)
42         if self.parametres['type_graph'] == 1:
43             if os.path.exists(self.pathout['liste_graph']):
44                 graph_simi = read_list_file(self.pathout['liste_graph'])
45                 graph_simi.append([os.path.basename(script.filename), script.txtgraph])
46             else :
47                 graph_simi = [[os.path.basename(script.filename), script.txtgraph]]
48             print_liste(self.pathout['liste_graph'], graph_simi)
49
50     def preferences(self) :
51         dial = StatDialog(self, self.parent)
52         dial.CenterOnParent()
53         val = dial.ShowModal()
54         if val == 5100 :
55             if dial.radio_lem.GetSelection() == 0 :
56                 lem = 1
57             else :
58                 lem = 0            
59             self.parametres['lem'] = lem
60             dial.Destroy()
61             return self.parametres
62         else :
63             dial.Destroy()
64             return None
65
66     def makesimiparam(self) :
67         self.paramsimi = {'coeff' : 0,
68                           'layout' : 2,
69                           'type_graph' : 1,
70                           'arbremax' : 1,
71                           'coeff_tv' : 1,
72                           'coeff_tv_nb' : 0,
73                           'tvprop' : 0,
74                           'tvmin' : 5,
75                           'tvmax' : 30,
76                           'coeff_te' : 1,
77                           'coeff_temin' : 1,
78                           'coeff_temax' : 10,
79                           'label_v': 1,
80                           'label_e': 0,
81                           'vcex' : 1,
82                           'cexfromchi' : False,
83                           'vcexmin' : 10,
84                           'vcexmax' : 25,
85                           'cex' : 10,
86                           'seuil_ok' : 0,
87                           'seuil' : 1,
88                           'cols' : (255,0,0),
89                           'cola' : (200,200,200),
90                           'width' : 1000,
91                           'height' : 1000,
92                           'bystar' : False,
93                           'first' : True,
94                           'keep_coord' : True,
95                           'alpha' : 20,
96                           'film': False,
97                           #'ira' : self.pathout['Analyse.ira']
98                           }
99         self.parametres.update(self.paramsimi)
100
101     def makefiles(self, lim=3) :
102         #self.actives, lim = self.corpus.make_actives_nb(self.parametres.get('max_actives',1500), 1)
103         self.parametres['eff_min_forme'] = lim
104         self.parametres['nbactives'] = len(self.actives)
105         self.parametres['fromprof'] = True
106         self.corpus.make_and_write_sparse_matrix_from_uces(self.actives, self.pathout['mat01.csv'], self.pathout['listeuce1.csv'])
107         with open(self.pathout['actives.csv'], 'w') as f :
108             f.write('\n'.join(self.actives).encode(self.ira.syscoding))
109
110         self.listet = self.corpus.make_etoiles()
111         self.listet.sort()
112         self.parametres['stars'] = copy(self.listet)
113         self.stars = copy(self.listet)
114         self.parametres['sfromchi'] = False
115
116
117
118 #        self.tableau = Tableau(self.parent, '')
119 #        self.tableau.listactives = self.actives
120 #        self.tableau.parametre['fromtxt'] = True
121 #        self.corpus.lems_eff = dict([[lem,[self.corpus.lems[lem].freq]] for lem in self.actives])
122 #        #print('ATTENTION  ETOILES')
123 #        #self.paramsimi['bystar'] = True
124 #        self.tableau.listet = copy(self.listet)
125 #        #self.paramsimi['cexfromchi'] = True
126 #        #self.paramsimi['vlabcolor'] = True
127 #        self.tableau.actives = copy(self.corpus.lems_eff)
128 #        DoSimi(self, fromprof = self.pathout['mat01.csv'], param = self.paramsimi, pathout = self.pathout.dirout)
129
130 #class SimiTxt :
131 #    def __init__(self, parent, cmd = False, param = None):
132 #        self.parent = parent
133 #        self.cmd = cmd
134 #        self.ConfigPath = parent.ConfigPath
135 #        self.DictPath = parent.DictPath
136 #        self.KeyConf = RawConfigParser()
137 #        self.KeyConf.read(self.ConfigPath['key'])
138 #        self.indices = indices_simi
139 #        self.paramsimi = {'coeff' : 0,
140 #                          'layout' : 2,
141 #                          'type' : 1,
142 #                          'arbremax' : 1,
143 #                          'coeff_tv' : 1,
144 #                          'coeff_tv_nb' : 0,
145 #                          'tvprop' : 0,
146 #                          'tvmin' : 5,
147 #                          'tvmax' : 30,
148 #                          'coeff_te' : 1,
149 #                          'coeff_temin' : 1,
150 #                          'coeff_temax' : 10,
151 #                          'label_v': 1,
152 #                          'label_e': 0,
153 #                          'vcex' : 1,
154 #                          'cexfromchi' : False,
155 #                          'vcexmin' : 10,
156 #                          'vcexmax' : 25,
157 #                          'cex' : 10,
158 #                          'seuil_ok' : 0,
159 #                          'seuil' : 1,
160 #                          'cols' : (255,0,0),
161 #                          'cola' : (200,200,200),
162 #                          'width' : 1000,
163 #                          'height' : 1000,
164 #                          'bystar' : False,
165 #                          'first' : True,
166 #                          'keep_coord' : True,
167 #                          'alpha' : 20,
168 #                          'film': False,
169 #                          }
170 #        page = getPage(self.parent)
171 #        if page is not None :
172 #            self.corpus = getCorpus(page)
173 #            if self.corpus is not None :
174 #                self.pathout = ConstructPathOut(self.corpus.parametre['openpath'], 'simitxt')
175 #                self.dictpathout = construct_simipath(self.pathout)
176 #                self.val = wx.ID_OK
177 #                self.make_table()
178 #                self.make_simi()
179 #        else :
180 #            self.corpus = Corpus(parent)
181 #            self.corpus.content = self.parent.content
182 #            self.corpus.parametre['encodage'] = parent.corpus_encodage
183 #            self.corpus.parametre['lang'] = parent.corpus_lang
184 #            self.corpus.parametre['filename'] = parent.filename
185 #            self.corpus.parametre['eff_min_uce'] = None
186 #            self.pathout = ConstructPathOut(self.corpus.parametre['filename'], 'simitxt')
187 #            self.dictpathout = construct_simipath(self.pathout)
188 #            dial = StatDialog(self, self.parent)
189 #            dial.check_uce.SetValue(True)
190 #            dial.check_uce.Enable(False)
191 #            dial.OnCheckUce(wx.EVT_MENU)
192 #            self.val = dial.ShowModal()
193 #            if self.val == wx.ID_OK :
194 #                with open(self.parent.ConfigPath['key'], 'w') as f:
195 #                    self.KeyConf.write(f)
196 #                if dial.radio_lem.GetSelection() == 0 : lem = True
197 #                else : lem = False
198 #                if dial.exp.GetSelection() == 0 : exp = True
199 #                else : exp = False
200 #                dial.Destroy()
201 #                self.corpus.parametre['lem'] = lem
202 #                self.corpus.parametre['expressions'] = exp
203 #                self.corpus.parametre['nbforme_uce'] = dial.spin_ctrl_4.GetValue()
204 #                self.corpus.parametre['eff_min_forme'] = dial.spin_ctrl_5.GetValue()
205 #                self.corpus.parametre['max_actives'] = dial.spin_max_actives.GetValue()
206 #                self.make_corpus()
207 #                self.make_table()
208 #                self.make_simi()
209 #            else :
210 #                dial.Destroy()
211 #
212 #    def make_corpus(self) :
213 #        print 'make corpus'
214 #        if not self.cmd :
215 #            dlg = progressbar(self, maxi = 6)
216 #        self.corpus.supplementaires = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "2"]
217 #        self.corpus.typeactive = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "1"]
218 #        ucis_txt, ucis_paras_txt = self.corpus.start_analyse(self.parent, dlg = dlg, cmd = self.cmd)
219 #        del ucis_txt
220 #    
221 #        if not self.cmd :
222 #            dlg.Update(5, '%i ucis - Construction des uces' % len(ucis_paras_txt))
223 #        self.corpus.make_ucis_paras_uces(ucis_paras_txt, make_uce = True)
224 #        del ucis_paras_txt
225 #
226 #        if self.corpus.para_coords != [[] for val in self.corpus.para_coords] :
227 #            self.corpus.parametre['para'] = True
228 #        else :
229 #            self.corpus.parametre['para'] = False
230 #        self.corpus.make_etoiles(self.corpus.para_coords)
231 #        print 'len(ucis_paras_uces)', len(self.corpus.ucis_paras_uces)
232 #
233 #        if not self.cmd :
234 #            dlg.Update(6, u'Dictionnaires')
235 #        uces, self.orderuces = self.corpus.make_forms_and_uces()
236 #        self.corpus.ucenb = len(uces)
237 #        self.corpus.make_lems(self.parent.lexique)
238 #
239 #        self.corpus.make_var_actives() 
240 #        self.corpus.make_var_supp()
241 #        self.corpus.lems_eff = self.corpus.make_lem_eff()
242 #
243 #        #variables = treat_var_mod(listet)
244 #        #print(variables)
245 #        #self.corpus.write_etoiles(self.dictpathout['etoiles'])
246 #        if not self.cmd :
247 #            dlg.Destroy()
248 #                
249 #    def make_table(self) :
250 #        if 'orderuces' not in dir(self) :
251 #            self.orderuces = [(i,j,k) for i, uci in enumerate(self.corpus.ucis_paras_uces) for j, para in enumerate(uci) for k, uce in enumerate(para)]
252 #            self.orderuces = dict([[val, i] for i, val in enumerate(self.orderuces)])
253 #            self.corpus.ucenb = len(self.orderuces)
254 #        #tabuc1 = self.corpus.make_table_with_uce(self.orderuces)
255 #        #tabuc1.insert(0,self.corpus.actives)
256 #        #tabuc1 = self.corpus.make_sparse_matrix_with_uce(self.orderuces)
257 #        #self.corpus.write_sparse_matrix(self.dictpathout['mat01'], tabuc1, self.corpus.ucenb, len(self.corpus.actives))
258 #        if self.corpus.actives is None :
259 #            self.corpus.typeactive = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "1"]
260 #            self.corpus.min_eff_formes()
261 #            self.corpus.make_var_actives()
262 #        self.corpus.make_and_write_sparse_matrix_from_uce(self.orderuces, self.dictpathout['mat01'])
263 #        #self.corpus.write_tab(tabuc1,self.dictpathout['mat01'])
264 #
265 #    def make_simi(self) :
266 #        self.tableau = Tableau(self.parent, '')
267 #        self.tableau.listactives = self.corpus.actives
268 #        self.tableau.parametre['fromtxt'] = True
269 #        if 'lems_eff' not in dir(self.corpus) :
270 #            self.corpus.lems_eff = self.corpus.make_lem_eff()
271 #        #print('ATTENTION  ETOILES')
272 #        #self.paramsimi['bystar'] = True
273 #        self.listet = self.corpus.get_unique_etoiles()
274 #        self.listet.sort()
275 #        self.tableau.listet = copy(self.listet)
276 #        self.paramsimi['stars'] = copy(self.listet)
277 #        #self.paramsimi['cexfromchi'] = True
278 #        self.paramsimi['sfromchi'] = False
279 #        #self.paramsimi['vlabcolor'] = True
280 #        self.tableau.actives = dict([[lem, self.corpus.lems_eff[lem]] for lem in self.corpus.actives])
281 #        self.corpus.save_corpus(self.dictpathout['corpus']) 
282 #        DoSimi(self, fromprof = self.dictpathout['mat01'], param = self.paramsimi, pathout = self.pathout)