<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yash Garg on Medium]]></title>
        <description><![CDATA[Stories by Yash Garg on Medium]]></description>
        <link>https://medium.com/@yashagrawal0601?source=rss-dc61a575eed2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*zryEgH6vKwDT6-YW</url>
            <title>Stories by Yash Garg on Medium</title>
            <link>https://medium.com/@yashagrawal0601?source=rss-dc61a575eed2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 06:46:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yashagrawal0601/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building Plagiarism Checker Using FAISS]]></title>
            <link>https://medium.com/naukri-engineering/building-plagiarism-checker-using-faiss-8c933c8afe1e?source=rss-dc61a575eed2------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c933c8afe1e</guid>
            <category><![CDATA[plagiarism-checker]]></category>
            <category><![CDATA[faiss]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[tech]]></category>
            <dc:creator><![CDATA[Yash Garg]]></dc:creator>
            <pubDate>Sun, 02 Jul 2023 09:12:56 GMT</pubDate>
            <atom:updated>2023-07-02T09:12:56.156Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*axzCH2UazwyCwiuX" /><figcaption>Photo by <a href="https://unsplash.com/@supersnapper27">Super Snapper</a><strong> </strong>on <a href="https://unsplash.com/photos/zIwAchjDirM">Unsplash</a></figcaption></figure><p>In today’s world, data is a very crucial resource that should be managed efficiently.</p><p>Let me tell you a scenario in Naukri, we are having a platform <strong>Doselect</strong> where freelancers submits assessment questions and in return we pay them. So, why we are willing to pay them for the content that is already in our database or the content which is readily available on internet?</p><p>This is just one scenario, but in reality there are many. To deal with the above issue, we need a system that can efficiently check for duplicate content in the database. We call this system as “Plagiarism Checker”.</p><p>Now, the major concern is how we can build this system?</p><h3>How we can build?</h3><p>Let’s say we are having two documents “A” and “B”. Our first task is to check for the text similarity between these documents. On the basis of similarity, we can say how much these documents are relevant for us.</p><h4><strong>Text similarity</strong></h4><p>Text similarity is the process of comparing a piece of text with another and finding similarities between them. It’s basically about determining the degree of closeness of these text. This processing will take us to the field of NLP, where we are using different NLP approaches to detect the similarity.</p><h4><strong>How does this text similarity works?</strong></h4><p>To check for similarity in text, we need to filter/clean our data. Then, this filtered data needs to be converted to an machine-readable form with the help of various vector embedding. Further, these vector embedding are useful to calculate the similarity among documents.</p><h4>Pre-Processing data</h4><p>To clean the data, you can perform various steps:<br>1. Remove extra spaces<br>2. Add space before and after a special characters<br>3. Lowercase the content<br>4. Remove stop words<br>5. <strong>Lemmatization</strong> — Process of replacing a word with its <em>root</em> or <em>base word. Example — </em>“Best”, “Well”, “Better” can be lemmatized to word “well”.</p><h4>Algorithm to transform text to vector embedding</h4><p>There are various libraries that provides the ease to create vector embeddings in few lines of code. One of them is “Sentence Transformers”.</p><p><strong>Sentence Transformer<br>“</strong>Sentence-transformers” is an open source library that provides methods to compute vector embedding. This library has various pre-trained models. Further, these models can be trained and fine-tuned with your data. Also, there are some models (like — all-mpnet-base-v2, multi-qa-MiniLM-L6-cos-v1) that can be helpful while considering semantic search.<br>You can read more about them on the official <a href="https://www.sbert.net/docs/pretrained_models.html">website</a>.</p><h4><strong>Metrics for calculating similarity between vector embeddings</strong></h4><p>To find the Similarity scores we have multiple metrics that measure distances between vector embeddings, but for our case we are going to use cosine similarity.</p><p><strong>Cosine Similarity<br></strong>Cosine similarity is the measure of cosine of the angle between two vectors embeddings. If the vectors are very close to each other, then the angle between them is smaller which indicates higher similarity. Opposite to which, if angle is larger then, there is less similarity match among documents.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/0*LlfY1LATGh01gjrK.png" /></figure><h3>Implementing similarity search</h3><p>We can now pre-process data, form vector embeddings and find similarity scores using the above mentioned algorithms. A simple way to implement a similarity search is to compute the distance of a given vector with all the other vectors. Based on the score, we can say that the content is duplicate or highly similar to the one present in the database.</p><p>This approach is considered as the brute-force as the complexity of the search is linear. In this, we have to generate the score against every document in the database which degrades the performance of the system.</p><h4>FAISS(Facebook AI Similarity Search) — An efficient Approach</h4><p><a href="https://github.com/facebookresearch/faiss"><strong>FAISS</strong></a> is an open-source library which enables efficient similarity search and clustering of dense vectors. It is designed to work with large datasets of vectors, and it can be used to find the nearest neighbours of a given vector. It contains algorithms that search in sets of vectors of any size, up to ones that possibly do not fit in RAM. FAISS is written in C++ with complete wrappers for Python. It also supports GPU implementation for faster performance.</p><p>To install FAISS in python, you can use the following command:</p><pre># make sure that the order of installation is same<br>pip install faiss<br>pip install faiss-cpu</pre><p><strong>FAISS uses a variety of techniques to speed up similarity search, including:</strong></p><ul><li><strong>Indexing</strong>: FAISS creates an index of the vectors, which allows it to quickly find the nearest neighbours of a given vector.</li><li><strong>Quantization</strong>: FAISS can quantize the vectors, which reduces their size and makes them easier to index.</li><li><strong>Approximate nearest neighbours</strong>: FAISS can use approximate nearest neighbours algorithms, which can find the nearest neighbours of a given vector more quickly than exact nearest neighbours algorithms.</li></ul><h4><strong>Indexing in FAISS</strong></h4><p>FAISS supports a variety of different types of indexes, each with its own strengths and weaknesses. Here is a brief overview of the most commonly used indexes:</p><ul><li><strong>Flat index</strong>: A flat index simply stores the vectors in a sorted list. This is the simplest type of index, but it is also the least efficient.</li><li><strong>LSH index</strong>: LSH stands for locality-sensitive hashing. It is a type of approximate nearest neighbours algorithm that can be used to find the nearest neighbours of a given vector. LSH works by creating a set of hash functions that map the vectors to a smaller space. The vectors that are mapped to the same bucket are likely to be similar to each other.</li><li><strong>Product Quantization (PQ) index</strong>: A PQ index is a type of index that compresses the vectors using a technique called product quantization. This type of index is very efficient, but it can also be less accurate than other types of indexes.</li><li><strong>Hierarchical Navigable Small World (HNSW) index</strong>: An HNSW index is a hierarchical index that uses a graph structure to store the vectors. This type of index is more efficient than a flat index, but it is also more complex to build and maintain.</li></ul><p>For our purpose, we are going to use <strong>LSH index</strong>. <strong>LSH index </strong>supports hamming distance. Some of the index doesn’t support “<strong>add_with_ids</strong>” method, which indicates that you can’t add an unique id to your vector for identification while indexing. So, there exists an index named as “<strong>IndexIDMap</strong>” that provides the ease to add id’s explicitly with the vectors. That’s the reason we are using a hybrid index. The implementation for the same is provided below</p><pre># d is dimensionality of vector<br># nbits tis he number of bits use per stored vector.<br>index = faiss.IndexIDMap(faiss.IndexLSH(d, nbits))</pre><h4>Working of FAISS</h4><p><strong>FAISS</strong> works around indexes and vector to implement fast similarity search. FAISS creates an index of the vectors by first quantizing them. Once the vectors have been quantized, FAISS can use them to create an index. The index is a data structure that stores the locations of the vectors in the dataset. FAISS can use the index to quickly find the nearest neighbours of a given vector. The candidate neighbours are then ranked according to their similarity to the given vector. The top k candidate neighbours are then returned as the nearest neighbours of the given vector.</p><p>We can create vectors with the help of previously mentioned sentence-transformers library. Post-creation of vector, we can add them to the index along with their unique ids. FAISS only supports integer ids. Given below is the demonstration of the same.</p><pre>from sentence_transformers import SentenceTransformer<br># You can use any model of your choice from https://www.sbert.net/docs/pretrained_models.html<br>model = SentenceTransformer(&#39;write the name of model&#39;)<br><br># normalize or clean the content first before forming vectors.<br>content = &quot;A group of childrens are playing football.&quot;<br><br># create vector embedding for content<br>vector = model.encode(content)<br>id = [1] <br><br># Now, add vector with the id in faiss Index<br>index.add_with_ids(vector,id)</pre><p>Further, we will search a query vector in the same index to find the nearest similar result.</p><h4><strong>Search in FAISS</strong></h4><p>To search similar results in FAISS, you need to first transform your query text to vector. After vectorization, you can use the search method of the FAISS index object. The search method takes two parameters:</p><ul><li>The first parameter is the query vector.</li><li>The second parameter is the number of nearest similar documents.</li></ul><p>The search method returns a list of tuples, where each tuple contains the ids of the nearest neighbour and the distance between the query vector and the nearest neighbour.</p><pre># K represent the number of similar documents to be returned<br>k = 10 <br>query = &quot;Childrens are playing on the gound.&quot;<br># generate vector for query text<br>query_vector = model.encode(query)<br># search in faiss<br># will provide 10 nearest documents as k=10<br>distance, ids = index.search(vector, k)</pre><h4>Storing index on disk</h4><p>FAISS indexes can be stored on disk. We can use FAISS write_index function to write index in a binary file. Moreover, we can also read an index from disk using read_index function.</p><pre>import faiss<br># To write an index to binary file<br>faiss.write_index(index, &quot;index.bin&quot;)<br><br># To read an index from binary file<br>index = faiss.read_index(&quot;index.bin&quot;)</pre><h3>Conclusion</h3><p><strong>FAISS</strong> offers a powerful solution for efficient similarity search, enabling the discovery of similar items within large datasets. Its advanced indexing techniques and optimized algorithms allow for faster retrieval of nearest neighbors.</p><p>However, it’s important to maintain a balance between accuracy and time when utilizing FAISS. While it excels in delivering fast search times, there may be a slight compromise on result precision. It’s crucial to fine-tune FAISS parameters and select appropriate indexing methods based on the specific use case, considering the data nature and application requirements.</p><p>By carefully navigating the accuracy-time trade-off, FAISS can significantly enhance the performance of data-driven applications, making it a valuable tool for similarity search.</p><p>Thanks for reading the article feel free to leave a comment about the article or any questions related to it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c933c8afe1e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/naukri-engineering/building-plagiarism-checker-using-faiss-8c933c8afe1e">Building Plagiarism Checker Using FAISS</a> was originally published in <a href="https://medium.com/naukri-engineering">Naukri Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>