#! /usr/bin/Python

from gensim.models.keyedvectors import KeyedVectors
from scipy import spatial
from numpy import linalg
import argparse
import sys

vector_file = sys.argv[1]
if len(sys.argv) != 6:
	print('arguments wrong!')
	print(len(sys.argv))
	exit()
else:
	words = [sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]]
	print(words)

	
wvs = KeyedVectors.load_word2vec_format(vector_file, binary=True)
print('WVs loaded.')
for w in words:
	if w not in wvs.vocab:
		print('out of vocab!')
		exit()

#print(wvs.most_similar(positive=[words[1], words[2]], negative=[words[0]], topn=3))

w1 = wvs[words[0]]
w2 = wvs[words[1]]
w3 = wvs[words[2]]
w4 = wvs[words[3]]

m1 = w1 / linalg.norm(w1)
m2 = w2 / linalg.norm(w2)
m3 = w3 / linalg.norm(w3)
m4 = w4 / linalg.norm(w4)

diff1 = w1 - w2
diff2 = w3 - w4
miff1 = m1 - m2
miff2 = m3 - m4

print('-------Word Space---------')
print('to word-4: ', 1-spatial.distance.cosine(m2+m3-m1, m4))
print('to word-3: ', 1-spatial.distance.cosine(m1+m4-m2, m3))
print('to word-2: ', 1-spatial.distance.cosine(m4+m1-m3, m2))
print('to word-1: ', 1-spatial.distance.cosine(m2+m3-m4, m1))
print('------Analogy Space-------')
print('     cosine: ', 1-spatial.distance.cosine(diff1, diff2))
print('  Euclidean: ', 1-linalg.norm(diff1-diff2)/(linalg.norm(diff1)+linalg.norm(diff2)))
print('   M-cosine: ', 1-spatial.distance.cosine(miff1, miff2))
print('M-Euclidean: ', 1-linalg.norm(miff1-miff2)/(linalg.norm(miff1)+linalg.norm(miff2)))



[((19, 6, 19, 65), 'gensim.models.keyedvectors.KeyedVectors.load_word2vec_format', 'KeyedVectors.load_word2vec_format', (), '', False, 'from gensim.models.keyedvectors import KeyedVectors\n'), ((33, 10, 33, 25), 'numpy.linalg.norm', 'linalg.norm', ({(33, 22, 33, 24): 'w1'}, {}), '(w1)', False, 'from numpy import linalg\n'), ((34, 10, 34, 25), 'numpy.linalg.norm', 'linalg.norm', ({(34, 22, 34, 24): 'w2'}, {}), '(w2)', False, 'from numpy import linalg\n'), ((35, 10, 35, 25), 'numpy.linalg.norm', 'linalg.norm', ({(35, 22, 35, 24): 'w3'}, {}), '(w3)', False, 'from numpy import linalg\n'), ((36, 10, 36, 25), 'numpy.linalg.norm', 'linalg.norm', ({(36, 22, 36, 24): 'w4'}, {}), '(w4)', False, 'from numpy import linalg\n'), ((44, 23, 44, 60), 'scipy.spatial.distance.cosine', 'spatial.distance.cosine', ({(44, 47, 44, 55): '(m2 + m3 - m1)', (44, 57, 44, 59): 'm4'}, {}), '(m2 + m3 - m1, m4)', False, 'from scipy import spatial\n'), ((45, 23, 45, 60), 'scipy.spatial.distance.cosine', 'spatial.distance.cosine', ({(45, 47, 45, 55): '(m1 + m4 - m2)', (45, 57, 45, 59): 'm3'}, {}), '(m1 + m4 - m2, m3)', False, 'from scipy import spatial\n'), ((46, 23, 46, 60), 'scipy.spatial.distance.cosine', 'spatial.distance.cosine', ({(46, 47, 46, 55): '(m4 + m1 - m3)', (46, 57, 46, 59): 'm2'}, {}), '(m4 + m1 - m3, m2)', False, 'from scipy import spatial\n'), ((47, 23, 47, 60), 'scipy.spatial.distance.cosine', 'spatial.distance.cosine', ({(47, 47, 47, 55): '(m2 + m3 - m4)', (47, 57, 47, 59): 'm1'}, {}), '(m2 + m3 - m4, m1)', False, 'from scipy import spatial\n'), ((49, 25, 49, 62), 'scipy.spatial.distance.cosine', 'spatial.distance.cosine', ({(49, 49, 49, 54): 'diff1', (49, 56, 49, 61): 'diff2'}, {}), '(diff1, diff2)', False, 'from scipy import spatial\n'), ((51, 25, 51, 62), 'scipy.spatial.distance.cosine', 'spatial.distance.cosine', ({(51, 49, 51, 54): 'miff1', (51, 56, 51, 61): 'miff2'}, {}), '(miff1, miff2)', False, 'from scipy import spatial\n'), ((50, 25, 50, 49), 'numpy.linalg.norm', 'linalg.norm', ({(50, 37, 50, 48): '(diff1 - diff2)'}, {}), '(diff1 - diff2)', False, 'from numpy import linalg\n'), ((52, 25, 52, 49), 'numpy.linalg.norm', 'linalg.norm', ({(52, 37, 52, 48): '(miff1 - miff2)'}, {}), '(miff1 - miff2)', False, 'from numpy import linalg\n'), ((50, 51, 50, 69), 'numpy.linalg.norm', 'linalg.norm', ({(50, 63, 50, 68): 'diff1'}, {}), '(diff1)', False, 'from numpy import linalg\n'), ((50, 70, 50, 88), 'numpy.linalg.norm', 'linalg.norm', ({(50, 82, 50, 87): 'diff2'}, {}), '(diff2)', False, 'from numpy import linalg\n'), ((52, 51, 52, 69), 'numpy.linalg.norm', 'linalg.norm', ({(52, 63, 52, 68): 'miff1'}, {}), '(miff1)', False, 'from numpy import linalg\n'), ((52, 70, 52, 88), 'numpy.linalg.norm', 'linalg.norm', ({(52, 82, 52, 87): 'miff2'}, {}), '(miff2)', False, 'from numpy import linalg\n')]