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)
45 script = PrintSimiScript(self)
47 if not self.doR(script.scriptout, dlg = self.dlg, message = 'R...') :
50 if self.parametres['type_graph'] == 1:
51 if self.parametres['svg'] :
52 filename, ext = os.path.splitext(script.filename)
53 fileout = filename + '.svg'
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])
60 graph_simi = [[os.path.basename(fileout), script.txtgraph]]
61 print_liste(self.pathout['liste_graph'], graph_simi)
65 def makesimiparam(self) :
66 self.paramsimi = {'coeff' : 0,
88 'cola' : (200,200,200),
100 #'ira' : self.pathout['Analyse.ira']
102 self.parametres.update(self.paramsimi)
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))
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
119 parametres['name'] = 'simi_classe_%i' % (numcluster + 1)
121 SimiTxt.__init__(self, ira, corpus, parametres, dlg=True, lemdial = False)
123 def preferences(self) :
124 return self.parametres
126 def doanalyse(self) :
127 self.parametres['type'] = 'clustersimitxt'
128 self.pathout.basefiles(simipath)
129 self.indices = indices_simi
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)])
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 :
145 self.parametres = prep.parametres
149 self.dlg = progressbar(self.parent, 3)
151 self.parametres['type'] = 'clustersimitxt'
152 script = PrintSimiScript(self)
154 if not self.doR(script.scriptout, dlg = self.dlg, message = 'R ...') :
156 if self.parametres['type_graph'] == 1:
157 if self.parametres['svg'] :
158 filename, ext = os.path.splitext(script.filename)
159 fileout = filename + '.svg'
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])
166 graph_simi = [[os.path.basename(fileout), script.txtgraph]]
167 print_liste(self.pathout['liste_graph'], graph_simi)
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]))