File size: 2,876 Bytes
ebb1d8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f83defa
 
 
 
 
ebb1d8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f83defa
 
aad26aa
 
 
 
 
 
 
 
 
 
 
 
f83defa
ebb1d8b
 
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
from datasets import load_dataset, load_metric, ClassLabel, Sequence, Dataset, DatasetDict, concatenate_datasets
import pandas as pd

def load_klue():
    dataset = load_dataset('klue', 'nli')
    dataset = dataset.filter(lambda row: row['label'] in [0, 1, 2])
    def label_map(row):
        labels = [
            'entailment',
            'neutral',
            'contradiction',
        ]
        row['labell'] = list(map(lambda x: labels[x], row['label']))
        return row
    dataset = dataset.map(label_map, batched=True, remove_columns=['label'])
    dataset = dataset.rename_column('labell', 'label')
    return dataset.select_columns(['premise', 'hypothesis', 'label'])

def load_dacon():
    dataset = load_dataset('csv', data_files={'train': ['data/dacon_train_data.csv'], 'validation': 'data/dacon_test_data.csv'})
    return dataset.select_columns(['premise', 'hypothesis', 'label'])

def load_kakao():
    kakao_snli = pd.read_csv('data/snli_1.0_train.ko.tsv', sep='\t', encoding='utf-8')
    kakao_dev = pd.read_csv('data/xnli.dev.ko.tsv', sep='\t', encoding='utf-8')
    kakao_train = pd.concat([kakao_dev, kakao_snli])
    kakao_train.rename(columns = {'sentence1':'premise','sentence2':'hypothesis','gold_label':'label'}, inplace=True)
    kakao_train = kakao_train[['premise', 'hypothesis', 'label']]
    kakao_train.reset_index(drop=True, inplace=True)
    kakao_test = pd.read_csv('data/xnli.test.ko.tsv', sep='\t', encoding='utf-8')
    kakao_test.rename(columns = {'sentence1':'premise','sentence2':'hypothesis','gold_label':'label'}, inplace=True)
    kakao_test = kakao_test[['premise', 'hypothesis', 'label']]
    kakao_test.reset_index(drop=True, inplace=True)
    train_ds = Dataset.from_pandas(kakao_train)
    test_ds = Dataset.from_pandas(kakao_test)
    return DatasetDict({
        'train': train_ds,
        'validation': test_ds,
    })

def drop_na(example):
    na = False
    for column in example.keys():
        na = na or pd.isna(example[column])
    return not na

datasets = {}
datasets['klue'] = load_klue()
datasets['dacon'] = load_dacon()
datasets['kakao'] = load_kakao()

trains, tests = zip(*[
    [ds_dict['train'], ds_dict['validation']]
    for source, ds_dict in datasets.items()
])

datasets = DatasetDict({
    'train': concatenate_datasets(trains),
    'validation': concatenate_datasets(tests),
})

datasets = datasets.filter(drop_na)

labels = {
    'entailment': 2,
    'neutral': 1,
    'contradiction': 0,
}
def label_map(row):
    row['labell'] = list(map(lambda x: labels[x], row['label']))
    return row
datasets = datasets.filter(lambda row: row['label'] in labels.keys())
datasets = datasets.map(label_map, batched=True, remove_columns=['label'])
datasets = datasets.rename_column('labell', 'label')

datasets.push_to_hub("seongs1024/DKK-nli", private=True)

# datasets = load_dataset('seongs1024/DKK-nli')