marksaroufim commited on
Commit
b54c1ac
·
1 Parent(s): 35b5dc4
Files changed (2) hide show
  1. output_dataset.parquet +2 -2
  2. par.py +27 -33
output_dataset.parquet CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:ca47ac46bfc9643c362f1e7ff017d807e587320b45e66c147a257421c20cbad6
3
- size 22455970
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1a670b3dc111fe3c5c2522545ba62b74df0da6beb2b1d413ba574f983728ebdf
3
+ size 22475137
par.py CHANGED
@@ -4,11 +4,20 @@ 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."""
@@ -16,40 +25,25 @@ def extract_images(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
 
 
4
  import pyarrow.parquet as pq
5
  import argparse
6
  import re
7
+ import base64
8
 
9
  def encode_file(file_path):
10
+ """Encode text files or base64 encode image files."""
11
+ try:
12
+ if file_path.endswith('.jpg'):
13
+ with open(file_path, "rb") as image_file:
14
+ return base64.b64encode(image_file.read()).decode('utf-8')
15
+ else:
16
+ with open(file_path, 'r', encoding='utf-8') as file:
17
+ return file.read()
18
+ except UnicodeDecodeError as e:
19
+ print(f"Error decoding file {file_path}: {e}")
20
+ return None
21
 
22
  def extract_images(markdown_content):
23
  """Extract PHOTO_IDs from markdown files and return as a list."""
 
25
 
26
  def collect_data(directory):
27
  data = []
28
+ image_files = {re.search(r'(\d+)', filename).group(1): filename
29
+ for filename in os.listdir(directory) if filename.endswith('.jpg')}
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  for filename in os.listdir(directory):
32
  problem_id = filename.split('.')[0]
33
+ if not any(d['Problem ID'] == problem_id for d in data):
34
+ data.append({'Problem ID': problem_id, 'Images': [], 'in': None, 'out': None, 'cpp': None, 'md': None, 'sol.md': None})
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ current_entry = next(item for item in data if item['Problem ID'] == problem_id)
37
+ file_type = filename.split('.')[-1]
38
+ file_path = os.path.join(directory, filename)
39
+ if file_type in ['in', 'out', 'cpp', 'md', 'sol.md']:
40
+ content = encode_file(file_path)
41
+ if content is not None: # Make sure we successfully read the file
42
+ current_entry[file_type] = content
43
+ if file_type in ['md', 'sol.md']:
44
+ image_ids = extract_images(content)
45
+ current_entry['Images'] += [image_files[id] for id in image_ids if id in image_files]
46
+ current_entry['Images'] = list(set(current_entry['Images'])) # Remove duplicates if any
47
 
48
  return data
49