multisplit
[iramuteq] / textaslexico.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2020 Pierre Ratinaud
4 #modification pour python 3 : Laurent Mérat, 6x7 - mai 2020
5 #License: GNU/GPL
6
7 #------------------------------------
8 # import des modules python
9 #------------------------------------
10 import os
11 import tempfile
12 from time import sleep
13 import logging
14
15 #------------------------------------
16 # import des modules wx
17 #------------------------------------
18 import wx
19
20 #------------------------------------
21 # import des fichiers du projet
22 #------------------------------------
23 from chemins import ConstructPathOut, StatTxtPathOut, PathOut, ffr
24 from analysetxt import AnalyseText
25 from functions import exec_rcode, progressbar, check_Rresult, CreateIraFile, print_liste, treat_var_mod, write_tab, DoConf, TGen
26 from dialog import OptLexi #, StatDialog 
27 from PrintRScript import TgenSpecScript
28
29
30 log = logging.getLogger('iramuteq.spec')
31
32
33 class Lexico(AnalyseText) :
34
35     def doanalyse(self) :
36         pathout = self.pathout.dirout
37         self.dictpathout = StatTxtPathOut(pathout)
38         self.parametres['ira'] = self.dictpathout['ira']
39         self.dlg = progressbar(self, 3)
40         self.make_lexico()
41         if self.dlg :
42             try :
43                 self.dlg.Destroy()
44             except :
45                 pass
46
47     def DoR(self):
48         nbligne = 5
49         colonne = 1
50         txt = """
51         source("%s")
52         source("%s")
53         """ % (ffr(self.parent.RscriptsPath['chdfunct']), ffr(self.parent.RscriptsPath['Rgraph']))
54         txt += """
55         dmf<-read.csv2("%s",row.names=1)
56         """ % ffr(self.dictpathout['tableafcm'])
57         txt += """
58         dmt<-read.csv2("%s",row.names=1)
59         """ % ffr(self.dictpathout['tabletypem'])
60         txt += """
61         indice <- "%s"
62         """ % self.parametres['indice']
63         if self.parametres['indice'] == 'hypergeo' :
64             txt += """
65             outf <- make.spec.hypergeo(dmf)
66             outt <- make.spec.hypergeo(dmt)
67             """
68         elif self.parametres['indice'] == 'chi2' :
69             txt += """
70             outf<-AsLexico2(dmf)
71             outt<-AsLexico2(dmt)
72             """
73         txt += """
74         if (indice == 'hypergeo') {
75             banseuil <- 2
76         } else if (indice == 'chi2') {
77             banseuil <- 3
78         }
79         banal <- apply(abs(outf[[1]]), 1, max)
80         banal <- which(banal < banseuil)
81         banalfreq <- rowSums(dmf[banal,])
82         banalspec <- specf<-outf[[1]][banal,]
83         banal <- cbind(banalfreq, banalspec)
84         write.csv2(banal,file="%s")
85         """ % ffr(self.pathout['banalites.csv'])
86         txt += """
87         specf<-outf[[1]]
88         spect<-outt[[1]]
89         write.csv2(specf,file="%s")
90         """ % ffr(self.dictpathout['tablespecf'])
91         txt += """
92         write.csv2(spect,file="%s")
93         """ % ffr(self.dictpathout['tablespect'])
94         txt += """
95         write.csv2(outf[[3]],file="%s")
96         """ % ffr(self.dictpathout['eff_relatif_forme'])
97         txt += """
98         write.csv2(outt[[3]],file="%s")
99         """ % ffr(self.dictpathout['eff_relatif_type'])
100         if self.parametres['clnb'] > 2 :
101             txt += """
102             library(ca)
103             nd <- ncol(specf) - 1
104             if (nd > 6) nd <- 6
105             slf <- rowSums(dmf)
106             if (min(slf) == 0) {
107                 dmfp<-dmf[-which(slf==0),]
108                 specfp <- specf[-which(slf==0),]
109             } else { 
110                 dmfp <- dmf
111                 specfp <- specf
112                 }
113             afcf <- ca(dmfp, nd = nd)
114             slt <- rowSums(dmt)
115             if (min(slt) == 0) {
116                 dmtp<-dmt[-which(slt==0),]
117                 spectp <- spect[-which(slt==0),]
118             } else { 
119                 dmtp <- dmt
120                 spectp <- spect
121                 }
122             afct <- ca(dmtp, nd = nd)
123             open_file_graph("%s", widt = 1000, height=1000)
124             plot(afcf, what=c('all','none'), labels=c(1,1))
125             open_file_graph("%s", widt = 1000, height=1000)
126             plot(afcf, what=c('none','all'), labels=c(1,1))
127             open_file_graph("%s", widt = 1000, height=1000)
128             plot(afct, what=c('all','none'), labels=c(1,1))
129             open_file_graph("%s", widt = 1000, height=1000)
130             plot(afct, what=c('none','all'), labels=c(1,1))
131             afcf <- AddCorrelationOk(afcf)
132             afct <- AddCorrelationOk(afct)
133             afcf <- summary.ca.dm(afcf)
134             afct <- summary.ca.dm(afct)
135             afcf_table <- create_afc_table(afcf)
136             afct_table <- create_afc_table(afct)
137             write.csv2(afcf_table$facteur, file = "%s")
138             write.csv2(afcf_table$colonne, file = "%s")
139             write.csv2(afcf_table$ligne, file = "%s")
140             write.csv2(afct_table$facteur, file = "%s")
141             write.csv2(afct_table$colonne, file = "%s")
142             write.csv2(afct_table$ligne, file = "%s")
143             debsup <- NULL
144             debet <- NULL
145             clnb <-  ncol(specf)
146             """ % (ffr(self.dictpathout['afcf_row']), ffr(self.dictpathout['afcf_col']), ffr(self.dictpathout['afct_row']), ffr(self.dictpathout['afct_col']), ffr(self.dictpathout['afcf_facteur_csv']), ffr(self.dictpathout['afcf_col_csv']), ffr(self.dictpathout['afcf_row_csv']), ffr(self.dictpathout['afct_facteur_csv']), ffr(self.dictpathout['afct_col_csv']), ffr(self.dictpathout['afct_row_csv']))
147         txt += """
148         save.image("%s")
149         """ % ffr(self.dictpathout['RData'])
150         tmpfile = tempfile.mktemp(dir=self.parent.TEMPDIR)
151         tmpscript = open(tmpfile, 'w' ,encoding='utf8')
152         tmpscript.write(txt)
153         tmpscript.close()
154         self.doR(tmpfile, dlg = self.dlg, message = 'R...')
155
156     def preferences(self) :
157         listet = self.corpus.make_etoiles()
158         listet.sort()
159         variables = treat_var_mod(listet)
160         var = [v for v in variables]
161         dial = OptLexi(self.parent)
162         dial.listet = listet
163         dial.variables = var
164         for et in var :
165             dial.list_box_1.Append(et)
166         dial.CenterOnParent()
167         self.dialok = dial.ShowModal()
168         if self.dialok == wx.ID_OK :
169             if dial.choice.GetSelection() == 1 :
170                 ListEt = [listet[i] for i in dial.list_box_1.GetSelections()]
171             else :
172                 ListEt = variables[var[dial.list_box_1.GetSelections()[0]]]
173             self.listet = ListEt
174             self.listet.sort()
175             self.parametres['mineff'] = dial.spin.GetValue()
176             if dial.choice_indice.GetSelection() == 0 :
177                 self.parametres['indice'] = 'hypergeo'
178             else :
179                 self.parametres['indice'] = 'chi2'
180             self.parametres['typeformes'] = dial.typeformes.GetSelection()
181             self.parametres['clnb'] = len(ListEt)
182             dial.Destroy()
183             return self.parametres
184         else :
185             dial.Destroy()
186             return None
187
188     def make_lexico(self) :
189         mineff = self.parametres['mineff']
190         #dlg = progressbar(self, maxi = 3)
191         tabout = self.corpus.make_lexitable(mineff, self.listet, gram = self.parametres['typeformes'])
192         #log.warning('Fmax a 200')
193         #Fmax = [line for line in tabout[1:] if sum(line[1:]) > 199]
194         #formesmax = [line[0] for line in Fmax
195         #Fmax = [line[1:] for line in Fmax]
196         #summax = [sum(col) for col in zip(*Fmax)]
197         #tabout.append(['Fmax'] + summax)
198         #tabout = [line for line in tabout if line[0] not in formesmax]
199         #log.warning('ATTENTION : hapax par etoile')
200         #tabout.append(['hapax'] + self.corpus.gethapaxbyet(self.listet))
201         write_tab(tabout, self.dictpathout['tableafcm'])
202         #log.warning('ATTENTION : gethapaxuces')
203         #self.corpus.gethapaxuces()
204         tabout = self.corpus.make_efftype_from_etoiles(self.listet)
205         write_tab(tabout, self.dictpathout['tabletypem'])
206         if self.dlg :
207             self.dlg.Update(2, 'R...')
208         self.DoR()
209         if self.dlg :
210             self.dlg.Update(3, 'Chargement...')
211         afcf_graph_list = [[os.path.basename(self.dictpathout['afcf_row']), 'lignes'],\
212                             [os.path.basename(self.dictpathout['afcf_col']), 'colonnes']]
213         afct_graph_list = [[os.path.basename(self.dictpathout['afct_row']), 'lignes'],\
214                             [os.path.basename(self.dictpathout['afct_col']), 'colonnes']]
215         print_liste(self.dictpathout['liste_graph_afcf'],afcf_graph_list)
216         print_liste(self.dictpathout['liste_graph_afct'],afct_graph_list)
217         #DoConf().makeoptions(['spec'],[self.parametres], self.dictpathout['ira'])
218
219
220 class TgenSpec(AnalyseText):
221
222     def __init__(self, ira, corpus, parametres):
223         self.ira = ira
224         self.corpus = corpus
225         self.parametres = parametres
226         self.pathout = PathOut(dirout = self.parametres['pathout'])
227         self.doanalyse()
228
229     def doanalyse(self):
230         self.tgen = TGen(path = self.parametres['tgenpath'], encoding = self.ira.syscoding)
231         self.tgen.read(self.tgen.path)
232         self.parametres['etoiles'].sort()
233         tgenocc, totocc = self.corpus.make_tgen_table(self.tgen, self.parametres['etoiles'])
234         self.parametres['tgeneff'] = os.path.join(self.parametres['pathout'], 'tgeneff.csv')
235         self.tgen.writetable(self.parametres['tgeneff'], tgenocc, totocc)
236         self.parametres['tgenspec'] = os.path.join(self.parametres['pathout'], 'tgenspec.csv')
237         self.Rscript = TgenSpecScript(self)
238         self.Rscript.make_script()
239         self.Rscript.write()
240         self.doR(self.Rscript.scriptout, dlg = False, message = 'R...')