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