Upload nq_open.py
Browse files- nq_open.py +103 -0
nq_open.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import datasets
|
4 |
+
from datasets import BuilderConfig, Features, Value, Sequence
|
5 |
+
|
6 |
+
|
7 |
+
_DESCRIPTION = """
|
8 |
+
# ํ๊ตญ์ด ์ง์ํ์ต ๋ฐ์ดํฐ์
|
9 |
+
- nq_open ๋ฐ์ดํฐ์
์ ํ๊ตญ์ด๋ก ๋ณ์ญํ ๋ฐ์ดํฐ์
|
10 |
+
"""
|
11 |
+
|
12 |
+
_CITATION = """
|
13 |
+
@inproceedings{KITD,
|
14 |
+
title={์ธ์ด ๋ฒ์ญ ๋ชจ๋ธ์ ํตํ ํ๊ตญ์ด ์ง์ ํ์ต ๋ฐ์ดํฐ ์ธํธ ๊ตฌ์ถ},
|
15 |
+
author={์์์, ์ถํ์ฐฝ, ๊น์ฐ, ์ฅ์ง์, ์ ๋ฏผ์, ์ ์ฌ์},
|
16 |
+
booktitle={์ 35ํ ํ๊ธ ๋ฐ ํ๊ตญ์ด ์ ๋ณด์ฒ๋ฆฌ ํ์ ๋ํ},
|
17 |
+
pages={591--595},
|
18 |
+
month=oct,
|
19 |
+
year={2023}
|
20 |
+
}
|
21 |
+
"""
|
22 |
+
|
23 |
+
def _list(data_list):
|
24 |
+
result = list()
|
25 |
+
for data in data_list:
|
26 |
+
result.append(data)
|
27 |
+
return result
|
28 |
+
|
29 |
+
# nq_open
|
30 |
+
_NQ_OPEN_FEATURES = Features({
|
31 |
+
"data_index_by_user": Value(dtype="int32"),
|
32 |
+
"question": Value(dtype="string"),
|
33 |
+
"answer": Sequence(Value(dtype="string"))
|
34 |
+
})
|
35 |
+
|
36 |
+
def _parsing_nq_open(file_path):
|
37 |
+
with open(file_path, mode="r") as f:
|
38 |
+
dataset = json.load(f)
|
39 |
+
for _i, data in enumerate(dataset):
|
40 |
+
_data_index_by_user = data["data_index_by_user"]
|
41 |
+
_question = data["question"]
|
42 |
+
_answer = _list(data["answer"])
|
43 |
+
|
44 |
+
yield _i, {
|
45 |
+
"data_index_by_user": _data_index_by_user,
|
46 |
+
"question": _question,
|
47 |
+
"answer": _answer
|
48 |
+
}
|
49 |
+
|
50 |
+
class Nq_openConfig(BuilderConfig):
|
51 |
+
def __init__(self, name, feature, reading_fn, parsing_fn, citation, **kwargs):
|
52 |
+
super(Nq_openConfig, self).__init__(
|
53 |
+
name = name,
|
54 |
+
version=datasets.Version("1.0.0"),
|
55 |
+
**kwargs)
|
56 |
+
self.feature = feature
|
57 |
+
self.reading_fn = reading_fn
|
58 |
+
self.parsing_fn = parsing_fn
|
59 |
+
self.citation = citation
|
60 |
+
|
61 |
+
class NQ_OPEN(datasets.GeneratorBasedBuilder):
|
62 |
+
BUILDER_CONFIGS = [
|
63 |
+
Nq_openConfig(
|
64 |
+
name = "base",
|
65 |
+
data_dir = "./nq_open",
|
66 |
+
feature = _NQ_OPEN_FEATURES,
|
67 |
+
reading_fn = _parsing_nq_open,
|
68 |
+
parsing_fn = lambda x:x,
|
69 |
+
citation = _CITATION,
|
70 |
+
),
|
71 |
+
]
|
72 |
+
|
73 |
+
def _info(self) -> datasets.DatasetInfo:
|
74 |
+
"""Returns the dataset metadata."""
|
75 |
+
return datasets.DatasetInfo(
|
76 |
+
description=_DESCRIPTION,
|
77 |
+
features=_NQ_OPEN_FEATURES,
|
78 |
+
citation=_CITATION,
|
79 |
+
)
|
80 |
+
|
81 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager):
|
82 |
+
"""Returns SplitGenerators"""
|
83 |
+
path_kv = {
|
84 |
+
datasets.Split.TRAIN:[
|
85 |
+
os.path.join(dl_manager.manual_dir, f"train.json")
|
86 |
+
],
|
87 |
+
datasets.Split.VALIDATION:[
|
88 |
+
os.path.join(dl_manager.manual_dir, f"validation.json")
|
89 |
+
],
|
90 |
+
}
|
91 |
+
return [
|
92 |
+
datasets.SplitGenerator(name=k, gen_kwargs={"path_list": v})
|
93 |
+
for k, v in path_kv.items()
|
94 |
+
]
|
95 |
+
|
96 |
+
def _generate_examples(self, path_list):
|
97 |
+
"""Yields examples."""
|
98 |
+
for path in path_list:
|
99 |
+
try:
|
100 |
+
for example in iter(self.config.reading_fn(path)):
|
101 |
+
yield self.config.parsing_fn(example)
|
102 |
+
except Exception as e:
|
103 |
+
print(e)
|