galician
[iramuteq] / tabverges.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2012 Pierre Ratinaud
5 #Lisense: GNU GPL
6
7 import os
8 import string
9 import wx
10 import os
11 import sys
12 import tempfile
13 from chemins import ffr,FFF, ConstructPathOut
14 import wx.lib.sized_controls as sc
15 from time import sleep
16 from functions import exec_rcode, check_Rresult, progressbar
17 from PrintRScript import ProtoScript
18 from analysematrix import AnalyseMatrix
19 from dialog import ProtoDial
20
21 class Prototypical(AnalyseMatrix) :
22     def __init__(self, parent, parametres):
23         self.parent = parent
24         self.tableau = self.parent.tableau
25         self.parametres = parametres
26         self.parametres['filename'] = parent.tableau.parametre['filename']
27         self.parametres['pathout'] = ConstructPathOut(parent.tableau.parametre['filename'], 'proto')
28         self.parametres['type'] = 'proto'
29         dlg = progressbar(self.parent, 2)
30         self.colnames = self.tableau.get_colnames()
31         AnalyseMatrix.__init__(self, parent, parent.tableau, self.parametres, dlg = dlg)
32     
33     def doanalyse(self) :
34         res = self.check_val()
35         return res
36
37     def check_val(self) :
38         self.dial = ProtoDial(self.ira, self.colnames)
39         self.dial.CenterOnParent()
40
41         self.val = self.dial.ShowModal()
42         if self.val==wx.ID_OK :
43                 self.ColSel1 = self.dial.variables.GetSelections()
44                 self.ColSel2 = self.dial.rangs.GetSelections()
45
46                 if len(self.ColSel1) != len(self.ColSel2) :
47                     print 'pas meme taille'
48                     self.check_val()
49                 else :
50                     if self.dial.choix_freq.GetSelection() == 0 :
51                         self.parametres['limfreq'] = 'NULL'
52                     else :
53                         self.parametres['limfreq'] = self.dial.freqlim.GetValue()
54                     if self.dial.choix_rang.GetSelection() == 0 :
55                         self.parametres['limrang'] = 'NULL'
56                     else :
57                         self.parametres['limrang'] = self.dial.ranglim.GetValue()
58                     self.parametres['freqmin'] = int(self.dial.m_textCtrl4.GetValue())
59                     table_assoc, table_rank = self.dotable()
60                     self.makedatas(table_assoc, table_rank)
61                     self.DoR()
62         else :
63              return 'stop'
64
65     def dotable(self) :
66         table_assoc = self.tableau.select_col(self.ColSel1)
67         table_rank = self.tableau.select_col(self.ColSel2)
68         return table_assoc, table_rank
69
70     def makedatas(self, table_assoc, table_rank) :
71         words = {}
72         for i in range(0, len(table_assoc)) :
73             for j, word in enumerate(table_assoc[i]) :
74                 if word in words :
75                     words[word][0] += 1
76                     if table_rank[i][j] != '' :
77                         words[word][1].append(int(table_rank[i][j]))
78                 else :
79                     if table_rank[i][j] != '' :
80                         words[word] = [1, [int(table_rank[i][j])]]
81                     else :
82                          words[word] = [1, []]
83         res = [[word, words[word][0], float(sum(words[word][1])) / len(words[word][1])] for word in words if len(words[word][1]) != 0 and words[word][0] >= self.parametres['freqmin']]
84         with open(self.pathout['table.csv'], 'w') as f :
85             f.write('\n'.join(['\t'.join(['"' + val[0] +'"', `val[1]`, `val[2]`]) for val in res]))
86         self.parent.tableau.parametres = self.parent.tableau.parametre
87         self.parent.tableau.save_tableau(self.pathout['analyse.db'])
88
89     def DoR(self) :
90         script = ProtoScript(self)
91         script.make_script()
92         self.doR(script.scriptout) 
93         
94