-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwordgraph.py
More file actions
141 lines (102 loc) · 3.2 KB
/
wordgraph.py
File metadata and controls
141 lines (102 loc) · 3.2 KB
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
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 3 23:59:23 2018
"""
from time import time
from ppsing import sin_ant
import numpy as np
from sklearn.metrics.pairwise import pairwise_kernels
import pickle
import scipy.spatial.distance as sp
def T_syn_ant(vocab):
""" Funcion que calcula las matrices de antonimos y sinonimos.
Entradas:
vocab : Vocabulario del wordmebedding implementado
Salidas
T_syn: matriz de sinonimos, T_syn (ij)= 1 si wi y wj son sinonimos, 0 otro caso
T_ant: matriz de antonimos, T_ant (ij)= -1 si wi y wj son antonimos, 0 otro caso
"""
n=len(vocab)
# inicializa arrays con 0
T_syn = np.array([[0.0]*n]*n)
T_ant = np.array([[0.0]*n]*n)
for i in range(n):
#obtener sinonimos y antonimos
syn,ant = sin_ant(vocab[i])
for j in syn:
try: T_syn[i][vocab.index(j)]= 1.0
except: continue
for j in ant:
try: T_ant[i][vocab.index(j)]= -1.0
except: continue
return T_syn,T_ant
def kernel_gaussean (X,Y,gamma=1,epsilon=0.0):
dif=X-Y
out=np.exp(-np.dot(dif,dif)*gamma)
if (out<epsilon):
out=0.0
return out
def kernel_mahalanobis (X,Y,inv_cov):
mah = sp.mahalanobis(X,Y,inv_cov)
return mah
#funcion de metricas
KERNELS={
'gaus_e':kernel_gaussean,
'gaus':'rbf',
'cosine':'cosine',
'mah':kernel_mahalanobis }
def W_init(X,kernel='gaus',**kwds ):
"""
hay varias formas para implementar el calculode W.
uno es con cosine similarity.
otro es distance mahalanobis, revizar
"""
try:
kernel_func=KERNELS[kernel]
except:
print('metrica no correspondiente')
return 1
matrix=pairwise_kernels(X,metric=kernel_func,filter_params=True,n_jobs=-1,**kwds)
return matrix
def W(W,T_ant,T_syn,gama,b_ant,b_syn):
W_final = gama*W + b_ant*T_ant*W + b_syn*T_syn*W
return W_final
def cont_neg(W):
"""
Esta funcion cuenta enlaces negativos de la matriz W
"""
cont=0
for i in range(len(W)):
for j in range(i+1,len(W)):
if(W[i][j]<0.0):
print(i,'-',j,' y ',j,'-',i)
cont +=1
return cont
def cont_pos(W):
"""
Esta funcion cuenta enlaces positivos de la matriz W
"""
cont=0
for i in range(len(W)):
for j in range(i+1,len(W)):
if(W[i][j] > 0.0):
print(i,'-',j,' y ',j,'-',i)
cont +=1
return cont
# de aca no se ocupan
def time_f(W,*args):
tiempo_inicial = time()
W_final,W,T_syn,T_ant=W(*args)
tiempo_final = time()
tiempo_ejecucion = tiempo_final - tiempo_inicial
print('Se demoro ',tiempo_ejecucion/60,' minutos')
return W_final,W,T_syn,T_ant
def save_matrix(X,name):
outfile=open(name,'wb')
pickle.dump(X,outfile)
outfile.close()
def load_matrix(name):
infile=open(name,'rb')
X=pickle.load(infile)
infile.close()
return X