...
[iramuteq] / textwordcloud.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008-2009 Pierre Ratinaud
5 #License: GNU/GPL
6
7 from analysetxt import AnalyseText
8 from guifunct import getPage, getCorpus, SelectColumn
9 #from ConfigParser import RawConfigParser
10 from functions import sortedby, progressbar 
11 from dialog import StatDialog, PrefWordCloud
12 from PrintRScript import WordCloudRScript
13 #from ttparser import * 
14 import tempfile
15 #from time import sleep
16 import wx
17 import os
18 import logging
19
20 logger = logging.getLogger('iramuteq.textwordcloud')
21
22
23 class WordCloud(AnalyseText):
24     def doanalyse(self) :
25         self.parametres['type'] = 'wordcloud'
26         #FIXME
27         limit = 3
28         self.dlg.Destroy()
29         res = self.make_option()
30         if res == wx.ID_OK :
31             if self.parametres['mode'] == 2 :
32                 self.actives = self.corpus.make_actives_limit(limit, 1)
33                 self.actives += self.corpus.make_actives_limit(limit, 2)
34             elif self.parametres['mode'] == 0 :
35                 self.actives = self.corpus.make_actives_limit(limit, 1)
36             elif self.parametres['mode'] == 1 :
37                 self.actives = self.corpus.make_actives_limit(limit, 2)
38             dictcol = dict([[i, [act, self.corpus.getlemeff(act)]] for i, act in enumerate(self.actives)]) 
39             SelectColumn(self.ira, dictcol, self.actives, self.pathout['selected.csv'], dlg = True)
40             self.dlg = progressbar(self.ira, 2)
41             self.make_wordcloud()
42             script = WordCloudRScript(self)
43             script.make_script()
44             self.doR(script.scriptout, dlg = self.dlg, message = 'R...')
45         else :
46             return 'NOK'
47     
48     def make_option(self, fromcluster = False) :
49         dial = PrefWordCloud(self.ira, fromcluster)
50         dial.CenterOnParent()
51         res = dial.ShowModal()
52         if res == wx.ID_OK :
53             if dial.format.GetSelection() == 0 :
54                 svg = 0
55             else :
56                 svg = 1
57             self.parametres['width'] = dial.spin_L.GetValue()
58             self.parametres['height'] = dial.spin_H.GetValue()
59             self.parametres['maxword'] = dial.spin_maxword.GetValue()
60             self.parametres['mincex'] = float(dial.spin_mincex.GetValue())/float(10)
61             self.parametres['maxcex'] = float(dial.spin_maxcex.GetValue())/float(10)
62             self.parametres['col_text'] = dial.color_text.GetColour()
63             self.parametres['col_bg'] = dial.color_bg.GetColour()
64             self.parametres['mode'] = dial.typeformeschoice.GetSelection()
65             self.parametres['svg'] = svg
66             if fromcluster :
67                 self.parametres['indice'] = dial.indice.GetSelection()
68             outgraph = os.path.join(os.path.dirname(self.pathout['zipf.png']), 'nuage_')
69             nb = 1
70             if svg :
71                 end = '.svg'
72             else :
73                 end = '.png'
74             while os.path.exists(outgraph + str(nb) + end) :
75                 nb += 1
76             self.parametres['graphout'] = outgraph + str(nb) + end
77         dial.Destroy()
78         return res
79
80     def make_wordcloud(self) :
81         act = ['\t'.join([act, `self.corpus.getlemeff(act)`]) for act in self.actives]
82         with open(self.pathout['actives_eff.csv'], 'w') as f :
83             f.write('\n'.join(act).encode(self.ira.syscoding))
84
85
86 class ClusterCloud(WordCloud) :
87     def doanalyse(self) :
88         self.parametres['type'] = 'clustercloud'
89         #FIXME
90         limit = 2
91         res = self.make_option(True)
92         if res == wx.ID_OK :
93             prof = self.parametres['clusterprof']
94             del self.parametres['clusterprof']
95             if self.parametres['indice'] == 0 :
96                 tokeep = 1
97             else : 
98                 tokeep = 2
99             prof = [[val[0], int(round(val[tokeep]))] for val in prof]
100             with open(self.pathout['actives_eff.csv'], 'w') as f :
101                 f.write('\n'.join(['\t'.join([val[0], `val[1]`]) for val in prof]).encode(self.ira.syscoding))
102             dictcol = dict([[i, val] for i, val in enumerate(prof)])
103             self.actives = [val[0] for val in prof]
104             SelectColumn(self.ira, dictcol, self.actives, self.pathout['selected.csv'], dlg = True)
105             script = WordCloudRScript(self)
106             script.make_script()
107             self.doR(script.scriptout, dlg = self.dlg, message = 'R...')
108         else :
109             return 'NOK'