(directory_path)
| 51 | |
| 52 | |
| 53 | def process_directory(directory_path): |
| 54 | # Convert to Path object for easier path handling |
| 55 | dir_path = Path(directory_path) |
| 56 | |
| 57 | # Expected files |
| 58 | expected_files = {'dev.json', 'train.json', 'test.json', 'labels.json'} |
| 59 | |
| 60 | # Get all JSON files in directory |
| 61 | json_files = {f.name for f in dir_path.glob('*.json')} |
| 62 | |
| 63 | # Check if directory contains exactly the expected files |
| 64 | if json_files != expected_files: |
| 65 | missing_files = expected_files - json_files |
| 66 | extra_files = json_files - expected_files |
| 67 | error_msg = [] |
| 68 | if missing_files: |
| 69 | error_msg.append(f"Missing files: {', '.join(missing_files)}") |
| 70 | if extra_files: |
| 71 | error_msg.append(f"Unexpected files: {', '.join(extra_files)}") |
| 72 | raise ValueError(f"Directory does not contain exactly the expected files.\n" + "\n".join(error_msg)) |
| 73 | |
| 74 | # Process files except label.json |
| 75 | for json_file in dir_path.glob('*.json'): |
| 76 | if json_file.name == 'labels.json': |
| 77 | continue |
| 78 | |
| 79 | # Create backup of original file |
| 80 | backup_path = json_file.parent / f'raw_{json_file.name}' |
| 81 | shutil.copy2(json_file, backup_path) |
| 82 | |
| 83 | # Determine output filename |
| 84 | output_name = 'eval.json' if json_file.name == 'dev.json' else json_file.name |
| 85 | output_path = json_file.parent / output_name |
| 86 | |
| 87 | # Process the file |
| 88 | transform_json(str(json_file), str(output_path)) |
| 89 | print(f'Processed: {json_file.name} -> {output_name}') |
| 90 | |
| 91 | |
| 92 | if __name__ == '__main__': |
no test coverage detected