forked from thiagodepaulo/total
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
256 lines (220 loc) · 8.75 KB
/
util.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 7 15:46:22 2017
@author: thiagodepaulo
"""
import re
import glob
import os.path
import codecs
import numpy as np
from collections import defaultdict
class Loader:
def __init__(self):
pass
# load supervised dataset
def from_files(self, path, encod="ISO-8859-1"):
dirs = glob.glob(os.path.join(path,'*'));
class_names = []
class_idx = []
cid = -1
corpus = []
for _dir in dirs:
cid+= 1
class_names.append(os.path.basename(_dir))
arqs = glob.glob(os.path.join(_dir,'*'))
for arq in arqs:
with codecs.open(arq, "r", encod) as myfile:
data=myfile.read().replace('\n', '')
corpus.append(data)
class_idx.append(cid)
result = {'corpus':corpus, 'class_index': class_idx, 'class_names':class_names}
return result
def from_files_2(self, path, encod="UTF-8"):
corpus = []
for arq in glob.iglob(path):
with codecs.open(arq, "r", encod) as myfile:
corpus.append(myfile.read().replace('\n',''))
return corpus
def from_text_line_by_line(self, arq):
doc = []
for line in open(arq):
doc.append(line)
return doc
def _str_to_list(self, s):
_s = re.split(',|{|}',s)
return [ x for x in _s if len(x) > 0]
def _str_to_date(self, s):
pass
def _convert(self, x, i, attr_list):
if attr_list[i][1] == self.attr_numeric[1]:
return float(x)
elif attr_list[i][1] == self.attr_numeric[2]:
return int(x)
elif attr_list[i][1] == self.attr_string[0]:
return x.replace("'","").replace('\'',"").replace('\"',"")
else:
return x.replace("'","").replace('\'',"").replace('\"',"")
def from_arff(self, arq, delimiter=','):
relation_name = ''
attr_count = 0
attr_list = []
data = []
self.attr_numeric = ['numeric', 'real', 'integer']
self.attr_string = ['string']
self.attr_date = ['date']
read_data = False
for line in open(arq):
line = line.lower().strip()
if line.startswith('#'): continue
if read_data:
vdata = line.split(delimiter)
data.append([ self._convert(x,i,attr_list) for i,x in enumerate(vdata) ])
elif not line.startswith('#'):
if line.startswith('@relation'):
relation_name = line.split()[1]
elif line.startswith('@attribute'):
attr_count += 1
attr = line.split()
attr_type = attr[2]
if attr_type in self.attr_numeric or attr_type in self.attr_string:
attr_list.append((attr[1], attr[2]))
elif attr_type in self.attr_date:
attr_list.append((attr[1], self._str_to_date(attr[2])))
else:
attr_list.append((attr[1], self._str_to_list(''.join(attr[2:]))))
elif line.startswith('@data'):
read_data = True
continue
d = dict()
d['attributes'] = attr_list
d['data'] = data
d['relation'] = relation_name
return d
def from_sparse_arff(self,arq, delimiter=','):
pass
class ConfigLabels:
def __init__(self, unlabelled_idx=-1, list_n_labels=[10,20,30,40,50]):
self.unlabelled_idx = unlabelled_idx
self.list_n_labels = list_n_labels
def pick_n_labelled(self, y, n_labelled_per_class):
class_idx = set(y)
labelled_idx = []
for c in class_idx:
r=np.isin(y, c)
labelled_idx = np.concatenate((labelled_idx, np.random.choice(np.where(r)[0], n_labelled_per_class)))
return labelled_idx.astype(int)
# colocar o valor self.unlabelled_idx nos exemplos não rotulados de y
def config_labels(self, y,labelled):
unlabelled = []
for i in range(len(y)):
if i not in labelled:
y[i] = self.unlabelled_idx
unlabelled.append(i)
return unlabelled
# return a dictionary key=<number of labels>, value is a list: [vector
# with unlabels and labels, vector only with unlabels]
def select_labelled_index(self, y, n_labels=[10,20,30,40,50]):
dict_y = {}
#pega ni documentos rotulados por classe
for ni in n_labels:
dict_y[ni] = [np.array(y), None]
nl = self.pick_n_labelled(y,ni)
unl = self.config_labels(dict_y[ni][0],nl)
dict_y[ni][1] = unl
return dict_y
def fit(self, y):
dict_y = self.select_labelled_index(y, n_labels=self.list_n_labels)
self.unlabelled_idx = {k:dict_y[k][1] for k in dict_y}
self.semi_labels = { k:dict_y[k][0] for k in dict_y}
return self
class RandMatrices:
def create_rand_maps(self, D, W, K):
A = self.create_rand_matrix_A(D, K)
B = self.create_rand_matrix_B(W, K)
Amap = dict()
Bmap = dict()
for j, d_j in enumerate(D):
Amap[d_j] = A[j]
for i, w_i in enumerate(W):
Bmap[w_i] = B[i]
return Amap, Bmap
def oi(self):
print('oi')
def create_rand_matrices(self, D, W, K):
return (self.create_rand_matrix_A(D, K), self.create_rand_matrix_B(W, K))
def create_rand_matrix_B(self, W, K):
N = len(W) # number of words
return np.random.dirichlet(np.ones(N), K).transpose() # B (N x K) matrix
def create_rand_matrix_A(self, D, K):
M = len(D) # number of documents
return np.random.dirichlet(np.ones(K), M) # A (M x K) matrix
def create_ones_matrix_A(self, D, K):
M = len(D) # number of documents
return np.ones(shape=(M,K))
def create_label_init_matrix_B(self, M, D, y, K, beta=0.0, unlabelled_idx=-1):
ndocs,nwords = M.shape
B = np.full((nwords, K),beta)
count={}
for word in range(nwords): count[word] = defaultdict(int)
rows,cols = M.nonzero()
for row,col in zip(rows,cols):
label = y[row]
if label != unlabelled_idx:
count[col][y[row]] += M[row,col]
count[col][-1] += M[row,col]
for word in range(nwords):
for cls in count[word]:
if cls != -1: B[word][cls] = (beta + count[word][cls])/(beta + count[word][-1])
return B
def create_label_init_matrices(self, X, D, W, K, y, beta=0.0, unlabelled_idx=-1):
return (self.create_rand_matrix_A(D, K), self.create_label_init_matrix_B(X, D, y, K, beta, unlabelled_idx))
def create_fromB_matrix_A(self, X, D, B):
K = len(B[0])
M = len(D) # number of documents
A = np.zeros(shape=(M,K))
for d_j in D:
for w_i, f_ji in zip(X.indices[X.indptr[d_j]:X.indptr[d_j+1]],
X.data[X.indptr[d_j]:X.indptr[d_j+1]]):
A[d_j] += f_ji * B[w_i]
return A
def create_fromA_matrix_B(self, A):
K = len(A[0])
N = self.G.b_len() # number of words
B = np.zeros(shape=(N,K))
for w_i in self.G.b_vertices():
for d_j, f_ji in self.G.w_b_neig(w_i):
B[w_i] += f_ji * A[d_j]
return self.normalizebycolumn(B)
#
#l = Loader()
##d = l.from_arff('datasets/SyskillWebert.arff')
#d = l.from_files('/exp/datasets/docs_rotulados/SyskillWebert-Parsed')
#
#import preprocessor
#from sklearn.feature_extraction.text import CountVectorizer
#from sklearn.datasets import fetch_20newsgroups
#from sklearn.feature_extraction.text import TfidfTransformer
#from sklearn.naive_bayes import MultinomialNB
#from sklearn.linear_model import SGDClassifier
#from sklearn.pipeline import Pipeline
#import numpy as np
#from sklearn import metrics
#from sklearn.model_selection import GridSearchCV
#
##text_clf = Pipeline([('vect', CountVectorizer()), ('tfidf', TfidfTransformer()),
## ('clf', SGDClassifier(loss='hinge', penalty='l2', alpha=1e-3, n_iter=5, random_state=42)),])
#
##parameters = {'vect__ngram_range': [(1, 1), (1, 2)], 'tfidf__use_idf': (True, False), 'clf__alpha': (1e-2, 1e-3),}
#
#text_clf = Pipeline([('text_preproc',preprocessor.Preprocessor()), ('vect', CountVectorizer()), ('tfidf', TfidfTransformer()),
# ('clf', MultinomialNB()),])
#
#parameters = {'vect__ngram_range': [(1, 1), (1, 2)], 'tfidf__use_idf': (True, False),}
#
#
#gs_clf = GridSearchCV(text_clf, parameters, cv=10, n_jobs=-1)
#gs_clf = gs_clf.fit(d['corpus'], d['class_index'])
#print(gs_clf.cv_results_)
#