multisplit
[iramuteq] / mergeclustergraph.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2020 Pierre Ratinaud
4 #modification pour python 3 : Laurent Mérat, 6x7 - mai 2020
5 #License: GNU/GPL
6
7 #------------------------------------
8 # import des modules python
9 #------------------------------------
10 import os
11 import sys
12
13 #------------------------------------
14 # import des modules wx
15 #------------------------------------
16
17 #------------------------------------
18 # import des fichiers du projet
19 #------------------------------------
20 from functions import DoConf, read_chd, ReadProfileAsDico
21 from chemins import ConstructPathOut, ChdTxtPathOut, FFF, ffr, PathOut, StatTxtPathOut, simipath
22 from layout import SimiLayout
23 from textsimi import *
24 from analyse_merge import AnalyseMerge
25
26
27 class MergeClusterGraph :
28
29     def __init__(self, ira, corpus, parametres) :
30         self.ira = ira
31         self.corpus = corpus
32         self.parametres = parametres
33         self.pathout = PathOut(parametres['ira'])
34         self.pathout.basefiles(ChdTxtPathOut)
35         self.corpus.make_ucecl_from_R(self.pathout['uce'])
36         self.encoding = self.parametres['encoding']
37         self.clnb = parametres['clnb']
38         dictprofile = ReadProfileAsDico(self.pathout['PROFILE_OUT'], True, self.encoding)
39         self.dograph(dictprofile)
40
41     def dograph(self, dictprofile) :
42         tomerge = []
43         #OUTCH!!First cluster removed !!
44         #print 'ELMINATION CLUSTER 1'
45         for i in range(0, self.clnb):
46             self.pathout = PathOut(self.parametres['ira'])
47             simiparam = DoConf(self.ira.ConfigPath['simitxt']).getoptions()
48             simiparam['coeff'] = 3
49             simiparam['cexfromchi'] = True
50             profclasse = dictprofile[repr(i+1)]
51             line1 = profclasse.pop(0)
52             classen = [line for line in profclasse if line[0] != '*' and line[0] != '*****']
53             try :
54                 self.lenact = profclasse.index(['*****', '*', '*', '*', '*', '*', '', ''])
55                 profclasse.pop(self.lenact)
56             except ValueError:
57                 try :
58                     self.lenact = profclasse.index(['*', '*', '*', '*', '*', '*', '', ''])
59                     profclasse.pop(self.lenact)
60                 except ValueError:
61                     self.lenact = len(profclasse)
62             try :
63                 self.lensup = profclasse.index(['*', '*', '*', '*', '*', '*', '', ''])
64                 self.lensup = self.lensup - self.lenact
65                 profclasse.pop(self.lensup)
66             except ValueError:
67                 self.lensup = len(profclasse) - self.lenact
68             self.lenet = len(profclasse) - (self.lenact + self.lensup)
69 #            print self.lenact, self.lensup, self.lenet
70             for l,  line in enumerate(classen) :
71                 line[0] = l
72             dictdata = dict(list(zip([l for l in range(0,len(classen))], classen)))
73             if self.lenact != 0 :
74                 self.la = [dictdata[l][6] for l in range(0, self.lenact)]
75                 self.lchi = [dictdata[l][4] for l in range(0, self.lenact)]
76                 self.lfreq = [dictdata[l][1] for l in range(0, self.lenact)]
77             else :
78                 self.la = []
79                 self.lchi = []
80                 self.lfreq = []
81             print('cluster : ', i)
82             simi = SimiFromCluster(self.ira, self.corpus, self.la, self.lfreq,
83                             self.lchi, i, parametres = simiparam, limit=100)
84             tomerge.append(simi.parametres['ira'])
85             print(tomerge)
86         newparam = {'type': 'merge', 'fileout' : '/tmp/test.txt'}
87         newparam['graphs'] = tomerge
88         AnalyseMerge(self.ira, newparam, dlg=None)