...
[iramuteq] / textsimi.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2013 Pierre Ratinaud
4 #License: GNU/GPL
5
6 from chemins import ffr, simipath
7 import os
8 from analysetxt import AnalyseText
9 from guifunct import PrepSimi
10 from functions import indices_simi, progressbar, treat_var_mod, read_list_file, print_liste
11 from PrintRScript import PrintSimiScript
12 import wx
13 from copy import copy
14 import logging
15
16 log = logging.getLogger('iramuteq.textsimi')
17
18 class SimiTxt(AnalyseText): 
19     def doanalyse(self) :
20         self.parametres['type'] = 'simitxt'
21         self.pathout.basefiles(simipath)
22         self.indices = indices_simi
23         if self.dlg :
24             self.makesimiparam()
25         #FIXME
26         self.actives = self.corpus.make_actives_limit(3)
27         dictcol = dict([[i, [act, self.corpus.getlemeff(act)]] for i, act in enumerate(self.actives)]) 
28         continu = False
29         if self.dlg :
30             self.listet = self.corpus.make_etoiles()
31             self.listet.sort()
32             self.stars = copy(self.listet)
33             self.parametres['stars'] = copy(self.listet)
34             self.parametres['sfromchi'] = False
35             self.dlg.Destroy()
36             prep = PrepSimi(self.ira, self, self.parametres, self.pathout['selected.csv'], self.actives, indices_simi, wordlist=dictcol)
37             if prep.val == wx.ID_OK :
38                 continu = True
39                 self.parametres = prep.parametres
40                 self.dlg = progressbar(self.ira, 4)
41         else :
42             continu = True
43         if continu :
44             self.makefiles()
45             script = PrintSimiScript(self)
46             script.make_script()
47             if not self.doR(script.scriptout, dlg = self.dlg, message = 'R...') :
48                 log.info('Problem')
49                 return False
50             if self.parametres['type_graph'] == 1:
51                 if self.parametres['svg'] :
52                     filename, ext = os.path.splitext(script.filename)
53                     fileout = filename + '.svg'                    
54                 else :
55                     fileout = script.filename
56                 if os.path.exists(self.pathout['liste_graph']):
57                     graph_simi = read_list_file(self.pathout['liste_graph'])
58                     graph_simi.append([os.path.basename(fileout), script.txtgraph])
59                 else :
60                     graph_simi = [[os.path.basename(fileout), script.txtgraph]]
61                 print_liste(self.pathout['liste_graph'], graph_simi)
62         else : 
63             return False
64
65     def makesimiparam(self) :
66         self.paramsimi = {'coeff' : 0,
67                           'layout' : 2,
68                           'type_graph' : 1,
69                           'arbremax' : 1,
70                           'coeff_tv' : 1,
71                           'coeff_tv_nb' : 0,
72                           'tvprop' : 0,
73                           'tvmin' : 5,
74                           'tvmax' : 30,
75                           'coeff_te' : 1,
76                           'coeff_temin' : 1,
77                           'coeff_temax' : 10,
78                           'label_v': 1,
79                           'label_e': 0,
80                           'vcex' : 1,
81                           'cexfromchi' : False,
82                           'vcexmin' : 10,
83                           'vcexmax' : 25,
84                           'cex' : 10,
85                           'seuil_ok' : 0,
86                           'seuil' : 1,
87                           'cols' : (255,0,0),
88                           'cola' : (200,200,200),
89                           'width' : 1000,
90                           'height' : 1000,
91                           'bystar' : False,
92                           'first' : True,
93                           'keep_coord' : False,
94                           'alpha' : 20,
95                           'film': False,
96                           'svg' : 0,
97                           'com' : 0,
98                           'communities' : 0,
99                           'halo' : 0,
100                           #'ira' : self.pathout['Analyse.ira']
101                           }
102         self.parametres.update(self.paramsimi)
103
104     def makefiles(self, lim=3) :
105         #self.actives, lim = self.corpus.make_actives_nb(self.parametres.get('max_actives',1500), 1)
106         self.parametres['eff_min_forme'] = lim
107         self.parametres['nbactives'] = len(self.actives)
108         self.parametres['fromprof'] = False
109         self.corpus.make_and_write_sparse_matrix_from_uces(self.actives, self.pathout['mat01.csv'], self.pathout['listeuce1.csv'])
110         with open(self.pathout['actives.csv'], 'w') as f :
111             f.write('\n'.join(self.actives).encode(self.ira.syscoding))
112
113 class SimiFromCluster(SimiTxt) :
114     def __init__(self, ira, corpus, actives, lfreq, lchi, numcluster, parametres = None, dlg = False) :
115         self.actives = actives
116         self.numcluster = numcluster
117         self.lfreq = lfreq
118         self.lchi = lchi
119         parametres['name'] = 'simi_classe_%i' % (numcluster + 1)
120         dlg.Destroy()
121         SimiTxt.__init__(self, ira, corpus, parametres, dlg=True, lemdial = False)
122     
123     def preferences(self) :
124         return self.parametres
125
126     def doanalyse(self) :
127         self.parametres['type'] = 'clustersimitxt'
128         self.pathout.basefiles(simipath)
129         self.indices = indices_simi
130         if self.dlg  :
131             self.makesimiparam()
132         if 'bystar' in self.parametres :
133             del self.parametres['bystar']
134         dictcol = dict([[i, [act, self.corpus.getlemclustereff(act, self.numcluster)]] for i, act in enumerate(self.actives)]) 
135         continu = True
136         #print self.dlg
137         if self.dlg :
138             self.dlg.Destroy()
139             self.stars = []#copy(self.listet)
140             self.parametres['stars'] = 0#copy(self.listet)
141             self.parametres['sfromchi'] = 1
142             prep = PrepSimi(self.ira, self, self.parametres, self.pathout['selected.csv'], self.actives, indices_simi, wordlist=dictcol)
143             if prep.val == wx.ID_OK :
144                 continu = True
145                 self.parametres = prep.parametres
146             else :
147                 continu = False
148         if continu :
149             self.dlg = progressbar(self.parent, 3)
150             self.makefiles()
151             self.parametres['type'] = 'clustersimitxt'
152             script = PrintSimiScript(self)
153             script.make_script()
154             if not self.doR(script.scriptout, dlg = self.dlg, message = 'R ...') :
155                 return False
156             if self.parametres['type_graph'] == 1:
157                 if self.parametres['svg'] :
158                     filename, ext = os.path.splitext(script.filename)
159                     fileout = filename + '.svg'                    
160                 else :
161                     fileout = script.filename
162                 if os.path.exists(self.pathout['liste_graph']):
163                     graph_simi = read_list_file(self.pathout['liste_graph'])
164                     graph_simi.append([os.path.basename(fileout), script.txtgraph])
165                 else :
166                     graph_simi = [[os.path.basename(fileout), script.txtgraph]]
167                 print_liste(self.pathout['liste_graph'], graph_simi)
168         else : 
169             return False
170
171     def makefiles(self) :
172         self.parametres['eff_min_forme'] = 3
173         self.parametres['nbactives'] = len(self.actives)
174         self.parametres['fromprof'] = True
175         self.corpus.make_and_write_sparse_matrix_from_classe(self.actives, self.corpus.lc[self.numcluster], self.pathout['mat01.csv'])
176         with open(self.pathout['actives.csv'], 'w') as f :
177             f.write('\n'.join(self.actives).encode(self.ira.syscoding))        
178         with open(self.pathout['actives_nb.csv'], 'w') as f :
179             f.write('\n'.join([`val` for val in self.lfreq]))
180         with open(self.pathout['actives_chi.csv'], 'w') as f :
181             f.write('\n'.join([`val` for val in self.lchi]))
182