Last active
October 3, 2023 05:27
-
-
Save robert-mcdermott/67cf2623237989bc2315d35a108246ef to your computer and use it in GitHub Desktop.
sentence-similarity.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sentence_transformers import SentenceTransformer, util | |
model = SentenceTransformer('all-MiniLM-L6-v2') | |
# Two lists of sentences | |
sentences1 = ['The cat sits outside', | |
'A man is playing guitar', | |
'The new movie is awesome', | |
'Jim can run very fast', | |
'My goldfish is hungry'] | |
sentences2 = ['The dog plays in the garden', | |
'A woman watches TV', | |
'The new movie is so great', | |
'James is the fastest runner', | |
'Pluto is a planet!'] | |
#Compute embedding for both lists | |
embeddings1 = model.encode(sentences1, convert_to_tensor=True) | |
embeddings2 = model.encode(sentences2, convert_to_tensor=True) | |
#Compute cosine-similarities | |
cosine_scores = util.cos_sim(embeddings1, embeddings2) | |
#Output the pairs with their score | |
for i in range(len(sentences1)): | |
print("{:<28} {:<28} Score: {:.4f}".format(sentences1[i], sentences2[i], cosine_scores[i][i])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment