import glove_util as gut 
import numpy as np 
from sklearn.decomposition import TruncatedSVD
import json

with open('freq_count_pred.json') as f:
	freq_count_pred = json.load(f)

def get_pc(sentences):
	svd = TruncatedSVD(n_components=1, n_iter=7, random_state=0)
	svd.fit(sentences)
	return svd.components_


def weighted_avg(predicate,a,dim):

	predicate = predicate.lower().strip().split()

	if len(predicate) == 1:
		return gut.glove(predicate[0],dim)
	else:
		support = np.zeros(dim)
		for word in predicate:
			vector = gut.glove(word,dim)
			if len(vector) == 0:
				vector = np.zeros(300)
			support += (a/(a+freq_count_pred[word]))*vector

		return support




with open('relationships.json') as f:
	relationships = json.load(f)

predicate_embedding = {}

sentences = []
i = 0
for image in relationships:
	i+=1
	if i%1000 == 0:
		print (i)

	for relation in image['relationships']:
		w_avg = weighted_avg(relation['predicate'],0.001,300)
		sentences.append(w_avg)
		predicate_embedding[relation['relationship_id']] = w_avg 

pc = get_pc(np.array(sentences))[0]
projection_space = np.outer(pc,pc)

i = 0
for image in relationships:
	i+=1
	if i%1000 == 0:
		print (i)

	for relation in image['relationships']:
		predicate_embedding[relation['relationship_id']] = predicate_embedding[relation['relationship_id']]  - np.matmul(projection_space,predicate_embedding[relation['relationship_id']])
		

with open('predicate_embedding_300.json','w') as f:
	json.dump(predicate_embedding,f)

[((52, 19, 52, 34), 'numpy.outer', 'np.outer', ({(52, 28, 52, 30): 'pc', (52, 31, 52, 33): 'pc'}, {}), '(pc, pc)', True, 'import numpy as np\n'), ((7, 19, 7, 31), 'json.load', 'json.load', ({(7, 29, 7, 30): 'f'}, {}), '(f)', False, 'import json\n'), ((10, 7, 10, 61), 'sklearn.decomposition.TruncatedSVD', 'TruncatedSVD', (), '', False, 'from sklearn.decomposition import TruncatedSVD\n'), ((35, 17, 35, 29), 'json.load', 'json.load', ({(35, 27, 35, 28): 'f'}, {}), '(f)', False, 'import json\n'), ((65, 1, 65, 33), 'json.dump', 'json.dump', ({(65, 11, 65, 30): 'predicate_embedding', (65, 31, 65, 32): 'f'}, {}), '(predicate_embedding, f)', False, 'import json\n'), ((20, 9, 20, 36), 'glove_util.glove', 'gut.glove', ({(20, 19, 20, 31): 'predicate[0]', (20, 32, 20, 35): 'dim'}, {}), '(predicate[0], dim)', True, 'import glove_util as gut\n'), ((22, 12, 22, 25), 'numpy.zeros', 'np.zeros', ({(22, 21, 22, 24): 'dim'}, {}), '(dim)', True, 'import numpy as np\n'), ((51, 12, 51, 31), 'numpy.array', 'np.array', ({(51, 21, 51, 30): 'sentences'}, {}), '(sentences)', True, 'import numpy as np\n'), ((24, 12, 24, 31), 'glove_util.glove', 'gut.glove', ({(24, 22, 24, 26): 'word', (24, 27, 24, 30): 'dim'}, {}), '(word, dim)', True, 'import glove_util as gut\n'), ((61, 105, 61, 181), 'numpy.matmul', 'np.matmul', ({(61, 115, 61, 131): 'projection_space', (61, 132, 61, 180): "predicate_embedding[relation['relationship_id']]"}, {}), "(projection_space, predicate_embedding[relation['relationship_id']])", True, 'import numpy as np\n'), ((26, 13, 26, 26), 'numpy.zeros', 'np.zeros', ({(26, 22, 26, 25): '300'}, {}), '(300)', True, 'import numpy as np\n')]