Skip to content

Instantly share code, notes, and snippets.

@ahmetalsan
Last active May 29, 2024 23:44
Show Gist options
  • Save ahmetalsan/06596e3f2ea3182e185a to your computer and use it in GitHub Desktop.
Save ahmetalsan/06596e3f2ea3182e185a to your computer and use it in GitHub Desktop.
python cosine similarity algorithm between two strings
import re
import math
from collections import Counter
def get_cosine(vec1, vec2):
intersection = set(vec1.keys()) & set(vec2.keys())
numerator = sum([vec1[x] * vec2[x] for x in intersection])
sum1 = sum([vec1[x]**2 for x in vec1.keys()])
sum2 = sum([vec2[x]**2 for x in vec2.keys()])
denominator = math.sqrt(sum1) * math.sqrt(sum2)
if not denominator:
return 0.0
else:
return float(numerator) / denominator
def text_to_vector(text):
word = re.compile(r'\w+')
words = word.findall(text)
return Counter(words)
def get_result(content_a, content_b):
text1 = content_a
text2 = content_b
vector1 = text_to_vector(text1)
vector2 = text_to_vector(text2)
cosine_result = get_cosine(vector1, vector2)
return cosine_result
print get_result('I love github', 'Who love github') #0.65565
@moinabyssinia
Copy link

thank you, it does the job well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment