tablesense / process_data.py
Kyle Li
add files
a555f47
raw
history blame
2.54 kB
import os
import json
from pathlib import Path
from xls_to_xlsx import convert_xls_to_xlsx
# Set up paths
BASE_DIR = Path(__file__).parent
ORIGINAL_DATA = BASE_DIR / 'original_data'
CLEAN_DATA = 'clean_data'
ANNOTATIONS_FILE = ORIGINAL_DATA / 'Table range annotations.txt'
# Create clean_data directory if it doesn't exist
def clean_path(path):
"""Convert Windows path to a clean filename."""
return path.replace('\\', '_').replace('/', '_')
def process_table_regions(regions):
"""Convert table regions string into a list of regions."""
return [region.strip() for region in regions if region.strip()]
def process_annotations():
# List to store JSONL entries
jsonl_entries = []
# Process each line
with open(ANNOTATIONS_FILE, 'r', encoding='utf-8') as f:
for line in f:
# Split line by tabs
parts = line.strip().split('\t')
if len(parts) < 4: # Need at least filename, sheet, split, folder
continue
file_name = parts[0]
sheet_name = parts[1]
split = parts[2]
file_folder = parts[3]
table_regions = parts[4:] if len(parts) > 4 else []
original_path = os.path.join(ORIGINAL_DATA, file_folder, file_name) # ORIGINAL_DATA file_folder / file_name
# Create clean filename using the full path
clean_filename = clean_path(f"{file_folder}_{file_name}")
converted_path = original_path.replace('.xlsx', '.xls').replace('\\','/')
print(converted_path)
convert_xls_to_xlsx(converted_path, CLEAN_DATA)
#rename the file to the clean filename
os.rename(os.path.join(CLEAN_DATA, file_name), os.path.join(CLEAN_DATA, clean_filename))
# Create entry for JSONL
entry = {
'original_file': str(file_name),
'original_path': str(file_folder),
'clean_file': str(clean_filename),
'sheet_name': str(sheet_name),
'split': str(split),
'table_regions': process_table_regions(table_regions)
}
jsonl_entries.append(entry)
# Write to JSONL file
output_file = BASE_DIR / 'annotations.jsonl'
with open(output_file, 'w') as f:
for entry in jsonl_entries:
f.write(json.dumps(entry) + '\n')
if __name__ == '__main__':
process_annotations()