Skip to content

Instantly share code, notes, and snippets.

@arjunprakash027
Last active September 19, 2023 17:50
Show Gist options
  • Select an option

  • Save arjunprakash027/448ad323bccc479bf3992d2db1ef1089 to your computer and use it in GitHub Desktop.

Select an option

Save arjunprakash027/448ad323bccc479bf3992d2db1ef1089 to your computer and use it in GitHub Desktop.
Compare similarity between 2 sentences using cosine similarity
from gensim.parsing.preprocessing import STOPWORDS
import re
import numpy as np
from numpy.linalg import norm
def find_similarity(x,y):
#print(STOPWORDS)
token_rules = r'\s+|[,\.]'
x_token = re.split(token_rules,x)
y_token = re.split(token_rules,y)
print(x_token)
print(y_token)
x_set = {word for word in x_token if not word in STOPWORDS}
y_set = {word for word in y_token if not word in STOPWORDS}
print(x_set)
print(y_set)
rvector = x_set.union(y_set)
print(rvector)
l1 = []
l2 = []
for word in rvector:
if word in x_set: l1.append(1)
else: l1.append(0)
if word in y_set: l2.append(1)
else: l2.append(0)
vec1 = np.array(l1)
vec2 = np.array(l2)
cosine = np.dot(vec1,vec2)/(norm(vec1)*norm(vec2))
return (cosine)
print(find_similarity("I am a boy","I am a girl"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment