Commit
·
35b5dc4
1
Parent(s):
a8c8816
update
Browse files- output_dataset.parquet +2 -2
- par.py +71 -0
output_dataset.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ca47ac46bfc9643c362f1e7ff017d807e587320b45e66c147a257421c20cbad6
|
| 3 |
+
size 22455970
|
par.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import pyarrow as pa
|
| 4 |
+
import pyarrow.parquet as pq
|
| 5 |
+
import argparse
|
| 6 |
+
import re
|
| 7 |
+
|
| 8 |
+
def encode_file(file_path):
|
| 9 |
+
"""Read file content as string."""
|
| 10 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 11 |
+
return file.read()
|
| 12 |
+
|
| 13 |
+
def extract_images(markdown_content):
|
| 14 |
+
"""Extract PHOTO_IDs from markdown files and return as a list."""
|
| 15 |
+
return re.findall(r'\{\{PHOTO_ID:(\d+)\|WIDTH:\d+\}\}', markdown_content)
|
| 16 |
+
|
| 17 |
+
def collect_data(directory):
|
| 18 |
+
data = []
|
| 19 |
+
# Debugging: Print directory content
|
| 20 |
+
print(f"Directory contents: {os.listdir(directory)}")
|
| 21 |
+
|
| 22 |
+
# Create a dictionary to map PHOTO_ID to actual image filenames
|
| 23 |
+
image_files = {}
|
| 24 |
+
for filename in os.listdir(directory):
|
| 25 |
+
if filename.endswith('.jpg'):
|
| 26 |
+
photo_id_match = re.search(r'(\d+)', filename)
|
| 27 |
+
if photo_id_match:
|
| 28 |
+
photo_id = photo_id_match.group(1)
|
| 29 |
+
image_files[photo_id] = filename
|
| 30 |
+
|
| 31 |
+
# Debugging: Print image file mappings
|
| 32 |
+
print(f"Image Files: {image_files}")
|
| 33 |
+
|
| 34 |
+
for filename in os.listdir(directory):
|
| 35 |
+
problem_id = filename.split('.')[0]
|
| 36 |
+
row = {'Problem ID': problem_id, 'Images': []}
|
| 37 |
+
for file in os.listdir(directory):
|
| 38 |
+
if file.startswith(problem_id):
|
| 39 |
+
file_type = file.split('.')[-1]
|
| 40 |
+
file_path = os.path.join(directory, file)
|
| 41 |
+
if file_type in ['in', 'out', 'sol.md' 'cpp', 'md']:
|
| 42 |
+
content = encode_file(file_path)
|
| 43 |
+
row[file_type] = content
|
| 44 |
+
if file_type in ['md', 'sol.md']:
|
| 45 |
+
image_ids = extract_images(content)
|
| 46 |
+
# Debugging: Print extracted image IDs
|
| 47 |
+
print(f"Extracted Image IDs from {file_path}: {image_ids}")
|
| 48 |
+
row['Images'] = [image_files[id] for id in image_ids if id in image_files]
|
| 49 |
+
data.append(row)
|
| 50 |
+
|
| 51 |
+
# Debugging: Print final data before conversion to verify
|
| 52 |
+
# print(f"Final Data Collected: {data}")
|
| 53 |
+
|
| 54 |
+
return data
|
| 55 |
+
|
| 56 |
+
def create_parquet_file(data, output_file):
|
| 57 |
+
df = pd.DataFrame(data)
|
| 58 |
+
table = pa.Table.from_pandas(df)
|
| 59 |
+
pq.write_table(table, output_file)
|
| 60 |
+
|
| 61 |
+
def main():
|
| 62 |
+
parser = argparse.ArgumentParser(description='Convert dataset to Parquet format.')
|
| 63 |
+
parser.add_argument('directory', type=str, help='Directory containing the dataset files.')
|
| 64 |
+
parser.add_argument('-o', '--output', type=str, default='output_dataset.parquet', help='Output Parquet file name.')
|
| 65 |
+
args = parser.parse_args()
|
| 66 |
+
|
| 67 |
+
data = collect_data(args.directory)
|
| 68 |
+
create_parquet_file(data, args.output)
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
main()
|