keyframe
[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             selectcol = SelectColumn(self.ira, dictcol, self.actives, self.pathout['selected.csv'], dlg = True)
40             if selectcol.ok :
41                 self.dlg = progressbar(self.ira, 2)
42                 self.make_wordcloud()
43                 script = WordCloudRScript(self)
44                 script.make_script()
45                 self.doR(script.scriptout, dlg = self.dlg, message = 'R...')
46             else :
47                 return 'NOK'
48         else :
49             return 'NOK'
50     
51     def make_option(self, fromcluster = False) :
52         dial = PrefWordCloud(self.ira, fromcluster)
53         dial.CenterOnParent()
54         res = dial.ShowModal()
55         if res == wx.ID_OK :
56             if dial.format.GetSelection() == 0 :
57                 svg = 0
58             else :
59                 svg = 1
60             self.parametres['width'] = dial.spin_L.GetValue()
61             self.parametres['height'] = dial.spin_H.GetValue()
62             self.parametres['maxword'] = dial.spin_maxword.GetValue()
63             self.parametres['mincex'] = float(dial.spin_mincex.GetValue())/float(10)
64             self.parametres['maxcex'] = float(dial.spin_maxcex.GetValue())/float(10)
65             self.parametres['col_text'] = dial.color_text.GetColour()
66             self.parametres['col_bg'] = dial.color_bg.GetColour()
67             self.parametres['mode'] = dial.typeformeschoice.GetSelection()
68             self.parametres['svg'] = svg
69             if fromcluster :
70                 self.parametres['indice'] = dial.indice.GetSelection()
71             outgraph = os.path.join(os.path.dirname(self.pathout['zipf.png']), 'nuage_')
72             nb = 1
73             if svg :
74                 end = '.svg'
75             else :
76                 end = '.png'
77             while os.path.exists(outgraph + str(nb) + end) :
78                 nb += 1
79             self.parametres['graphout'] = outgraph + str(nb) + end
80         dial.Destroy()
81         return res
82
83     def make_wordcloud(self) :
84         act = ['\t'.join([act, `self.corpus.getlemeff(act)`]) for act in self.actives]
85         with open(self.pathout['actives_eff.csv'], 'w') as f :
86             f.write('\n'.join(act).encode(self.ira.syscoding))
87
88
89 class ClusterCloud(WordCloud) :
90     def doanalyse(self) :
91         self.parametres['type'] = 'clustercloud'
92         #FIXME
93         limit = 2
94         res = self.make_option(True)
95         if res == wx.ID_OK :
96             prof = self.parametres['clusterprof']
97             del self.parametres['clusterprof']
98             if self.parametres['indice'] == 0 :
99                 tokeep = 1
100             else : 
101                 tokeep = 2
102             prof = [[val[0], int(round(val[tokeep]))] for val in prof]
103             with open(self.pathout['actives_eff.csv'], 'w') as f :
104                 f.write('\n'.join(['\t'.join([val[0], `val[1]`]) for val in prof]).encode(self.ira.syscoding))
105             dictcol = dict([[i, val] for i, val in enumerate(prof)])
106             self.actives = [val[0] for val in prof]
107             SelectColumn(self.ira, dictcol, self.actives, self.pathout['selected.csv'], dlg = True)
108             script = WordCloudRScript(self)
109             script.make_script()
110             self.doR(script.scriptout, dlg = self.dlg, message = 'R...')
111         else :
112             return 'NOK'