-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrolador.py
179 lines (158 loc) · 7.53 KB
/
controlador.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
#+--------------------------------------------------------------------------+#
# Importem mòduls #
#+--------------------------------------------------------------------------+#
from agrupador import Agrupador
from visualitzador import Visualitzador_Recuperacio, Visualitzador_Agrupacio
from recuperador import Recuperador
from representacio import Bow, TfIdf
from indexador import Index
from vocabulari import Txt_Vocabulary, Img_Vocabulary, Tfidf_Vocabulary
from classes_arxius import Document, Imatge
from distancies import Cosinus, Intersection
import pickle
from tqdm import tqdm
import os
#+--------------------------------------------------------------------------+#
# Definim les classes #
#+--------------------------------------------------------------------------+#
class Controller():
def __init__(self, t_document, t_representacio, t_distancia, train):
self._t_document = t_document
self._t_representacio = t_representacio
self._t_distancia = t_distancia
self._train_ = train
def prepara_database(self, arxiu = None):
index = self.recuperar("index.pckl")
try:
database = index[arxiu]
except:
raise AssertionError("Index no indicat. Intenta actualitzar-lo.")
if self._t_document == "text":
vocabulary = Txt_Vocabulary()
vocabulary.read("./newsgroups/retrieval/vocabulary.txt")
if self._t_representacio == "bow":
representador = Bow(self._t_document, vocabulary.vocabulary)
else:
vocabulary_tfidf = Tfidf_Vocabulary(vocabulary.vocabulary)
vocabulary_tfidf.read(self._t_document, "./newsgroups/vocabulary_idf.txt")
representador = TfIdf(self._t_document, vocabulary_tfidf)
train = []
#index = 0
for file in database:
#print(index)
#index += 1
train.append(Document(file, self._train_+"/"+file, vocabulary, representador))
train[len(train)-1].read()
train[len(train)-1].get_representation()
else:
vocabulary = Img_Vocabulary()
vocabulary.read("./cifrar/retrieval/vocabulary.dat")
if self._t_representacio == "bow":
representador = Bow(self._t_document, vocabulary.vocabulary)
else:
vocabulary_tfidf = Tfidf_Vocabulary(vocabulary.vocabulary)
vocabulary_tfidf.read(self._t_document, "./cifrar/vocabulary/idf.txt")
representador = TfIdf(self._t_document, vocabulary_tfidf)
train = []
#index = 0
for file in database:
#print(index)
#index += 1
train.append(Imatge(file, self._train_+"/"+file, vocabulary, representador))
train[len(train)-1].read()
train[len(train)-1].get_representation()
self._train = train
def crea_index(self):
if self._t_document == "text":
vocabulary = Txt_Vocabulary()
vocabulary.read("./newsgroups/retrieval/vocabulary.txt")
if self._t_representacio == "bow":
representador = Bow(self._t_document, vocabulary.vocabulary)
else:
vocabulary_tfidf = Tfidf_Vocabulary(vocabulary.vocabulary)
vocabulary_tfidf.read(self._t_document, "./newsgroups/vocabulary_idf.txt")
representador = TfIdf(self._t_document, vocabulary_tfidf)
self._index = Index(self._t_document, vocabulary.vocabulary)
train = []
index = 0
file_list = os.listdir(self._train_)
for file in file_list:
#print(index)
index += 1
train.append(Document(file, self._train_+"/"+file, vocabulary, representador))
train[len(train)-1].read()
train[len(train)-1].get_representation()
self._index.afegeix_document(train[len(train)-1])
else:
vocabulary = Img_Vocabulary()
vocabulary.read("./cifrar/retrieval/vocabulary.dat")
if self._t_representacio == "bow":
representador = Bow(self._t_document, vocabulary.vocabulary)
else:
vocabulary_tfidf = Tfidf_Vocabulary(vocabulary.vocabulary)
vocabulary_tfidf.read(self._t_document, "./cifrar/vocabulary/idf.txt")
representador = TfIdf(vocabulary.vocabulary, vocabulary_tfidf)
self._index = Index(self._t_document, vocabulary.vocabulary)
train = []
index = 0
file_list = os.listdir(self._train_)
for file in file_list:
#print(index)
index += 1
train.append(Imatge(file, self._train_+"/"+file, vocabulary, representador))
train[len(train)-1].read()
train[len(train)-1].get_representation()
self._index.afegeix_document(train[len(train)-1])
self._train = train
self.guardar("index.pckl", self._index.recuperar_documents_on_buscar(self._train))
def realitza_recuperacio(self, document_query, t_document):
self.prepara_database(document_query)
self._recuperador = Recuperador(document_query, self._train, t_document, self._t_distancia)
self._recuperador.processa_recuperacio()
self._resultat = ["recuperacio", self._recuperador.get_results()]
def realitza_agrupacio(self, k):
try:
self.prepara_database()
if self._t_distancia == "cosinus":
operador = Cosinus()
else:
operador = Intersection()
self._agrupador = Agrupador(self._train, k, operador)
final = False
self._agrupador.calcula_distancies()
self._agrupador.calcula_grups()
for i in range(20):
final = self._agrupador.calcula_representant()
self._agrupador.calcula_distancies()
self._agrupador.calcula_grups()
resultat = self._agrupador.get_results()
self._resultat = ["agrupacio", resultat]
except:
raise AssertionError("Index incorrecte!")
def visualitza_resultats(self):
if self._resultat[0] == "recuperacio":
self._visualitzador = Visualitzador_Recuperacio(self._resultat)
elif self._resultat[0] == "agrupacio":
self._visualitzador = Visualitzador_Agrupacio(self._resultat)
self._visualitzador.visualitza()
loop = tqdm(total = 3000, position = 0, leave = False)
for k in range(3000):
loop.set_description("Tancant visualitzador...".format(k))
loop.update(1)
loop.close()
print("\n--------------------------------------\n")
def recuperar(self, nom_database):
try:
fitxer = open(nom_database, 'ab+')
fitxer.seek(0)
database = pickle.load(fitxer)
fitxer.close()
print("\nS'ha carregat correctament l'índex!")
return database
except:
raise AssertionError("\nL'índex no existeix!")
def guardar(self, fitxer, dades):
fitxer = open(fitxer, 'wb')
pickle.dump(dades, fitxer)
fitxer.close()