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