2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008-2009 Pierre Ratinaud
7 from chemins import ConstructPathOut, StatTxtPathOut, ffr, FFF
8 from corpus import Corpus
9 from guifunct import getPage, getCorpus
10 from ConfigParser import RawConfigParser
11 from functions import sortedby, progressbar, CreateIraFile, exec_rcode, check_Rresult, MessageImage
12 from dialog import StatDialog, PrefWordCloud
13 from openanalyse import OpenAnalyse
14 #from ttparser import *
16 from time import sleep
21 def __init__(self, parent, cmd = False, lem = True, exp = True):
22 ####################################################################
27 self.ConfigPath = parent.ConfigPath
28 self.DictPath = parent.DictPath
29 self.KeyConf = RawConfigParser()
30 self.KeyConf.read(self.ConfigPath['key'])
31 page = getPage(self.parent)
33 self.corpus = getCorpus(page)
34 if self.corpus is not None :
35 self.pathout = ConstructPathOut(self.corpus.parametre['openpath'], 'WordCloud')
36 self.dictpathout = StatTxtPathOut(self.pathout)
39 self.corpus = Corpus(parent)
40 self.corpus.parametre['encodage'] = parent.corpus_encodage
41 self.corpus.parametre['lang'] = parent.corpus_lang
42 self.corpus.parametre['filename'] = parent.filename
43 self.pathout = ConstructPathOut(self.corpus.parametre['filename'], 'WordCloud')
44 self.dictpathout = StatTxtPathOut(self.pathout)
45 self.corpus.dictpathout = self.dictpathout
47 dial = StatDialog(self,parent)
49 self.val = dial.ShowModal()
52 if self.val == wx.ID_OK :
54 if dial.radio_lem.GetSelection() == 0 : lem = True
56 if dial.exp.GetSelection() == 0 : exp = True
58 self.make_uce = dial.check_uce.GetValue()
59 self.corpus.parametre['nbforme_uce'] = dial.spin_ctrl_4.GetValue()
60 self.corpus.parametre['max_actives'] = dial.spin_max_actives.GetValue()
61 self.corpus.parametre['eff_min_uce'] = self.corpus.parametre['nbforme_uce']
66 self.corpus.parametre['nbforme_uce'] = None
67 self.corpus.parametre['eff_min_uce'] = None
68 self.corpus.parametre['lem'] = lem
69 self.corpus.parametre['expressions'] = exp
70 self.corpus.supplementaires = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "2"]
71 self.corpus.typeactive = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "1"]
74 if self.val == wx.ID_OK :
75 if 'supplementaires' not in dir(self.corpus) :
76 self.corpus.supplementaires = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "2"]
77 if 'typeactive' not in dir(self.corpus) :
78 self.corpus.typeactive = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "1"]
81 def make_corpus(self) :
83 self.dlg = progressbar(self, 7)
86 self.corpus.content = self.parent.content
87 ucis_txt, ucis_paras_txt = self.corpus.start_analyse(self.parent, dlg = self.dlg, cmd = self.cmd, fromtt = False)
91 self.dlg.Update(5, '%i UCI...' % len(ucis_paras_txt))
92 self.corpus.make_ucis_paras_uces(ucis_paras_txt, make_uce = self.make_uce)
95 if self.corpus.para_coords != [[] for val in self.corpus.para_coords] :
96 self.corpus.parametre['para'] = True
98 self.corpus.parametre['para'] = False
99 self.corpus.make_etoiles(self.corpus.para_coords)
102 self.dlg.Update(6, u'Dictionnaires')
103 uces, orderuces = self.corpus.make_forms_and_uces()
104 self.corpus.make_lems(self.parent.lexique)
106 def make_stats(self):
108 if not 'dlg' in dir(self) :
109 self.dlg = progressbar(self, 7)
110 if not self.corpus.parametre['lem'] :
111 formes = self.corpus.formes
113 formes = self.corpus.make_lem_eff()
114 act = [[forme, formes[forme][0], formes[forme][2]] for forme in formes if formes[forme][2] in
115 self.corpus.typeactive]
116 act = sortedby(act, 2, 1)
117 act = [[val[0], `val[1]`, val[2]] for val in act]
118 with open(os.path.join(self.pathout, 'actives.csv'), 'w') as f:
119 f.write('\n'.join([';'.join(line) for line in act]))
120 #act = [[i, val] for i, val in enumerate(act)]
121 self.corpus.dictpathout = self.dictpathout
122 #self.corpus.make_type_tot()
126 pref = PrefWordCloud(self.parent)
127 pref.CenterOnParent()
128 res = pref.ShowModal()
132 self.dlg = progressbar(self, 3)
133 width = pref.spin_L.GetValue()
134 height = pref.spin_H.GetValue()
135 maxword = pref.spin_maxword.GetValue()
136 mincex = float(pref.spin_mincex.GetValue())/float(10)
137 maxcex = float(pref.spin_maxcex.GetValue())/float(10)
138 col_text = pref.color_text.GetColour()
139 col_bg = pref.color_bg.GetColour()
140 col_text = str(col_text).replace(')', ', max=255)')
141 col_bg = str(col_bg).replace(')', ', max=255)')
142 outgraph = os.path.join(os.path.dirname(self.dictpathout['zipf']), 'nuage_')
144 while os.path.exists(outgraph + str(nb) + '.png') :
146 outgraph = outgraph + str(nb) + '.png'
151 act <- read.csv2("%s", header = FALSE, row.names = 1)
153 maxtoprint <- ifelse(nrow(act) > maxword, maxword, nrow(act))
154 toprint <- act[1:maxtoprint,]
155 open_file_graph("%s", width = %i, height = %i)
157 wordcloud(row.names(toprint), toprint[,1], scale=c(%f,%f), random.order=FALSE, colors=rgb%s)
159 """ % (self.parent.RscriptsPath['Rgraph'], ffr(os.path.join(self.pathout, 'actives.csv')), maxword, ffr(outgraph), width, height, col_bg, maxcex, mincex, col_text)
160 tmpscript = tempfile.mktemp(dir=self.parent.TEMPDIR)
161 with open(tmpscript, 'w') as f :
163 pid = exec_rcode(self.parent.RPath, tmpscript, wait = False)
164 while pid.poll() == None :
165 self.dlg.Pulse('R...')
167 check_Rresult(self.parent, pid)
168 self.corpus.save_corpus(self.dictpathout['db'])
169 CreateIraFile(self.dictpathout, 0, corpname = os.path.basename(self.corpus.parametre['filename']), section =
171 #win = MessageImage(self.parent, -1, u"Graphique", size=(700, 500),style = wx.DEFAULT_FRAME_STYLE)
172 #win.imagename = "nuage.png"
173 #win.addsaveimage(outgraph)
174 #txt = "<img src='%s'>" % FFF(outgraph).replace('//','/')
175 #win.HtmlPage.SetPage(txt)
178 #OpenAnalyse(self.parent, self.dictpathout['ira'])
179 #self.DoLayout(self.parent)
180 OpenAnalyse(self.parent, self.dictpathout['ira'])
181 self.dlg.Update(7, 'fini')