images
[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 self.parametres['svg'] :
56                     filename, ext = os.path.splitext(script.filename)
57                     fileout = filename + '.svg'                    
58                 else :
59                     fileout = script.filename
60                 if os.path.exists(self.pathout['liste_graph']):
61                     graph_simi = read_list_file(self.pathout['liste_graph'])
62                     graph_simi.append([os.path.basename(fileout), script.txtgraph])
63                 else :
64                     graph_simi = [[os.path.basename(fileout), script.txtgraph]]
65                 print_liste(self.pathout['liste_graph'], graph_simi)
66         else : 
67             return False
68
69     def makesimiparam(self) :
70         self.paramsimi = {'coeff' : 0,
71                           'layout' : 2,
72                           'type_graph' : 1,
73                           'arbremax' : 1,
74                           'coeff_tv' : 1,
75                           'coeff_tv_nb' : 0,
76                           'tvprop' : 0,
77                           'tvmin' : 5,
78                           'tvmax' : 30,
79                           'coeff_te' : 1,
80                           'coeff_temin' : 1,
81                           'coeff_temax' : 10,
82                           'label_v': 1,
83                           'label_e': 0,
84                           'vcex' : 1,
85                           'cexfromchi' : False,
86                           'vcexmin' : 10,
87                           'vcexmax' : 25,
88                           'cex' : 10,
89                           'seuil_ok' : 0,
90                           'seuil' : 1,
91                           'cols' : (255,0,0),
92                           'cola' : (200,200,200),
93                           'width' : 1000,
94                           'height' : 1000,
95                           'bystar' : False,
96                           'first' : True,
97                           'keep_coord' : True,
98                           'alpha' : 20,
99                           'film': False,
100                           'svg' : 0,
101                           'com' : 0,
102                           'communities' : 0,
103                           'halo' : 0,
104                           #'ira' : self.pathout['Analyse.ira']
105                           }
106         self.parametres.update(self.paramsimi)
107
108     def makefiles(self, lim=3) :
109         #self.actives, lim = self.corpus.make_actives_nb(self.parametres.get('max_actives',1500), 1)
110         self.parametres['eff_min_forme'] = lim
111         self.parametres['nbactives'] = len(self.actives)
112         self.parametres['fromprof'] = False
113         self.corpus.make_and_write_sparse_matrix_from_uces(self.actives, self.pathout['mat01.csv'], self.pathout['listeuce1.csv'])
114         with open(self.pathout['actives.csv'], 'w') as f :
115             f.write('\n'.join(self.actives).encode(self.ira.syscoding))
116
117 class SimiFromCluster(SimiTxt) :
118     def __init__(self, ira, corpus, actives, lfreq, lchi, numcluster, parametres = None, dlg = False) :
119         self.actives = actives
120         self.numcluster = numcluster
121         self.lfreq = lfreq
122         self.lchi = lchi
123         parametres['name'] = 'simi_classe_%i' % (numcluster + 1)
124         SimiTxt.__init__(self, ira, corpus, parametres, dlg, lemdial = False)
125     
126     def preferences(self) :
127         return self.parametres
128
129     def doanalyse(self) :
130         self.parametres['type'] = 'clustersimitxt'
131         self.pathout.basefiles(simipath)
132         self.indices = indices_simi
133         self.makesimiparam()
134         if 'bystar' in self.parametres :
135             del self.parametres['bystar']
136         dictcol = dict([[i, [act, self.corpus.getlemclustereff(act, self.numcluster)]] for i, act in enumerate(self.actives)]) 
137         continu = True
138         if self.dlg :
139             #self.listet = self.corpus.make_etoiles()
140             #self.listet.sort()
141             self.stars = []#copy(self.listet)
142             self.parametres['stars'] = False#copy(self.listet)
143             self.parametres['sfromchi'] = True
144             prep = PrepSimi(self.ira, self, self.parametres, self.pathout['selected.csv'], self.actives, indices_simi, wordlist=dictcol)
145             if prep.val == wx.ID_OK :
146                 continu = True
147                 self.parametres = prep.parametres
148             else :
149                 continu = False
150         if continu :
151             self.makefiles()
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