Python | Measure similarity between two sentences using cosine similarity
Cosine similarity is a measure of similarity between two non-zero vectors of an inner product space that measures the cosine of the angle between them.
Similarity = (A.B) / (||A||.||B||) where A and B are vectors.
Cosine similarity and nltk toolkit module are used in this program. To execute this program nltk must be installed in your system. In order to install nltk module follow the steps below –
1. Open terminal(Linux). 2. sudo pip3 install nltk 3. python3 4. import nltk 5. nltk.download(‘all’)
Functions used –
nltk.tokenize: It is used for tokenization. Tokenization is the process by which big quantity of text is divided into smaller parts called tokens.
word_tokenize(X)split the given sentence X into words and return list.nltk.corpus: In this program, it is used to get a list of stopwords. A stop word is a commonly used word (such as “the”, “a”, “an”, “in”).
# Program to measure similarity between # two sentences using cosine similarity. from nltk.corpus import stopwords from nltk.tokenize import word_tokenize # X = input("Enter first string: ").lower() # Y = input("Enter second string: ").lower() X ="I love horror movies"Y ="Lights out is a horror movie" # tokenization X_list = word_tokenize(X) Y_list = word_tokenize(Y) # sw contains the list of stopwords sw = stopwords.words('english') l1 =[];l2 =[] # remove stop words from string X_set = {w for w in X_list if not w in sw} Y_set = {w for w in Y_list if not w in sw} # form a set containing keywords of both strings rvector = X_set.union(Y_set) for w in rvector: if w in X_set: l1.append(1) # create a vector else: l1.append(0) if w in Y_set: l2.append(1) else: l2.append(0) c = 0 # cosine formula for i in range(len(rvector)): c+= l1[i]*l2[i] cosine = c / float((sum(l1)*sum(l2))**0.5) print("similarity: ", cosine) |
Output:
similarity: 0.6666666666666666
Recommended Posts:
- Python | Percentage similarity of lists
- Python | Word Similarity using spaCy
- Python | Similarity metrics of strings
- Python | Test list element similarity
- NLP | WuPalmer - WordNet Similarity
- NLP | Leacock Chordorow (LCH) and Path similarity for Synset
- scipy stats.cosine() | Python
- Statistical Functions in Python | Set 2 ( Measure of Spread)
- Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)
- ML | V-Measure for Evaluating Clustering Performance
- Degree Centrality (Centrality Measure)
- Katz Centrality (Centrality Measure)
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- Python | Merge Python key values to list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



