File size: 3,215 Bytes
35b5dc4
 
 
 
 
 
05a6587
35b5dc4
05a6587
 
 
 
 
 
 
 
 
 
 
 
35b5dc4
 
 
 
 
 
087591e
05a6587
 
35b5dc4
05a6587
 
 
 
 
cd08c07
 
 
 
 
 
 
 
 
087591e
05a6587
b010c7c
aeada81
 
05a6587
 
 
b010c7c
a8d95cd
05a6587
a8d95cd
 
 
 
 
 
 
 
35b5dc4
087591e
35b5dc4
 
 
05a6587
35b5dc4
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
import argparse
import re
import base64

def encode_file(file_path):
    """Encode text files or base64 encode image files."""
    if file_path.endswith('.jpg'):
        with open(file_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')
    else:
        try:
            with open(file_path, 'r', encoding='utf-8') as file:
                return file.read()
        except UnicodeDecodeError as e:
            print(f"Error decoding file {file_path}: {e}")
            return None

def extract_images(markdown_content):
    """Extract PHOTO_IDs from markdown files and return as a list."""
    return re.findall(r'\{\{PHOTO_ID:(\d+)\|WIDTH:\d+\}\}', markdown_content)

def collect_data(directory):
    data = {}
    image_files = {re.search(r'(\d+)', filename).group(1): filename
                   for filename in os.listdir(directory) if filename.endswith('.jpg')}

    markdown_files = [f for f in os.listdir(directory) if f.endswith('.md') or f.endswith('.sol.md')]
    for mfile in markdown_files:
        # Adjust the pattern if problem IDs include characters before "sol"
        problem_id = re.sub(r'sol$', '', mfile.split('.')[0])  # Strip "sol" from end
        if problem_id not in data:
            data[problem_id] = {
                'Problem ID': problem_id,
                'Problem': None,
                'in': None,
                'Solution': None,
                'cpp': None,
                'out': None,
                'Images': []
            }

    # Now associate other files with these problem IDs
    for filename in os.listdir(directory):
        problem_id = re.sub(r'sol$', '', filename.split('.')[0])
        if problem_id in data:
            file_type = filename.split('.')[-1]
            file_path = os.path.join(directory, filename)
            content = encode_file(file_path) if not filename.endswith('.jpg') else None

            if file_type in ['in', 'out', 'cpp']:
                data[problem_id][file_type] = content
            if file_type == "md":
                if "sol" in filename:
                    data[problem_id]['Solution'] = content
                else:
                    data[problem_id]['Problem'] = content
                image_ids = extract_images(content)
                data[problem_id]['Images'] += [image_files[id] for id in image_ids if id in image_files]
                data[problem_id]['Images'] = list(set(data[problem_id]['Images']))  # Remove duplicates

    return list(data.values())

def create_parquet_file(data, output_file):
    df = pd.DataFrame(data)
    table = pa.Table.from_pandas(df)
    pq.write_table(table, output_file)

def main():
    parser = argparse.ArgumentParser(description='Convert dataset to Parquet format.')
    parser.add_argument('directory', type=str, help='Directory containing the dataset files.')
    parser.add_argument('-o', '--output', type=str, default='output_dataset.parquet', help='Output Parquet file name.')
    args = parser.parse_args()

    data = collect_data(args.directory)
    create_parquet_file(data, args.output)

if __name__ == "__main__":
    main()