File size: 3,193 Bytes
49544b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""

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...")
    
    # Trova file 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:
                # Filtro per linguaggio
                if language_filter and record['language'] not in language_filter:
                    continue
                
                # Ripristina newlines
                record['content'] = record['content'].replace('\\n', '\n').replace('\\r', '\r')
                
                # Converti tipi
                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
                
                # Limite file se specificato
                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)

# Esempio
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'])")