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)

[((1089, 1105), 'numpy.outer', 'np.outer', (['pc', 'pc'], {}), '(pc, pc)\n', (1097, 1105), True, 'import numpy as np\n'), ((165, 177), 'json.load', 'json.load', (['f'], {}), '(f)\n', (174, 177), False, 'import json\n'), ((209, 263), 'sklearn.decomposition.TruncatedSVD', 'TruncatedSVD', ([], {'n_components': '(1)', 'n_iter': '(7)', 'random_state': '(0)'}), '(n_components=1, n_iter=7, random_state=0)\n', (221, 263), False, 'from sklearn.decomposition import TruncatedSVD\n'), ((725, 737), 'json.load', 'json.load', (['f'], {}), '(f)\n', (734, 737), False, 'import json\n'), ((1456, 1489), 'json.dump', 'json.dump', (['predicate_embedding', 'f'], {}), '(predicate_embedding, f)\n', (1465, 1489), False, 'import json\n'), ((428, 456), 'glove_util.glove', 'gut.glove', (['predicate[0]', 'dim'], {}), '(predicate[0], dim)\n', (437, 456), True, 'import glove_util as gut\n'), ((475, 488), 'numpy.zeros', 'np.zeros', (['dim'], {}), '(dim)\n', (483, 488), True, 'import numpy as np\n'), ((1046, 1065), 'numpy.array', 'np.array', (['sentences'], {}), '(sentences)\n', (1054, 1065), True, 'import numpy as np\n'), ((526, 546), 'glove_util.glove', 'gut.glove', (['word', 'dim'], {}), '(word, dim)\n', (535, 546), True, 'import glove_util as gut\n'), ((1322, 1399), 'numpy.matmul', 'np.matmul', (['projection_space', "predicate_embedding[relation['relationship_id']]"], {}), "(projection_space, predicate_embedding[relation['relationship_id']])\n", (1331, 1399), True, 'import numpy as np\n'), ((583, 596), 'numpy.zeros', 'np.zeros', (['(300)'], {}), '(300)\n', (591, 596), True, 'import numpy as np\n')]