Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,943 questions

51,884 answers

573 users

How to measure similarity between two sentences using cosine similarity in Python

1 Answer

0 votes
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

sent1 = "The cat sat on the mat"
#sent2 = "The cat sat on the mat" # Cosine similarity: 1.0000000000000002
sent2 = "The dog sat on the rug"

# Convert sentences to TF‑IDF vectors
vectorizer = TfidfVectorizer()
tfidf = vectorizer.fit_transform([sent1, sent2])

# Compute cosine similarity
similarity = cosine_similarity(tfidf[0:1], tfidf[1:2])

print("Cosine similarity:", similarity[0][0])



'''
run:

Cosine similarity: 0.6029748160380571

'''

 



answered 2 hours ago by avibootz
...