...
[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=False)
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         if options.configfile :
67             self.ConfigPath[options.type_analyse] = os.path.abspath(options.configfile)
68         self.TEMPDIR = tempfile.mkdtemp('iramuteq') 
69         self.RscriptsPath = ConstructRscriptsPath(AppliPath)
70         self.PathPath = ConfigParser()
71         self.PathPath.read(self.ConfigPath['path'])        
72         self.RPath = self.PathPath.get('PATHS', 'rpath')
73         self.pref = RawConfigParser()
74         self.pref.read(self.ConfigPath['preferences'])
75         self.history = History(self.ConfigPath['history'])
76         #self.history.write()
77         if options.filename or options.read or options.build:
78             self.corpus_encodage = options.encodage
79             self.corpus_lang = options.language
80                 
81
82             #print 'PAS DE CODECS POUR CABLE'
83             ReadLexique(self, lang = options.language)
84             self.expressions = ReadDicoAsDico(self.DictPath.get(options.language + '_exp', 'french_exp'))
85             if options.filename :
86                 self.filename = os.path.abspath(options.filename)
87                 corpus_parametres = DoConf('/home/pierre/.iramuteq/corpus.cfg').getoptions('corpus')
88                 corpus_parametres['filename'] = self.filename
89                 corpus_parametres['encoding'] = self.corpus_encodage
90                 corpus_parametres['syscoding'] = 'utf8'
91                 corpus_parametres['pathout'] = PathOut(options.filename, 'corpus').dirout
92                 corpus = BuildFromAlceste(self.filename, corpus_parametres, self.lexique, self.expressions).corpus
93                 self.history.add(corpus.parametres)
94
95                 #with codecs.open(self.filename, 'r', self.corpus_encodage) as f:
96             elif options.read :
97                 corpus = Corpus(self, parametres = DoConf(options.read).getoptions('corpus'), read = options.read)
98                 corpus.parametres['path'] = os.path.abspath(options.read)
99                 pathout = os.path.dirname(os.path.dirname(os.path.abspath(options.read)))
100
101
102             corpus.conn_all()
103             corpus.make_lems()
104             corpus.parse_active(gramact, gramsup)
105             log.warning('ATTENTION gethapaxuces')
106             corpus.gethapaxuces()
107                 #    self.content = f.read()
108                 #self.content = self.content.replace('\r','')
109             if options.type_analyse == 'alceste' :
110                 log.debug('ATTENTION : ANALYSE NG')
111                     #print corpus.make_etoiles()
112                     #zerzre
113                 #corpus.read_corpus()
114                 #corpus.parse_active(gramact, gramsup)
115                 Alceste(self, corpus)
116             #    self.Text = AnalyseAlceste(self, cmd = True, big = True)
117                 #self.Text = AnalyseAlceste(self, cmd = True)
118             elif options.type_analyse == 'pam' :
119                 self.Text = PamTxt(self, cmd = True)
120             elif options.type_analyse == 'afcuci' :
121                 self.Text = AfcUci(self, cmd = True)
122             elif options.type_analyse == 'stat' :
123                 self.Text = Stat(self, corpus, config = {'type' : 'stat'})
124             elif options.type_analyse == 'spec' :
125                 self.Text = Lexico(self, corpus, config = {'type' : 'spec'})
126             #print self.Text.corpus.hours, 'h', self.Text.corpus.minutes,'min', self.Text.corpus.seconds, 's'
127 #            self.Text.corpus.make_colored_corpus('colored.html')
128
129 if __name__ == '__main__':
130     __name__ = 'Main'
131     CmdLine()
132