File size: 1,372 Bytes
24e9bb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import random
import os

positive_path = 'positive/positive.jsonl'
negative_path = 'negative/negative.jsonl'

def read_jsonl(path):
    with open(path, 'r', encoding='utf-8') as f:
        return [line for line in f if line.strip()]

positives = read_jsonl(positive_path)
negatives = read_jsonl(negative_path)

datasets_config = {
    '1_1': {'total': 2000, 'pos_ratio': 1, 'neg_ratio': 1},  
    '1_5': {'total': 1200, 'pos_ratio': 1, 'neg_ratio': 5},  
    '5_1': {'total': 1200, 'pos_ratio': 5, 'neg_ratio': 1}   
}

for name, config in datasets_config.items():
    total = config['total']
    pos_ratio = config['pos_ratio']
    neg_ratio = config['neg_ratio']
    
    pos_count = int(total * pos_ratio / (pos_ratio + neg_ratio))
    neg_count = total - pos_count
    
    pos_count = min(pos_count, len(positives))
    neg_count = min(neg_count, len(negatives))
    
    pos_samples = random.sample(positives, pos_count)
    neg_samples = random.sample(negatives, neg_count)
    
    dataset = pos_samples + neg_samples
    random.shuffle(dataset)
    
    out_path = f'dataset_{name}.jsonl'
    with open(out_path, 'w', encoding='utf-8') as f:
        for line in dataset:
            f.write(line if line.endswith('\n') else line + '\n')
    
    print(f'{out_path} created: {pos_count} positive, {neg_count} negative samples (total: {len(dataset)})')