...
[iramuteq] / textaslexico.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2011 Pierre Ratinaud
4 #Lisense: GNU/GPL
5
6 from chemins import ConstructPathOut, StatTxtPathOut
7 #from corpus import Corpus
8 from analysetxt import AnalyseText
9 import wx
10 import os
11 import sys
12 from listlex import *
13 from functions import exec_rcode, progressbar, check_Rresult, CreateIraFile, print_liste, treat_var_mod, write_tab, DoConf
14 from dialog import OptLexi, StatDialog #LexDialog
15 from openanalyse import OpenAnalyse
16 import tempfile
17 from ConfigParser import RawConfigParser
18 from guifunct import getPage, getCorpus
19 from time import sleep
20 import logging
21
22 log = logging.getLogger('iramuteq.spec')
23
24 class Lexico(AnalyseText) :
25     def doanalyse(self) :
26         pathout = self.pathout.dirout
27         self.dictpathout = StatTxtPathOut(pathout)
28         self.parametres['ira'] = self.dictpathout['ira']
29         self.make_lexico()
30         if self.dlg :
31             try :
32                 self.dlg.Destroy()
33             except :
34                 pass
35 #    def __init__(self, parent, cmd = False):
36 #        self.parent = parent
37 #        self.cmd = False
38 #        self.ConfigPath = parent.ConfigPath
39 #        self.DictPath = parent.DictPath
40 #        self.KeyConf = RawConfigParser()
41 #        self.KeyConf.read(self.ConfigPath['key'])
42 #
43 #        page = getPage(self.parent)
44 #        if page is not None :
45 #            self.corpus = getCorpus(page)
46 #            if self.corpus is not None :
47 #                pathout = ConstructPathOut(self.corpus.parametre['openpath'], 'lexico')
48 #                self.dictpathout = StatTxtPathOut(pathout)
49 #                self.val = wx.ID_OK
50 #                #print self.corpus.lems
51 #                self.make_lexico()
52 #
53 #        else :
54 #            self.corpus = Corpus(parent)
55 #            self.corpus.content = self.parent.content
56 #            self.corpus.parametre['encodage'] = parent.corpus_encodage
57 #            self.corpus.parametre['lang'] = parent.corpus_lang
58 #            self.corpus.parametre['filename'] = parent.filename
59 #            dial = StatDialog(self, self.parent)
60 #            dial.check_uce.SetValue(True)
61 #            dial.check_uce.Enable(False)
62 #            dial.OnCheckUce(wx.EVT_MENU)
63 #            self.val = dial.ShowModal()           
64 ##            dial = LexDialog(self.parent)
65 ##            dial.CenterOnParent()
66 ##            res = dial.ShowModal()
67 #            if self.val == wx.ID_OK : 
68 #                #if dial.m_radioBox2.GetSelection() == 0 : self.corpus.parametre['lem'] = True
69 #                if dial.radio_lem.GetSelection() == 0 : self.corpus.parametre['lem'] = True
70 #                else : self.corpus.parametre['lem'] = False
71 #                #if dial.m_radioBox21.GetSelection() == 0 : self.corpus.parametre['expressions'] = True
72 #                if dial.exp.GetSelection() == 0 : self.corpus.parametre['expressions'] = True
73 #                else : self.corpus.parametre['expressions'] = False
74 #                self.make_uce = dial.check_uce.GetValue()
75 #                self.corpus.parametre['nbforme_uce'] = dial.spin_ctrl_4.GetValue()
76 #                self.corpus.parametre['max_actives'] = dial.spin_max_actives.GetValue()
77 #                self.corpus.parametre['eff_min_uce'] = self.corpus.parametre['nbforme_uce']
78 #                dial.Destroy()
79 #                pathout = ConstructPathOut(self.corpus.parametre['filename'], 'lexico')
80 #                self.dictpathout = StatTxtPathOut(pathout)
81 #                self.make_corpus()
82 #                #print self.corpus.ucis
83 #                self.make_lexico()
84 #
85 #    def make_corpus(self) :
86 #        print 'make corpus'
87 #        if not self.cmd :
88 #            dlg = progressbar(self, maxi = 6)
89 #        self.corpus.supplementaires = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "2"]
90 #        self.corpus.typeactive = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "1"]
91 #        ucis_txt, ucis_paras_txt = self.corpus.start_analyse(self.parent, dlg = dlg, cmd = self.cmd)
92 #        del ucis_txt
93 #    
94 #        if not self.cmd :
95 #            dlg.Update(5, '%i ucis - Construction des uces' % len(ucis_paras_txt))
96 #        self.corpus.make_ucis_paras_uces(ucis_paras_txt, make_uce = self.make_uce)
97 #        del ucis_paras_txt
98 #
99 #        if self.corpus.para_coords != [[] for val in self.corpus.para_coords] :
100 #            self.corpus.parametre['para'] = True
101 #        else :
102 #            self.corpus.parametre['para'] = False
103 #        self.corpus.make_etoiles(self.corpus.para_coords)
104 #
105 #        print 'len(ucis_paras_uces)', len(self.corpus.ucis_paras_uces)
106 #    
107 #        if not self.cmd :
108 #            dlg.Update(6, u'Dictionnaires')
109 #        uces, orderuces = self.corpus.make_forms_and_uces()
110 #        self.corpus.make_lems(self.parent.lexique)
111 ##        if not self.corpus.parametre['lem'] :
112 ##            formes = self.corpus.formes
113 ##        else :
114 ##            formes = self.corpus.make_lem_eff()
115 #        if not self.cmd :
116 #            dlg.Destroy()
117    
118     def DoR(self):
119         nbligne = 5
120         colonne = 1
121         txt = """
122         source("%s")
123         source("%s")
124         """ % (self.parent.RscriptsPath['chdfunct'], self.parent.RscriptsPath['Rgraph'])
125         txt += """
126         dmf<-read.csv2("%s",row.names=1)
127         """ % self.dictpathout['tableafcm']
128         txt += """
129         dmt<-read.csv2("%s",row.names=1)
130         """ % self.dictpathout['tabletypem']
131         if self.parametres['indice'] == 'hypergeo' :
132             txt += """
133             outf <- make.spec.hypergeo(dmf)
134             outt <- make.spec.hypergeo(dmt)
135             """
136         elif self.parametres['indice'] == 'chi2' :
137             txt += """
138             outf<-AsLexico2(dmf)
139             outt<-AsLexico2(dmt)
140             """
141
142         txt += """
143         specf<-outf[[1]]
144         spect<-outt[[1]]
145         write.csv2(specf,file="%s")
146         """ % self.dictpathout['tablespecf']
147         txt += """
148         write.csv2(spect,file="%s")
149         """ % self.dictpathout['tablespect']
150         txt += """
151         write.csv2(outf[[3]],file="%s")
152         """ % self.dictpathout['eff_relatif_forme']
153         txt += """
154         write.csv2(outt[[3]],file="%s")
155         """ % self.dictpathout['eff_relatif_type']
156         if self.parametres['clnb'] > 2 :
157             txt += """
158             library(ca)
159             nd <- ncol(specf) - 1
160             if (nd > 6) nd <- 6
161             slf <- rowSums(dmf)
162             if (min(slf) == 0) {
163                 dmfp<-dmf[-which(slf==0),]
164                 specfp <- specf[-which(slf==0),]
165             } else { 
166                 dmfp <- dmf
167                 specfp <- specf
168                 }
169             afcf <- ca(dmfp, nd = nd)
170             slt <- rowSums(dmt)
171             if (min(slt) == 0) {
172                 dmtp<-dmt[-which(slt==0),]
173                 spectp <- spect[-which(slt==0),]
174             } else { 
175                 dmtp <- dmt
176                 spectp <- spect
177                 }
178             afct <- ca(dmtp, nd = nd)
179             open_file_graph("%s", widt = 1000, height=1000)
180             plot(afcf, what=c('all','none'), labels=c(1,1))
181             open_file_graph("%s", widt = 1000, height=1000)
182             plot(afcf, what=c('none','all'), labels=c(1,1))
183             open_file_graph("%s", widt = 1000, height=1000)
184             plot(afct, what=c('all','none'), labels=c(1,1))
185             open_file_graph("%s", widt = 1000, height=1000)
186             plot(afct, what=c('none','all'), labels=c(1,1))
187             afcf <- AddCorrelationOk(afcf)
188             afct <- AddCorrelationOk(afct)
189             afcf <- summary.ca.dm(afcf)
190             afct <- summary.ca.dm(afct)
191             afcf_table <- create_afc_table(afcf)
192             afct_table <- create_afc_table(afct)
193             write.csv2(afcf_table$facteur, file = "%s")
194             write.csv2(afcf_table$colonne, file = "%s")
195             write.csv2(afcf_table$ligne, file = "%s")
196             write.csv2(afct_table$facteur, file = "%s")
197             write.csv2(afct_table$colonne, file = "%s")
198             write.csv2(afct_table$ligne, file = "%s")
199             debsup <- NULL
200             debet <- NULL
201             clnb <-  ncol(specf)
202             """ % (self.dictpathout['afcf_row'], self.dictpathout['afcf_col'], self.dictpathout['afct_row'], self.dictpathout['afct_col'], self.dictpathout['afcf_facteur_csv'], self.dictpathout['afcf_col_csv'], self.dictpathout['afcf_row_csv'], self.dictpathout['afct_facteur_csv'], self.dictpathout['afct_col_csv'], self.dictpathout['afct_row_csv'])
203
204         txt += """
205         save.image("%s")
206         """ % self.dictpathout['RData']
207         tmpfile = tempfile.mktemp(dir=self.parent.TEMPDIR)
208         tmpscript = open(tmpfile, 'w')
209         tmpscript.write(txt)
210         tmpscript.close()
211         self.doR(tmpfile, dlg = self.dlg, message = 'R...')
212         #pid = exec_rcode(self.parent.RPath, tmpfile, wait = False)
213         #while pid.poll() == None :
214         #    sleep(0.2)
215         #check_Rresult(self.parent, pid)
216
217     def preferences(self) :
218         listet = self.corpus.make_etoiles()
219         listet.sort()
220         variables = treat_var_mod(listet)
221         var = [v for v in variables]
222         dial = OptLexi(self.parent)
223         dial.listet = listet
224         dial.variables = var
225         for et in var :
226             dial.list_box_1.Append(et)
227         dial.CenterOnParent()
228         self.dialok = dial.ShowModal()
229         if self.dialok == wx.ID_OK :
230             if dial.choice.GetSelection() == 1 :
231                 ListEt = [listet[i] for i in dial.list_box_1.GetSelections()]
232             else :
233                 ListEt = variables[var[dial.list_box_1.GetSelections()[0]]]
234             self.listet = ListEt
235             self.parametres['mineff'] = dial.spin.GetValue()
236             if dial.choice_indice.GetSelection() == 0 :
237                 self.parametres['indice'] = 'hypergeo'
238             else :
239                 self.parametres['indice'] = 'chi2'
240             self.parametres['clnb'] = len(ListEt)
241             #if dial.checklem.GetValue() :
242             #    self.parametres['lem'] = 1 
243             #else :
244             #    self.parametres['lem'] = 0
245             dial.Destroy()
246             return self.parametres
247         else :
248             dial.Destroy()
249             return None
250
251     def make_lexico(self) :
252 #        listet = self.corpus.make_etoiles()
253 #        listet.sort()
254 #        variables = treat_var_mod(listet)
255 #        var = [v for v in variables]
256 #        if self.dlg :
257 #            dial = OptLexi(self.parent)
258 #            dial.listet = listet
259 #            dial.variables = var
260 #            for et in var :
261 #                dial.list_box_1.Append(et)
262 #            dial.CenterOnParent()
263 #            val = dial.ShowModal()
264 #            if val == wx.ID_OK :
265 #                if dial.choice.GetSelection() == 1 :
266 #                    ListEt = [listet[i] for i in dial.list_box_1.GetSelections()]
267 #                else :
268 #                    ListEt = variables[var[dial.list_box_1.GetSelections()[0]]]
269 #                mineff = dial.spin.GetValue()
270 #                if dial.choice_indice.GetSelection() == 0 :
271 #                    indice = 'hypergeo'
272 #                else :
273 #                    indice = 'chi2'
274 #                self.parametres = {'indice' : indice}
275 #                dial.Destroy()
276 #            else :
277 #                dial.Destroy()
278 #        else :
279         mineff = self.parametres['mineff']
280         #dlg = progressbar(self, maxi = 3)
281         tabout = self.corpus.make_lexitable(mineff, self.listet)
282         #log.warning('Fmax a 200')
283         #Fmax = [line for line in tabout[1:] if sum(line[1:]) > 199]
284         #formesmax = [line[0] for line in Fmax]
285         #Fmax = [line[1:] for line in Fmax]
286         #summax = [sum(col) for col in zip(*Fmax)]
287         #tabout.append(['Fmax'] + summax)
288         #tabout = [line for line in tabout if line[0] not in formesmax]
289         #log.warning('ATTENTION : hapax par etoile')
290         #tabout.append(['hapax'] + self.corpus.gethapaxbyet(self.listet))
291         write_tab(tabout, self.dictpathout['tableafcm'])
292         
293         #log.warning('ATTENTION : gethapaxuces')
294         #self.corpus.gethapaxuces()
295
296         tabout = self.corpus.make_efftype_from_etoiles(self.listet)
297         write_tab(tabout, self.dictpathout['tabletypem'])
298         if self.dlg :
299             self.dlg.Update(2, u'R...')
300         self.DoR()
301         if self.dlg :
302             self.dlg.Update(3, u'Chargement...')
303         afcf_graph_list = [[os.path.basename(self.dictpathout['afcf_row']), u'lignes'],\
304                             [os.path.basename(self.dictpathout['afcf_col']), u'colonnes']]
305         afct_graph_list = [[os.path.basename(self.dictpathout['afct_row']), u'lignes'],\
306                             [os.path.basename(self.dictpathout['afct_col']), u'colonnes']]
307         print_liste(self.dictpathout['liste_graph_afcf'],afcf_graph_list)
308         print_liste(self.dictpathout['liste_graph_afct'],afct_graph_list)
309         #CreateIraFile(self.dictpathout, 0, corpname = os.path.basename(self.corpus.parametre['filename']), section = 'lexico')
310         DoConf().makeoptions(['spec'],[self.parametres], self.dictpathout['ira'])
311             #OpenAnalyse(self.parent, self.dictpathout['ira']) 
312             #dolayout(self)
313             #dlg.Destroy()