|
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: |
|
|
|
problem_id = re.sub(r'sol$', '', mfile.split('.')[0]) |
|
if problem_id not in data: |
|
data[problem_id] = { |
|
'Problem ID': problem_id, |
|
'Problem': None, |
|
'in': None, |
|
'Solution': None, |
|
'cpp': None, |
|
'out': None, |
|
'Images': [] |
|
} |
|
|
|
|
|
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'])) |
|
|
|
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() |
|
|