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