VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/python-measure-similarity-between-two-sentences-using-cosine-similarity/

⇱ Python | Measure similarity between two sentences using cosine similarity - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python | Measure similarity between two sentences using cosine similarity

Last Updated : 11 Jan, 2023
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”).
Below is the Python implementation - Output:
similarity: 0.2886751345948129
Comment