File size: 4,943 Bytes
7af9206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
preprocess.py
-------------
Step 1: Filter Python-tagged questions, join best answers, clean HTML.
Output: data/clean_qa.parquet  (~15K top-scored Python Q&A pairs)

Run once locally before ingesting:
    python preprocess.py
"""

import pandas as pd
from bs4 import BeautifulSoup
import re
import os

DATASET_PATH = "dataset/archive"
OUTPUT_PATH  = "data/clean_qa.parquet"
TOP_N        = 15000   # Keep top N questions by score (keeps file sizes manageable)

# ─── 1. Load Tags β†’ get Python question IDs ──────────────────────────────────
print("πŸ“‚ Loading Tags.csv...")
tags = pd.read_csv(f"{DATASET_PATH}/Tags.csv", encoding="latin-1")
python_ids = set(tags[tags["Tag"] == "python"]["Id"].tolist())
print(f"   βœ… {len(python_ids):,} Python-tagged question IDs found")

# ─── 2. Load Questions in chunks (file is ~888MB) ────────────────────────────
print("\nπŸ“‚ Loading Questions.csv (chunked, this may take a minute)...")
q_chunks = []
for chunk in pd.read_csv(
    f"{DATASET_PATH}/Questions.csv",
    encoding="latin-1",
    chunksize=50_000,
    on_bad_lines="skip",
):
    filtered = chunk[chunk["Id"].isin(python_ids)]
    if not filtered.empty:
        q_chunks.append(filtered)

questions = pd.concat(q_chunks, ignore_index=True)
print(f"   βœ… {len(questions):,} Python questions loaded")

# Keep only top N by vote score
questions = questions.sort_values("Score", ascending=False).head(TOP_N)
print(f"   βœ… Trimmed to top {len(questions):,} by score")

# ─── 3. Load Answers for only those questions ────────────────────────────────
print("\nπŸ“‚ Loading Answers.csv (chunked, this may take a minute)...")
target_ids = set(questions["Id"].tolist())
a_chunks = []
for chunk in pd.read_csv(
    f"{DATASET_PATH}/Answers.csv",
    encoding="latin-1",
    chunksize=50_000,
    on_bad_lines="skip",
):
    filtered = chunk[chunk["ParentId"].isin(target_ids)]
    if not filtered.empty:
        a_chunks.append(filtered)

answers = pd.concat(a_chunks, ignore_index=True)
print(f"   βœ… {len(answers):,} answers loaded")

# ─── 4. Pick best answer per question (highest score) ────────────────────────
best_answers = (
    answers.sort_values("Score", ascending=False)
    .groupby("ParentId", as_index=False)
    .first()
)
best_answers = best_answers.rename(
    columns={"ParentId": "QuestionId", "Body": "AnswerBody", "Score": "AnswerScore"}
)
print(f"   βœ… {len(best_answers):,} best answers selected")

# ─── 5. Clean HTML ────────────────────────────────────────────────────────────
def clean_html(html: str) -> str:
    """Strip HTML tags, preserve code blocks with backtick markers."""
    if pd.isna(html):
        return ""
    soup = BeautifulSoup(str(html), "html.parser")
    # Mark code blocks so they stay readable
    for code in soup.find_all("code"):
        code.string = f"\n```\n{code.get_text()}\n```\n"
    text = soup.get_text(separator=" ")
    text = re.sub(r"\s+", " ", text).strip()
    return text

print("\n🧹 Cleaning HTML bodies...")
questions["CleanBody"]    = questions["Body"].apply(clean_html)
best_answers["CleanAnswer"] = best_answers["AnswerBody"].apply(clean_html)

# ─── 6. Merge questions + best answers ───────────────────────────────────────
merged = questions.merge(
    best_answers[["QuestionId", "CleanAnswer", "AnswerScore"]],
    left_on="Id",
    right_on="QuestionId",
    how="inner",
)

merged = merged[[
    "Id", "Title", "CleanBody", "Score", "CleanAnswer", "AnswerScore"
]].copy()
merged.columns = [
    "question_id", "title", "question_body", "question_score",
    "answer_body", "answer_score"
]

# ─── 7. Basic quality filters ─────────────────────────────────────────────────
merged = merged[merged["answer_body"].str.len() > 50]
merged = merged[merged["question_body"].str.len() > 20]
merged = merged[merged["title"].str.len() > 5]
merged = merged.drop_duplicates(subset="question_id")

print(f"\nβœ… Final dataset: {len(merged):,} Q&A pairs")

# ─── 8. Save ──────────────────────────────────────────────────────────────────
os.makedirs("data", exist_ok=True)
merged.to_parquet(OUTPUT_PATH, index=False)
print(f"πŸ’Ύ Saved β†’ {OUTPUT_PATH}")

print("\nSample row:")
print(merged.iloc[0][["title", "question_body", "answer_body"]].to_string())