Merge branch 'master' of 192.168.0.2:/home/pouns/iramuteqNG
[iramuteq] / textwordcloud.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008-2009 Pierre Ratinaud
5 #Lisense: GNU/GPL
6
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 * 
15 import tempfile
16 from time import sleep
17 import wx
18 import os
19
20 class WordCloud():
21     def __init__(self, parent, cmd = False, lem = True, exp = True):
22 ####################################################################        
23         self.conf = None
24         self.parent = parent
25         self.type = 'alceste'
26         self.cmd = cmd
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)
32         if page is not None :
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)
37                 self.val = wx.ID_OK
38         else :
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
46             if not self.cmd :
47                 dial = StatDialog(self,parent)
48                 dial.CenterOnParent()
49                 self.val = dial.ShowModal()
50             else :
51                 self.val = wx.ID_OK
52             if self.val == wx.ID_OK :
53                 if not self.cmd :
54                     if dial.radio_lem.GetSelection() == 0 : lem = True
55                     else : lem = False
56                     if dial.exp.GetSelection() == 0 : exp = True
57                     else : exp = False
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']
62                 else :
63                     lem = True
64                     exp = True
65                     self.make_uce = False
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"]
72                 self.make_corpus()
73
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"]
79             self.make_stats()
80
81     def make_corpus(self) :
82         if not self.cmd :
83             self.dlg = progressbar(self, 7)
84         else :
85             self.dlg = None
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)
88         del ucis_txt
89         
90         if not self.cmd :
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)
93         del ucis_paras_txt
94
95         if self.corpus.para_coords != [[] for val in self.corpus.para_coords] :
96             self.corpus.parametre['para'] = True
97         else :
98             self.corpus.parametre['para'] = False
99         self.corpus.make_etoiles(self.corpus.para_coords)
100
101         if not self.cmd :
102             self.dlg.Update(6, u'Dictionnaires')
103         uces, orderuces = self.corpus.make_forms_and_uces()
104         self.corpus.make_lems(self.parent.lexique)
105
106     def make_stats(self):
107         if not self.cmd :
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
112         else :
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()
123
124         if not self.cmd :
125             self.dlg.Destroy()
126             pref = PrefWordCloud(self.parent)
127             pref.CenterOnParent()
128             res = pref.ShowModal()
129         else :
130             res = wx.ID_OK
131         if res == wx.ID_OK :
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_')
143             nb = 1
144             while os.path.exists(outgraph + str(nb) + '.png') :
145                 nb += 1
146             outgraph = outgraph + str(nb) + '.png'
147
148             txt = """
149             source("%s")
150             library(wordcloud)
151             act <- read.csv2("%s", header = FALSE, row.names = 1)
152             maxword <- %i
153             maxtoprint <- ifelse(nrow(act) > maxword, maxword, nrow(act))
154             toprint <- act[1:maxtoprint,]
155             open_file_graph("%s", width = %i, height = %i)
156             par(bg=rgb%s)
157             wordcloud(row.names(toprint), toprint[,1], scale=c(%f,%f), random.order=FALSE, colors=rgb%s) 
158             dev.off()
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 :
162                 f.write(txt)
163             pid = exec_rcode(self.parent.RPath, tmpscript, wait = False)
164             while pid.poll() == None :
165                 self.dlg.Pulse('R...')
166                 sleep(0.2)
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 =
170                     'wordcloud')
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)
176             #win.Show(True)
177             if not self.cmd :
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')
182                 self.dlg.Destroy()