2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008-2009 Pierre Ratinaud
7 from analysetxt import AnalyseText
8 from guifunct import getPage, getCorpus, SelectColumn
9 from ConfigParser import RawConfigParser
10 from functions import sortedby, progressbar, CreateIraFile, exec_rcode, check_Rresult, MessageImage
11 from dialog import StatDialog, PrefWordCloud
12 from PrintRScript import WordCloudRScript
13 #from ttparser import *
15 from time import sleep
20 logger = logging.getLogger('iramuteq.textwordcloud')
23 class WordCloud(AnalyseText):
25 self.parametres['type'] = 'wordcloud'
29 if self.parametres['mode'] == 2 :
30 self.actives = self.corpus.make_actives_limit(limit, 1)
31 self.actives += self.corpus.make_actives_limit(limit, 2)
32 elif self.parametres['mode'] == 0 :
33 self.actives = self.corpus.make_actives_limit(limit, 1)
34 elif self.parametres['mode'] == 1 :
35 self.actives = self.corpus.make_actives_limit(limit, 2)
36 dictcol = dict([[i, [act, self.corpus.getlemeff(act)]] for i, act in enumerate(self.actives)])
37 SelectColumn(self.ira, dictcol, self.actives, self.pathout['selected.csv'], dlg = True)
39 script = WordCloudRScript(self)
41 self.doR(script.scriptout)
43 def make_option(self) :
44 dial = PrefWordCloud(self.ira)
46 res = dial.ShowModal()
48 self.parametres['width'] = dial.spin_L.GetValue()
49 self.parametres['height'] = dial.spin_H.GetValue()
50 self.parametres['maxword'] = dial.spin_maxword.GetValue()
51 self.parametres['mincex'] = float(dial.spin_mincex.GetValue())/float(10)
52 self.parametres['maxcex'] = float(dial.spin_maxcex.GetValue())/float(10)
53 self.parametres['col_text'] = dial.color_text.GetColour()
54 self.parametres['col_bg'] = dial.color_bg.GetColour()
55 self.parametres['mode'] = dial.typeformeschoice.GetSelection()
56 outgraph = os.path.join(os.path.dirname(self.pathout['zipf.png']), 'nuage_')
58 while os.path.exists(outgraph + str(nb) + '.png') :
60 self.parametres['graphout'] = outgraph + str(nb) + '.png'
64 def make_wordcloud(self) :
65 act = ['\t'.join([act, `self.corpus.getlemeff(act)`]) for act in self.actives]
66 with open(self.pathout['actives_eff.csv'], 'w') as f :
67 f.write('\n'.join(act).encode(self.ira.syscoding))
68 ####################################################################
70 # self.parent = parent
71 # self.type = 'alceste'
73 # self.ConfigPath = parent.ConfigPath
74 # self.DictPath = parent.DictPath
75 # self.KeyConf = RawConfigParser()
76 # self.KeyConf.read(self.ConfigPath['key'])
77 # page = getPage(self.parent)
78 # if page is not None :
79 # self.corpus = getCorpus(page)
80 # if self.corpus is not None :
81 # self.pathout = ConstructPathOut(self.corpus.parametre['openpath'], 'WordCloud')
82 # self.dictpathout = StatTxtPathOut(self.pathout)
85 # self.corpus = Corpus(parent)
86 # self.corpus.parametre['encodage'] = parent.corpus_encodage
87 # self.corpus.parametre['lang'] = parent.corpus_lang
88 # self.corpus.parametre['filename'] = parent.filename
89 # self.pathout = ConstructPathOut(self.corpus.parametre['filename'], 'WordCloud')
90 # self.dictpathout = StatTxtPathOut(self.pathout)
91 # self.corpus.dictpathout = self.dictpathout
93 # dial = StatDialog(self,parent)
94 # dial.CenterOnParent()
95 # self.val = dial.ShowModal()
98 # if self.val == wx.ID_OK :
100 # if dial.radio_lem.GetSelection() == 0 : lem = True
102 # if dial.exp.GetSelection() == 0 : exp = True
104 # self.make_uce = dial.check_uce.GetValue()
105 # self.corpus.parametre['nbforme_uce'] = dial.spin_ctrl_4.GetValue()
106 # self.corpus.parametre['max_actives'] = dial.spin_max_actives.GetValue()
107 # self.corpus.parametre['eff_min_uce'] = self.corpus.parametre['nbforme_uce']
111 # self.make_uce = False
112 # self.corpus.parametre['nbforme_uce'] = None
113 # self.corpus.parametre['eff_min_uce'] = None
114 # self.corpus.parametre['lem'] = lem
115 # self.corpus.parametre['expressions'] = exp
116 # self.corpus.supplementaires = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "2"]
117 # self.corpus.typeactive = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "1"]
120 # if self.val == wx.ID_OK :
121 # if 'supplementaires' not in dir(self.corpus) :
122 # self.corpus.supplementaires = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "2"]
123 # if 'typeactive' not in dir(self.corpus) :
124 # self.corpus.typeactive = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "1"]
127 # def make_corpus(self) :
129 # self.dlg = progressbar(self, 7)
132 # self.corpus.content = self.parent.content
133 # ucis_txt, ucis_paras_txt = self.corpus.start_analyse(self.parent, dlg = self.dlg, cmd = self.cmd, fromtt = False)
137 # self.dlg.Update(5, '%i UCI...' % len(ucis_paras_txt))
138 # self.corpus.make_ucis_paras_uces(ucis_paras_txt, make_uce = self.make_uce)
141 # if self.corpus.para_coords != [[] for val in self.corpus.para_coords] :
142 # self.corpus.parametre['para'] = True
144 # self.corpus.parametre['para'] = False
145 # self.corpus.make_etoiles(self.corpus.para_coords)
148 # self.dlg.Update(6, u'Dictionnaires')
149 # uces, orderuces = self.corpus.make_forms_and_uces()
150 # self.corpus.make_lems(self.parent.lexique)
152 # def make_stats(self):
154 # if not 'dlg' in dir(self) :
155 # self.dlg = progressbar(self, 7)
156 # if not self.corpus.parametre['lem'] :
157 # formes = self.corpus.formes
159 # formes = self.corpus.make_lem_eff()
160 # act = [[forme, formes[forme][0], formes[forme][2]] for forme in formes if formes[forme][2] in
161 # self.corpus.typeactive]
162 # act = sortedby(act, 2, 1)
163 # act = [[val[0], `val[1]`, val[2]] for val in act]
164 # with open(os.path.join(self.pathout, 'actives.csv'), 'w') as f:
165 # f.write('\n'.join([';'.join(line) for line in act]))
166 # #act = [[i, val] for i, val in enumerate(act)]
167 # self.corpus.dictpathout = self.dictpathout
168 # #self.corpus.make_type_tot()
172 # pref = PrefWordCloud(self.parent)
173 # pref.CenterOnParent()
174 # res = pref.ShowModal()
177 # if res == wx.ID_OK :
178 # self.dlg = progressbar(self, 3)
179 # width = pref.spin_L.GetValue()
180 # height = pref.spin_H.GetValue()
181 # maxword = pref.spin_maxword.GetValue()
182 # mincex = float(pref.spin_mincex.GetValue())/float(10)
183 # maxcex = float(pref.spin_maxcex.GetValue())/float(10)
184 # col_text = pref.color_text.GetColour()
185 # col_bg = pref.color_bg.GetColour()
186 # col_text = str(col_text).replace(')', ', max=255)')
187 # col_bg = str(col_bg).replace(')', ', max=255)')
188 # outgraph = os.path.join(os.path.dirname(self.dictpathout['zipf']), 'nuage_')
190 # while os.path.exists(outgraph + str(nb) + '.png') :
192 # outgraph = outgraph + str(nb) + '.png'
197 # act <- read.csv2("%s", header = FALSE, row.names = 1)
199 # maxtoprint <- ifelse(nrow(act) > maxword, maxword, nrow(act))
200 # toprint <- act[1:maxtoprint,]
201 # open_file_graph("%s", width = %i, height = %i)
203 # wordcloud(row.names(toprint), toprint[,1], scale=c(%f,%f), random.order=FALSE, colors=rgb%s)
205 # """ % (self.parent.RscriptsPath['Rgraph'], ffr(os.path.join(self.pathout, 'actives.csv')), maxword, ffr(outgraph), width, height, col_bg, maxcex, mincex, col_text)
206 # tmpscript = tempfile.mktemp(dir=self.parent.TEMPDIR)
207 # with open(tmpscript, 'w') as f :
209 # pid = exec_rcode(self.parent.RPath, tmpscript, wait = False)
210 # while pid.poll() == None :
211 # self.dlg.Pulse('R...')
213 # check_Rresult(self.parent, pid)
214 # self.corpus.save_corpus(self.dictpathout['db'])
215 # CreateIraFile(self.dictpathout, 0, corpname = os.path.basename(self.corpus.parametre['filename']), section =
217 # #win = MessageImage(self.parent, -1, u"Graphique", size=(700, 500),style = wx.DEFAULT_FRAME_STYLE)
218 # #win.imagename = "nuage.png"
219 # #win.addsaveimage(outgraph)
220 # #txt = "<img src='%s'>" % FFF(outgraph).replace('//','/')
221 # #win.HtmlPage.SetPage(txt)
224 # #OpenAnalyse(self.parent, self.dictpathout['ira'])
225 # #self.DoLayout(self.parent)
226 # OpenAnalyse(self.parent, self.dictpathout['ira'])
227 # self.dlg.Update(7, 'fini')