3787fe6a7c5290de11da4990c723e836aaec6ce6
[iramuteq] / openanalyse.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008-2012, Pierre Ratinaud
5 #Lisense: GNU/GPL
6
7 from chemins import ChdTxtPathOut, StatTxtPathOut, construct_simipath
8 from layout import OpenCHDS, dolexlayout, StatLayout, WordCloudLayout, OpenCorpus, SimiLayout
9 #from corpus import Corpus
10 from corpusNG import Corpus, copycorpus
11 from tableau import Tableau
12 import os
13 import shelve
14 from ConfigParser import *
15 from tabsimi import DoSimi
16 from functions import BugReport, DoConf
17 import logging
18
19 log = logging.getLogger('iramuteq.openanalyse')
20
21 class OpenAnalyse():
22     def __init__(self, parent, parametres, Alceste=True, simifromprof = False):
23         #self.conf = RawConfigParser()
24         #self.conf.read(filename)
25         log.info('OpenAnalyse')
26         self.parent = parent
27         if isinstance(parametres, dict) :
28             self.conf = DoConf(parametres['ira']).getoptions()
29             self.path = parametres['ira']
30         else :
31             self.conf = DoConf(parametres).getoptions()
32             self.path = parametres
33             self.conf = self.redopath(self.conf, parametres)
34         
35         if self.conf['type'] == 'corpus' :
36             corpus = self.opencorpus()
37         elif self.conf['corpus'] in self.parent.history.history :
38             corpus = self.openanalyse()
39             if self.conf.get('lem',1) :
40                corpus.make_lems(True)
41             else :
42                corpus.make_lems(False)
43             self.doopen(corpus)
44         else :
45             corpus = None
46         self.parent.history.addtab(self.conf)
47     
48     def redopath(self, conf, path) :
49         #if not os.path.exists(conf['ira']) :
50         conf['ira'] = os.path.realpath(path)
51         conf['pathout'] = os.path.dirname(os.path.realpath(path))
52         DoConf(conf['ira']).makeoptions([conf['type']], [conf])
53         return conf
54     
55     def opencorpus(self) :
56         if self.conf['uuid'] not in self.parent.history.history :
57             self.parent.history.add(self.conf) 
58             log.info('add to history')
59             self.parent.tree.OnItemAppend(self.conf)
60         if self.conf['uuid'] in self.parent.history.openedcorpus :
61             log.info('corpus is already opened')
62         else :
63             corpus = Corpus(self, parametres = self.conf, read = self.parent.history.history[self.conf['uuid']]['ira'])
64             self.parent.history.openedcorpus[self.conf['uuid']] = corpus
65             self.opencorpus_analyses()
66             self.doopen(corpus)
67
68     def opencorpus_analyses(self) :
69         basepath = self.conf['pathout']
70         for root, subfolders, files in os.walk(basepath) :
71             for folder in subfolders :
72                 if os.path.exists(os.path.join(folder, 'Analyse.ira')) :
73                     analyse_conf = DoConf(os.path.join(folder, 'Analyse.ira')).getoptions()
74                     analyse_conf = self.redopath(analyse_conf, os.path.join(folder, 'Analyse.ira'))
75                     if analyse_conf['corpus'] == self.conf['uuid'] :
76                         self.parent.history.add(analyse_conf)
77                         self.parent.tree.AddAnalyse(analyse_conf, bold = False)
78
79             #self.parent.tree.OnItemAppend(self.conf)
80     def openanalyse(self) :
81         if self.conf['corpus'] in self.parent.history.openedcorpus :
82             log.info('corpus is already opened')
83             corpus = self.parent.history.openedcorpus[self.conf['corpus']]
84         else :
85             if os.path.exists(self.parent.history.history[self.conf['corpus']]['ira']) :
86                 corpus = Corpus(self, parametres = DoConf(self.parent.history.history[self.conf['corpus']]['ira']).getoptions('corpus'), read = self.parent.history.history[self.conf['corpus']]['ira'])
87                 self.parent.history.openedcorpus[self.conf['corpus']] = corpus
88         return corpus
89
90     def doopen(self, corpus) :
91         print self.conf
92         if self.conf['type'] == 'corpus' :
93             self.parent.ShowMenu(_("Text analysis"))
94             OpenCorpus(self.parent, self.conf) 
95         elif self.conf['type'] == 'stat' :
96             self.parent.ShowMenu(_("Text analysis"))
97             StatLayout(self.parent, corpus, self.conf)
98         elif self.conf['type'] == 'spec' :
99             self.parent.ShowMenu(_("Text analysis"))
100             dolexlayout(self.parent, corpus, self.conf)
101         elif self.conf['type'] == 'alceste' :
102             self.parent.ShowMenu(_("Text analysis"))
103             OpenCHDS(self.parent,  corpus, self.conf, Alceste = True)
104         elif self.conf['type'] == 'simitxt' :
105             self.parent.ShowMenu(_("Text analysis"))
106             SimiLayout(self.parent, corpus, self.conf)
107         elif self.conf['type'] == 'wordcloud' :
108             self.parent.ShowMenu(_("Text analysis"))
109             WordCloudLayout(self.parent, corpus, self.conf)
110             #self.tableau = Tableau(self.parent, self.conf['ira'])
111             #self.DictPathOut=construct_simipath(self.conf['pathout'])
112             #self.tableau.dictpathout = self.DictPathOut
113             #self.tableau.read_tableau(self.tableau.dictpathout['db'])
114             #if self.tableau.parametre.get('corpus', False) :
115             #    self.corpus=corpus
116                 #self.corpus.read_corpus_from_shelves(self.DictPathOut['corpus'])
117             #    self.corpus.parametres['openpath'] = self.conf['pathout']
118             #DoSimi(self.parent, self.conf, isopen = True, filename = self.conf['ira'], gparent = self, openfromprof=False) 
119             
120 #        try :
121 #            #if self.conf['type'] in ['analyse','lexico','stat','wordcloud'] :
122 #            #    self.corpus = Corpus(parent)
123 #            if 'analyse' in self.conf.sections() :
124 #                DictPathOut=ChdTxtPathOut(os.path.dirname(filename))
125 #                self.pathout = os.path.dirname(filename)
126 #                self.DictPathOut=DictPathOut
127 #                #self.corpus = Corpus(parent)
128 #                self.corpus.dictpathout = self.DictPathOut
129 #                self.corpus.read_corpus_from_shelves(self.DictPathOut['db'])
130 #                self.corpus.parametre['analyse'] = 'alceste'
131 #                self.corpus.make_lem_type_list()
132 #    #            print 'EXTRACT NR'
133 #    #            self.corpus.extractnr()
134 #                Alceste=True
135 #                #self.corpus.save_corpus(self.corpus.dictpathout['db'])
136 #                #self.corpus.make_uci_stat()
137 #                #self.corpus.make_et_table()
138 #                #self.corpus.prof_type()
139 #                #self.corpus.make_type_tot()
140 #                #self.corpus.make_size_uci()
141 #                #self.corpus.get_stat_by_cluster()
142 #                OpenCHDS(parent, self, filename, Alceste)
143 #                self.parent.ShowMenu(_("Text analysis"))
144 #                self.parent._mgr.Update()
145 #            elif 'questionnaire' in self.conf.sections() :
146 #                self.DictPathOut=ChdTxtPathOut(os.path.dirname(filename))
147 #                self.pathout = os.path.dirname(filename)
148 #                self.tableau = Tableau(parent, filename)
149 #                self.tableau.dictpathout = self.DictPathOut
150 #                self.tableau.read_tableau(self.tableau.dictpathout['db'])
151 #                OpenCHDS(parent, self, filename, False)
152 #            elif 'simi' in self.conf.sections():
153 #                self.tableau = Tableau(parent, filename)
154 #                self.DictPathOut=construct_simipath(os.path.abspath(os.path.dirname(filename)))
155 #                self.tableau.dictpathout = self.DictPathOut
156 #                self.tableau.read_tableau(self.tableau.dictpathout['db'])
157 #                if self.tableau.parametre.get('fromtxt', False) :
158 #                    self.corpus=Corpus(parent)
159 #                    self.corpus.read_corpus_from_shelves(self.DictPathOut['corpus'])
160 #                    self.corpus.parametre['openpath'] = os.path.dirname(filename)
161 #                    self.parent.ShowMenu(_("Text analysis"))
162 #                DoSimi(parent, self.conf, isopen = True, filename = filename, gparent = self, openfromprof=simifromprof) 
163 #            elif 'lexico' in self.conf.sections():
164 #                print 'lexico'
165 #                #self.corpus = Corpus(parent)
166 #                self.corpus.dictpathout = StatTxtPathOut(os.path.dirname(filename))
167 #                self.parent.ShowMenu(_("Text analysis"))
168 #                dolexlayout(parent, self, filename)
169 #            elif 'stat' in self.conf.sections():
170 #                print 'stat'
171 #                #self.corpus = Corpus(parent)
172 #                self.corpus.dictpathout = StatTxtPathOut(os.path.dirname(filename))
173 #                self.parent.ShowMenu(_("Text analysis"))
174 #                StatLayout(parent, self, filename)
175 #            elif 'chd_dist_quest' in self.conf.sections():
176 #                self.DictPathOut = ChdTxtPathOut(os.path.dirname(filename))
177 #                self.pathout = os.path.dirname(filename)
178 #                self.tableau = Tableau(parent, filename)
179 #                self.tableau.dictpathout = self.DictPathOut
180 #                self.tableau.read_tableau(self.tableau.dictpathout['db'])
181 #                OpenCHDS(parent, self, filename, False)
182 #            elif 'wordcloud' in self.conf.sections() :
183 #                self.corpus.dictpathout = StatTxtPathOut(os.path.dirname(filename))
184 #                self.corpus.read_corpus_from_shelves(self.corpus.dictpathout['db'])
185 #                self.parent.ShowMenu(_("Text analysis"))
186 #                WordCloudLayout(parent, self, filename)
187 #            if self.conf.sections()[0] in ['analyse','lexico','stat','wordcloud'] :
188 #                self.corpus.parametre['openpath'] = os.path.dirname(filename)
189 #        except :
190 #            BugReport(self.parent)
191
192