| |
|
|
| 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}...") |
| |
| |
| train_dir = Path(language_dir) / "train" |
| train_dir.mkdir(exist_ok=True) |
| |
| |
| 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_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}") |
| |
| |
| df = pd.read_parquet(parquet_file) |
| |
| |
| for _, row in df.iterrows(): |
| |
| 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'] |
| } |
| |
| |
| 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 |
| |
| |
| 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""" |
| |
| 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() |
|
|