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