images
[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    
36     def DoR(self):
37         nbligne = 5
38         colonne = 1
39         txt = """
40         source("%s")
41         source("%s")
42         """ % (self.parent.RscriptsPath['chdfunct'], self.parent.RscriptsPath['Rgraph'])
43         txt += """
44         dmf<-read.csv2("%s",row.names=1)
45         """ % self.dictpathout['tableafcm']
46         txt += """
47         dmt<-read.csv2("%s",row.names=1)
48         """ % self.dictpathout['tabletypem']
49         if self.parametres['indice'] == 'hypergeo' :
50             txt += """
51             outf <- make.spec.hypergeo(dmf)
52             outt <- make.spec.hypergeo(dmt)
53             """
54         elif self.parametres['indice'] == 'chi2' :
55             txt += """
56             outf<-AsLexico2(dmf)
57             outt<-AsLexico2(dmt)
58             """
59
60         txt += """
61         specf<-outf[[1]]
62         spect<-outt[[1]]
63         write.csv2(specf,file="%s")
64         """ % self.dictpathout['tablespecf']
65         txt += """
66         write.csv2(spect,file="%s")
67         """ % self.dictpathout['tablespect']
68         txt += """
69         write.csv2(outf[[3]],file="%s")
70         """ % self.dictpathout['eff_relatif_forme']
71         txt += """
72         write.csv2(outt[[3]],file="%s")
73         """ % self.dictpathout['eff_relatif_type']
74         if self.parametres['clnb'] > 2 :
75             txt += """
76             library(ca)
77             nd <- ncol(specf) - 1
78             if (nd > 6) nd <- 6
79             slf <- rowSums(dmf)
80             if (min(slf) == 0) {
81                 dmfp<-dmf[-which(slf==0),]
82                 specfp <- specf[-which(slf==0),]
83             } else { 
84                 dmfp <- dmf
85                 specfp <- specf
86                 }
87             afcf <- ca(dmfp, nd = nd)
88             slt <- rowSums(dmt)
89             if (min(slt) == 0) {
90                 dmtp<-dmt[-which(slt==0),]
91                 spectp <- spect[-which(slt==0),]
92             } else { 
93                 dmtp <- dmt
94                 spectp <- spect
95                 }
96             afct <- ca(dmtp, nd = nd)
97             open_file_graph("%s", widt = 1000, height=1000)
98             plot(afcf, what=c('all','none'), labels=c(1,1))
99             open_file_graph("%s", widt = 1000, height=1000)
100             plot(afcf, what=c('none','all'), labels=c(1,1))
101             open_file_graph("%s", widt = 1000, height=1000)
102             plot(afct, what=c('all','none'), labels=c(1,1))
103             open_file_graph("%s", widt = 1000, height=1000)
104             plot(afct, what=c('none','all'), labels=c(1,1))
105             afcf <- AddCorrelationOk(afcf)
106             afct <- AddCorrelationOk(afct)
107             afcf <- summary.ca.dm(afcf)
108             afct <- summary.ca.dm(afct)
109             afcf_table <- create_afc_table(afcf)
110             afct_table <- create_afc_table(afct)
111             write.csv2(afcf_table$facteur, file = "%s")
112             write.csv2(afcf_table$colonne, file = "%s")
113             write.csv2(afcf_table$ligne, file = "%s")
114             write.csv2(afct_table$facteur, file = "%s")
115             write.csv2(afct_table$colonne, file = "%s")
116             write.csv2(afct_table$ligne, file = "%s")
117             debsup <- NULL
118             debet <- NULL
119             clnb <-  ncol(specf)
120             """ % (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'])
121
122         txt += """
123         save.image("%s")
124         """ % self.dictpathout['RData']
125         tmpfile = tempfile.mktemp(dir=self.parent.TEMPDIR)
126         tmpscript = open(tmpfile, 'w')
127         tmpscript.write(txt)
128         tmpscript.close()
129         self.doR(tmpfile, dlg = self.dlg, message = 'R...')
130
131     def preferences(self) :
132         listet = self.corpus.make_etoiles()
133         listet.sort()
134         variables = treat_var_mod(listet)
135         var = [v for v in variables]
136         dial = OptLexi(self.parent)
137         dial.listet = listet
138         dial.variables = var
139         for et in var :
140             dial.list_box_1.Append(et)
141         dial.CenterOnParent()
142         self.dialok = dial.ShowModal()
143         if self.dialok == wx.ID_OK :
144             if dial.choice.GetSelection() == 1 :
145                 ListEt = [listet[i] for i in dial.list_box_1.GetSelections()]
146             else :
147                 ListEt = variables[var[dial.list_box_1.GetSelections()[0]]]
148             self.listet = ListEt
149             self.parametres['mineff'] = dial.spin.GetValue()
150             if dial.choice_indice.GetSelection() == 0 :
151                 self.parametres['indice'] = 'hypergeo'
152             else :
153                 self.parametres['indice'] = 'chi2'
154             self.parametres['typeformes'] = dial.typeformes.GetSelection()
155             self.parametres['clnb'] = len(ListEt)
156             dial.Destroy()
157             return self.parametres
158         else :
159             dial.Destroy()
160             return None
161
162     def make_lexico(self) :
163         mineff = self.parametres['mineff']
164         #dlg = progressbar(self, maxi = 3)
165         tabout = self.corpus.make_lexitable(mineff, self.listet, gram = self.parametres['typeformes'])
166         #log.warning('Fmax a 200')
167         #Fmax = [line for line in tabout[1:] if sum(line[1:]) > 199]
168         #formesmax = [line[0] for line in Fmax
169         #Fmax = [line[1:] for line in Fmax]
170         #summax = [sum(col) for col in zip(*Fmax)]
171         #tabout.append(['Fmax'] + summax)
172         #tabout = [line for line in tabout if line[0] not in formesmax]
173         #log.warning('ATTENTION : hapax par etoile')
174         #tabout.append(['hapax'] + self.corpus.gethapaxbyet(self.listet))
175         write_tab(tabout, self.dictpathout['tableafcm'])
176         
177         #log.warning('ATTENTION : gethapaxuces')
178         #self.corpus.gethapaxuces()
179
180         tabout = self.corpus.make_efftype_from_etoiles(self.listet)
181         write_tab(tabout, self.dictpathout['tabletypem'])
182         if self.dlg :
183             self.dlg.Update(2, u'R...')
184         self.DoR()
185         if self.dlg :
186             self.dlg.Update(3, u'Chargement...')
187         afcf_graph_list = [[os.path.basename(self.dictpathout['afcf_row']), u'lignes'],\
188                             [os.path.basename(self.dictpathout['afcf_col']), u'colonnes']]
189         afct_graph_list = [[os.path.basename(self.dictpathout['afct_row']), u'lignes'],\
190                             [os.path.basename(self.dictpathout['afct_col']), u'colonnes']]
191         print_liste(self.dictpathout['liste_graph_afcf'],afcf_graph_list)
192         print_liste(self.dictpathout['liste_graph_afct'],afct_graph_list)
193         #DoConf().makeoptions(['spec'],[self.parametres], self.dictpathout['ira'])