jinmang2 commited on
Commit
33eb32b
1 Parent(s): 95bd06a

add datasets script

Browse files
Files changed (1) hide show
  1. KorQuADv1.py +103 -0
KorQuADv1.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import pandas as pd
3
+
4
+ _DATA_URLS = {
5
+ "train": "https://korquad.github.io/dataset/KorQuAD_v1.0_train.json",
6
+ "dev": "https://korquad.github.io/dataset/KorQuAD_v1.0_dev.json",
7
+ }
8
+ _VERSION = "0.0.0"
9
+ _LICENSE = "CC BY-ND 2.0 KR"
10
+ _CITATION = """\
11
+ @misc{lim2019korquad1.0,
12
+ title={KorQuAD1.0: Korean QA Dataset for Machine Reading Comprehension},
13
+ author={Seungyoung Lim, Myungji Kim, Jooyoul Lee},
14
+ year={2019},
15
+ eprint={1909.07005},
16
+ archivePrefix={arXiv},
17
+ primaryClass={cs.CL}
18
+ }
19
+ """
20
+ _HOMEPAGE = "https://korquad.github.io/KorQuad%201.0/"
21
+ _DESCRIPTION = """\
22
+ KorQuAD 1.0 (Korean Question Answering Dataset v1.0)
23
+ KorQuAD 1.0 is a dataset created for Korean Machine Reading Comprehension.
24
+ The answers to all your questions are made up of some subareas in the corresponding Wikipedia article paragraphs.
25
+ It is structured in the same way as the Stanford Question Answering Dataset (SQuAD) v1.0.
26
+ """
27
+
28
+
29
+ class KorQuADV1Config(datasets.BuilderConfig):
30
+ def __init__(self, data_url, features, **kwargs):
31
+ super().__init__(version=datasets.Version(_VERSION, ""), **kwargs)
32
+ self.features = features
33
+ self.data_url = data_url
34
+
35
+
36
+ class KorQuADV1(datasets.GeneratorBasedBuilder):
37
+ DEFAULT_CONFIG_NAME = "korquad"
38
+ BUILDER_CONFIGS = [
39
+ KorQuADV1Config(
40
+ name="korquad",
41
+ data_url=_DATA_URLS,
42
+ features=datasets.Features(
43
+ {
44
+ "answers": datasets.Sequence(
45
+ feature={
46
+ "text": datasets.Value(dtype="string"),
47
+ "answer_start": datasets.Value(dtype="int32"),
48
+ },
49
+ ),
50
+ "context": datasets.Value(dtype="string"),
51
+ "guid": datasets.Value(dtype="string"),
52
+ "question": datasets.Value(dtype="string"),
53
+ "title": datasets.Value(dtype="string"),
54
+ }
55
+ )
56
+ ),
57
+ ]
58
+
59
+ def _info(self):
60
+ return datasets.DatasetInfo(
61
+ features=self.config.features,
62
+ description=_DESCRIPTION,
63
+ homepage=_HOMEPAGE,
64
+ citation=_CITATION,
65
+ license=_LICENSE,
66
+ )
67
+
68
+ def _split_generators(self, dl_manager):
69
+ data_path = dl_manager.download(self.config.data_url)
70
+ return [
71
+ datasets.SplitGenerator(
72
+ name=datasets.Split.TRAIN,
73
+ # These kwargs will be passed to _generate_examples
74
+ gen_kwargs={"data_file": data_path["train"]}
75
+ ),
76
+ datasets.SplitGenerator(
77
+ name=datasets.Split.VALIDATION,
78
+ # These kwargs will be passed to _generate_examples
79
+ gen_kwargs={"data_file": data_path["dev"]}
80
+ ),
81
+ ]
82
+
83
+ def _generate_examples(self, data_file: str):
84
+ idx = 0
85
+ korquad = pd.read_json(data_file)
86
+ for example in korquad["data"].tolist():
87
+ paragraphs = example["paragraphs"]
88
+ title = example["title"]
89
+ for paragraph in paragraphs:
90
+ qas = paragraph["qas"]
91
+ context = paragraph["context"]
92
+ for qa in qas:
93
+ text = [answers["text"] for answers in qa["answers"]]
94
+ answer_start = [answers["answer_start"] for answers in qa["answers"]]
95
+ features = {
96
+ "guid": qa["id"],
97
+ "question": qa["question"],
98
+ "answers": {"text": text, "answer_start": answer_start},
99
+ "context": context,
100
+ "title": title,
101
+ }
102
+ yield idx, features
103
+ idx += 1