File size: 11,726 Bytes
803b5e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""

Quality filtering, contamination control, and token-density validation.



Takes the deduplicated documents and applies final quality gates:



  QUALITY FILTERS:

    - Real source code (language signatures at line starts)

    - Balanced delimiters (braces/parens/brackets roughly match)

    - Reasonable length (200–100k chars)

    - Low repetition (unique line ratio)

    - Clean ASCII (low non-ASCII ratio)

    - High code-to-prose ratio

    - Has structure (function/class/struct definitions)



  CONTAMINATION CONTROL:

    - No test files (already filtered in download, double-check here)

    - No auto-generated code markers

    - No license-only files

    - No binary/garbage content

    - No files with extremely high repetition (copy-paste blocks)



  TOKEN DENSITY:

    - Every kept document must be "dense in tokens" β€” meaning the

      content tokenizes to a meaningful number of tokens relative to

      its character length (no whitespace-padding, no huge comment blocks).

    - Reports token density stats using the project tokenizer.



Input:  data/dedup/documents_dedup.jsonl

Output: data/quality/documents_quality.jsonl  +  quality_stats.json

"""

import json
import os
import re
import sys

PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_DIR = os.path.join(PROJECT_DIR, "data")
DEDUP_DOCS_PATH = os.path.join(DATA_DIR, "dedup", "documents_dedup.jsonl")
QUALITY_DIR = os.path.join(DATA_DIR, "quality")
QUALITY_DOCS_PATH = os.path.join(QUALITY_DIR, "documents_quality.jsonl")
STATS_PATH = os.path.join(QUALITY_DIR, "quality_stats.json")

# ─── Code structure patterns (must be at line start) ─────────────────────────
CODE_SIGNATURES = {
    "python":   re.compile(r"^(def |class |import |from \S+ import |if __name__|@|async def )", re.M),
    "js_ts":    re.compile(r"^(function |const |let |var |class |export |import |async function|interface |type \w+ =|enum )", re.M),
    "rust":     re.compile(r"^(fn |pub fn |impl |struct |enum |trait |mod |use |pub struct|pub enum|macro_rules!|pub trait)", re.M),
    "go":       re.compile(r"^(func |package |import |type \w+ struct|var |const )", re.M),
    "c":        re.compile(r"^(#include|#define|#ifndef|#ifdef|#if |#endif|typedef |struct \w+|static |void |int |char )", re.M),
    "cpp":      re.compile(r"^(#include|#define|#ifndef|#ifdef|#if |#endif|template |class \w+|namespace |struct \w+|void |int )", re.M),
    "java":     re.compile(r"^(public |private |protected |class \w+|import |package |interface )", re.M),
    "csharp":   re.compile(r"^(public |private |protected |internal |class \w+|using |namespace |interface )", re.M),
    "ruby":     re.compile(r"^(def |class |module |require |require_relative |attr_|include )", re.M),
}

# ─── Contamination / exclusion patterns ──────────────────────────────────────
AUTO_GEN = re.compile(
    r"(?:auto[- ]generated|do not edit|generated by|code generated|"
    r"DO NOT MODIFY|@generated|automatically generated|"
    r"this file was generated)",
    re.IGNORECASE,
)
LICENSE_ONLY = re.compile(r"^(?:/\*|//|#)\s*(?:copyright|licensed|mit license|apache license|bsd license|gnu|gpl)", re.I)
PROSE_LINE = re.compile(r"^[A-Z][a-z]+ .* [a-z]+\.$", re.M)

# ─── Quality scoring ─────────────────────────────────────────────────────────

def detect_language_signatures(doc: dict) -> int:
    """Count code-structure keywords at line starts. Returns count."""
    content = doc["content"]
    lang = doc["language"]
    pat = CODE_SIGNATURES.get(lang)
    if pat is None:
        # Try all
        return sum(len(p.findall(content)) for p in CODE_SIGNATURES.values())
    return len(pat.findall(content))


def score_document(doc: dict) -> tuple[float, str | None]:
    """Score a document 0.0–1.0 on quality. Returns (score, reject_reason)."""
    content = doc["content"]
    lines = content.split("\n")
    n_lines = len(lines)
    length = len(content)

    # ─── Hard rejects (contamination) ──────────────────────────────────────
    if length < 200:
        return 0.0, "too_short"
    if length > 200_000:
        return 0.0, "too_long"
    if n_lines < 5:
        return 0.0, "too_few_lines"

    # Auto-generated code
    if AUTO_GEN.search(content[:2000]):
        return 0.0, "auto_generated"

    # License-only files (first 10 lines are all license comments)
    first_lines = "\n".join(lines[:10])
    if LICENSE_ONLY.search(first_lines) and n_lines < 30:
        return 0.0, "license_only"

    # High non-ASCII (garbage/encoding issues)
    non_ascii = sum(1 for c in content if ord(c) > 127)
    if length > 0 and non_ascii / length > 0.03:
        return 0.0, "high_non_ascii"

    # Extremely high repetition (copy-paste blocks)
    unique_lines = len(set(lines))
    unique_ratio = unique_lines / max(n_lines, 1)
    if unique_ratio < 0.20:
        return 0.0, "high_repetition"

    # ─── Soft scoring ──────────────────────────────────────────────────────
    score = 0.0

    # Base: passes hard filters
    score += 0.15

    # Length quality (sweet spot: 500–30000 chars)
    if 500 <= length <= 30000:
        score += 0.15
    elif 200 <= length <= 80000:
        score += 0.08

    # Delimiter balance
    braces = content.count("{") - content.count("}")
    parens = content.count("(") - content.count(")")
    brackets = content.count("[") - content.count("]")
    total_delims = content.count("{") + content.count("(") + content.count("[")
    if total_delims > 0:
        imbalance = abs(braces) + abs(parens) + abs(brackets)
        balance_ratio = 1.0 - (imbalance / max(total_delims, 1))
        score += 0.15 * max(balance_ratio, 0.0)

    # Code structure density
    struct_count = detect_language_signatures(doc)
    struct_density = min(struct_count / max(n_lines, 1) * 10, 1.0)
    score += 0.15 * struct_density

    # Low repetition (unique line ratio)
    if unique_ratio > 0.7:
        score += 0.10
    elif unique_ratio > 0.5:
        score += 0.05
    else:
        score -= 0.05

    # Clean ASCII
    if length > 0 and non_ascii / length < 0.005:
        score += 0.05

    # Indentation quality (indented lines indicate real code structure)
    indented = sum(1 for l in lines if l.startswith("    ") or l.startswith("\t"))
    if indented > 0 and indented / max(n_lines, 1) > 0.15:
        score += 0.05

    # Penalize high prose ratio (documentation, not code)
    prose_lines = len(PROSE_LINE.findall(content))
    prose_ratio = prose_lines / max(n_lines, 1)
    if prose_ratio > 0.20:
        score -= 0.15

    # Comment density (sweet spot: 3–30%)
    comment_lines = 0
    for line in lines:
        s = line.strip()
        if s.startswith("#") or s.startswith("//") or s.startswith("/*") \
           or s.startswith("*") or s.startswith('"""') or s.startswith("'''") \
           or s.startswith("///") or s.startswith("//!"):
            comment_lines += 1
    comment_ratio = comment_lines / max(n_lines, 1)
    if 0.03 <= comment_ratio <= 0.30:
        score += 0.10
    elif comment_ratio > 0.50:
        score -= 0.10  # too many comments = doc, not code

    return min(max(score, 0.0), 1.0), None


def check_token_density(doc: dict) -> tuple[bool, float]:
    """Check that a document is dense in tokens (not whitespace-padded).



    Returns (passes, chars_per_token_ratio).

    A good code document should have ~2.5-5 chars per token.

    If the ratio is very high (>15), it's likely whitespace/garbage.

    If very low (<1.5), it may be all symbols.

    """
    content = doc["content"]
    # Rough estimate: count non-whitespace characters as a proxy
    # Real tokenization happens with the tokenizer, but this is a fast filter
    non_ws = len(content) - content.count(" ") - content.count("\n") - content.count("\t") - content.count("\r")
    # Approximate token count: split on whitespace + common code delimiters
    approx_tokens = len(re.findall(r"\w+|[^\w\s]", content))
    if approx_tokens == 0:
        return False, 0.0
    chars_per_token = len(content) / approx_tokens
    # Good density: 2.0 - 8.0 chars per token
    passes = 2.0 <= chars_per_token <= 12.0
    return passes, chars_per_token


def main():
    os.makedirs(QUALITY_DIR, exist_ok=True)

    print(f"Loading deduped documents from {DEDUP_DOCS_PATH}...")
    docs = []
    with open(DEDUP_DOCS_PATH, "r", encoding="utf-8") as f:
        for line in f:
            docs.append(json.loads(line))
    print(f"  Loaded {len(docs):,} documents")

    kept = []
    reject_reasons = {}
    scores = []
    token_densities = []

    for i, doc in enumerate(docs):
        score, reject = score_document(doc)

        if reject:
            reject_reasons[reject] = reject_reasons.get(reject, 0) + 1
            continue

        if score < 0.40:
            reject_reasons["low_score"] = reject_reasons.get("low_score", 0) + 1
            continue

        # Token density check
        dense, cpt = check_token_density(doc)
        token_densities.append(cpt)
        if not dense:
            reject_reasons["low_token_density"] = reject_reasons.get("low_token_density", 0) + 1
            continue

        doc = dict(doc)
        doc["quality_score"] = round(score, 4)
        kept.append(doc)
        scores.append(score)

        if (i + 1) % 5000 == 0:
            print(f"  Processed {i+1}/{len(docs)} | kept {len(kept)} | "
                  f"rejected {i+1 - len(kept)}")

    # Stats
    final_size = sum(len(d["content"]) for d in kept)
    avg_score = sum(scores) / len(scores) if scores else 0
    avg_cpt = sum(token_densities) / len(token_densities) if token_densities else 0

    lang_counts = {}
    for d in kept:
        lang_counts[d["language"]] = lang_counts.get(d["language"], 0) + 1

    stats = {
        "input": len(docs),
        "kept": len(kept),
        "rejected": len(docs) - len(kept),
        "reject_reasons": reject_reasons,
        "final_size_mb": final_size / 1e6,
        "avg_quality_score": round(avg_score, 4),
        "avg_chars_per_token": round(avg_cpt, 2),
        "language_distribution": lang_counts,
        "min_score_threshold": 0.40,
    }

    print("\n" + "=" * 60)
    print("QUALITY FILTERING COMPLETE")
    print("=" * 60)
    print(f"  Input:    {len(docs):,}")
    print(f"  Kept:     {len(kept):,}")
    print(f"  Rejected: {len(docs) - len(kept):,}")
    print(f"  Reject reasons: {reject_reasons}")
    print(f"  Final size: {final_size / 1e6:.1f} MB")
    print(f"  Avg quality score: {avg_score:.3f}")
    print(f"  Avg chars/token: {avg_cpt:.2f}")
    print(f"  Languages: {lang_counts}")

    # Write quality-filtered documents
    with open(QUALITY_DOCS_PATH, "w", encoding="utf-8") as f:
        for doc in kept:
            f.write(json.dumps(doc, ensure_ascii=False) + "\n")
    print(f"\nQuality documents written to {QUALITY_DOCS_PATH}")

    with open(STATS_PATH, "w") as f:
        json.dump(stats, f, indent=2)
    print(f"Stats written to {STATS_PATH}")


if __name__ == "__main__":
    main()