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