...+++...
[iramuteq] / textsimi.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2013 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 log = 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             if not self.doR(script.scriptout, dlg = self.dlg, message = 'R...') :
52                 log.info('Problem')
53                 return False
54             if self.parametres['type_graph'] == 1:
55                 if os.path.exists(self.pathout['liste_graph']):
56                     graph_simi = read_list_file(self.pathout['liste_graph'])
57                     graph_simi.append([os.path.basename(script.filename), script.txtgraph])
58                 else :
59                     graph_simi = [[os.path.basename(script.filename), script.txtgraph]]
60                 print_liste(self.pathout['liste_graph'], graph_simi)
61         else : 
62             return False
63
64 #    def preferences(self) :
65 #        dial = StatDialog(self, self.parent)
66 #        dial.CenterOnParent()
67 #        val = dial.ShowModal()
68 #        if val == 5100 :
69 #            if dial.radio_lem.GetSelection() == 0 :
70 #                lem = 1
71 #            else :
72 #                lem = 0            
73 #            self.parametres['lem'] = lem
74 #            dial.Destroy()
75 #            return self.parametres
76 #        else :
77 #            dial.Destroy()
78 #            return None
79
80     def makesimiparam(self) :
81         self.paramsimi = {'coeff' : 0,
82                           'layout' : 2,
83                           'type_graph' : 1,
84                           'arbremax' : 1,
85                           'coeff_tv' : 1,
86                           'coeff_tv_nb' : 0,
87                           'tvprop' : 0,
88                           'tvmin' : 5,
89                           'tvmax' : 30,
90                           'coeff_te' : 1,
91                           'coeff_temin' : 1,
92                           'coeff_temax' : 10,
93                           'label_v': 1,
94                           'label_e': 0,
95                           'vcex' : 1,
96                           'cexfromchi' : False,
97                           'vcexmin' : 10,
98                           'vcexmax' : 25,
99                           'cex' : 10,
100                           'seuil_ok' : 0,
101                           'seuil' : 1,
102                           'cols' : (255,0,0),
103                           'cola' : (200,200,200),
104                           'width' : 1000,
105                           'height' : 1000,
106                           'bystar' : False,
107                           'first' : True,
108                           'keep_coord' : True,
109                           'alpha' : 20,
110                           'film': False,
111                           'svg' : 0,
112                           'com' : 0,
113                           'communities' : 0,
114                           'halo' : 0,
115                           #'ira' : self.pathout['Analyse.ira']
116                           }
117         self.parametres.update(self.paramsimi)
118
119     def makefiles(self, lim=3) :
120         #self.actives, lim = self.corpus.make_actives_nb(self.parametres.get('max_actives',1500), 1)
121         self.parametres['eff_min_forme'] = lim
122         self.parametres['nbactives'] = len(self.actives)
123         self.parametres['fromprof'] = False
124         self.corpus.make_and_write_sparse_matrix_from_uces(self.actives, self.pathout['mat01.csv'], self.pathout['listeuce1.csv'])
125         with open(self.pathout['actives.csv'], 'w') as f :
126             f.write('\n'.join(self.actives).encode(self.ira.syscoding))
127
128 class SimiFromCluster(SimiTxt) :
129     def __init__(self, ira, corpus, actives, numcluster, parametres = None, dlg = False) :
130         self.actives = actives
131         self.numcluster = numcluster
132         parametres['name'] = 'simi_classe_%i' % (numcluster + 1)
133         SimiTxt.__init__(self, ira, corpus, parametres, dlg, lemdial = False)
134     
135     def preferences(self) :
136         return self.parametres
137
138     def doanalyse(self) :
139         self.parametres['type'] = 'clustersimitxt'
140         self.pathout.basefiles(simipath)
141         self.indices = indices_simi
142         self.makesimiparam()
143         if 'bystar' in self.parametres :
144             del self.parametres['bystar']
145         dictcol = dict([[i, [act, self.corpus.getlemclustereff(act, self.numcluster)]] for i, act in enumerate(self.actives)]) 
146         continu = True
147         if self.dlg :
148             #self.listet = self.corpus.make_etoiles()
149             #self.listet.sort()
150             self.stars = []#copy(self.listet)
151             self.parametres['stars'] = False#copy(self.listet)
152             self.parametres['sfromchi'] = True
153             prep = PrepSimi(self.ira, self, self.parametres, self.pathout['selected.csv'], self.actives, indices_simi, wordlist=dictcol)
154             if prep.val == wx.ID_OK :
155                 continu = True
156                 self.parametres = prep.parametres
157             else :
158                 continu = False
159         if continu :
160             self.makefiles()
161             script = PrintSimiScript(self)
162             script.make_script()
163             if not self.doR(script.scriptout, dlg = self.dlg, message = 'R ...') :
164                 return False
165             if self.parametres['type_graph'] == 1:
166                 if self.parametres['svg'] :
167                     filename, ext = os.path.splitext(script.filename)
168                     fileout = filename + '.svg'                    
169                 else :
170                     fileout = self.script.filename
171                 if os.path.exists(self.pathout['liste_graph']):
172                     graph_simi = read_list_file(self.pathout['liste_graph'])
173                     graph_simi.append([os.path.basename(fileout), script.txtgraph])
174                 else :
175                     graph_simi = [[os.path.basename(fileout), script.txtgraph]]
176                 print_liste(self.pathout['liste_graph'], graph_simi)
177         else : 
178             return False
179
180     def makefiles(self) :
181         self.parametres['eff_min_forme'] = 3
182         self.parametres['nbactives'] = len(self.actives)
183         self.parametres['fromprof'] = True
184         self.corpus.make_and_write_sparse_matrix_from_classe(self.actives, self.corpus.lc[self.numcluster], self.pathout['mat01.csv'])
185         with open(self.pathout['actives.csv'], 'w') as f :
186             f.write('\n'.join(self.actives).encode(self.ira.syscoding))