Merge branch 'master' of 192.168.0.2:/home/pouns/iramuteqNG
[iramuteq] / textsimi.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2011 Pierre Ratinaud
4 #Lisense: GNU/GPL
5
6 from chemins import ConstructPathOut, construct_simipath
7 from corpus import Corpus
8 import os
9 from ConfigParser import RawConfigParser
10 from guifunct import getPage, getCorpus
11 from dialog import StatDialog
12 from functions import indices_simi, progressbar, treat_var_mod
13 from tableau import Tableau
14 from tabsimi import DoSimi
15 import wx
16 from copy import copy
17
18 class SimiTxt :
19     def __init__(self, parent, cmd = False, param = None):
20         self.parent = parent
21         self.cmd = cmd
22         self.ConfigPath = parent.ConfigPath
23         self.DictPath = parent.DictPath
24         self.KeyConf = RawConfigParser()
25         self.KeyConf.read(self.ConfigPath['key'])
26         self.indices = indices_simi
27         self.paramsimi = {'coeff' : 0,
28                           'layout' : 2,
29                           'type' : 1,
30                           'arbremax' : 1,
31                           'coeff_tv' : 1,
32                           'coeff_tv_nb' : 0,
33                           'tvprop' : 0,
34                           'tvmin' : 5,
35                           'tvmax' : 30,
36                           'coeff_te' : 1,
37                           'coeff_temin' : 1,
38                           'coeff_temax' : 10,
39                           'label_v': 1,
40                           'label_e': 0,
41                           'vcex' : 1,
42                           'cexfromchi' : False,
43                           'vcexmin' : 10,
44                           'vcexmax' : 25,
45                           'cex' : 10,
46                           'seuil_ok' : 0,
47                           'seuil' : 1,
48                           'cols' : (255,0,0),
49                           'cola' : (200,200,200),
50                           'width' : 1000,
51                           'height' : 1000,
52                           'bystar' : False,
53                           'first' : True,
54                           'keep_coord' : True,
55                           'alpha' : 20,
56                           'film': False,
57                           }
58         page = getPage(self.parent)
59         if page is not None :
60             self.corpus = getCorpus(page)
61             if self.corpus is not None :
62                 self.pathout = ConstructPathOut(self.corpus.parametre['openpath'], 'simitxt')
63                 self.dictpathout = construct_simipath(self.pathout)
64                 self.val = wx.ID_OK
65                 self.make_table()
66                 self.make_simi()
67         else :
68             self.corpus = Corpus(parent)
69             self.corpus.content = self.parent.content
70             self.corpus.parametre['encodage'] = parent.corpus_encodage
71             self.corpus.parametre['lang'] = parent.corpus_lang
72             self.corpus.parametre['filename'] = parent.filename
73             self.corpus.parametre['eff_min_uce'] = None
74             self.pathout = ConstructPathOut(self.corpus.parametre['filename'], 'simitxt')
75             self.dictpathout = construct_simipath(self.pathout)
76             dial = StatDialog(self, self.parent)
77             dial.check_uce.SetValue(True)
78             dial.check_uce.Enable(False)
79             dial.OnCheckUce(wx.EVT_MENU)
80             self.val = dial.ShowModal()
81             if self.val == wx.ID_OK :
82                 with open(self.parent.ConfigPath['key'], 'w') as f:
83                     self.KeyConf.write(f)
84                 if dial.radio_lem.GetSelection() == 0 : lem = True
85                 else : lem = False
86                 if dial.exp.GetSelection() == 0 : exp = True
87                 else : exp = False
88                 dial.Destroy()
89                 self.corpus.parametre['lem'] = lem
90                 self.corpus.parametre['expressions'] = exp
91                 self.corpus.parametre['nbforme_uce'] = dial.spin_ctrl_4.GetValue()
92                 self.corpus.parametre['eff_min_forme'] = dial.spin_ctrl_5.GetValue()
93                 self.corpus.parametre['max_actives'] = dial.spin_max_actives.GetValue()
94                 self.make_corpus()
95                 self.make_table()
96                 self.make_simi()
97             else :
98                 dial.Destroy()
99
100     def make_corpus(self) :
101         print 'make corpus'
102         if not self.cmd :
103             dlg = progressbar(self, maxi = 6)
104         self.corpus.supplementaires = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "2"]
105         self.corpus.typeactive = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "1"]
106         ucis_txt, ucis_paras_txt = self.corpus.start_analyse(self.parent, dlg = dlg, cmd = self.cmd)
107         del ucis_txt
108     
109         if not self.cmd :
110             dlg.Update(5, '%i ucis - Construction des uces' % len(ucis_paras_txt))
111         self.corpus.make_ucis_paras_uces(ucis_paras_txt, make_uce = True)
112         del ucis_paras_txt
113
114         if self.corpus.para_coords != [[] for val in self.corpus.para_coords] :
115             self.corpus.parametre['para'] = True
116         else :
117             self.corpus.parametre['para'] = False
118         self.corpus.make_etoiles(self.corpus.para_coords)
119         print 'len(ucis_paras_uces)', len(self.corpus.ucis_paras_uces)
120
121         if not self.cmd :
122             dlg.Update(6, u'Dictionnaires')
123         uces, self.orderuces = self.corpus.make_forms_and_uces()
124         self.corpus.ucenb = len(uces)
125         self.corpus.make_lems(self.parent.lexique)
126
127         self.corpus.make_var_actives() 
128         self.corpus.make_var_supp()
129         self.corpus.lems_eff = self.corpus.make_lem_eff()
130
131         #variables = treat_var_mod(listet)
132         #print(variables)
133         #self.corpus.write_etoiles(self.dictpathout['etoiles'])
134         if not self.cmd :
135             dlg.Destroy()
136                 
137     def make_table(self) :
138         if 'orderuces' not in dir(self) :
139             self.orderuces = [(i,j,k) for i, uci in enumerate(self.corpus.ucis_paras_uces) for j, para in enumerate(uci) for k, uce in enumerate(para)]
140             self.orderuces = dict([[val, i] for i, val in enumerate(self.orderuces)])
141             self.corpus.ucenb = len(self.orderuces)
142         #tabuc1 = self.corpus.make_table_with_uce(self.orderuces)
143         #tabuc1.insert(0,self.corpus.actives)
144         #tabuc1 = self.corpus.make_sparse_matrix_with_uce(self.orderuces)
145         #self.corpus.write_sparse_matrix(self.dictpathout['mat01'], tabuc1, self.corpus.ucenb, len(self.corpus.actives))
146         if self.corpus.actives is None :
147             self.corpus.typeactive = [option for option in self.KeyConf.options('KEYS') if self.KeyConf.get('KEYS', option) == "1"]
148             self.corpus.min_eff_formes()
149             self.corpus.make_var_actives()
150         self.corpus.make_and_write_sparse_matrix_from_uce(self.orderuces, self.dictpathout['mat01'])
151         #self.corpus.write_tab(tabuc1,self.dictpathout['mat01'])
152
153     def make_simi(self) :
154         self.tableau = Tableau(self.parent, '')
155         self.tableau.listactives = self.corpus.actives
156         self.tableau.parametre['fromtxt'] = True
157         if 'lems_eff' not in dir(self.corpus) :
158             self.corpus.lems_eff = self.corpus.make_lem_eff()
159         #print('ATTENTION  ETOILES')
160         #self.paramsimi['bystar'] = True
161         self.listet = self.corpus.get_unique_etoiles()
162         self.listet.sort()
163         self.tableau.listet = copy(self.listet)
164         self.paramsimi['stars'] = copy(self.listet)
165         #self.paramsimi['cexfromchi'] = True
166         self.paramsimi['sfromchi'] = False
167         #self.paramsimi['vlabcolor'] = True
168         self.tableau.actives = dict([[lem, self.corpus.lems_eff[lem]] for lem in self.corpus.actives])
169         self.corpus.save_corpus(self.dictpathout['corpus']) 
170         DoSimi(self, fromprof = self.dictpathout['mat01'], param = self.paramsimi, pathout = self.pathout)