Download code_data/process_code_data.py from tyzhu/cont_data: direct link, hf CLI and curl.
- Browser
- Download file 3.01 kB
-
https://huggingface.co/datasets/tyzhu/cont_data/resolve/main/code_data/process_code_data.py
- Command line
-
hf download hf://datasets/tyzhu/cont_data/code_data/process_code_data.py
-
curl -L -o process_code_data.py https://huggingface.co/datasets/tyzhu/cont_data/resolve/main/code_data/process_code_data.py
3.01 kB
| #!/usr/bin/env python3 | |
| import os | |
| import pandas as pd | |
| import json | |
| from pathlib import Path | |
| import glob | |
| def process_language(language_dir): | |
| """Process parquet files for a specific language and create train.jsonl""" | |
| print(f"Processing {language_dir}...") | |
| # Create train directory | |
| train_dir = Path(language_dir) / "train" | |
| train_dir.mkdir(exist_ok=True) | |
| # Find all parquet files for this language | |
| parquet_pattern = f"{language_dir}/data/{Path(language_dir).name}/*.parquet" | |
| parquet_files = glob.glob(parquet_pattern) | |
| if not parquet_files: | |
| print(f" No parquet files found in {parquet_pattern}") | |
| return | |
| print(f" Found {len(parquet_files)} parquet files") | |
| # Output JSONL file | |
| output_file = train_dir / "train.jsonl" | |
| total_records = 0 | |
| with open(output_file, 'w', encoding='utf-8') as f: | |
| for parquet_file in sorted(parquet_files): | |
| print(f" Processing {parquet_file}") | |
| # Read parquet file | |
| df = pd.read_parquet(parquet_file) | |
| # Process each row | |
| for _, row in df.iterrows(): | |
| # Create record with text content and metadata | |
| record = { | |
| 'text': row['content'], | |
| 'language': row['lang'], | |
| 'size': int(row['size']), | |
| 'ext': row['ext'], | |
| 'avg_line_length': float(row['avg_line_length']) if pd.notna(row['avg_line_length']) else None, | |
| 'max_line_length': int(row['max_line_length']), | |
| 'alphanum_fraction': float(row['alphanum_fraction']) if pd.notna(row['alphanum_fraction']) else None, | |
| 'hexsha': row['hexsha'] | |
| } | |
| # Add repository metadata if available | |
| if pd.notna(row['max_stars_repo_name']): | |
| record['repo_name'] = row['max_stars_repo_name'] | |
| record['stars_count'] = int(row['max_stars_count']) if pd.notna(row['max_stars_count']) else None | |
| # Write as JSONL | |
| f.write(json.dumps(record, ensure_ascii=False) + '\n') | |
| total_records += 1 | |
| print(f" Created {output_file} with {total_records} records") | |
| return total_records | |
| def main(): | |
| """Process all language directories""" | |
| # Get all language directories | |
| language_dirs = [d for d in os.listdir('.') if os.path.isdir(d) and not d.startswith('.')] | |
| print(f"Found language directories: {language_dirs}") | |
| total_processed = 0 | |
| for lang_dir in sorted(language_dirs): | |
| try: | |
| records = process_language(lang_dir) | |
| if records: | |
| total_processed += records | |
| except Exception as e: | |
| print(f"Error processing {lang_dir}: {e}") | |
| print(f"\nTotal records processed: {total_processed}") | |
| if __name__ == "__main__": | |
| main() | |