File size: 2,805 Bytes
962f2ee
 
 
 
 
 
 
 
 
 
8a4117c
962f2ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a4117c
 
 
 
 
 
 
 
962f2ee
 
 
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
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)
    # since given to the student straight away, we can ignore it
    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():
        # Match a line number at the start: digits, a dot, optional space(s)
        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()