from gensim.models import Word2Vec
import argparse
from gensim.models.keyedvectors import KeyedVectors

#return a similarity score

class entity2rel(object):

	def __init__(self, embedding_file, binary = True):

		self.embedding_file = embedding_file
		self.binary = binary

		self._load_embedding()

	def _load_embedding(self):

		self.embedding = KeyedVectors.load_word2vec_format(self.embedding_file, binary=self.binary) #a set of embeddings as a generator


	def similarity(self, uri1, uri2):

		return self.embedding.similarity(uri1,uri2)


	@staticmethod
	def parse_args():

		parser = argparse.ArgumentParser(description="Measure entity relatedness.")

		parser.add_argument('--embedding', help='File with embeddings')

		parser.add_argument('--binary', help='Whether the embeddings are stored in binary format')


if __name__ == '__main__':

	#test

	uri1 = "http://dbpedia.org/resource/Pulp_Fiction"

	uri2 = "http://dbpedia.org/resource/Jackie_Brown_(film)"

	uri3 = "http://dbpedia.org/resource/Romeo_and_Juliet_(1996_movie)"

	embedding = "../emb/movielens_1m_no_overwrite/feedback/num500_p1_q4_l10_d500.emd"

	args = entity2rel.parse_args()

	rel = entity2rel(embedding)

	print('\n')
	print("Similarity between Pulp Fiction and Jackie Brown is:\n")
	s = rel.similarity(uri1, uri2)
	print(s)
	print('\n')

	print("Similarity between Pulp Fiction and Romeo and Juliet is:\n")
	s = rel.similarity(uri1, uri3)
	print(s)
	print('\n')

	print("Most similar entities to Pulp Fiction are:\n")

	for i in rel.embedding.most_similar(positive=[uri1]):
		print(i)
@@ -1,24 +1,62 @@
+from __future__ import print_function%0A
 from gensim.models impor
@@ -133,16 +133,33 @@
 dVectors
+%0Aimport itertools
 %0A%0A#retur
@@ -162,16 +162,23 @@
 eturn a 
+set of 
 similari
@@ -185,16 +185,60 @@
 ty score
+s from a set of property-specific embeddings
 %0A%0Aclass 
@@ -281,24 +281,8 @@
 elf,
- embedding_file,
  bin
@@ -291,24 +291,47 @@
 y = True):%0A%0A
+%09%09self.binary = binary%0A
 %09%09self.embed
@@ -343,63 +343,167 @@
 file
+s
  = 
-embedding_file%0A%09%09self.binary = binary%0A%0A%09%09self._load_
+%5B%5D%0A%0A%09def add_embedding(self, embedding_file):%0A%0A%09%09#generator of embeddings, don't have to store everything in memory together%0A%09%09self.embedding_files.append(
 embe
@@ -511,17 +511,21 @@
 ding
-(
+_file
 )%0A%0A%09def 
 _loa
@@ -524,48 +524,110 @@
 def 
-_load_embedding(self):%0A%0A%09%09self.embedding
+relatedness_scores(self, uri1, uri2):%0A%0A%09%09scores = %5B%5D%0A%0A%09%09for embedding in self.embedding_files:%0A%0A%09%09%09emb
  = K
@@ -659,21 +659,16 @@
 _format(
-self.
 embeddin
@@ -668,21 +668,16 @@
 mbedding
-_file
 , binary
@@ -693,52 +693,40 @@
 ary)
- #a set of embeddings as a generator%0A%0A%0A%09def 
+%0A%0A%09%09%09try:%0A%09%09%09%09scores.append(emb.
 simi
@@ -736,73 +736,91 @@
 ity(
-self, 
 uri1,
- 
 uri2)
-:
+)
 %0A%0A%09%09
+%09
-return self.embedding.similarity(uri1,uri2)
+except KeyError:%0A%09%09%09%09scores.append(0.)%0A%0A%09%09%09del emb%0A%0A%09%09return scores
 %0A%0A%0A%09
@@ -1324,15 +1324,13 @@
 ding
+1
  = %22
-../
 emb/
@@ -1397,67 +1397,207 @@
 %22%0A%0A%09
-args = entity2rel.parse_args()%0A%0A%09rel = entity2rel(embedding
+embedding2 = %22emb/movielens_1m_no_overwrite/dbo:director/num500_p1_q4_l10_d500.emd%22%0A%0A%09args = entity2rel.parse_args()%0A%0A%09rel = entity2rel()%0A%0A%09rel.add_embedding(embedding1)%0A%09rel.add_embedding(embedding2
 )%0A%0A%09
@@ -1679,38 +1679,70 @@
 )%0A%09s
+cores
  = rel.
-similarity(uri1, uri2)%0A
+relatedness_scores(uri1, uri2)%0A%09for s in scores:%0A%09
 %09pri
@@ -1739,32 +1739,33 @@
 res:%0A%09%09print(s)%0A
+%09
 %09print('%5Cn')%0A%0A%09p
@@ -1837,172 +1837,80 @@
 )%0A%09s
+cores
  = rel.
-similarity(uri1, uri3)%0A%09print(s)%0A%09print('%5Cn')%0A%0A%09print(%22Most similar entities to Pulp Fiction are:%5Cn%22)%0A%0A%09for i in rel.embedding.most_similar(positive=%5Buri1%5D):
+relatedness_scores(uri1, uri3)%0A%0A%09for s in scores:%0A%09%09print(s)
 %0A%09%09p
@@ -1918,6 +1918,10 @@
 int(
-i)
+'%5Cn')%0A

add multiple embeddings