0.6 alpha 3
[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 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) :
47         dial = PrefWordCloud(self.ira)
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             outgraph = os.path.join(os.path.dirname(self.pathout['zipf.png']), 'nuage_')
65             nb = 1
66             if svg :
67                 end = '.svg'
68             else :
69                 end = '.png'
70             while os.path.exists(outgraph + str(nb) + end) :
71                 nb += 1
72             self.parametres['graphout'] = outgraph + str(nb) + end
73         dial.Destroy()
74         return res
75
76     def make_wordcloud(self) :
77         act = ['\t'.join([act, `self.corpus.getlemeff(act)`]) for act in self.actives]
78         with open(self.pathout['actives_eff.csv'], 'w') as f :
79             f.write('\n'.join(act).encode(self.ira.syscoding))