<?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 Yifan Yang on Medium]]></title>
        <description><![CDATA[Stories by Yifan Yang on Medium]]></description>
        <link>https://medium.com/@yang1fan2?source=rss-31228acc3fbe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*S7sp-Zv47TqjbbSIB4rMVg.jpeg</url>
            <title>Stories by Yifan Yang on Medium</title>
            <link>https://medium.com/@yang1fan2?source=rss-31228acc3fbe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 14:35:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yang1fan2/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[Document deduplication via exact match]]></title>
            <link>https://medium.com/@yang1fan2/document-deduplication-via-exact-match-662c26107e5f?source=rss-31228acc3fbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/662c26107e5f</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[spark]]></category>
            <category><![CDATA[deduplication]]></category>
            <dc:creator><![CDATA[Yifan Yang]]></dc:creator>
            <pubDate>Sun, 15 Oct 2023 00:37:39 GMT</pubDate>
            <atom:updated>2023-10-15T00:40:16.839Z</atom:updated>
            <content:encoded><![CDATA[<p>Ensuring high-quality data is very important, especially in the context of large language models (LLMs) like GPT, Llama and etc. <a href="https://commoncrawl.org/">Common Crawl</a> (CC), a critical data source, emerges prominently in the training data across various LLMs. However, a notable issue with the CC dataset is containing duplicated documents, largely attributed to content being replicated across multiple websites.</p><h3>The Consequence of Duplication in Training Data</h3><p>Training on identical documents not only slows down the training but also potentially leads to a bad model performance. The intuition might be to retain one singular document of similar documents. Then the question becomes, what qualifies documents as similar? Typically, there are three approaches:</p><ul><li><strong>Exact matching</strong>: Leveraging <a href="https://en.wikipedia.org/wiki/MD5">MD5</a> hashing to ensure documents are identical.</li><li><strong>Near matching</strong>: Using min-hash/sim-hash to locate overlapped text, measured by jaccard similarity scores.</li><li><strong>Semantical matching</strong>: Clustering documents with pretrained embeddings.</li></ul><p>In this article, we deep dive the exact matching approach.</p><h3>The Challenges of Lengthy Documents</h3><p>Given that documents can span tens of thousands characters, a direct, character by character comparison would be time-consuming. A more efficient approach involves converting the entire text into an MD5 hash value (128-bit) and deduplicating based on this hash value. Below is a Python function demonstrating this:</p><pre>def get_hash(text: str):<br>  import hashlib<br>  return hashlib.md5(text.strip().encode(&#39;utf-8&#39;)).hexdigest()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*sjaW90BLuQFoU-l5cFShjg.png" /></figure><h3>PySpark for Deduplication</h3><p>Using PySpark’s API dropDuplicates, we can efficiently filter through duplicated rows:</p><pre># Generated by GPT-4<br>from pyspark.sql import SparkSession<br>from pyspark.sql.functions import udf<br>from pyspark.sql.types import StringType<br>from pyspark import SparkConf<br>import hashlib<br><br># Given function to get MD5 hash<br>def get_hash(text: str):<br>    return hashlib.md5(text.strip().encode(&#39;utf-8&#39;)).hexdigest()<br><br># UDF registration to use within Spark DataFrame API<br>hash_udf = udf(get_hash, StringType())<br><br>conf = (SparkConf()<br>        .set(&quot;spark.driver.port&quot;, &quot;4040&quot;)  # Set your preferred port number<br>        .set(&quot;spark.driver.bindAddress&quot;, &quot;127.0.0.1&quot;)<br>        .set(&quot;spark.port.maxRetries&quot;, &quot;20&quot;)  # Set your preferred number of retries<br>        .set(&quot;spark.driver.host&quot;, &quot;localhost&quot;))<br><br>spark = SparkSession.builder \<br>        .config(conf=conf) \<br>        .appName(&quot;MD5 Deduplication&quot;) \<br>        .master(&quot;local[1]&quot;) \<br>        .getOrCreate()<br><br># Sample DataFrame creation<br>data = [(&quot;abc&quot;,), (&quot; def &quot;,), (&quot;ghi&quot;,), (&quot;abc&quot;,), (&quot;def&quot;,), (&quot;xyz&quot;,)]<br>df = spark.createDataFrame(data, [&quot;text&quot;])<br><br># Add new column with MD5 hash using the given function<br>df_with_hash = df.withColumn(&quot;hash&quot;, hash_udf(df[&quot;text&quot;]))<br><br># Perform deduplication<br>df_deduped = df_with_hash.dropDuplicates([&quot;hash&quot;])<br><br># Optionally: Keep relevant columns<br>df_result = df_deduped.select(&quot;text&quot;, &quot;hash&quot;)<br><br># Show the results<br>df_result.show()<br><br># Stop Spark session<br>spark.stop()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*tnjasRhlc0DfBp1esR_hgQ.png" /></figure><p>As evidenced in the image above, duplicated documents, such as“abc” and “def”, are successfully removed.</p><h3>Coming Up Next…</h3><p>Hope you find this article useful! In the next chapter, we’ll delve into the world of near-matching algorithms, exploring min-hash in particular. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=662c26107e5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>