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
7 #------------------------------------
8 # import des modules python
9 #------------------------------------
12 from time import sleep
15 #------------------------------------
16 # import des modules wx
17 #------------------------------------
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
30 log = logging.getLogger('iramuteq.spec')
33 class Lexico(AnalyseText) :
36 pathout = self.pathout.dirout
37 self.dictpathout = StatTxtPathOut(pathout)
38 self.parametres['ira'] = self.dictpathout['ira']
39 self.dlg = progressbar(self, 3)
53 """ % (ffr(self.parent.RscriptsPath['chdfunct']), ffr(self.parent.RscriptsPath['Rgraph']))
55 dmf<-read.csv2("%s",row.names=1)
56 """ % ffr(self.dictpathout['tableafcm'])
58 dmt<-read.csv2("%s",row.names=1)
59 """ % ffr(self.dictpathout['tabletypem'])
62 """ % self.parametres['indice']
63 if self.parametres['indice'] == 'hypergeo' :
65 outf <- make.spec.hypergeo(dmf)
66 outt <- make.spec.hypergeo(dmt)
68 elif self.parametres['indice'] == 'chi2' :
74 if (indice == 'hypergeo') {
76 } else if (indice == 'chi2') {
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'])
89 write.csv2(specf,file="%s")
90 """ % ffr(self.dictpathout['tablespecf'])
92 write.csv2(spect,file="%s")
93 """ % ffr(self.dictpathout['tablespect'])
95 write.csv2(outf[[3]],file="%s")
96 """ % ffr(self.dictpathout['eff_relatif_forme'])
98 write.csv2(outt[[3]],file="%s")
99 """ % ffr(self.dictpathout['eff_relatif_type'])
100 if self.parametres['clnb'] > 2 :
103 nd <- ncol(specf) - 1
107 dmfp<-dmf[-which(slf==0),]
108 specfp <- specf[-which(slf==0),]
113 afcf <- ca(dmfp, nd = nd)
116 dmtp<-dmt[-which(slt==0),]
117 spectp <- spect[-which(slt==0),]
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")
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']))
149 """ % ffr(self.dictpathout['RData'])
150 tmpfile = tempfile.mktemp(dir=self.parent.TEMPDIR)
151 tmpscript = open(tmpfile, 'w')
154 self.doR(tmpfile, dlg = self.dlg, message = 'R...')
156 def preferences(self) :
157 listet = self.corpus.make_etoiles()
159 variables = treat_var_mod(listet)
160 var = [v for v in variables]
161 dial = OptLexi(self.parent)
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()]
172 ListEt = variables[var[dial.list_box_1.GetSelections()[0]]]
175 self.parametres['mineff'] = dial.spin.GetValue()
176 if dial.choice_indice.GetSelection() == 0 :
177 self.parametres['indice'] = 'hypergeo'
179 self.parametres['indice'] = 'chi2'
180 self.parametres['typeformes'] = dial.typeformes.GetSelection()
181 self.parametres['clnb'] = len(ListEt)
183 return self.parametres
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'])
207 self.dlg.Update(2, 'R...')
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'])
220 class TgenSpec(AnalyseText):
222 def __init__(self, ira, corpus, parametres):
225 self.parametres = parametres
226 self.pathout = PathOut(dirout = self.parametres['pathout'])
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()
240 self.doR(self.Rscript.scriptout, dlg = False, message = 'R...')