Spaces:
Running
Running
from fasthtml.common import * | |
from fasthtml.components import * | |
import json | |
import random | |
import string | |
from rich import print | |
import jsonlines | |
from data.url_blocklist import urls_high_matches, urls_false_positives | |
from data.non_web_urls import non_web_urls | |
from data_viewer import DV, DV2, DVS | |
from fasthtml.components import D_code | |
import pandas as pd | |
data_filtering_table_data = pd.DataFrame( | |
{ | |
"Dataset": [ | |
"TxT360", | |
"FineWeb", | |
"RefinedWeb", | |
"RedPajamaV2", | |
"C4", | |
"Dolma", | |
"RedPajamaV1", | |
"The Pile", | |
], | |
"Data Reading": [ | |
"warc", | |
"warc", | |
"warc", | |
"wet", | |
"wet", | |
"warc", | |
"wet", | |
"warc", | |
], | |
"Text Extraction": [ | |
"trafilatura", | |
"trafilatura", | |
"trafilatura", | |
"n/a", | |
"n/a", | |
"?", | |
"n/a", | |
"jusText", | |
], | |
"URL Filtering": [ | |
"Yes", | |
"Yes", | |
"Yes", | |
"Yes", | |
"No", | |
"No", | |
"No", | |
"No", | |
], | |
"Language Identification": [ | |
"fastText", | |
"fastText", | |
"fastText", | |
"fastText", | |
"langdetect", | |
"fastText", | |
"fastText", | |
"pycld2", | |
], | |
"Line Removal": [ | |
"Yes", | |
"Yes", | |
"Yes", | |
"Yes", | |
"Yes", | |
"Yes", | |
"No", | |
"No", | |
], | |
"PII Filtering": [ | |
"Yes", | |
"Yes", | |
"No", | |
"No", | |
"No", | |
"Yes", | |
"No", | |
"No", | |
], | |
"Exact Deduplication": [ | |
"Bloom Filter", | |
"n/a", | |
"ExactSubStr", | |
"Bloom Filter", | |
"n/a", | |
"Bloom Filter", | |
"n/a", | |
"n/a", | |
], | |
"Fuzzy Deduplication": [ | |
"Global", | |
"Local", | |
"Local", | |
"Local", | |
"Local", | |
"Local", | |
"Local", | |
"Global", | |
], | |
} | |
) | |
styled_table = ( | |
data_filtering_table_data.style.set_properties( | |
**{"background-color": "#E1EEDB"}, | |
subset=pd.IndexSlice[0, :], # Row 0 with a light green background | |
) | |
.apply( | |
lambda x: [ | |
"background-color: #E1EEDB" | |
if i == 0 | |
else ( | |
"background-color: rgb(237, 242, 251)" | |
if i % 2 == 0 | |
else "background-color: white" | |
) | |
for i in range(len(x)) | |
], | |
axis=0, | |
) | |
.hide(axis="index") | |
) # Hide the row index | |
# Use _repr_html_() method to get the HTML representation of the styled DataFrame | |
table_html_filter_data = styled_table._repr_html_() | |
table_div_filter_data = Div(NotStr(table_html_filter_data), style="margin: 40px;") | |
qf_filtering_table_data = pd.DataFrame( | |
{ | |
"Dataset": [ | |
"TxT360", | |
"FineWeb", | |
"RefinedWeb", | |
"RedPajamaV2", | |
"C4", | |
"Dolma", | |
"RedPajamaV1", | |
"The Pile", | |
], | |
"QF: ML-based": [ | |
"No", | |
"No", | |
"No", | |
"Yes", | |
"No", | |
"No", | |
"Yes", | |
"Yes", | |
], | |
"QF: Repition-based": [ | |
"Yes", | |
"Yes", | |
"Yes", | |
"Yes", | |
"No", | |
"Yes", | |
"No", | |
"No", | |
], | |
"QF: Correction-based": [ | |
"Yes", | |
"Yes", | |
"Yes", | |
"No", | |
"No", | |
"No", | |
"No", | |
"No", | |
], | |
"QF: Gopher Rules": [ | |
"Yes", | |
"Yes", | |
"Yes", | |
"Yes", | |
"No", | |
"Yes", | |
"No", | |
"No", | |
], | |
"QF: C4 Rules": [ | |
"Yes", | |
"Yes", | |
"Yes", | |
"Yes", | |
"Yes", | |
"Yes", | |
"No", | |
"No", | |
], | |
} | |
) | |
styled_table = ( | |
qf_filtering_table_data.style.set_properties( | |
**{"background-color": "#E1EEDB"}, | |
subset=pd.IndexSlice[0, :], # Row 0 with a light green background | |
) | |
.apply( | |
lambda x: [ | |
"background-color: #E1EEDB" | |
if i == 0 | |
else ( | |
"background-color: rgb(237, 242, 251)" | |
if i % 2 == 0 | |
else "background-color: white" | |
) | |
for i in range(len(x)) | |
], | |
axis=0, | |
) | |
.hide(axis="index") | |
) # Hide the row index | |
# Use _repr_html_() method to get the HTML representation of the styled DataFrame | |
table_html_qf_filter_data = styled_table._repr_html_() | |
table_div_qf_filter_data = Div(NotStr(table_html_qf_filter_data), style="margin: 40px;") | |
dolma311 = """ | |
words = text.split() | |
word_count = len(words) | |
character_count = sum(len(word) for word in words) | |
... | |
lines = text.split("\\n") | |
line_count = len(lines) | |
... | |
line_counts = Counter(lines) | |
attrs.fraction_of_duplicate_lines = sum(count for line, count in line_counts.items() if count > 1) / max( | |
line_count, 1 | |
) | |
attrs.fraction_of_characters_in_duplicate_lines = sum( | |
len(line) * count for line, count in line_counts.items() if count > 1 | |
) / max(character_count, 1) | |
""" | |
def web_data(): | |
return Div( | |
Section( | |
Div( | |
H2("Common Crawl Snapshot Processing"), | |
H3("What This Section Contains"), | |
P("This section provides a complete discussion on the filtering applied to the 99 Common Crawl snapshots that comprise the web data section of TxT360. The section is split into the following topic areas: "), | |
Ul( | |
Li("Web Data Processing Summary", style = "margin-bottom: 5px"), | |
Li("Document Preperation", style = "margin-bottom: 5px"), | |
Li("Line-Level Filtering", style = "margin-bottom: 5px"), | |
Li("Local Deduplication", style = "margin-bottom: 5px"), | |
Li("Each section is complete with code and comparisons to Dolma, DataTrove, and/or RedPajama-V-2", style = "margin-bottom: 5px"), | |
), | |
P("To generate a high-quality dataset from large-scale webpages, we have investigated the processing steps used by the community and made our choices based on careful manual inspection. Below is a comprehensive list of datasets we reviewed the comparison of filters we have applied."), | |
), | |
id="section1",), | |
Section( | |
H3("TxT360 CommonCrawl Filtering vs Other Pretraining Datasets"), | |
P("The following section provides explicit details covering the reasoning and decisions behind each of the filters we applied. The table below provides a high-level comparison of TxT360's filtering compared to other commonly used pretraining datasets."), | |
table_div_filter_data, | |
P("The table below provides a comparison of the quality filters that have been applied to each dataset. Of note, TxT360 does not use any machine learning (ML) based filters. ML filters are a useful and effecient filtering processing that should be consider for any filtering project. However, we are leaving that option to TxT360's end users."), | |
table_div_qf_filter_data, | |
P("Our filtering rate is illustrated below. Before deduplication, our filtering rate is comparable to RefinedWeb. During global deduplication, we removed approximately 85.89% of the data, significantly higher than previous works, indicating a large number of duplicates across dumps. "), | |
Img(src="images/filter_rate.jpg", height = "300", width = "600" ), | |
P("Note: All percentages are based on the number of documents. The gray bars represent the relative percentages of removed documents at each step, while the colorful bars represent the percentages of retained documents relative to the total number of documents in the raw Common Crawl."), | |
# H3("TxT360 Filter Summary"), | |
# P("This section provides highlevel details into the filtering that is applied to CommonCrawl in TxT360. Each decision listed is discussed in detail further on in this section."), | |
# P("We adopt rules from RefinedWeb [1] to remove lines if they satisfy any of the following criteria:"), | |
# Ul( | |
# Li("the line is only composed of uppercase characters", style = "margin-bottom: 5px"), | |
# Li("the line is only composed of numerical characters", style = "margin-bottom: 5px"), | |
# Li("the line matches the pattern “r'^\d+\s+likes$", style = "margin-bottom: 5px"), | |
# Li("the line only contains one word.", style = "margin-bottom: 5px"), | |
# ), | |
# P("We summarize other statistics-based rules originated from Gopher [7] in this section. The statistics can be used include:"), | |
# Ul( | |
# Li("the word count in the document", style = "margin-bottom: 5px"), | |
# Li("the mean word length", style = "margin-bottom: 5px"), | |
# Li("the number of sentences", style = "margin-bottom: 5px"), | |
# Li("the symbol-to-word ratio", style = "margin-bottom: 5px"), | |
# Li("the fraction of alphabetic words", style = "margin-bottom: 5px"), | |
# Li("and the number of stop words", style = "margin-bottom: 5px"), | |
# ), | |
# P("Specifically, we remove any document which satisfies any of the following criteria:"), | |
# Ul( | |
# Li("it contains less than 50 words or more than 100,000 words", style = "margin-bottom: 5px"), | |
# Li("its mean word length is outside the range of 3 to 10", style = "margin-bottom: 5px"), | |
# Li("it contains less than 3 sentences", style = "margin-bottom: 5px"), | |
# Li("its symbol-to-word ratio is greater than 0.1", style = "margin-bottom: 5px"), | |
# Li("the words that contain at least one alphabetic character are less than 80% of the whole words", style = "margin-bottom: 5px"), | |
# Li("it contains less than two of the stop words (the, be, to, of, and, that, have, with", style = "margin-bottom: 5px"), | |
# ), | |
# P("Following C4, we remove any page where the phrase “lorem ipsum” appears since some pages have placeholder “lorem ipsum” text."), | |
id="section2",), | |
Section( | |
H2("Document Preparation"), | |
P(B("Text Extraction: "), """ | |
Common Crawl provides webpage texts via two formats: WARC (Web ARChive format) and WET (WARC Encapsulated Text). | |
WARC files contain the raw data from the crawl, which store the full HTTP response and request metadata. | |
WET files contain plaintexts extracted by Common Crawl. In line with previous works ([1], [2], [3], [4]), | |
we found WET files to include boilerplate content like navigation menus, ads, and other irrelevant texts. | |
Accordingly, our pipeline starts from raw WARC files, reads with the warcio library, and extracts texts using trafilatura. | |
"""), | |
P("We directly read WARC files instead of WET files and extracted text using Trafilatura. Similar to RefinedWeb, we avoid using Machine Learning (ML)-based metrics for filtering documents to prevent bias introduced by ML models. Importantly, we apply global deduplication across the entire dataset, whereas previous works only use local deduplication. Note that although The Pile also employed global deduplication on its web data (Pile-CC), this accounted for just 0.6\% of 74 snapshots."), | |
Details( | |
Summary("Text Extraction Examples"), | |
Div( | |
DV2("data/sample_wet.json", "data/sample_warc.json", 3), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light blue background */ | |
padding: 15px; | |
# border: 1px solid #949494; /* Grey border */ | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", #https://colors.muz.li/palette/d3d3d3/949494/d3d3d3/d3d3d3/949494 | |
), | |
#DV2("data/sample_wet.json", "data/sample_warc.json", 3), | |
P(B("Language Identification: "), """ | |
After text extraction, the non-English texts are then filtered out by fastText language identifier with a threshold of 0.65. | |
This step removes over 60% of the whole data. | |
"""), | |
Details( | |
Summary("Non-English Document Examples"), | |
Div( | |
DV("data/sample_non_en.json", 3, "Sample documents that are classified as non-English"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
#DV("data/sample_non_en.json", 3, "Sample documents that are classified as non-English"), | |
Details( | |
Summary("English Documents Scoring Lower than 0.65 Examples"), | |
Div( | |
DV("data/sample_en_low.json", 3, "Sample documents that are classified as English but with score less than 0.65"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light green background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(B("URL Filtering: "), """ | |
The following section details the decisions behind utilizing the UT1 blocklist. We chose to use the UT1 blocklist as a simple method for filtering | |
out potentially harmful content such as adult content. We also excluded URLs that contained the digital version of the curated curated data (e.g. wikipedia.org) to avoid duplication. | |
"""), | |
P(B("URL Blocklist: "), """ | |
Following RefinedWeb [3], we manually inspected the UT1 blocklist to reduce false positives like news | |
articles, sex education, technical blogs, etc. Specifically, we randomly took 903M URLs and matched them with | |
4.6M domain names in the UT1 blocklist. Of note, 24 URLs were detected with more than 4k matches and are shown below. | |
"""), | |
Details( | |
Summary(" List of 24 URLs with 4k+ Matches"), | |
Div ( | |
DVS(urls_high_matches, "24 URL domains with more than 4k matches"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #FAEAEA; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(""" | |
We manually removed the following 6 domains from the UT1 blocklist so that they will not be removed from our dataset. | |
"""), | |
Details( | |
Summary("6 URLS Manually Removed from the Blocklist"), | |
Div ( | |
DVS(urls_false_positives, "6 url domains that are removed from the blocklist"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #FAEAEA; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Blocked Document Examples from the URL Blocklist"), | |
Div( | |
DV( | |
"data/bad_url_doc.jsonl", | |
3, | |
"Sample documents whose urls are blocked by the refined url blocklist", | |
), style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #FAEAEA; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(B("Excluded High Quality Sources: "), """ | |
To avoid duplication with our high-quality curated datasets, we exclude the following domains from our dataset. | |
"""), | |
Details( | |
Summary("TxT360 Excluded URLs"), | |
Div ( | |
DVS( | |
non_web_urls, | |
"curated url domains that are excluded from our dataset", | |
), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #FAEAEA; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("TxT360 Excluded URLs Example Documents"), | |
Div ( | |
DV("data/sample_url_exclusion.json", 0, "Sample documents whose urls are in our curated url domain list"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light green background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
id="section3",), | |
Section( | |
H2("Line-Level Removal"), | |
P(""" | |
Before filtering low-quality documents, we perform the line-level removal to remove low-quality lines. | |
This ensured that computing quality signals would align with the final kept texts. | |
"""), | |
P(B("Terminal Punctuation: "), """ | |
The terminal punctuation has been used in C4 [5] and Dolma [6] to remove lines that do not end with a terminal | |
punctuation mark (i.e., “.”, “?”, “!”, or “"”). However, we found it could be too aggressive to remove these | |
lines, especially when the text extraction tool “trafilatura”. | |
"""), | |
P(""" | |
For instance, in the CommonCrawl file | |
CC-MAIN-20230126210844-20230127000844-00000.warc.jsonl, the terminal punctuation rule led to the removal | |
of 56,292 additional lines, resulting in the complete exclusion of 2,203 documents from a total of 13,560 | |
documents (16.25%). Accordingly, we choose to not use terminal punctuation as a signal to remove lines. | |
"""), | |
Details( | |
Summary("Terminal Punctuation Filtering Examples"), | |
Div ( | |
DV( | |
"data/sample_terminal_punc.json", | |
0, | |
"Sample documents with lines that are removed by the rule of terminal punctuation", | |
), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(B('"Word "Javascript"'), """ | |
In C4 [5], the authors remove any line with the word "Javascript" since they found that many of the scraped | |
pages contained warnings stating that Javascript should be enabled. However, this filtering strategy is too | |
strict, which will filter out many lines that are really talking about “Javascript”. | |
"""), | |
P(""" | |
In our pipeline, we | |
propose to refine the strategy by adding one more keyword to the word "javascript" to avoid false positives. | |
The additional keyword could be any one of “enable” / “disable” / “require” / “activate” / “browser”. | |
"""), | |
Details( | |
Summary("Javascript Documents Filtered by C4 but Kept in TxT360"), | |
Div ( | |
DV( | |
"data/sample_java.jsonl", | |
0, | |
"Sample documents that are removed by original C4 javascript rule but are kept after our refinement", | |
), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(B("Other Rules from RefinedWeb: "), """ | |
We also adopt rules from RefinedWeb [3] to remove lines if they satisfy any of the following criteria: | |
"""), | |
Ul( | |
Li("The line is only composed of uppercase characters,", style = "margin-bottom: 5px"), | |
Li("the line is only composed of numerical characters", style = "margin-bottom: 5px"), | |
Li("the line matches the pattern “r'^\d+\s+likes$", style = "margin-bottom: 5px"), | |
Li("the line only contains one word.", style = "margin-bottom: 5px"), | |
), | |
Details( | |
Summary("Documents Filtered using RefinedWeb Rules."), | |
Div ( | |
DV( | |
"data/sample_refinedweb_line.json", | |
0, | |
"Sample documents with lines that are removed by the RefinedWeb rules", | |
), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(B("Toxic Lines: "), """ | |
When doing manual inspection on the data, we found that there are some adult ads in the beginning or end of the | |
document (with a sample shown below), which are hard to remove via document-level filtering strategies. Inspired | |
by this, we develop line-level detoxification using a bad word list from LDNOOBW (+ rule: word length < 10 + the | |
line is in the first 3 lines or in the last 3 lines) to remove toxic lines. Specifically, we do not only consider | |
the bad words from English but also consider the bad words from other languages. | |
"""), | |
Details( | |
Summary("Toxic Line Examples (WARNING: MAY CONTAIN OFFENSIVE MATERIAL)"), | |
Div ( | |
DVS( | |
json.load(open("data/toxic_lines.json")), | |
"Sample documents with toxic lines", | |
), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #FAEAEA; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
id="section4",), | |
Section( | |
H2("Document-Level Filtering"), | |
P(""" | |
In this section, we introduce each quality signal used to filter out low-quality documents. | |
"""), | |
Details( | |
Summary("Quality Signals Used For Filtering"), | |
Div ( | |
DVS( | |
json.load(open("data/all_signals.json")), | |
"Overview of all the quality signals that are used for filtering", | |
), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light green background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P("""Similar to previous sections, we will present sample documents filtered out by the given quality signals. | |
Most quality signals were initially introduced by Gopher [2] and subsequently adopted by later | |
studies ([3], [6], [4]). However, we observed that, despite following the same descriptions, the implementation | |
of each quality signal can vary significantly among different dataset pipelines, resulting in disparate | |
outcomes for the same quality signals. | |
In our pipeline, we referenced earlier implementations that were publicly available such as Dolma [6], DataTrove [4], | |
and RedPajama V2 [7], and selected the most suitable method based on manual inspections. | |
"""), | |
P(B("Repetition-based Heuristics: "), """ | |
Many documents contain repeated sequences, potentially due to crawling errors or low-quality sources. In line with previous | |
work ([2], [3], [6]), we choose to remove any document with excessive line, paragraph, or n-gram repetitions. | |
"""), | |
P(B("Fraction of Characters in Repeated Lines: "), """ | |
Following Gopher [2], we remove documents containing mupltiple, short duplicate passages, as well as those with few, | |
but longer duplicate passages. To achieve this goal, we calculate over the document both the fraction of passages | |
that are duplicates, and the fraction of characters contained within those duplicated passages. | |
"""), | |
Details( | |
Summary("Implementations from Dolma"), | |
Div( | |
D_code(""" | |
words = text.split() | |
word_count = len(words) | |
character_count = sum(len(word) for word in words) | |
... | |
lines = text.split("\n") | |
line_count = len(lines) | |
... | |
line_counts = Counter(lines) | |
attrs.fraction_of_duplicate_lines = sum(count for line, count in line_counts.items() if count > 1) / max( | |
line_count, 1 | |
) | |
attrs.fraction_of_characters_in_duplicate_lines = sum( | |
len(line) * count for line, count in line_counts.items() if count > 1 | |
) / max(character_count, 1) | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Implementations from DataTrove"), | |
Div( | |
D_code(""" | |
def find_duplicates(x: list[str]) -> tuple[int, int]: | |
unique_x = set() | |
duplicate_chars = 0 | |
duplicate_elements = 0 | |
for element in x: | |
if element in unique_x: | |
duplicate_chars += len(element) | |
duplicate_elements += 1 | |
else: | |
unique_x.add(element) | |
return duplicate_elements, duplicate_chars | |
... | |
self.paragraph_exp = re.compile(r"\n{2,}") | |
self._line_splitter = re.compile("\n+") | |
... | |
paragraphs = self.paragraph_exp.split(text.strip()) | |
paragraphs_duplicates, char_duplicates = find_duplicates(paragraphs) | |
if self.dup_para_frac and paragraphs_duplicates / len(paragraphs) > self.dup_para_frac: | |
return False, "dup_para_frac" | |
if self.dup_para_char_frac and char_duplicates / len(text) > self.dup_para_char_frac: | |
return False, "dup_para_char_frac" | |
lines = self._line_splitter.split(text) | |
line_duplicates, char_duplicates = find_duplicates(lines) | |
if self.dup_line_frac and line_duplicates / len(lines) > self.dup_line_frac: | |
return False, "dup_line_frac" | |
if self.dup_line_char_frac and char_duplicates / len(text) > self.dup_line_char_frac: | |
return False, "dup_line_char_frac" | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(""" | |
After evaluating the implementations of Dolma and DataTrove (note: RedPajama V2 does not implement these two quality | |
signals), we have made the following decisions: | |
"""), | |
P(B("Passage Separation: "), """ | |
Our manual review of the data revealed that documents extracted using trafilatura do not feature more than one newline | |
symbol separating passages. Testing the splitting pattern "\\n(2,)" on 10,000 sample documents resulted in no more than | |
one split. Consequently, we decided to disregard the distinction between lines and paragraphs in our implementation, | |
opting instead to use a single newline symbol to segment the text into passages. | |
"""), | |
P(B("First Occurrence: "), """ | |
In line with DataTrove's implementation, we chose to exclude the first occurrence. This more conservative strategy | |
helps retain a larger number of documents. | |
"""), | |
P(B("Character Count: "), """ | |
We adjusted the method in Dolma for counting characters within lines by excluding whitespace. This modification | |
ensures consistency with the overall document character count calculation. | |
"""), | |
Details( | |
Summary("TxT360 Implementation"), | |
Div( | |
D_code(""" | |
words = text.split() | |
word_count = len(words) | |
character_count = sum(len(word) for word in words) | |
... | |
lines = text.split("\n") | |
line_count = len(lines) | |
line_counts = Counter(lines) | |
attrs.fraction_of_duplicate_lines = ( | |
sum((count - 1) for line, count in line_counts.items() if count > 1) / line_count | |
) | |
attrs.fraction_of_characters_in_duplicate_lines = ( | |
sum(sum(len(w) for w in line.split()) * (count - 1) for line, count in | |
line_counts.items() if count > 1) / character_count | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light green background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Excessive Line and Character Repetition Filtered Examples"), | |
Div( | |
DV( | |
"data/repeat_line_frac.jsonl", | |
0, | |
"Sample documents filtered by excessive line repetitions / characters in repeated lines", | |
), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(B("Fraction of Characters in the Most Common N-grams (n=2,3,4): "), """ | |
Following Gopher [2], we remove documents with a high portion of n-grams. For each n ∈ (2, 3, 4), we calculate the | |
fraction of characters contained within the most frequently-occurring n-gram. | |
"""), | |
Details( | |
Summary("Implementations from Dolma"), | |
Div( | |
D_code(""" | |
def all_ngram_counts(words) -> List[Tuple[int, CounterType[Tuple[str, ...]]]]: | |
return [(n, Counter(list(zip(*[words[i:] for i in range(n)])))) for n in range(2, 11)] | |
... | |
all_counts = all_ngram_counts(words) | |
count_most_common_ngrams = (2, 3, 4) | |
for n, ngram_counts in all_counts: | |
if not ngram_counts: | |
continue | |
if n in count_most_common_ngrams: | |
most_common_ngram, count = ngram_counts.most_common(1)[0] | |
value = count * sum(len(w) for w in most_common_ngram) / max(character_count, 1) | |
attrs.fraction_of_characters_in_most_common_ngram.append((n, value)) | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Implementations from RedPajama-V2"), | |
Div( | |
D_code(""" | |
class Base_RPS_Frac_Chars_In_Top_NGram(RPSBase): # noqa | |
## Base class for calculating the fraction of characters in the top N-gram. This operates on the lower-cased, punctation removed content. | |
NGRAM_SIZE: int = None | |
__slots__ = [] | |
def __call__(self, document: Document) -> SignalType: | |
if self.NGRAM_SIZE is None: | |
raise NotImplementedError( | |
"NGRAM_SIZE must be set in the subclass" | |
) | |
# get the most common ngram | |
most_common_ngram = Counter( | |
# fetch the ngrams from the document if they exist, otherwise | |
# compute them | |
getattr(document, f"norm_self.NGRAM_SIZEgrams", None) | |
or | |
form_ngrams(iter(document.normalized_words), self.NGRAM_SIZE) | |
).most_common(1) | |
if len(most_common_ngram) == 0: | |
return [(0, len(document), 0.0)] | |
ngram, count = most_common_ngram[0] | |
if count <= 1: | |
return [(0, len(document), 0.0)] | |
total_chars = sum(len(w) for w in document.normalized_words) | |
score = sum(len(w) for w in ngram) * count / total_chars | |
score = round(score, PRECISION) | |
return [(0, len(document), score)] | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Implementations from DataTrove"), | |
Div( | |
D_code(""" | |
def get_n_grams(words: list[str], n: int) -> list[str]: | |
return [" ".join(words[i : i + n]) for i in range(len(words) - n + 1)] | |
def find_top_duplicate(x: list[str]) -> int: | |
counter = Counter() | |
for element in x: | |
counter[element] += 1 | |
top_n_gram = counter.most_common(1)[0] | |
return len(top_n_gram[0]) * top_n_gram[1] | |
... | |
for n, n_frac in self.top_n_grams: | |
n_grams = get_n_grams(words, n) | |
if not n_grams: | |
continue | |
top_char_length = find_top_duplicate(n_grams) | |
if top_char_length / len(text) > n_frac: | |
return False, f"top_n_gram" | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(""" | |
There are almost no contradictions between each implementations of fractions of characters in the most common | |
n-gram. The main process involves counting the occurrences of each n-gram and selecting the most common one. The | |
fraction is then determined by dividing the number of characters in the most common n-gram by the total number of | |
characters. One minor difference is that Dolma and DataTrove calculate the fraction of the most common n-gram even | |
if it only appears once, while RedPajama V2 skips this case. | |
We choose to follow Dolma and DataTrove by not skipping cases where the most common n-gram occurs only once. | |
In practice, documents affected by this rule — where the most common n-gram exceeds a given threshold and occurs | |
only once — tend to be short. | |
"""), | |
Details( | |
Summary("TxT360 Implementation"), | |
Div( | |
D_code(""" | |
def all_ngram_counts_new(words) -> List[Tuple[int, CounterType[Tuple[str, ...]]]]: | |
return [(n, list(zip(*[words[i:] for i in range(n)]))) for n in range(2, 11)] | |
... | |
all_counts = all_ngram_counts_new(words) | |
count_most_common_ngrams = (2, 3, 4) | |
for n, ngram_counts in all_counts: | |
if not ngram_counts: | |
continue | |
if n in count_most_common_ngrams: | |
most_common_ngram, count = Counter(ngram_counts).most_common(1)[0] | |
value = count * sum(len(w) for w in most_common_ngram) / character_count | |
attrs.fraction_of_characters_in_most_common_ngram.append((n, value)) | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light green background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Documents Filtered Using Most Common n-Grams (n=2,3,4)"), | |
Div( | |
DV( | |
"data/sample_top_ngram.json", | |
0, | |
"Sample documents filtered by the fraction of characters in the most common n-grams (n=2,3,4)", | |
), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(B("Fraction of Characters in Duplicated N-grams (n=5,...,10): "), """ | |
Following Gopher [2], we remove documents with a high portion of n-grams. For each n ∈ (5, ..., 10), we calculate the | |
fraction of characters contained within all duplicate n-grams, taking care not to count characters that occur in | |
overlapping n-grams more than once. | |
"""), | |
Details( | |
Summary("Implementations from Dolma"), | |
Div( | |
D_code(""" | |
def all_ngram_counts(words) -> List[Tuple[int, CounterType[Tuple[str, ...]]]]: | |
return [(n, Counter(list(zip(*[words[i:] for i in range(n)])))) for n in range(2, 11)] | |
... | |
all_counts = all_ngram_counts(words) | |
for n, ngram_counts in all_counts: | |
if not ngram_counts: | |
continue | |
if n in count_most_common_ngrams: | |
... | |
else: | |
ng_char_count = sum(count * sum(len(w) for w in ng) for ng, count in ngram_counts.items()) | |
value = sum( | |
count * sum(len(w) for w in ng) for ng, count in ngram_counts.items() if count > 1 | |
) / max(ng_char_count, 1) | |
attrs.fraction_of_characters_in_duplicate_ngrams.append((n, value)) | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Implementations from RedPajama-V2"), | |
Div( | |
D_code(""" | |
class Base_RPS_Frac_Chars_In_Dupe_NGrams(RPSBase): # noqa | |
## Base class for calculating the fraction of characters in duplicate word N-grams. This operates on the lower-cased, punctation removed content. The function also ensures that characters in overlapping ngrams are only counted once. | |
NGRAM_SIZE: int = None | |
__slots__ = [] | |
def __call__(self, document: Document) -> SignalType: | |
if self.NGRAM_SIZE is None: | |
raise NotImplementedError( | |
"NGRAM_SIZE must be set in the subclass" | |
) | |
if len(document.normalized_words) < self.NGRAM_SIZE: | |
return [(0, len(document), 0.0)] | |
# fetch the ngrams from the document if they exist, otherwise | |
# compute them | |
doc_n_grams = ( | |
getattr(document, f"norm_self.NGRAM_SIZEgrams", None) | |
or | |
tuple(form_ngrams( | |
iter(document.normalized_words), self.NGRAM_SIZE | |
)) | |
) | |
# keep only ngrams which occur at least twice | |
ngram_dupes = | |
ngram for ngram, count in Counter(doc_n_grams).items() if count > 1 | |
duplicated_grams = np.zeros(len(document.normalized_words), dtype=int) | |
i = 0 | |
for ngram in doc_n_grams: | |
if ngram in ngram_dupes: | |
duplicated_grams[i: i + self.NGRAM_SIZE] = 1 | |
i += 1 | |
word_lengths = np.array(list(map(len, document.normalized_words))) | |
chars_duped = np.sum(word_lengths * duplicated_grams) | |
total_chars = np.sum(word_lengths) | |
if total_chars == 0: | |
return [(0, len(document), 0.0)] | |
score = float(chars_duped / total_chars) | |
score = round(score, PRECISION) | |
return [(0, len(document), score)] | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Implementations from DataTrove"), | |
Div( | |
D_code(""" | |
def find_all_duplicate(words: list[str], n: int) -> int: | |
n_words = len(words) | |
unique = set() | |
repeated_chars, idx = 0, 0 | |
while idx < n_words - n + 1: | |
n_gram = "".join(words[idx : idx + n]) | |
if n_gram in unique: | |
repeated_chars += len(n_gram) | |
idx += n | |
else: | |
unique.add(n_gram) | |
idx += 1 | |
assert repeated_chars <= len("".join(words)) | |
return repeated_chars | |
... | |
for n, n_frac in self.dup_n_grams: | |
n_duplicates_char = find_all_duplicate(words, n) | |
if n_duplicates_char / len(text) > n_frac: | |
return False, f"duplicated_n_grams" | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(""" | |
For the computation of fraction of characters in duplicate n-gram, Dolma uses the number of characters in all | |
n-grams (with overlapping) as the denominator, and uses the number of characters in all duplicated n-grams | |
(with overlapping) as the numerator."""), | |
P("""RedPajama V2 uses the number of all characters in (the words of) the document | |
(without overlapping) as the denominator, and uses the number of characters that are recognized as part of the | |
duplicate n-gram as the numerator."""), | |
P("""Datatrove uses the number of all characters in the document (including white | |
spaces, without overlapping) as the denominator, and uses the number of characters that are recognized as | |
duplicate n-gram as the numerator. However, there is a mismatch in DataTrove’s calculation, as the number of | |
characters in the duplicated n-grams excludes white spaces, while the total character count of the document | |
does not."""), | |
P("""We decided to use the RedPajama V2 implementation but skip the 1st occurrence of the duplicate n-gram. | |
"""), | |
Details( | |
Summary("TxT360 Implementation"), | |
Div( | |
D_code(""" | |
def get_dup_ngram_frac(n, doc_n_grams, text): | |
# fetch the ngrams from the document if they exist, otherwise compute them | |
# doc_n_grams = list(zip(*[words[i:] for i in range(n)])) | |
duplicated_grams = np.zeros(len(text.split()), dtype=int) | |
unique_ngrams = set() | |
for i, ngram in enumerate(doc_n_grams): | |
if ngram in unique_ngrams: | |
duplicated_grams[i: i + n] = 1 | |
else: | |
unique_ngrams.add(ngram) | |
word_lengths = np.array(list(map(len, text.split()))) | |
chars_duped = np.sum(word_lengths * duplicated_grams) | |
total_chars = np.sum(word_lengths) | |
return float(chars_duped / total_chars) | |
def all_ngram_counts_new(words) -> List[Tuple[int, CounterType[Tuple[str, ...]]]]: | |
return [(n, list(zip(*[words[i:] for i in range(n)]))) for n in range(2, 11)] | |
... | |
all_counts = all_ngram_counts_new(words) | |
count_most_common_ngrams = (2, 3, 4) | |
for n, ngram_counts in all_counts: | |
if not ngram_counts: | |
continue | |
if n in count_most_common_ngrams: | |
... | |
else: | |
score = get_dup_ngram_frac(n, ngram_counts, text) | |
attrs.fraction_of_characters_in_duplicate_ngrams.append((n, score)) | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light green background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Comparison of Coding Implementations"), | |
P(""" | |
Considering n = 5 and the sample sentence: | |
"word_a word_b word_c word_d word_e word_f word_g word_a word_b word_c word_d word_e word_f word_g word_a word_b word_c" | |
In Dolma's implementation, there are 13 5-grams in total with 6 duplicated 5-grams. The resulting fraction of characters in duplicate 5-gram is 6/13. | |
In RedPajama's V2 implementation, there are 17*6 characters in total and 14*6 characters that are contained in duplicate 5-grams. The fraction is 14/17. | |
In DataTrove's implementation, there are 17*6 + 16(white spaces) characters in total and 10 duplicated 5-grams after excluding the first occurrence. The resulting fraction number is 10*6/(17*6+16). | |
In our implementation, there are 17*6 characters in total with 10*6 characters that are duplicated after excluding the first occurence. This results in a fraction of 10/17. | |
"""), | |
style=""" | |
background-color: #EAFFF1; /* Light green background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Documents Filtered by Duplicated n-Grams (n=5,...,10)"), | |
Div( | |
DV( | |
"data/sample_dup_ngram.json", | |
0, | |
"Sample documents filtered by the fraction of characters in duplicated n-grams (n=5,...,10)", | |
), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(B("Line-wise Heuristics: "), """ | |
Some line-wise information could also be helpful to distinguish low-quality and high-quality documents. Following | |
RefinedWeb [3], we remove the document if the corrected lines represent more than 5% of words. In line with previous | |
works ([2], [3], [6]), we remove the documents if more than 30% of the lines end with an ellipsis or more than | |
90% of lines start with a bullet point. | |
"""), | |
Details( | |
Summary("Ellipsis Symbol Identification Implemetations"), | |
Div( | |
P("Dolma: "), | |
D_code(""" | |
ELLIPSIS_SYMBOLS = ("…") | |
""", block="block", language="python"), | |
P("RedPajamaV2: "), | |
D_code(""" | |
ELLIPSIS_SYMBOLS = ("...", "…") | |
""", block="block", language="python"), | |
P("DataTrove: "), | |
D_code(""" | |
ELLIPSIS_SYMBOLS = ("...", "…") | |
""", block="block", language="python"), | |
P("TxT360: "), | |
D_code(""" | |
ELLIPSIS_SYMBOLS = ("...", "…", "[...]", "[…]") | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Bullet Point Identification Implemetations"), | |
Div( | |
P("Dolma: "), | |
D_code(""" | |
BULLET_POINTS = ("*", "-" | |
""", block="block", language="python"), | |
P("RedPajamaV2: "), | |
D_code(""" | |
BULLET_POINT_SYMBOLS = ( | |
"•", # bullet point | |
"‣", # triangular bullet point | |
"▶", # black right pointing triangle | |
"◀", # black left pointing triangle | |
"◦", # white bullet point | |
"■", # black square | |
"□", # white square | |
"▪", # black small square | |
"▫", # white small square | |
"–", # en dash | |
) | |
""", block="block", language="python"), | |
P("DataTrove: "), | |
D_code(""" | |
BULLET_POINT_SYMBOLS = ("•" , "-") | |
""", block="block", language="python"), | |
P("TxT360: "), | |
D_code(""" | |
BULLET_POINT_SYMBOLS = ( | |
"•", # • bullet point | |
"‣", # ‣ triangular bullet point | |
"▶", # ▶ black right pointing triangle | |
"◀", # ◀ black left pointing triangle | |
"◦", # ◦ white bullet point | |
"■", # ■ black square | |
"□", # □ white square | |
"▪", # ▪ black small square | |
"▫", # ▫ white small square | |
"-", # - en dash | |
"–", # – dash | |
"—", # — zh dash | |
"*", # * star | |
) | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ #light yellow FFFAEA | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Documents Filtered by Line-Wise Heuristics"), | |
Div( | |
DV( | |
"data/line_info.json", | |
0, | |
"Sample documents that are filtered out by line-wise heuristics", | |
), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(B("Statistics-based Heuristics: "), """ | |
We summarize other statistics-based rules originated from Gopher [7] in this section. The statistics can be used include: | |
"""), | |
Ul( | |
Li("the word count in the document", style = "margin-bottom: 5px"), | |
Li("the mean word length", style = "margin-bottom: 5px"), | |
Li("the number of sentences", style = "margin-bottom: 5px"), | |
Li("the symbol-to-word ratio", style = "margin-bottom: 5px"), | |
Li("the fraction of alphabetic words", style = "margin-bottom: 5px"), | |
Li("and the number of stop words", style = "margin-bottom: 5px"), | |
), | |
P("Specifically, we remove any document which satisfies any of the following criteria:"), | |
Ul( | |
Li("it contains less than 50 words or more than 100,000 words", style = "margin-bottom: 5px"), | |
Li("its mean word length is outside the range of 3 to 10", style = "margin-bottom: 5px"), | |
Li("it contains less than 3 sentences", style = "margin-bottom: 5px"), | |
Li("its symbol-to-word ratio is greater than 0.1", style = "margin-bottom: 5px"), | |
Li("the words that contain at least one alphabetic character are less than 80% of the whole words", style = "margin-bottom: 5px"), | |
Li("it contains less than two of the stop words (the, be, to, of, and, that, have, with", style = "margin-bottom: 5px"), | |
), | |
H3("Word Count Filters"), | |
Details( | |
Div( | |
Summary("Implementations from Dolma"), | |
D_code(""" | |
words = text.split() | |
word_count = len(words) | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Implementations from RedPajama-V2"), | |
Div( | |
D_code(""" | |
# the normalized content: lowercased and punctuation removed | |
self._normalized_content = normalize(content) | |
self._normalized_words = tuple(self._normalized_content.split()) | |
self._num_normalized_words = len(self._normalized_words) | |
... | |
def normalize( | |
text: str, | |
remove_punct: bool = True, | |
lowercase: bool = True, | |
nfd_unicode: bool = True, | |
white_space: bool = True | |
) -> str: | |
#Normalize the text by lowercasing and removing punctuation. | |
# remove punctuation | |
if remove_punct: | |
text = text.translate(TRANSLATION_TABLE_PUNCTUATION) | |
# lowercase | |
if lowercase: | |
text = text.lower() | |
if white_space: | |
text = text.strip() | |
text = re.sub(r"\s+", " ", text) | |
# NFD unicode normalization | |
if nfd_unicode: | |
text = unicodedata.normalize("NFD", text) | |
return text | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Implementations from DataTrove"), | |
Div( | |
D_code(""" | |
words = self.tokenizer.word_tokenize(text) | |
n_words = len(words) | |
non_symbol_words = [w for w in words if any(ch not in PUNCTUATION_SET for ch in w)] | |
n_non_symbol_words_words = len(non_symbol_words) | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(""" | |
Both Dolma and RedPajama V2 split texts into words using white spaces and newline symbols. However, | |
DataTrove employs a tokenizer to split texts into words and ignore punctuations, resulting in a higher | |
word count compared to simple `text.split()`. | |
We decided to use simple `len(text.split())` to compute the word count. | |
"""), | |
P(B("Mean Word Length: "), """ | |
There is minimal variation among existing pipeline implementations. We simply compute the mean word length as follows: | |
"""), | |
D_code(""" | |
words = text.split() | |
word_count = len(words) | |
character_count = sum(len(word) for word in words) | |
mean_word_length = character_count / word_count | |
""", block="block", language="python"), | |
P(""" | |
It's worth noting that Dolma used the median word length instead of the mean: | |
"""), | |
D_code(""" | |
from statistics import median | |
median_word_length = median(len(word) for word in words) | |
""", block="block", language="python"), | |
P(B("Number of Sentences: "), """ | |
The only publicly available implementation of this quality signal is from RedPajama V2, which uses regular expressions | |
to split text into sentences. | |
"""), | |
Details( | |
Summary("Implementations from RedPajama-V2"), | |
Div( | |
D_code(""" | |
class RPS_Doc_Num_Sentences(RPSBase): # noqa | |
##The number of sentences in the content. This is calculated using the regex r'[^.!?]+[.!?]*' | |
SENT_PATTERN = re.compile(r'[^.!?]+[.!?]*', flags=re.UNICODE) | |
__slots__ = () | |
def __call__(self, document: Document) -> SignalType: | |
##count the number of sentences in the content using regex | |
score = float(len(self.SENT_PATTERN.findall(document.raw_content))) | |
return [(0, len(document), score)] | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(""" | |
However, we found that this approach can mistakenly interpret periods in URLs as sentence endings. To address this, | |
we opted to use `nltk.tokenize.sent_tokenize` for more accurate sentence splitting. | |
"""), | |
Details( | |
Summary("TxT360 Implementation"), | |
Div( | |
D_code(""" | |
from nltk.tokenize import sent_tokenize | |
... | |
def count_sentences(text): | |
sentences = sent_tokenize(text) | |
return len(sentences) | |
... | |
attrs.num_of_sentences = count_sentences(text) | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light green background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(B("Symbol to Word Ratio: "), """ | |
Following RedPajama-V2 and DataTrove, we use the symbols of ("#", "...", "…"). | |
We calculate the ratio as the number of symbols divided by the total number of words. | |
"""), | |
Details( | |
Summary("Implementations from Dolma"), | |
Div( | |
D_code(""" | |
SYMBOLS = ("#", "…") | |
... | |
attrs.symbol_to_word_ratio = sum(1 for word in words if any(s in word for s in SYMBOLS)) / max( | |
word_count, 1 | |
) | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Implementations from RedPajama-V2"), | |
Div( | |
D_code(""" | |
class RPS_Doc_Symbol_To_Word_Ratio(RPSBase): # noqa | |
##The ratio of symbols to words in the content. This is analogous to | |
##the signal used in Gopher. Symbols are defined "#", "...", and "…". | |
SYMBOLS = ("#", "...", "…") | |
__slots__ = () | |
def __call__(self, document: Document) -> SignalType: | |
num_words = document.num_raw_words | |
if num_words == 0: | |
return [(0, len(document), None)] | |
# count the number of symbols in the content | |
num_symbols = float(sum( | |
document.raw_content.count(x) for x in self.SYMBOLS | |
)) | |
score = num_symbols / num_words | |
score = round(score, PRECISION) | |
return [(0, len(document), score)] | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Implementations from DataTrove"), | |
Div( | |
D_code(""" | |
if self.max_symbol_word_ratio and text.count("#") / n_words > self.max_symbol_word_ratio: | |
return False, "gopher_too_many_hashes" | |
if self.max_symbol_word_ratio and (text.count("...") + text.count("…")) / n_words > self.max_symbol_word_ratio: | |
return False, "gopher_too_many_ellipsis" | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("TxT360 Implementation"), | |
Div( | |
D_code(""" | |
SYMBOLS = ("#", "...", "…") | |
... | |
symbol_pattern = re.compile("|".join(re.escape(symbol) for symbol in SYMBOLS)) | |
... | |
attrs.symbol_to_word_ratio = sum(1 for word in words if symbol_pattern.search(word)) / word_count | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light green background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
H3("Fraction of Alphabetic Words"), | |
Details( | |
Summary("Implementations from Dolma"), | |
Div( | |
D_code(""" | |
attrs.fraction_of_words_with_alpha_character = sum( | |
1 for word in words if any(c.isalpha() for c in word) | |
) / max(word_count, 1) | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Implementations from RedPajama-V2"), | |
Div( | |
D_code(""" | |
class RPS_Doc_Frac_No_Alph_Words(RPSBase): # noqa | |
ALPH_REGEX = re.compile(r"[a-zA-Z]") | |
__slots__ = () | |
def __call__(self, document: Document) -> SignalType: | |
num_words = document.num_raw_words | |
if num_words == 0: | |
return [(0, len(document), None)] | |
num_words_with_alpha = float(sum( | |
int(self.ALPH_REGEX.search(word) is not None) | |
for word in document.raw_words | |
)) | |
score = 1.0 - num_words_with_alpha / num_words | |
score = round(score, PRECISION) | |
return [(0, len(document), score)] | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
Details( | |
Summary("Implementations from DataTrove"), | |
Div( | |
D_code(""" | |
# that 80 % of words in a document contain at least one alphabetic character | |
if ( | |
self.max_non_alpha_words_ratio | |
and sum([any((c.isalpha() for c in w)) for w in words]) / n_words < self.max_non_alpha_words_ratio | |
): | |
return False, "gopher_below_alpha_threshold" | |
""", block="block", language="python"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #EAFFF1; /* Light yellow background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(""" | |
Both Dolma and DataTrove use `char.isalpha()` to detect whether a word contains alphabetic characters while | |
RedPajama-V2 employs regular expressions for this purpose. We opt to use regular expressions since `char.isalpha()` | |
can also match words in other languages as long as they are not punctuations. | |
"""), | |
P(B("Number of Stop Words: "), """ | |
The implementations across existing pipelines are largely identical. We adopt them and apply them to our pipeline. | |
"""), | |
D_code(""" | |
STOP_WORDS = ('the', 'be', 'to', 'of', 'and', 'that', 'have', 'with') | |
... | |
stop_words_pattern = re.compile("|".join(re.escape(symbol) for symbol in STOP_WORDS)) | |
... | |
attrs.num_of_stop_words = sum(1 for word in words if stop_words_pattern.search(word)) | |
""", block="block", language="python"), | |
H3("TxT360 Implementation"), | |
Details( | |
Summary("Documents Filtered by Statistics-Based Heuristics"), | |
Div( | |
DV( | |
"data/sample_doc_stat.json", | |
0, | |
"Sample documents that are filtered out by statistics-based heuristics", | |
), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
P(B("Additional Filters: "), """ | |
Following C4, we remove any page where the phrase “lorem ipsum” appeared since some pages had placeholder “lorem ipsum” | |
text. | |
"""), | |
Details( | |
Summary("Documents Containing 'lorem ipsum'"), | |
Div( | |
DV("data/lorem_ipsum.json", 0, "Sample documents containing 'lorem ipsum'"), | |
style="background-color: white; padding: 15px; margin-top: 10px; margin-bottom: 10px; border-radius: 8px; border: none; " # Styling for the DV2 part | |
), | |
style=""" | |
background-color: #F0F8FF; /* Light pink background */ | |
padding: 15px; | |
border-radius: 12px; | |
margin-bottom: 15px | |
""", | |
), | |
id="section5",), | |
) | |