File size: 9,690 Bytes
5ae3e12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import csv
import html
import json
import logging
import re
import sqlite3
from pathlib import Path
from typing import Dict, Iterable, Iterator, List, Tuple

from tqdm import tqdm


HTML_TAG_RE = re.compile(r"<[^>]+>")
WS_RE = re.compile(r"\s+")
CODE_HINT_RE = re.compile(
    r"(\bdef\b|\bclass\b|#include|public\s+class|function\s+\w+|\breturn\b|\bimport\b)",
    re.IGNORECASE,
)


def setup_logger(log_path: Path) -> logging.Logger:
    log_path.parent.mkdir(parents=True, exist_ok=True)
    logger = logging.getLogger("codeforces_ingest")
    logger.setLevel(logging.INFO)
    if logger.handlers:
        return logger
    formatter = logging.Formatter("%(asctime)s | %(levelname)s | %(message)s")
    fh = logging.FileHandler(log_path, encoding="utf-8")
    fh.setFormatter(formatter)
    sh = logging.StreamHandler()
    sh.setFormatter(formatter)
    logger.addHandler(fh)
    logger.addHandler(sh)
    return logger


def clean_text(text: str) -> str:
    if not text:
        return ""
    text = html.unescape(str(text))
    text = HTML_TAG_RE.sub(" ", text)
    text = text.replace("\r\n", "\n").replace("\r", "\n")
    text = "\n".join(line.strip() for line in text.split("\n"))
    text = WS_RE.sub(" ", text)
    return text.strip()


def _safe_get(record: Dict[str, object], keys: Iterable[str]) -> str:
    for key in keys:
        val = record.get(key)
        if val:
            return str(val)
    return ""


def _extract_pair(record: Dict[str, object]) -> Tuple[str, str]:
    problem = _safe_get(
        record,
        [
            "problem_statement",
            "statement",
            "problem",
            "question",
            "content",
            "description",
            "prompt",
            "instruction",
        ],
    )
    solution = _safe_get(
        record,
        [
            "solution",
            "solution_code",
            "answer",
            "code",
            "response",
            "python",
            "cpp",
            "java",
            "javascript",
        ],
    )
    return clean_text(problem), clean_text(solution)


def _iter_jsonl(path: Path) -> Iterator[Dict[str, object]]:
    with path.open("r", encoding="utf-8", errors="ignore") as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            try:
                obj = json.loads(line)
                if isinstance(obj, dict):
                    yield obj
            except Exception:
                continue


def _iter_json_stream(path: Path, logger: logging.Logger) -> Iterator[Dict[str, object]]:
    # Streaming JSON parser for array/object JSON files if ijson is available.
    try:
        import ijson  # type: ignore
    except Exception:
        logger.warning("Skipping JSON file without ijson installed (to preserve streaming): %s", path)
        return iter(())

    def gen():
        with path.open("rb") as f:
            try:
                for obj in ijson.items(f, "item"):
                    if isinstance(obj, dict):
                        yield obj
            except Exception:
                # Some files may be top-level dicts with nested lists.
                f.seek(0)
                try:
                    root = next(ijson.items(f, ""))
                    if isinstance(root, dict):
                        for v in root.values():
                            if isinstance(v, list):
                                for obj in v:
                                    if isinstance(obj, dict):
                                        yield obj
                except Exception:
                    return

    return gen()


def _iter_csv_like(path: Path, delimiter: str) -> Iterator[Dict[str, object]]:
    with path.open("r", encoding="utf-8", errors="ignore", newline="") as f:
        reader = csv.DictReader(f, delimiter=delimiter)
        for row in reader:
            if isinstance(row, dict):
                yield row


def _iter_txt_records(path: Path) -> Iterator[Dict[str, object]]:
    # Heuristic fallback for text dumps:
    # split on obvious separators and map to pseudo records.
    with path.open("r", encoding="utf-8", errors="ignore") as f:
        blob = f.read()
    chunks = re.split(r"\n\s*[-=]{3,}\s*\n|\n\s*Problem\s+\d+\s*\n", blob, flags=re.IGNORECASE)
    for chunk in chunks:
        chunk = chunk.strip()
        if len(chunk) < 120:
            continue
        parts = re.split(r"\n\s*(Solution|Answer)\s*:\s*\n", chunk, flags=re.IGNORECASE)
        if len(parts) < 3:
            continue
        yield {"problem_statement": parts[0], "solution": parts[2]}


def _iter_candidate_files(input_dir: Path) -> Iterator[Path]:
    patterns = [
        "**/*.jsonl",
        "**/*.json",
        "**/*.csv",
        "**/*.tsv",
        "**/*.txt",
    ]
    seen = set()
    for pat in patterns:
        for path in input_dir.glob(pat):
            if ".git" in path.parts:
                continue
            lower = str(path).lower()
            if "codeforces" not in lower:
                continue
            if path.name.lower() == "codeforces.jsonl":
                continue
            if path.is_file() and path not in seen:
                seen.add(path)
                yield path


def ingest_codeforces(input_dir: Path, output_file: Path, logger: logging.Logger) -> Dict[str, int]:
    output_file.parent.mkdir(parents=True, exist_ok=True)
    extracted = 0
    filtered = 0
    bad = 0
    usable = 0
    deduped = 0

    files = list(_iter_candidate_files(input_dir))
    if not files:
        logger.warning("No Codeforces raw files found under %s", input_dir.resolve())
        output_file.write_text("", encoding="utf-8")
        return {"extracted": 0, "filtered": 0, "bad": 0, "usable": 0}

    dedupe_db = output_file.parent / "codeforces_ingest_dedupe.sqlite"
    if dedupe_db.exists():
        dedupe_db.unlink()
    for suffix in ("-wal", "-shm"):
        s = dedupe_db.with_name(dedupe_db.name + suffix)
        if s.exists():
            s.unlink()

    conn = sqlite3.connect(str(dedupe_db))
    conn.execute("PRAGMA journal_mode=WAL;")
    conn.execute("CREATE TABLE IF NOT EXISTS seen_hashes (h TEXT PRIMARY KEY)")

    def is_dup(instruction: str, response: str) -> bool:
        import hashlib

        h = hashlib.sha256(f"{instruction}||{response}".encode("utf-8")).hexdigest()
        try:
            conn.execute("INSERT INTO seen_hashes(h) VALUES (?)", (h,))
            return False
        except sqlite3.IntegrityError:
            return True

    with output_file.open("w", encoding="utf-8") as out_f:
        for file_path in tqdm(files, desc="codeforces_files", unit="file"):
            suffix = file_path.suffix.lower()
            if suffix == ".jsonl":
                rec_iter = _iter_jsonl(file_path)
            elif suffix == ".json":
                rec_iter = _iter_json_stream(file_path, logger)
            elif suffix == ".csv":
                rec_iter = _iter_csv_like(file_path, ",")
            elif suffix == ".tsv":
                rec_iter = _iter_csv_like(file_path, "\t")
            else:
                rec_iter = _iter_txt_records(file_path)

            for rec in tqdm(rec_iter, desc=f"ingest:{file_path.name}", unit="rows", leave=False):
                try:
                    extracted += 1
                    problem, solution = _extract_pair(rec)
                    if len(problem) <= 50 or len(solution) <= 20:
                        filtered += 1
                        continue
                    # Keep response as solution code; reject obvious non-code text.
                    if not CODE_HINT_RE.search(solution):
                        filtered += 1
                        continue
                    instruction = f"Solve the following problem:\n{problem}"
                    if is_dup(instruction, solution):
                        deduped += 1
                        continue
                    row = {"instruction": instruction, "response": solution}
                    out_f.write(json.dumps(row, ensure_ascii=False) + "\n")
                    usable += 1
                except Exception:
                    bad += 1
                    continue
            conn.commit()
    conn.close()

    skipped = filtered + bad + deduped
    logger.info("Codeforces ingest total_input=%d", extracted)
    logger.info("Codeforces ingest valid_output=%d", usable)
    logger.info("Codeforces ingest skipped=%d", skipped)
    logger.info("Codeforces ingest filtered=%d", filtered)
    logger.info("Codeforces ingest deduped=%d", deduped)
    logger.info("Codeforces ingest bad=%d", bad)
    return {
        "total_input": extracted,
        "valid_output": usable,
        "skipped": skipped,
        "filtered": filtered,
        "deduped": deduped,
        "bad": bad,
    }


def _build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(
        description="Ingest Codeforces problem-solution data into JSONL for MINDI pipeline."
    )
    parser.add_argument("--input-dir", type=Path, default=Path("./data/raw"))
    parser.add_argument("--output", type=Path, default=Path("./data/raw/codeforces.jsonl"))
    parser.add_argument("--log-file", type=Path, default=Path("./logs/codeforces_ingest.log"))
    return parser


if __name__ == "__main__":
    args = _build_parser().parse_args()
    log = setup_logger(args.log_file)
    stats = ingest_codeforces(args.input_dir, args.output, log)
    print(f"Output: {args.output.resolve()}")
    print(f"Total input: {stats['total_input']}")
    print(f"Valid output: {stats['valid_output']}")
    print(f"Skipped: {stats['skipped']}")