factiva
[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         log.info('OpenAnalyse')
24         self.parent = parent
25         if isinstance(parametres, dict) :
26             self.conf = DoConf(parametres['ira']).getoptions()
27             self.path = parametres['ira']
28         else :
29             self.conf = DoConf(parametres).getoptions()
30             self.path = parametres
31             self.conf = self.redopath(self.conf, parametres)
32         
33         if self.conf['type'] == 'corpus' :
34             corpus = self.opencorpus()
35         elif self.conf['corpus'] in self.parent.history.corpus :
36             corpus = self.openanalyse()
37             if self.conf.get('lem',1) :
38                corpus.make_lems(True)
39             else :
40                corpus.make_lems(False)
41             self.doopen(corpus)
42         else :
43             corpus = None
44         self.parent.history.addtab(self.conf)
45     
46     def redopath(self, conf, path) :
47         conf['ira'] = os.path.realpath(path)
48         conf['pathout'] = os.path.dirname(os.path.realpath(path))
49         DoConf(conf['ira']).makeoptions([conf['type']], [conf])
50         return conf
51     
52     def opencorpus(self) :
53         log.info('open corpus')
54         if self.conf['uuid'] not in self.parent.history.corpus :
55             self.parent.history.add(self.conf) 
56             log.info('add to history')
57             self.parent.tree.OnItemAppend(self.conf)
58         if self.conf['uuid'] in self.parent.history.openedcorpus :
59             log.info('corpus is already opened')
60             self.doopen(self.parent.history.openedcorpus[self.conf['uuid']])
61         else :
62             corpus = Corpus(self, parametres = self.conf, read = self.parent.history.history[self.parent.history.ordercorpus[self.conf['uuid']]]['ira'])
63             self.parent.history.openedcorpus[self.conf['uuid']] = corpus
64             self.opencorpus_analyses()
65             self.doopen(corpus)
66
67     def opencorpus_analyses(self) :
68         basepath = self.conf['pathout']
69         for root, subfolders, files in os.walk(basepath) :
70             for folder in subfolders :
71                 if os.path.exists(os.path.join(folder, 'Analyse.ira')) :
72                     analyse_conf = DoConf(os.path.join(folder, 'Analyse.ira')).getoptions()
73                     analyse_conf = self.redopath(analyse_conf, os.path.join(folder, 'Analyse.ira'))
74                     if analyse_conf['corpus'] == self.conf['uuid'] :
75                         self.parent.history.add(analyse_conf)
76                         self.parent.tree.AddAnalyse(analyse_conf, bold = False)
77
78     def openanalyse(self) :
79         if self.conf['corpus'] in self.parent.history.openedcorpus :
80             log.info('corpus is already opened')
81             corpus = self.parent.history.openedcorpus[self.conf['corpus']]
82         else :
83             if os.path.exists(self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira']) :
84                 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'])
85                 self.parent.history.openedcorpus[self.conf['corpus']] = corpus
86         return corpus
87
88     def doopen(self, corpus) :
89         if self.conf['type'] == 'corpus' :
90             self.parent.ShowMenu(_("Text analysis"))
91             OpenCorpus(self.parent, self.conf) 
92         elif self.conf['type'] == 'stat' :
93             self.parent.ShowMenu(_("Text analysis"))
94             StatLayout(self.parent, corpus, self.conf)
95         elif self.conf['type'] == 'spec' :
96             self.parent.ShowMenu(_("Text analysis"))
97             dolexlayout(self.parent, corpus, self.conf)
98         elif self.conf['type'] == 'alceste' :
99             self.parent.ShowMenu(_("Text analysis"))
100             OpenCHDS(self.parent,  corpus, self.conf, Alceste = True)
101         elif self.conf['type'] == 'simitxt' or self.conf['type'] == 'clustersimitxt' :
102             self.parent.ShowMenu(_("Text analysis"))
103             SimiLayout(self.parent, corpus, self.conf)
104         elif self.conf['type'] == 'wordcloud' :
105             self.parent.ShowMenu(_("Text analysis"))
106             WordCloudLayout(self.parent, corpus, self.conf)