Datasets:
GEM
/

Tasks:
Other
Modalities:
Text
Languages:
English
ArXiv:
Libraries:
Datasets
License:
File size: 3,606 Bytes
278a858
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cd48c5
278a858
 
 
 
 
5cd48c5
278a858
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9352866
 
278a858
 
727fcd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278a858
 
 
 
 
727fcd0
278a858
 
727fcd0
278a858
727fcd0
 
278a858
727fcd0
278a858
 
 
 
 
 
 
 
727fcd0
278a858
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
""" Script to prepare the SQuAD2.0 data to the GEM format

    @author: AbinayaM02
"""

# Import libraries
import json
import pandas as pd
from sklearn.model_selection import train_test_split

# Function to generate gem id
def add_gem_id(data: dict, split: str) -> dict:
    """ 
    Add gem id for each of the datapoint in the dataset.

    Parameters:
    -----------
    data: dict,
        data.
    split: str,
        split of data (train, test or validation).

    Returns:
    --------
    dict
        dictionary with updated id
    """
    gem_id = -1
    generated_data = {"data": []}
    id_list =[]
    for example in data:
        title = example["title"]
        for paragraph in example["paragraphs"]:
            context = paragraph["context"]  # do not strip leading blank spaces GH-2585
            for qa in paragraph["qas"]:
                temp_dict = {}
                question = qa["question"]
                qa_id = qa["id"]
                answer_starts = [answer["answer_start"] for answer in qa["answers"]]
                answers = [answer["text"] for answer in qa["answers"]]
                # Features currently used are "context", "question", and "answers".
                # Others are extracted here for the ease of future expansions.
                gem_id += 1
                temp_dict["id"] = qa_id
                temp_dict["gem_id"] = f"gem-squad_v2-{split}-{gem_id}"
                temp_dict["title"] = title
                temp_dict["context"] = context
                temp_dict["question"] = question
                temp_dict["answers"] = {
                        "answer_start": answer_starts,
                        "text": answers,
                    }
                generated_data["data"].append(temp_dict)
    return generated_data


# Function to split data
def split_data(file_name: str, data_type: str) -> (dict, dict):
    """
    Method to split the data specific to SQuAD2.0

    Parameters:
    -----------
    file_name: str,
        name of the file.
    data_type: str,
        type of the data file.

    Returns:
    --------
    (dict, dict)
        split of data
    """
    
    if data_type == "json":
        with open(file_name, 'r') as json_file:
            data = json.load(json_file)["data"]
            json_file.close()
    
    # split the data into train and test
    # 90% train data 10% test data
    train, test = train_test_split(data, train_size=0.9, random_state = 42)
    return(train, test)

# Function to save json file
def save_json(data: dict, file_name: str):
    """
    Method to save the json file.

    Parameters:
    ----------
    data: dict,
        data to be saved in file.
    file_name: str,
        name of the file.

    Returns:
    --------
    None
    """

    # save the split
    with open(file_name, "w") as data_file:
        json.dump(data, data_file, indent = 2)
        data_file.close()


if __name__ == "__main__":
    # split the train data
    train, test = split_data("squad_data/train-v2.0.json", "json")
    
    # add gem id to train split
    train = add_gem_id(train, "train")
    # save the train split
    save_json(train, "train.json")

    # add gem id to test split
    test = add_gem_id(test, "test")
    # save the test split
    save_json(test, "test.json")
    
    # load validation data
    with open("squad_data/dev-v2.0.json", "r") as dev_file:
        validation = json.load(dev_file)["data"]
        dev_file.close()
    
    # add gem id and save valid.json
    validation = add_gem_id(validation, "validation")
    save_json(validation, "validation.json")