| """
|
| Caricamento dataset CSV - The_Stack_Processed-v2
|
| """
|
|
|
| import csv
|
| import os
|
| import json
|
|
|
| def load_csv_dataset(data_path=".", language_filter=None, max_files=None):
|
| """
|
| Carica dataset da file CSV
|
|
|
| Args:
|
| data_path: Path ai file CSV
|
| language_filter: Lista linguaggi da includere
|
| max_files: Numero massimo di file da caricare
|
|
|
| Returns:
|
| Lista di record
|
| """
|
|
|
| print("π Caricamento dataset CSV...")
|
|
|
|
|
| csv_files = sorted([f for f in os.listdir(data_path) if f.endswith('.csv')])
|
| print(f"π Trovati {len(csv_files)} file CSV")
|
|
|
| all_records = []
|
| loaded_files = 0
|
|
|
| for csv_file in csv_files:
|
| print(f"π Caricando {csv_file}")
|
|
|
| with open(os.path.join(data_path, csv_file), 'r', encoding='utf-8') as f:
|
| reader = csv.DictReader(f)
|
|
|
| for record in reader:
|
|
|
| if language_filter and record['language'] not in language_filter:
|
| continue
|
|
|
|
|
| record['content'] = record['content'].replace('\\n', '\n').replace('\\r', '\r')
|
|
|
|
|
| record['size_bytes'] = int(record['size_bytes'])
|
| record['quality_score'] = float(record['quality_score'])
|
| record['complexity'] = float(record['complexity'])
|
| record['documentation_ratio'] = float(record['documentation_ratio'])
|
| record['stars'] = int(record['stars'])
|
| record['is_test'] = record['is_test'].lower() == 'true'
|
|
|
| all_records.append(record)
|
| loaded_files += 1
|
|
|
|
|
| if max_files and loaded_files >= max_files:
|
| break
|
|
|
| if max_files and loaded_files >= max_files:
|
| break
|
|
|
| print(f"β
Caricati {len(all_records)} record")
|
| return all_records
|
|
|
| def get_language_stats(data_path="."):
|
| """Statistiche linguaggi"""
|
| info_file = os.path.join(data_path, "dataset_info.json")
|
| if os.path.exists(info_file):
|
| with open(info_file, 'r') as f:
|
| info = json.load(f)
|
| return info.get('languages', {})
|
| return {}
|
|
|
| def load_sample(data_path=".", n_samples=10, language=None):
|
| """Carica campione veloce"""
|
| return load_csv_dataset(data_path,
|
| language_filter=[language] if language else None,
|
| max_files=n_samples)
|
|
|
|
|
| if __name__ == "__main__":
|
| print("π Statistiche linguaggi:")
|
| stats = get_language_stats()
|
| for lang, count in sorted(stats.items(), key=lambda x: x[1], reverse=True)[:10]:
|
| print(f" {lang}: {count:,}")
|
|
|
| print("\nπ Per caricare dataset:")
|
| print(" records = load_csv_dataset()")
|
| print("\nπ― Solo Python:")
|
| print(" records = load_csv_dataset(language_filter=['Python'])")
|
|
|