File size: 1,331 Bytes
04782c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""SO-arm datasets in the listing, and the per-author sample that was read.

usage: sample.py OUT_DIR/datasets.jsonl OUT_DIR
Replaces OUT_DIR/so_ids.txt (every SO-arm id, sorted) and OUT_DIR/sample_ids.txt (up to 3 per author, sorted).

SO-arm rule: the case-insensitive pattern below matches anywhere in the full repository id (author namespace included)
or in any tag. Sample: for each author in sorted order, that author's sorted ids are shuffled with one random.Random(SEED)
and the first 3 kept."""
import collections, json, os, random, re, sys

SO = re.compile(r"so[\W_]?10[01]|so[\W_]?arm|so[\W_]?follower", re.I)
SEED = 20261118

rows = [json.loads(l) for l in open(sys.argv[1]) if l.strip()]
ids = sorted({r["id"] for r in rows if r.get("id") and SO.search(" ".join([r["id"]] + (r.get("tags") or [])))})
by = collections.defaultdict(list)
for i in ids:
    by[i.split("/")[0]].append(i)
rng, sample = random.Random(SEED), []
for a in sorted(by):
    xs = sorted(by[a]); rng.shuffle(xs); sample += xs[:3]
os.makedirs(sys.argv[2], exist_ok=True)
for name, xs in (("so_ids.txt", ids), ("sample_ids.txt", sorted(sample))):
    with open(os.path.join(sys.argv[2], name), "w") as f:
        f.write("".join(x + "\n" for x in xs))
print(len(rows), "listed;", len(ids), "SO-arm from", len(by), "authors;", len(sample), "sampled")