|
|
import os |
|
|
import re |
|
|
import pandas as pd |
|
|
from argparse import ArgumentParser |
|
|
from src.conversation import parse_conversation |
|
|
|
|
|
def parse_args(): |
|
|
parser = ArgumentParser(description="Process the Socratic data.") |
|
|
parser.add_argument("--input", type=str, |
|
|
help="Input directory containing raw data.", |
|
|
default="data/raw/") |
|
|
return parser.parse_args() |
|
|
|
|
|
def process(data_folder): |
|
|
|
|
|
files = os.listdir(data_folder) |
|
|
files = sorted([f for f in files if f.endswith("socratic_dialogue.txt")], key= lambda t: t[: t.find("_")]) |
|
|
files = [os.path.join(data_folder, f) for f in files] |
|
|
|
|
|
dataframe = [] |
|
|
for path in files: |
|
|
with open(path, "r") as fp: |
|
|
info = extract_information(fp.read()) |
|
|
info["id"] = path.split("/")[-1].replace("_socratic_dialogue.txt", "") |
|
|
dataframe.append(info) |
|
|
|
|
|
dataframe = pd.DataFrame(dataframe) |
|
|
|
|
|
dataframe.bug_code = dataframe.bug_code.apply(remove_line_numbers) |
|
|
dataframe["original_text_for_feedback"] = dataframe["original_text"].apply(lambda t: t[:t.find("<stu_desc>")]) |
|
|
dataframe["conversation"] = dataframe.original_text.apply(parse_conversation) |
|
|
|
|
|
return dataframe |
|
|
|
|
|
def extract_information(data): |
|
|
beacons = ["problem", "bug_code", "bug_desc", "bug_fixes", "unit_tests"] |
|
|
information = {"original_text": data} |
|
|
for beacon in beacons: |
|
|
start = data.find(f"<{beacon}>") + len(f"<{beacon}>") |
|
|
end = data.find(f"</{beacon}>") |
|
|
information[beacon] = data[start: end].strip() |
|
|
|
|
|
return information |
|
|
|
|
|
def remove_line_numbers(code_str: str) -> str: |
|
|
""" |
|
|
Removes leading line numbers (e.g., '1. ', '10. ', etc.) from a string of Python code, |
|
|
preserving indentation so the output remains executable. |
|
|
|
|
|
Args: |
|
|
code_str (str): Multiline string containing Python code with line numbers. |
|
|
|
|
|
Returns: |
|
|
str: Code with line numbers removed. |
|
|
""" |
|
|
cleaned_lines = [] |
|
|
for line in code_str.splitlines(): |
|
|
|
|
|
cleaned_line = re.sub(r'^\d+\.\s?', '', line) |
|
|
cleaned_lines.append(cleaned_line) |
|
|
return "\n".join(cleaned_lines) |
|
|
|
|
|
def main(): |
|
|
args = parse_args() |
|
|
versions = os.listdir(args.input) |
|
|
for vx in versions: |
|
|
folders = os.listdir(os.path.join(args.input, vx)) |
|
|
for folder in folders: |
|
|
data_folder_path = os.path.join(args.input, vx, folder) |
|
|
dataframe = process(data_folder_path) |
|
|
dataframe.to_csv(os.path.join("./data", vx, f"{folder}-00000-of-000001.csv"), index=False) |
|
|
print(f"Processed {folder} with {len(dataframe)} entries.") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |