244f41f20774dcb5e5f31f6b0338277fcd187f54
[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 import tempfile
24 ######################################
25 print '#######LOGGING TEST###########'
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
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) :
50         self.DictPath = ConstructDicoPath(AppliPath)
51         self.ConfigPath = ConstructConfigPath(UserConfigPath)
52         self.syscoding = sys.getdefaultencoding()
53         parser = OptionParser()
54     
55         parser.add_option("-f", "--file", dest="filename", help="chemin du corpus", metavar="FILE", default=False)
56         parser.add_option("-t", "--type", dest="type_analyse", help="type d'analyse", metavar="TYPE D'ANALYSE", default=False)
57
58         parser.add_option("-c", "--conf", dest="configfile", help="chemin du fichier de configuration", metavar="CONF", default=None)
59         parser.add_option("-e", "--enc", dest="encodage", help="encodage du corpus", metavar="ENC", default=locale.getpreferredencoding())
60         parser.add_option("-l", "--lang", dest="language", help="langue du corpus", metavar="LANG", default='french')
61         parser.add_option("-r", "--read", dest="read", help="lire un corpus", metavar="READ", default = False)
62
63         (options, args) = parser.parse_args()
64         print args
65         print options
66         options.type_analyse
67         if options.configfile is not None:
68             config = DoConf(os.path.abspath(options.configfile)).getoptions()
69         elif options.type_analyse :
70             config = DoConf(self.ConfigPath[options.type_analyse]).getoptions()
71             #self.ConfigPath[options.type_analyse] = os.path.abspath(options.configfile)
72         self.TEMPDIR = tempfile.mkdtemp('iramuteq') 
73         self.RscriptsPath = ConstructRscriptsPath(AppliPath)
74         self.PathPath = ConfigParser()
75         self.PathPath.read(self.ConfigPath['path'])        
76         self.RPath = self.PathPath.get('PATHS', 'rpath')
77         self.pref = RawConfigParser()
78         self.pref.read(self.ConfigPath['preferences'])
79         self.history = History(self.ConfigPath['history'])
80         #self.history.write()
81         if options.filename or options.read or options.build:
82             self.corpus_encodage = options.encodage
83             self.corpus_lang = options.language
84                 
85
86             #print 'PAS DE CODECS POUR CABLE'
87             ReadLexique(self, lang = options.language)
88             self.expressions = ReadDicoAsDico(self.DictPath.get(options.language + '_exp', 'french_exp'))
89             if options.filename :
90                 self.filename = os.path.abspath(options.filename)
91                 corpus_parametres = DoConf('/home/pierre/.iramuteq/corpus.cfg').getoptions('corpus')
92                 corpus_parametres['filename'] = self.filename
93                 corpus_parametres['encoding'] = self.corpus_encodage
94                 corpus_parametres['syscoding'] = 'utf8'
95                 corpus_parametres['pathout'] = PathOut(options.filename, 'corpus').dirout
96                 corpus = BuildFromAlceste(self.filename, corpus_parametres, self.lexique, self.expressions).corpus
97                 self.history.add(corpus.parametres)
98
99                 #with codecs.open(self.filename, 'r', self.corpus_encodage) as f:
100             elif options.read :
101                 corpus = Corpus(self, parametres = DoConf(options.read).getoptions('corpus'), read = options.read)
102                 corpus.parametres['path'] = os.path.abspath(options.read)
103                 pathout = os.path.dirname(os.path.dirname(os.path.abspath(options.read)))
104
105
106             corpus.conn_all()
107             corpus.make_lems()
108             corpus.parse_active(gramact, gramsup)
109 #            log.warning('ATTENTION gethapaxuces')
110 #            MakeUciStat(corpus)
111 #            qfqsdf
112             #corpus.gethapaxuces()
113             #ucisize = corpus.getucisize()
114             #ucisize = [`val` for val in ucisize]
115             #uciet = [uci.etoiles[1] for uci in corpus.ucis]
116             #res = zip(uciet, ucisize)
117             #with open('ucisize.csv', 'w') as f :
118             #    f.write('\n'.join(['\t'.join(val) for val in res]))
119                 #    self.content = f.read()
120                 #self.content = self.content.replace('\r','')
121             if options.type_analyse == 'alceste' :
122                 log.debug('ATTENTION : ANALYSE NG')
123                     #print corpus.make_etoiles()
124                     #zerzre
125                 #corpus.read_corpus()
126                 #corpus.parse_active(gramact, gramsup)
127                 config['type'] = 'alceste'
128                 Alceste(self, corpus, parametres = config)
129             #    self.Text = AnalyseAlceste(self, cmd = True, big = True)
130                 #self.Text = AnalyseAlceste(self, cmd = True)
131             elif options.type_analyse == 'pam' :
132                 self.Text = PamTxt(self, cmd = True)
133             elif options.type_analyse == 'afcuci' :
134                 self.Text = AfcUci(self, cmd = True)
135             elif options.type_analyse == 'stat' :
136                 self.Text = Stat(self, corpus, config = {'type' : 'stat'})
137             elif options.type_analyse == 'spec' :
138                 self.Text = Lexico(self, corpus, config = {'type' : 'spec'})
139             #print self.Text.corpus.hours, 'h', self.Text.corpus.minutes,'min', self.Text.corpus.seconds, 's'
140 #            self.Text.corpus.make_colored_corpus('colored.html')
141
142 if __name__ == '__main__':
143     __name__ = 'Main'
144     CmdLine()
145