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