code-search-net-java-permissive / filter_permissive.py
PiotrSty's picture
CodeSearchNet Java permissive-only: 334,367 rows
9124411 verified
Raw History Blame Contribute Delete
4.08 kB
#!/usr/bin/env python
"""Build the permissive-only subset from the open-licence CSN Java parquet."""
import gc
import json
import random
from collections import Counter
from pathlib import Path
import pyarrow as pa
import pyarrow.parquet as pq
HERE = Path(__file__).resolve().parent
SRC = Path(r"C:\Users\pstyl\AppData\Local\Temp\csn-java\code_search_net_java.parquet")
OUT = HERE / "code_search_net_java.parquet"
BUILD = HERE / "code_search_net_java_permissive.build.json"
SAMPLE = HERE / "code_search_net_java_permissive.sample.jsonl"
SAMPLE_LIC = HERE / "sample_licenses.json"
SUMMARY = HERE / "license_summary.json"
def main():
pf = pq.ParquetFile(SRC)
schema = pf.schema_arrow
writer = pq.ParquetWriter(OUT, schema, compression="zstd")
read = kept = removed = chars = tokens = 0
repos, splits, lic_dist = set(), Counter(), Counter()
for batch in pf.iter_batches(batch_size=8192):
out = []
for r in batch.to_pylist():
read += 1
if r.get("license_class") != "permissive":
removed += 1
continue
out.append(r)
kept += 1
chars += len(r["text"])
tokens += r["token_count"]
repos.add(r["repo"])
splits[r["split"]] += 1
lic_dist[r["license"]] += 1
if out:
writer.write_table(pa.Table.from_pylist(out, schema=schema))
writer.close()
try:
pf.close()
except Exception:
pass
del pf
gc.collect()
build = {
"read": read,
"kept": kept,
"removed_not_permissive": removed,
"source": "code-search-net/code_search_net (java), open-licence build",
"filter": "keep license_class == permissive",
"chars": chars,
"tokens": tokens,
"repos": len(repos),
"splits": dict(splits),
"bytes": OUT.stat().st_size,
"licenses": dict(lic_dist.most_common()),
"license_class_rows": {"permissive": kept},
"tokenizer": "tiktoken:cl100k_base",
}
BUILD.write_text(json.dumps(build, indent=2, ensure_ascii=False), encoding="utf-8")
# 12-row sample: distinct repos, seeded row groups
pf2 = pq.ParquetFile(OUT)
groups = list(range(pf2.num_row_groups))
random.Random(0).shuffle(groups)
picked, seen = [], set()
for g in groups:
if len(picked) >= 12:
break
t = pf2.read_row_group(g, columns=["id", "repo", "text", "token_count", "license"])
for r in t.to_pylist():
if r["repo"] not in seen:
seen.add(r["repo"])
picked.append(r)
break
pf2.close()
with SAMPLE.open("w", encoding="utf-8") as f:
for r in picked:
f.write(json.dumps({
"added": "2026-12-05", "author": r["repo"], "created": "2019",
"id": r["id"], "license": r["license"], "source": "code_search_net",
"text": r["text"], "token_count": r["token_count"],
}, ensure_ascii=False) + "\n")
SAMPLE_LIC.write_text(json.dumps({r["repo"]: r["license"] for r in picked},
indent=2, ensure_ascii=False), encoding="utf-8")
SUMMARY.write_text(json.dumps({
"filter": "permissive only (license_class == permissive)",
"rows": kept,
"repos": len(repos),
"rows_by_license": dict(lic_dist.most_common()),
"removed_not_permissive": removed,
"removed_from_total": 496677 - kept,
"splits": dict(splits),
"checked_at": "2026-12-05",
"source": "GitHub REST /repos/{owner}/{repo}/license",
}, indent=2, ensure_ascii=False), encoding="utf-8")
print(f"read={read:,} kept={kept:,} removed={removed:,}")
print("licences:", dict(lic_dist.most_common()))
print("repos:", len(repos), "splits:", dict(splits))
print("chars:", chars, "tokens:", tokens, "bytes:", OUT.stat().st_size)
print("sample:", [(r["repo"], r["license"]) for r in picked])
if __name__ == "__main__":
main()