File size: 2,547 Bytes
a64817d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
"""12-record deterministic sample in the DynaWord sample schema.

Picks one row from 12 seeded distinct row groups, forcing 12 distinct repos,
then resolves each repo's SPDX id via the GitHub licence API (unauthenticated,
12 calls). Falls back to "other" when unknown/rate-limited.
"""
import json
import random
from pathlib import Path

import pyarrow.parquet as pq
import requests

HERE = Path(__file__).resolve().parent
PARQUET = HERE / "code_search_net_java.parquet"
OUT = HERE / "code_search_net_java.sample.jsonl"
CACHE = HERE / "sample_licenses.json"

ADDED = "2026-12-05"
CREATED = "2019"
SOURCE = "code_search_net"


def gh_license(repo):
    try:
        r = requests.get(
            f"https://api.github.com/repos/{repo}/license",
            headers={"Accept": "application/vnd.github+json"},
            timeout=20,
        )
        if r.status_code == 200:
            spdx = (r.json().get("license") or {}).get("spdx_id")
            if spdx and spdx != "NOASSERTION":
                return spdx
    except Exception:
        pass
    return "other"


def main():
    pf = pq.ParquetFile(PARQUET)
    groups = list(range(pf.num_row_groups))
    rng = random.Random(0)
    rng.shuffle(groups)

    picked, seen = [], set()
    for g in groups:
        if len(picked) >= 12:
            break
        table = pf.read_row_group(
            g, columns=["id", "repo", "text", "token_count"])
        for r in table.to_pylist():
            if r["repo"] not in seen:
                seen.add(r["repo"])
                picked.append(r)
                break

    licences = {}
    for r in picked:
        licences[r["repo"]] = gh_license(r["repo"])

    with OUT.open("w", encoding="utf-8") as f:
        for r in picked[:12]:
            rec = {
                "added": ADDED,
                "author": r["repo"],
                "created": CREATED,
                "id": r["id"],
                "license": licences[r["repo"]],
                "source": SOURCE,
                "text": r["text"],
                "token_count": r["token_count"],
            }
            f.write(json.dumps(rec, ensure_ascii=False) + "\n")

    CACHE.write_text(json.dumps(licences, indent=2, ensure_ascii=False),
                     encoding="utf-8")
    print("records:", len(picked[:12]))
    print("repos:", [r["repo"] for r in picked[:12]])
    print("licences:", licences)
    print("tokens:", sum(r["token_count"] for r in picked[:12]))
    print("size:", OUT.stat().st_size)


if __name__ == "__main__":
    main()