abf8ce26c144600a03eecd356293692812f8ecb1
[iramuteq] / iracmd.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2010 Pierre Ratinaud
5 #Lisense: GNU/GPL
6
7 import os
8 from optparse import OptionParser
9 import sys
10 reload(sys)
11 import locale
12 import codecs
13 sys.setdefaultencoding(locale.getpreferredencoding())
14 from chemins import ConstructConfigPath, ConstructDicoPath, ConstructRscriptsPath
15 from functions import ReadLexique, DoConf, History
16 from ConfigParser import *
17 #######################################
18 #from textchdalc import AnalyseAlceste
19 #from textdist import PamTxt
20 #from textafcuci import AfcUci
21 from textaslexico import Lexico
22 from textstat import Stat
23 from tools import SubCorpus
24 import tempfile
25 ######################################
26 import logging
27 log = logging.getLogger('iramuteq')
28 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
29 ch = logging.StreamHandler()
30 ch.setFormatter(formatter)
31 log.addHandler(ch)
32 log.setLevel(logging.DEBUG)
33 #######################################
34
35 log.debug('----------TEST corpusNG-----------------')
36 from analysetxt import Alceste,  gramact, gramsup
37 from corpusNG import *
38
39 #cmd = iracmd.CmdLine(args=['-f','/home/pierre/workspace/iramuteq/corpus/lru2.txt','-t','alceste'])
40
41 AppliPath = os.path.abspath(os.path.dirname(os.path.realpath(sys.argv[0])))
42 if os.getenv('HOME') != None:
43     user_home = os.getenv('HOME')
44 else:
45     user_home = os.getenv('HOMEPATH')
46 UserConfigPath = os.path.abspath(os.path.join(user_home, '.iramuteq'))
47
48 class CmdLine :
49     def __init__(self, args = None) :
50         self.DictPath = ConstructDicoPath(AppliPath)
51         self.ConfigPath = ConstructConfigPath(UserConfigPath)
52         self.syscoding = sys.getdefaultencoding()
53         self.TEMPDIR = tempfile.mkdtemp('iramuteq') 
54         self.RscriptsPath = ConstructRscriptsPath(AppliPath)
55         self.PathPath = ConfigParser()
56         self.PathPath.read(self.ConfigPath['path'])        
57         self.RPath = self.PathPath.get('PATHS', 'rpath')
58         self.pref = RawConfigParser()
59         self.pref.read(self.ConfigPath['preferences'])
60         self.history = History(os.path.join(UserConfigPath, 'history.db'))
61
62         parser = OptionParser()
63     
64         parser.add_option("-f", "--file", dest="filename", help="chemin du corpus", metavar="FILE", default=False)
65         parser.add_option("-t", "--type", dest="type_analyse", help="type d'analyse", metavar="TYPE D'ANALYSE", default=False)
66         parser.add_option("-c", "--conf", dest="configfile", help="chemin du fichier de configuration pour l'analyse", metavar="CONF", default=None)
67         parser.add_option("-d", "--confcorp", dest="corpusconfigfile", help="chemin du fichier de configuration pour le corpus", metavar="CONF", default=None)
68         parser.add_option("-e", "--enc", dest="encodage", help="encodage du corpus", metavar="ENC", default=locale.getpreferredencoding())
69         parser.add_option("-l", "--lang", dest="language", help="langue du corpus", metavar="LANG", default='french')
70         parser.add_option("-r", "--read", dest="read", help="lire un corpus", metavar="READ", default = False)
71         parser.add_option("-b", "--build", action="store_true", dest="build", help = "construire un corpus", default = False)
72
73         if args is None :
74             (options, args) = parser.parse_args()
75         else : 
76             (options, args) = parser.parse_args(args)
77         print args
78         print options
79         options.type_analyse
80         if options.configfile is not None:
81             config = DoConf(os.path.abspath(options.configfile)).getoptions()
82         elif options.filename and options.type_analyse :
83             config = DoConf(self.ConfigPath[options.type_analyse]).getoptions()
84             #self.ConfigPath[options.type_analyse] = os.path.abspath(options.configfile)
85         elif options.read and options.type_analyse :
86             config = DoConf(self.ConfigPath[options.type_analyse]).getoptions()
87         elif options.read :
88             pass
89         elif options.filename and options.build :
90             pass
91         else :
92             print 'rien a faire'
93             return
94
95         #self.history.write()
96         if options.filename or options.read :#or options.build:
97             self.corpus_encodage = options.encodage
98             self.corpus_lang = options.language
99                 
100
101             #print 'PAS DE CODECS POUR CABLE'
102             ReadLexique(self, lang = options.language)
103             self.expressions = ReadDicoAsDico(self.DictPath.get(options.language + '_exp', 'french_exp'))
104             if options.filename :
105                 self.filename = os.path.abspath(options.filename)
106                 if options.corpusconfigfile is not None :
107                     corpus_parametres = DoConf(options.corpusconfigfile).getoptions('corpus')
108                 else :
109                     corpus_parametres = DoConf(self.ConfigPath['corpus']).getoptions()
110                 dire, corpus_parametres['filename'] = os.path.split(self.filename)
111                 corpus_parametres['originalpath'] = self.filename
112                 corpus_parametres['encoding'] = self.corpus_encodage
113                 corpus_parametres['syscoding'] = locale.getpreferredencoding()
114                 corpus_parametres['pathout'] = PathOut(self.filename, 'corpus').mkdirout()
115                 try :
116                     corpus = BuildFromAlceste(self.filename, corpus_parametres, self.lexique, self.expressions).corpus
117                 except Exception, txt:
118                     log.info('probleme lors de la construction: %s' %txt)
119                     corpus = None
120                     raise
121                 else :
122                     self.history.add(corpus.parametres)
123                     corpus = copycorpus(corpus)
124
125                 #with codecs.open(self.filename, 'r', self.corpus_encodage) as f:
126             elif options.read :
127                 corpus = Corpus(self, parametres = DoConf(options.read).getoptions('corpus'), read = options.read)
128                 corpus.parametres['pathout'] = os.path.dirname(os.path.abspath(options.read))
129                 pathout = os.path.dirname(os.path.dirname(os.path.abspath(options.read)))
130                 self.corpus = corpus
131
132             if corpus is not None :
133                 corpus.conn_all()
134                 corpus = SubCorpus(self, corpus, [0,1,2,3,4,5,6,7])
135                 corpus.conn_all()
136                 corpus.make_lems()
137                 corpus.parse_active(gramact, gramsup)
138                 print corpus.getlemconcorde('de').fetchall()
139 #            log.warning('ATTENTION gethapaxuces')
140 #            MakeUciStat(corpus)
141 #            qfqsdf
142             #corpus.gethapaxuces()
143             #ucisize = corpus.getucisize()
144             #ucisize = [`val` for val in ucisize]
145             #uciet = [uci.etoiles[1] for uci in corpus.ucis]
146             #res = zip(uciet, ucisize)
147             #with open('ucisize.csv', 'w') as f :
148             #    f.write('\n'.join(['\t'.join(val) for val in res]))
149                 #    self.content = f.read()
150                 #self.content = self.content.replace('\r','')
151                 if options.type_analyse == 'alceste' :
152                     log.debug('ATTENTION : ANALYSE NG')
153                         #print corpus.make_etoiles()
154                         #zerzre
155                     #corpus.read_corpus()
156                     #corpus.parse_active(gramact, gramsup)
157                     config['type'] = 'alceste'
158                     self.Text = Alceste(self, corpus, parametres = config)
159                 #    self.Text = AnalyseAlceste(self, cmd = True, big = True)
160                     #self.Text = AnalyseAlceste(self, cmd = True)
161                 elif options.type_analyse == 'pam' :
162                     self.Text = PamTxt(self, cmd = True)
163                 elif options.type_analyse == 'afcuci' :
164                     self.Text = AfcUci(self, cmd = True)
165                 elif options.type_analyse == 'stat' :
166                     self.Text = Stat(self, corpus, parametres = {'type':'stat'})
167                 elif options.type_analyse == 'spec' :
168                     self.Text = Lexico(self, corpus, config = {'type' : 'spec'})
169             #print self.Text.corpus.hours, 'h', self.Text.corpus.minutes,'min', self.Text.corpus.seconds, 's'
170 #            self.Text.corpus.make_colored_corpus('colored.html')
171
172 if __name__ == '__main__':
173     __name__ = 'Main'
174     CmdLine()
175