# Application of cosine similarity to detect plagiarism

Can anyone tell me how using cosine similarity to see the correlation between two documents actually shows you if someone is plagiarising the other? I understand how cosine similarity works but don't understand how using this shows how closely related they are when the cosine deals with vectors and angles. How can this relate to a document?

• You'll have to explain Cosine Similarity to us. Inner product $\sum_i x_iy_i$ has interpretations other than as an un-normalized angle between vectors, for example correlation. Since it is maximized (after normalization) when $x_i = y_i$, in some sense it measures how close the two "statistics" are from being equal. – Yuval Filmus Oct 2 '14 at 12:26
• In the context of document and information retrieval. Cosine Similarity is a well-known and well-defined concept that doesn't need to be explained to people with expertise or enough knowledge in the area. – InformedA Nov 2 '14 at 4:58

## 2 Answers

Usually this would be used in conjunction with a bag of words model. You need to convert each document to a vector where the length of the vector is the number of words in the vocabulary. The entries in the vector correspond to the number of occurrences of each vocabulary word in the document. You can apply cosine similarity to these document vectors.

You should consider using tf-idf weighting to make sure that a small set of very common words doesn't end up dominating the distance computation. https://en.wikipedia.org/wiki/Tf%E2%80%93idf

I do NOT believe people use Cosine Similarity to detect plagiarism.

In information retrieval, using weighted TF-IDF and cosine similarity is a very common technique. It allows the system to quickly retrieve documents similar to a search query. This often works well, when the searched corpus is quite different.

However, in the case of detecting plagiarism. The searched corpus is very homogeneous from a bag of words perspective. This is because, say for an essay submission, ALL submitted essays cover the same topic so they have very similar bag of words. Also, since the same topic is covered in those essays, it is with almost certainty that those words with the highest weighted IDF such as proper nouns and uncommon words appear in most of the bags. Bag of words model is a very bad idea for this task. I would say if you run Cosine Similarity on TF-IDF vectors of the documents, there is a good chance that you find the entire class has similar essay.

I know for a fact that in order to check similarity, a much better algorithm exists. It is call 'Edit Distance', Levenshtein distance. This takes into account the order of the words in each documents. It runs slower in $O(n^2)$, but there are many ways to remedy this, and is often not a problem. This situation is different from the information retrieval situation.

For computer programs, the check can be done by turning the program into the symbolic representation. This is usually in form of a parsed tree. Then one can generate in-order, pre-order, post-order of the tree (or a set of major subtrees of this parsed tree such as of the main loop, main method etc..) and run 'Edit Distance' to compare similarity.

Remember that a TF-IDF approach does not take into account any structural property of documents unlike using parsed tree and edit distance as I mentioned above.

• It may be practical to use cosine similarity to detect plagiarism if the coordinates of the vectors are not individual words, but, say, trigrams. – jkff Dec 2 '14 at 6:23