Skip to content

Instantly share code, notes, and snippets.

@ashokc
Created October 28, 2019 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashokc/ba811c0d9e3457d284df99bb78e455c9 to your computer and use it in GitHub Desktop.
Save ashokc/ba811c0d9e3457d284df99bb78e455c9 to your computer and use it in GitHub Desktop.
import sys
import numpy as np
from bert_serving.client import BertClient
bc = BertClient()
filename = sys.argv[1]
f = open(filename, 'r')
s, sv = [], []
for line in f:
sentence = line.strip().lower()
print (sentence)
vector = bc.encode([sentence])[0]
s.append(sentence)
sv.append(vector / np.linalg.norm(vector))
print ('0 & 1 \t\t 1 & 2 \t\t 1 & 2')
print ('Cosine Similarity: ', round(np.dot(sv[0], sv[1]),3), '\t\t', round(np.dot(sv[0], sv[2]),3), '\t\t', round(np.dot(sv[1], sv[2]),3))
print ('Inv. Manhattan Distance:', round(1.0/np.linalg.norm((sv[0] - sv[1]),1),3), '\t\t', round(1.0/np.linalg.norm((sv[0] - sv[2]),1),3), '\t\t', round(1.0/np.linalg.norm((sv[1] - sv[2]),1),3))
print ('Inv. Euclidean Distance:', round(1/np.linalg.norm((sv[0] - sv[1]),2),3), '\t\t', round(1.0/np.linalg.norm((sv[0] - sv[2]),2),3), '\t\t', round(1.0/np.linalg.norm((sv[1] - sv[2]),2),3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment