1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2013 Pierre Ratinaud
6 from chemins import ffr, simipath
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
16 log = logging.getLogger('iramuteq.textsimi')
18 class SimiTxt(AnalyseText):
20 self.parametres['type'] = 'simitxt'
21 self.pathout.basefiles(simipath)
22 self.indices = indices_simi
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)])
30 self.listet = self.corpus.make_etoiles()
32 self.stars = copy(self.listet)
33 self.parametres['stars'] = copy(self.listet)
34 self.parametres['sfromchi'] = False
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 :
39 self.parametres = prep.parametres
40 self.dlg = progressbar(self.ira, 4)
47 script = PrintSimiScript(self)
49 if not self.doR(script.scriptout, dlg = self.dlg, message = 'R...') :
52 if self.parametres['type_graph'] == 1:
53 if self.parametres['svg'] :
54 filename, ext = os.path.splitext(script.filename)
55 fileout = filename + '.svg'
57 fileout = script.filename
58 if os.path.exists(self.pathout['liste_graph']):
59 graph_simi = read_list_file(self.pathout['liste_graph'])
60 graph_simi.append([os.path.basename(fileout), script.txtgraph])
62 graph_simi = [[os.path.basename(fileout), script.txtgraph]]
63 print_liste(self.pathout['liste_graph'], graph_simi)
67 def makesimiparam(self) :
68 self.paramsimi = {'coeff' : 0,
90 'cola' : (200,200,200),
102 #'ira' : self.pathout['Analyse.ira']
104 self.parametres.update(self.paramsimi)
106 def makefiles(self, lim=3) :
107 #self.actives, lim = self.corpus.make_actives_nb(self.parametres.get('max_actives',1500), 1)
108 self.parametres['eff_min_forme'] = lim
109 self.parametres['nbactives'] = len(self.actives)
110 self.parametres['fromprof'] = False
111 self.corpus.make_and_write_sparse_matrix_from_uces(self.actives, self.pathout['mat01.csv'], self.pathout['listeuce1.csv'])
112 with open(self.pathout['actives.csv'], 'w') as f :
113 f.write('\n'.join(self.actives).encode(self.ira.syscoding))
115 class SimiFromCluster(SimiTxt) :
116 def __init__(self, ira, corpus, actives, lfreq, lchi, numcluster, parametres = None, dlg = False) :
117 self.actives = actives
118 self.numcluster = numcluster
121 parametres['name'] = 'simi_classe_%i' % (numcluster + 1)
123 SimiTxt.__init__(self, ira, corpus, parametres, dlg=True, lemdial = False)
125 def preferences(self) :
126 return self.parametres
128 def doanalyse(self) :
129 self.parametres['type'] = 'clustersimitxt'
130 self.pathout.basefiles(simipath)
131 self.indices = indices_simi
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)])
141 self.stars = []#copy(self.listet)
142 self.parametres['stars'] = 0#copy(self.listet)
143 self.parametres['sfromchi'] = 1
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 :
147 self.parametres = prep.parametres
151 self.dlg = progressbar(self.parent, 3)
153 self.parametres['type'] = 'clustersimitxt'
154 script = PrintSimiScript(self)
156 if not self.doR(script.scriptout, dlg = self.dlg, message = 'R ...') :
158 if self.parametres['type_graph'] == 1:
159 if self.parametres['svg'] :
160 filename, ext = os.path.splitext(script.filename)
161 fileout = filename + '.svg'
163 fileout = script.filename
164 if os.path.exists(self.pathout['liste_graph']):
165 graph_simi = read_list_file(self.pathout['liste_graph'])
166 graph_simi.append([os.path.basename(fileout), script.txtgraph])
168 graph_simi = [[os.path.basename(fileout), script.txtgraph]]
169 print_liste(self.pathout['liste_graph'], graph_simi)
173 def makefiles(self) :
174 self.parametres['eff_min_forme'] = 3
175 self.parametres['nbactives'] = len(self.actives)
176 self.parametres['fromprof'] = True
177 self.corpus.make_and_write_sparse_matrix_from_classe(self.actives, self.corpus.lc[self.numcluster], self.pathout['mat01.csv'])
178 with open(self.pathout['actives.csv'], 'w') as f :
179 f.write('\n'.join(self.actives).encode(self.ira.syscoding))
180 with open(self.pathout['actives_nb.csv'], 'w') as f :
181 f.write('\n'.join([`val` for val in self.lfreq]))
182 with open(self.pathout['actives_chi.csv'], 'w') as f :
183 f.write('\n'.join([`val` for val in self.lchi]))