...
[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 #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 guifunct import PrepSimi
13 from functions import indices_simi, progressbar, treat_var_mod, read_list_file, print_liste
14 #from tableau import Tableau
15 #from tabsimi import DoSimi
16 from PrintRScript import PrintSimiScript
17 import wx
18 from copy import copy
19
20 import logging
21
22 log = logging.getLogger('iramuteq.textsimi')
23
24 class SimiTxt(AnalyseText): 
25     def doanalyse(self) :
26         self.parametres['type'] = 'simitxt'
27         self.pathout.basefiles(simipath)
28         self.indices = indices_simi
29         if self.dlg :
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             self.listet = self.corpus.make_etoiles()
37             self.listet.sort()
38             self.stars = copy(self.listet)
39             self.parametres['stars'] = copy(self.listet)
40             self.parametres['sfromchi'] = False
41             prep = PrepSimi(self.ira, self, self.parametres, self.pathout['selected.csv'], self.actives, indices_simi, wordlist=dictcol)
42             if prep.val == wx.ID_OK :
43                 continu = True
44                 self.parametres = prep.parametres
45         else :
46             continu = True
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' : False,
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         if self.dlg  :
134             self.makesimiparam()
135         if 'bystar' in self.parametres :
136             del self.parametres['bystar']
137         dictcol = dict([[i, [act, self.corpus.getlemclustereff(act, self.numcluster)]] for i, act in enumerate(self.actives)]) 
138         continu = True
139         if self.dlg :
140             #self.listet = self.corpus.make_etoiles()
141             #self.listet.sort()
142             self.stars = []#copy(self.listet)
143             self.parametres['stars'] = 0#copy(self.listet)
144             self.parametres['sfromchi'] = 1
145             prep = PrepSimi(self.ira, self, self.parametres, self.pathout['selected.csv'], self.actives, indices_simi, wordlist=dictcol)
146             if prep.val == wx.ID_OK :
147                 continu = True
148                 self.parametres = prep.parametres
149             else :
150                 continu = False
151         if continu :
152             self.makefiles()
153             self.parametres['type'] = 'clustersimitxt'
154             script = PrintSimiScript(self)
155             script.make_script()
156             if not self.doR(script.scriptout, dlg = self.dlg, message = 'R ...') :
157                 return False
158             if self.parametres['type_graph'] == 1:
159                 if self.parametres['svg'] :
160                     filename, ext = os.path.splitext(script.filename)
161                     fileout = filename + '.svg'                    
162                 else :
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])
167                 else :
168                     graph_simi = [[os.path.basename(fileout), script.txtgraph]]
169                 print_liste(self.pathout['liste_graph'], graph_simi)
170         else : 
171             return False
172
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]))
184