-
Notifications
You must be signed in to change notification settings - Fork 2
/
backend.py
64 lines (58 loc) · 1.82 KB
/
backend.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
# backend worker and embedder
# ===== imports =====
from reader import PattyReader
from embedder import Embedder
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from scipy import spatial
# ===== definitions =====
def processPattyData(pattyPath='dbpedia-relation-paraphrases_json.txt',glovePath='glove.6B.50d.txt'):
patty = PattyReader(path=pattyPath)
glove = Embedder(path =glovePath)
patty.processData()
#keys = patty.patterns.keys()
mat = []
key = 0
for label,patterns in patty.patterns.iteritems():
for pattern in patterns:
#print(pattern)
v = glove.getVector(pattern)
v = np.append(v,key)
mat.append(v)
key = key +1
#mat = np.asarray(mat)
return mat, glove, patty
def pad(A, length):
if len(A) >= length:
return A
arr = np.zeros(length)
arr[:len(A)] = A
return arr
def padVectors(mat, length=None):
if length is None:
maxLength = max(len(vector) for vector in mat)
maxLength = maxLength - 1
else:
maxLength = length
newMat = []
for vector in mat:
if length is None:
v = pad(vector[:-1],maxLength)
v = np.append(v,vector[-1])
else:
v = pad(vector,maxLength)
newMat.append(v)
return newMat, maxLength
def calculateSimilarity(vects,mat):
#if vects.shape[0] == 1:
# vects=vects.reshape(1, -1)
#if mat.shape[0] == 1:
# mat=mat.reshape(1, -1)
#print('vects',vects.shape)
#print('mat',mat.shape)
return cosine_similarity(vects,mat)
# ===== main testing =====
if __name__ == "__main__":
mat, _, _ = processPattyData()
mat, maxLength = padVectors(mat)
#calculateSimilarity(mat,padVectors())