Add Common Corpus English filter and held-out split
This commit is contained in:
1 parent
a7db571513
commit
6965ab25db
4 files changed
+249
No files matched your search
@@ -1,2 +1,6 @@
|
|||||||
.venv/
|
.venv/
|
||||||
*.jsonl
|
*.jsonl
|
||||||
|
|
||||||
|
# Raw downloads and generated corpora are rebuilt from source manifests
|
||||||
|
sources/raw/
|
||||||
|
!eval/*.jsonl
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"path": "sources/raw/common_corpus_1/subset_1_1.parquet",
|
||||||
|
"sha256": "ca4783ce361370c43a8c7b21378d8728807fb044af8ec0a9b2fa3535844037e6"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "sources/raw/common_corpus_1/subset_9_1.parquet",
|
||||||
|
"sha256": "f81d0cd4a43ac46d11c18845c93964ca14d6248092562066162d8e5d3a72735f"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "sources/raw/common_corpus_1/subset_9_2.parquet",
|
||||||
|
"sha256": "ced4bd8593d4fd0de27a5ef61491b6da335c3045f9f6564aa884d27dc08ee8a8"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "sources/raw/common_corpus_1/subset_9_3.parquet",
|
||||||
|
"sha256": "e6357cd7cd7fa8c77f4b4c9aff55168efc17bea979f0ebe3a73a89a3a37e3bd1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "sources/raw/common_corpus_1/subset_9_4.parquet",
|
||||||
|
"sha256": "270353569af46bd2d9b0e89700a7b190ef7db4a7b049886536dd51b9d9ae5211"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "sources/raw/common_corpus_1/subset_9_5.parquet",
|
||||||
|
"sha256": "3d4c3738055b2a3357566a8a19d9f170c5c315902249eea44e7dfe7e8f2ca487"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "sources/raw/common_corpus_1/subset_9_6.parquet",
|
||||||
|
"sha256": "181103133717dc17b251eca06010cd057dffaebf9660503b9c7893189d3fe21d"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "sources/raw/common_corpus_1/subset_9_7.parquet",
|
||||||
|
"sha256": "cf9d62b00f36cb3d52981305fa000340ff17db9f2799abf561d0efcc91e38716"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "sources/raw/common_corpus_1/subset_9_8.parquet",
|
||||||
|
"sha256": "fd895489e53bc0e5524e75cb7a8b393ac9c5b97b05f3baca2adfa63583897991"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "sources/raw/common_corpus_1/subset_9_9.parquet",
|
||||||
|
"sha256": "19e64bbd57b0f3d810f717dfdd0d12b0e5ffc27a78678aa7a152a1010e8401ea"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"output": {
|
||||||
|
"path": "corpora/english-prose.jsonl",
|
||||||
|
"sha256": "cb5bf1677813a1b09a411b365e2cbddd70def7ef5aff3e30cbe200614249f828"
|
||||||
|
},
|
||||||
|
"allowlists": {
|
||||||
|
"licenses": [
|
||||||
|
"CC-BY-4.0",
|
||||||
|
"CC-By",
|
||||||
|
"CC-By-SA",
|
||||||
|
"CC0",
|
||||||
|
"CC0-1.0",
|
||||||
|
"Public Domain"
|
||||||
|
],
|
||||||
|
"collections": [
|
||||||
|
"English-PD",
|
||||||
|
"US-PD-Books",
|
||||||
|
"Wikipedia"
|
||||||
|
],
|
||||||
|
"language": "English",
|
||||||
|
"language_type": "Written"
|
||||||
|
},
|
||||||
|
"min_words": 50,
|
||||||
|
"documents_kept": 27549,
|
||||||
|
"words_kept": 160029784,
|
||||||
|
"kept_by_license": {
|
||||||
|
"Public Domain": 21919,
|
||||||
|
"CC-By-SA": 5630
|
||||||
|
},
|
||||||
|
"share_alike_documents": 5630,
|
||||||
|
"removed_by_rule": {
|
||||||
|
"not_english_written": 526916,
|
||||||
|
"collection_not_allowed": 149497,
|
||||||
|
"too_short": 857,
|
||||||
|
"not_mostly_letters": 269,
|
||||||
|
"odd_word_length": 10
|
||||||
|
},
|
||||||
|
"documents_seen": 705098
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Write a training corpus with the held-out eval documents removed.
|
||||||
|
|
||||||
|
Documents are matched by identifier, which is how the held-out sample was drawn.
|
||||||
|
The script fails if any held-out identifier is missing from the input, because that
|
||||||
|
would mean the held-out list and the corpus have drifted apart.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
parser.add_argument("--input-file", required=True, type=Path)
|
||||||
|
parser.add_argument("--heldout-ids", required=True, type=Path)
|
||||||
|
parser.add_argument("--output-file", required=True, type=Path)
|
||||||
|
args = parser.parse_args()
|
||||||
|
if args.output_file.exists():
|
||||||
|
parser.error(f"refusing to overwrite: {args.output_file}")
|
||||||
|
heldout = {line.strip() for line in args.heldout_ids.read_text(encoding="utf-8").splitlines()
|
||||||
|
if line.strip()}
|
||||||
|
found, kept, removed = set(), 0, 0
|
||||||
|
with args.input_file.open(encoding="utf-8") as source, \
|
||||||
|
args.output_file.open("x", encoding="utf-8") as target:
|
||||||
|
for line in source:
|
||||||
|
identifier = json.loads(line)["identifier"]
|
||||||
|
if identifier in heldout:
|
||||||
|
found.add(identifier)
|
||||||
|
removed += 1
|
||||||
|
continue
|
||||||
|
target.write(line)
|
||||||
|
kept += 1
|
||||||
|
missing = heldout - found
|
||||||
|
if missing:
|
||||||
|
args.output_file.unlink()
|
||||||
|
parser.error(f"{len(missing)} held-out identifiers not found in the corpus")
|
||||||
|
print(json.dumps({"kept": kept, "removed": removed, "heldout_ids": len(heldout)}, indent=2))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Filter Common Corpus Parquet shards to openly licensed English prose.
|
||||||
|
|
||||||
|
Every kept document keeps its identifier, collection and license, so provenance
|
||||||
|
travels with the text. A manifest records the inputs (SHA-256), the allowlists, and
|
||||||
|
how many documents each rule removed. Run with an environment that has pyarrow.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from collections import Counter
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
RESERVED = ("<|pad|>", "<|im_start|>", "<|im_end|>")
|
||||||
|
LICENSES = {"Public Domain", "CC0", "CC0-1.0", "CC-By", "CC-BY-4.0", "CC-By-SA"}
|
||||||
|
COLLECTIONS = {"Wikipedia", "English-PD", "US-PD-Books"}
|
||||||
|
SHARE_ALIKE = {"CC-By-SA"}
|
||||||
|
COLUMNS = ["identifier", "collection", "license", "language", "language_type", "text"]
|
||||||
|
|
||||||
|
|
||||||
|
def file_digest(path):
|
||||||
|
with Path(path).open("rb") as stream:
|
||||||
|
return hashlib.file_digest(stream, "sha256").hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
def prose_quality(text, min_words):
|
||||||
|
"""Reject short, symbol-heavy or OCR-garbled text."""
|
||||||
|
words = text.split()
|
||||||
|
if len(words) < min_words:
|
||||||
|
return "too_short"
|
||||||
|
compact = "".join(text.split())
|
||||||
|
letters = sum(char.isalpha() for char in compact)
|
||||||
|
if letters / max(len(compact), 1) < 0.7:
|
||||||
|
return "not_mostly_letters"
|
||||||
|
mean_length = sum(len(word) for word in words) / len(words)
|
||||||
|
if not 2.5 <= mean_length <= 12:
|
||||||
|
return "odd_word_length"
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
import pyarrow.parquet as pq
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
parser.add_argument("--input-file", action="append", required=True, type=Path)
|
||||||
|
parser.add_argument("--output-file", required=True, type=Path)
|
||||||
|
parser.add_argument("--manifest-file", required=True, type=Path)
|
||||||
|
parser.add_argument("--min-words", type=int, default=50)
|
||||||
|
args = parser.parse_args()
|
||||||
|
for path in (args.output_file, args.manifest_file):
|
||||||
|
if path.exists():
|
||||||
|
parser.error(f"refusing to overwrite: {path}")
|
||||||
|
|
||||||
|
reasons = Counter()
|
||||||
|
seen = set()
|
||||||
|
kept = Counter()
|
||||||
|
words = 0
|
||||||
|
shares = 0
|
||||||
|
args.output_file.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
with args.output_file.open("x", encoding="utf-8") as out:
|
||||||
|
for path in args.input_file:
|
||||||
|
parquet = pq.ParquetFile(path)
|
||||||
|
missing = [c for c in COLUMNS if c not in parquet.schema_arrow.names]
|
||||||
|
if missing:
|
||||||
|
parser.error(f"{path}: missing column(s): {', '.join(missing)}")
|
||||||
|
for batch in parquet.iter_batches(columns=COLUMNS):
|
||||||
|
for row in batch.to_pylist():
|
||||||
|
reasons["seen"] += 1
|
||||||
|
if row["language"] != "English" or row["language_type"] != "Written":
|
||||||
|
reasons["not_english_written"] += 1
|
||||||
|
continue
|
||||||
|
if row["collection"] not in COLLECTIONS:
|
||||||
|
reasons["collection_not_allowed"] += 1
|
||||||
|
continue
|
||||||
|
if row["license"] not in LICENSES:
|
||||||
|
reasons["license_not_allowed"] += 1
|
||||||
|
continue
|
||||||
|
text = row["text"] or ""
|
||||||
|
if any(token in text for token in RESERVED):
|
||||||
|
reasons["reserved_token"] += 1
|
||||||
|
continue
|
||||||
|
problem = prose_quality(text, args.min_words)
|
||||||
|
if problem:
|
||||||
|
reasons[problem] += 1
|
||||||
|
continue
|
||||||
|
digest = hashlib.sha256(" ".join(text.split()).lower().encode()).hexdigest()
|
||||||
|
if digest in seen:
|
||||||
|
reasons["duplicate"] += 1
|
||||||
|
continue
|
||||||
|
seen.add(digest)
|
||||||
|
record = {"text": text, "identifier": row["identifier"],
|
||||||
|
"collection": row["collection"], "license": row["license"]}
|
||||||
|
out.write(json.dumps(record, ensure_ascii=False) + "\n")
|
||||||
|
kept[row["license"]] += 1
|
||||||
|
words += len(text.split())
|
||||||
|
shares += row["license"] in SHARE_ALIKE
|
||||||
|
reasons["kept"] += 1
|
||||||
|
if not reasons["kept"]:
|
||||||
|
args.output_file.unlink()
|
||||||
|
parser.error("no documents passed the filters")
|
||||||
|
|
||||||
|
manifest = {
|
||||||
|
"inputs": [{"path": str(p), "sha256": file_digest(p)} for p in args.input_file],
|
||||||
|
"output": {"path": str(args.output_file), "sha256": file_digest(args.output_file)},
|
||||||
|
"allowlists": {"licenses": sorted(LICENSES), "collections": sorted(COLLECTIONS),
|
||||||
|
"language": "English", "language_type": "Written"},
|
||||||
|
"min_words": args.min_words,
|
||||||
|
"documents_kept": reasons["kept"],
|
||||||
|
"words_kept": words,
|
||||||
|
"kept_by_license": dict(kept),
|
||||||
|
"share_alike_documents": shares,
|
||||||
|
"removed_by_rule": {k: v for k, v in reasons.items() if k not in ("kept", "seen")},
|
||||||
|
"documents_seen": reasons["seen"],
|
||||||
|
}
|
||||||
|
args.manifest_file.write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
|
||||||
|
print(json.dumps({k: manifest[k] for k in ("documents_seen", "documents_kept", "words_kept",
|
||||||
|
"kept_by_license", "removed_by_rule")}, indent=2))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
Reference in new issue
Block a user