File size: 2,355 Bytes
8ebda9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from tqdm import tqdm
import os
import argparse


def load_data(file_path,is_training=False):
    with open(file_path, 'r', encoding='utf8') as f:
        lines = json.loads(''.join(f.readlines()))
        result=[]
        for line in tqdm(lines): 
            data = line
            texta = '\n'.join(data[0])
            textb =''
            for qa in data[1]:
                question=qa['question']
                choice=qa['choice']
                answer=qa['answer'] if 'answer' in qa.keys() else ''
                label = qa['choice'].index(answer) if 'answer' in qa.keys() else 0
                text_id = qa['id'] if 'id' in qa.keys() else 0
                result.append({'texta':texta,
                                'textb':textb,
                                'question':question,
                                'choice':choice,
                                'answer':answer,
                                'label':label,
                                'id':text_id}) 
        return result


def save_data(data,file_path):
    with open(file_path, 'w', encoding='utf8') as f:
        for line in data:
            json_data=json.dumps(line,ensure_ascii=False)
            f.write(json_data+'\n')




if __name__=="__main__":
    parser = argparse.ArgumentParser(description="train")
    parser.add_argument("--data_path", type=str,default="")
    parser.add_argument("--save_path", type=str,default="")

    args = parser.parse_args()
    
    
    data_path = args.data_path
    save_path = args.save_path
    
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    
    file_list=['d-train','d-dev','c3-m-train','m-train','m-dev','test1.0','test1.1']
    train_data = []
    dev_data = []
    for file in file_list:
        file_path = os.path.join(data_path,file+'.json')
        data=load_data(file_path=file_path)
        if 'train' in file or 'd-dev' in file:
            train_data.extend(data)
        elif 'm-dev' in file:
            dev_data.extend(data)
        elif 'test' in file:
            output_path = os.path.join(save_path,file+'.json')
            save_data(data,output_path)
    
    output_path = os.path.join(save_path,'train.json')
    save_data(train_data,output_path)
            
    output_path = os.path.join(save_path,'dev.json')
    save_data(dev_data,output_path)