...
[iramuteq] / openanalyse.py
index 1cf1a03..c8089d2 100644 (file)
 # -*- coding: utf-8 -*-
 #Author: Pierre Ratinaud
 #Copyright (c) 2008-2012, Pierre Ratinaud
-#Lisense: GNU/GPL
+#License: GNU/GPL
 
-from chemins import ChdTxtPathOut, StatTxtPathOut, construct_simipath
-from layout import OpenCHDS, dolexlayout, StatLayout, WordCloudLayout, OpenCorpus
-#from corpus import Corpus
-from corpusNG import Corpus, copycorpus
+from chemins import ChdTxtPathOut, StatTxtPathOut, PathOut
+from layout import OpenCHDS, dolexlayout, StatLayout, WordCloudLayout, OpenCorpus, SimiLayout, SimiMatLayout, ProtoLayout, MatLayout, FreqLayout, Chi2Layout
+from corpus import Corpus, copycorpus
 from tableau import Tableau
 import os
-import shelve
-from ConfigParser import *
-from tabsimi import DoSimi
-from functions import BugReport, DoConf
+#import shelve
+#from tabsimi import DoSimi
+from functions import DoConf, ReadDicoAsDico
+from tableau import Tableau
 import logging
 
 log = logging.getLogger('iramuteq.openanalyse')
 
 class OpenAnalyse():
     def __init__(self, parent, parametres, Alceste=True, simifromprof = False):
-        #self.conf = RawConfigParser()
-        #self.conf.read(filename)
         log.info('OpenAnalyse')
         self.parent = parent
         if isinstance(parametres, dict) :
             self.conf = DoConf(parametres['ira']).getoptions()
+            self.path = parametres['ira']
         else :
             self.conf = DoConf(parametres).getoptions()
+            self.path = parametres
+            self.conf = self.redopath(self.conf, parametres)
         
-        if self.conf.get('corpus', '!!') in self.parent.history.history :
-            if self.conf.get('corpus', '!!') in self.parent.history.openedcorpus :
-                log.info('corpus is already opened')
-                corpus = copycorpus(self.parent.history.openedcorpus[self.conf['corpus']]) 
+        if self.conf['type'] == 'corpus' :
+            corpus = self.opencorpus()
+        elif self.conf['type'] == 'matrix' :
+            matrix = self.openmatrix()
+        elif self.conf.get('corpus', False) in self.parent.history.corpus :
+            if self.conf['uuid'] in self.parent.history.analyses :
+                intree  = True
             else :
-                if os.path.exists(self.parent.history.history[self.conf['corpus']]['ira']) :
-                    corpus = Corpus(self, parametres = DoConf(self.parent.history.history[self.conf['corpus']]['ira']).getoptions('corpus'), read = self.parent.history.history[self.conf['corpus']]['ira'])
-                self.parent.history.openedcorpus[self.conf['corpus']] = corpus
+                intree = False
+            corpus = self.openanalyse()
+            
             if self.conf.get('lem',1) :
-               corpus.make_lems(True)
+                dolem = True
             else :
-               corpus.make_lems(False)
-        else :
+                dolem = False
+            if self.conf.get('dictionary', False) :
+                dico = ReadDicoAsDico(self.conf['dictionary'])
+                corpus.make_lems_from_dict(dico, dolem = dolem)
+            else :
+                corpus.make_lems(lem = dolem)
+            if not intree :
+                self.parent.tree.AddAnalyse(self.conf, bold = True)
+            else :
+                self.parent.tree.GiveFocus(uuid = self.conf['uuid'], bold = True)
+            self.doopen(corpus)
+        elif self.conf.get('matrix', False) in self.parent.history.ordermatrix :
             corpus = None
-        self.doopen(corpus)
+            matrix = Tableau(self.parent, parametres = self.parent.history.matrix[self.parent.history.ordermatrix[self.conf['matrix']]])
+            matrix.open()
+            #if isinstance(parametres, dict) :
+            #    tableau = Tableau(parent, parametres['ira'])
+            #else :
+            #    tableau = Tableau(parent, parametres)
+            #tableau.parametres = self.conf 
+            #tableau.dictpathout = PathOut(filename = tableau.parametres['filename'], dirout = self.conf['pathout'], analyse_type = self.conf['type'])
+            #tableau.dictpathout.basefiles(ChdTxtPathOut)
+            #tableau.read_tableau(tableau.dictpathout['db'])
+            #if self.parent.tree.IsInTree(uuid = self.conf['uuid']) :
+            self.parent.tree.GiveFocus(uuid = self.conf['uuid'], bold = True)
+            self.doopen(matrix)
+        else :
+            self.parent.tree.AddAnalyse(self.conf, bold = True)
         self.parent.history.addtab(self.conf)
-        
+    
+    def redopath(self, conf, path) :
+        conf['ira'] = os.path.realpath(path)
+        conf['pathout'] = os.path.dirname(os.path.realpath(path))
+        DoConf(conf['ira']).makeoptions([conf['type']], [conf])
+        return conf
+    
+    def opencorpus(self) :
+        log.info('open corpus')
+        if self.conf['uuid'] not in self.parent.history.corpus :
+            self.parent.history.add(self.conf) 
+            log.info('add corpus to history')
+            self.parent.tree.OnItemAppend(self.conf)
+        if self.conf['uuid'] in self.parent.history.openedcorpus :
+            log.info('corpus is already opened')
+            self.doopen(self.parent.history.openedcorpus[self.conf['uuid']])
+        else :
+            #dial = progressbar(2)
+            #dial.Update(1, 'Ouverture du corpus')
+            corpus = Corpus(self, parametres = self.conf, read = self.parent.history.history[self.parent.history.ordercorpus[self.conf['uuid']]]['ira'])
+            #dial.Update(2, 'Fini')
+            #dial.Destroy()
+            self.parent.history.openedcorpus[self.conf['uuid']] = corpus
+            self.opencorpus_analyses()
+            self.doopen(corpus)
+    
+    def openmatrix(self):
+        log.info('open matrix')
+        if self.conf['uuid'] not in self.parent.history.ordermatrix :
+            self.parent.history.addMatrix(self.conf)
+            log.info('add matrix to history')
+            self.parent.tree.OnItemAppend(self.conf)
+        if self.conf['uuid'] in self.parent.history.openedmatrix :
+            log.info('matrix is already opened')
+            self.doopen(self.parent.history.openedmatrix[self.conf['uuid']])
+        else :
+            #dial = progressbar(2)
+            #dial.Update(1, 'Ouverture du corpus')
+            matrix = Tableau(self, parametres = self.conf)
+            matrix.open()
+            self.parent.history.openedmatrix[self.conf['uuid']] = matrix
+            self.openmatrix_analyses()
+            self.doopen(matrix)        
+            self.parent.history.addtab(self.conf)
+
+    def opencorpus_analyses(self) :
+        log.info('open analysis')
+        basepath = self.conf['pathout']
+        analyses = []
+        for root, subfolders, files in os.walk(basepath) :
+            for folder in subfolders :
+                if os.path.exists(os.path.join(folder, 'Analyse.ira')) :
+                    analyse_conf = DoConf(os.path.join(folder, 'Analyse.ira')).getoptions()
+                    analyse_conf = self.redopath(analyse_conf, os.path.join(folder, 'Analyse.ira'))
+                    if analyse_conf['corpus'] == self.conf['uuid'] :
+                        analyses.append(analyse_conf)
+        if len(analyses) :
+            self.parent.history.addmultiple(analyses)
+        for analyse in analyses :
+            self.parent.tree.AddAnalyse(analyse, bold = False)
+    
+    def openmatrix_analyses(self):
+        pass
+
+    def openanalyse(self) :
+        if self.conf['corpus'] in self.parent.history.openedcorpus :
+            log.info('corpus is already opened')
+            corpus = copycorpus(self.parent.history.openedcorpus[self.conf['corpus']])
+        else :
+            if os.path.exists(self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira']) :
+                corpus = Corpus(self, parametres = DoConf(self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira']).getoptions('corpus'), read = self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira'])
+                self.parent.history.openedcorpus[self.conf['corpus']] = corpus
+        self.parent.history.add(self.conf)
+        return corpus
+
     def doopen(self, corpus) :
-        print self.conf
         if self.conf['type'] == 'corpus' :
-            self.parent.ShowMenu(_("Text analysis"))
             OpenCorpus(self.parent, self.conf) 
         elif self.conf['type'] == 'stat' :
-            self.parent.ShowMenu(_("Text analysis"))
             StatLayout(self.parent, corpus, self.conf)
         elif self.conf['type'] == 'spec' :
-            self.parent.ShowMenu(_("Text analysis"))
             dolexlayout(self.parent, corpus, self.conf)
         elif self.conf['type'] == 'alceste' :
-            self.parent.ShowMenu(_("Text analysis"))
             OpenCHDS(self.parent,  corpus, self.conf, Alceste = True)
-        elif self.conf['type'] == 'simitxt' :
-            self.tableau = Tableau(self.parent, self.conf['ira'])
-            self.DictPathOut=construct_simipath(self.conf['pathout'])
-            self.tableau.dictpathout = self.DictPathOut
-            self.tableau.read_tableau(self.tableau.dictpathout['db'])
-            if self.tableau.parametre.get('corpus', False) :
-                self.corpus=corpus
-                #self.corpus.read_corpus_from_shelves(self.DictPathOut['corpus'])
-                self.corpus.parametres['openpath'] = self.conf['pathout']
-                self.parent.ShowMenu(_("Text analysis"))
-            DoSimi(self.parent, self.conf, isopen = True, filename = self.conf['ira'], gparent = self, openfromprof=False) 
-            
-            print 'simi'
-#        try :
-#            #if self.conf['type'] in ['analyse','lexico','stat','wordcloud'] :
-#            #    self.corpus = Corpus(parent)
-#            if 'analyse' in self.conf.sections() :
-#                DictPathOut=ChdTxtPathOut(os.path.dirname(filename))
-#                self.pathout = os.path.dirname(filename)
-#                self.DictPathOut=DictPathOut
-#                #self.corpus = Corpus(parent)
-#                self.corpus.dictpathout = self.DictPathOut
-#                self.corpus.read_corpus_from_shelves(self.DictPathOut['db'])
-#                self.corpus.parametre['analyse'] = 'alceste'
-#                self.corpus.make_lem_type_list()
-#    #            print 'EXTRACT NR'
-#    #            self.corpus.extractnr()
-#                Alceste=True
-#                #self.corpus.save_corpus(self.corpus.dictpathout['db'])
-#                #self.corpus.make_uci_stat()
-#                #self.corpus.make_et_table()
-#                #self.corpus.prof_type()
-#                #self.corpus.make_type_tot()
-#                #self.corpus.make_size_uci()
-#                #self.corpus.get_stat_by_cluster()
-#                OpenCHDS(parent, self, filename, Alceste)
-#                self.parent.ShowMenu(_("Text analysis"))
-#                self.parent._mgr.Update()
-#            elif 'questionnaire' in self.conf.sections() :
-#                self.DictPathOut=ChdTxtPathOut(os.path.dirname(filename))
-#                self.pathout = os.path.dirname(filename)
-#                self.tableau = Tableau(parent, filename)
-#                self.tableau.dictpathout = self.DictPathOut
-#                self.tableau.read_tableau(self.tableau.dictpathout['db'])
-#                OpenCHDS(parent, self, filename, False)
-#            elif 'simi' in self.conf.sections():
-#                self.tableau = Tableau(parent, filename)
-#                self.DictPathOut=construct_simipath(os.path.abspath(os.path.dirname(filename)))
-#                self.tableau.dictpathout = self.DictPathOut
-#                self.tableau.read_tableau(self.tableau.dictpathout['db'])
-#                if self.tableau.parametre.get('fromtxt', False) :
-#                    self.corpus=Corpus(parent)
-#                    self.corpus.read_corpus_from_shelves(self.DictPathOut['corpus'])
-#                    self.corpus.parametre['openpath'] = os.path.dirname(filename)
-#                    self.parent.ShowMenu(_("Text analysis"))
-#                DoSimi(parent, self.conf, isopen = True, filename = filename, gparent = self, openfromprof=simifromprof) 
-#            elif 'lexico' in self.conf.sections():
-#                print 'lexico'
-#                #self.corpus = Corpus(parent)
-#                self.corpus.dictpathout = StatTxtPathOut(os.path.dirname(filename))
-#                self.parent.ShowMenu(_("Text analysis"))
-#                dolexlayout(parent, self, filename)
-#            elif 'stat' in self.conf.sections():
-#                print 'stat'
-#                #self.corpus = Corpus(parent)
-#                self.corpus.dictpathout = StatTxtPathOut(os.path.dirname(filename))
-#                self.parent.ShowMenu(_("Text analysis"))
-#                StatLayout(parent, self, filename)
-#            elif 'chd_dist_quest' in self.conf.sections():
-#                self.DictPathOut = ChdTxtPathOut(os.path.dirname(filename))
-#                self.pathout = os.path.dirname(filename)
-#                self.tableau = Tableau(parent, filename)
-#                self.tableau.dictpathout = self.DictPathOut
-#                self.tableau.read_tableau(self.tableau.dictpathout['db'])
-#                OpenCHDS(parent, self, filename, False)
-#            elif 'wordcloud' in self.conf.sections() :
-#                self.corpus.dictpathout = StatTxtPathOut(os.path.dirname(filename))
-#                self.corpus.read_corpus_from_shelves(self.corpus.dictpathout['db'])
-#                self.parent.ShowMenu(_("Text analysis"))
-#                WordCloudLayout(parent, self, filename)
-#            if self.conf.sections()[0] in ['analyse','lexico','stat','wordcloud'] :
-#                self.corpus.parametre['openpath'] = os.path.dirname(filename)
-#        except :
-#            BugReport(self.parent)
-
-
+        elif self.conf['type'] == 'simitxt' or self.conf['type'] == 'clustersimitxt' :
+            SimiLayout(self.parent, corpus, self.conf)
+        elif self.conf['type'] == 'wordcloud' or self.conf['type'] == 'clustercloud':
+            WordCloudLayout(self.parent, corpus, self.conf)
+        elif self.conf['type'] == 'reinertmatrix' :
+            OpenCHDS(self.parent,  corpus, self.conf, Alceste = False)
+        elif self.conf['type'] == 'simimatrix' or self.conf['type'] == 'simiclustermatrix':
+            SimiMatLayout(self.parent, corpus, self.conf)
+        elif self.conf['type'] == 'proto' :
+            ProtoLayout(self.parent, corpus, self.conf)
+        elif self.conf['type'] == 'matrix' :
+            MatLayout(self.parent, corpus)
+        elif self.conf['type'] == 'freq' or self.conf['type'] == 'freqmulti':
+            FreqLayout(self.parent, corpus, self.conf)
+        elif self.conf['type'] == 'chi2' :
+            Chi2Layout(self.parent, corpus, self.conf)
+