Calculating Cosine Similarity
Cosine Similarity is a metric that assesses how similar two non-zero vectors are to one another in an n-dimensional space. It is frequently used in text analysis to compare the vector representations of two documents to ascertain how similar they are.
The formula for calculating cosine similarity between two vectors ‘A’ and ‘B’ is as follows:
Cosine_similarity(A,B) = (A . B) / (||A|| * ||B||)
Where,
‘A’ and ‘B’ = vector representations of documents or data points
(A.B) = dot product of vectors A and B.
‘||A||’ and ‘||B||’ = magnitude of vectors A and B.
Cosine similarity returns a value between -1 and 1 where:
- 1 indicates perfect similarity (vectors in same direction).
- -1 indicates prefect dissimilarity( Vectors in opposite direction).
- 0 indicates no similarity.
Now, let’s implement cosine similarity in the model.
Python3
# Function to calculate cosine similarity between two document vectors def calc_cosine_similarity(vector1, vector2): return cosine_similarity([vector1, vector2]) |
In the above code, it contains a function that calculates the cosine similarity between two document vectors. The cosine similarity score, that measures the degree of similarity between two texts represented by the two vectors, is returned when the function is called with the two vectors as input.
Plagiarism Detection using Python
In this article, we are going to learn how to check plagiarism using Python.
Plagiarism: Plagiarism basically refers to cheating. It means stealing someone’s else work, ideas, or information from the resources without providing the necessary credits to the author. For example, copying text from different resources from word to word without mentioning any quotation marks.
Table of Content
- What is Plagiarism detection?
- Importing Libraries
- Listing and Reading Files
- TF-IDF Vectorization
- Calculating Cosine Similarity
- Creating Document-vector Pairs
- Checking Plagiarism
- Word Cloud Visualization
- Conclusion