Update LEGAR_BENCH.py
Browse files- LEGAR_BENCH.py +71 -29
LEGAR_BENCH.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import json
|
2 |
import os
|
|
|
3 |
from pathlib import Path
|
4 |
import datasets
|
5 |
|
@@ -32,6 +33,8 @@ class LegarBench(datasets.GeneratorBasedBuilder):
|
|
32 |
description="Stricter version organized by difficulty",
|
33 |
),
|
34 |
]
|
|
|
|
|
35 |
|
36 |
def _info(self):
|
37 |
return datasets.DatasetInfo(
|
@@ -51,59 +54,98 @@ class LegarBench(datasets.GeneratorBasedBuilder):
|
|
51 |
)
|
52 |
|
53 |
def _split_generators(self, dl_manager):
|
54 |
-
repo_path = dl_manager.download_and_extract("")
|
55 |
-
|
56 |
if self.config.name == "standard":
|
57 |
-
|
58 |
elif self.config.name == "stricter":
|
59 |
-
|
60 |
elif self.config.name == "stricter_by_difficulty":
|
61 |
-
|
62 |
else:
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
return [
|
66 |
datasets.SplitGenerator(
|
67 |
name=datasets.Split.TRAIN,
|
68 |
gen_kwargs={
|
69 |
-
"
|
|
|
70 |
},
|
71 |
),
|
72 |
]
|
73 |
|
74 |
-
def _generate_examples(self,
|
75 |
example_id = 0
|
76 |
|
77 |
-
if
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
if
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
90 |
else:
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
97 |
|
98 |
def _load_json_file(self, filepath, difficulty=""):
|
99 |
with open(filepath, 'r', encoding='utf-8') as f:
|
100 |
data = json.load(f)
|
101 |
|
102 |
if isinstance(data, list):
|
103 |
-
for
|
104 |
-
yield
|
105 |
else:
|
106 |
-
yield
|
107 |
|
108 |
def _process_item(self, item, difficulty=""):
|
109 |
category = item.get("category", {})
|
|
|
1 |
import json
|
2 |
import os
|
3 |
+
import glob
|
4 |
from pathlib import Path
|
5 |
import datasets
|
6 |
|
|
|
33 |
description="Stricter version organized by difficulty",
|
34 |
),
|
35 |
]
|
36 |
+
|
37 |
+
DEFAULT_CONFIG_NAME = "standard"
|
38 |
|
39 |
def _info(self):
|
40 |
return datasets.DatasetInfo(
|
|
|
54 |
)
|
55 |
|
56 |
def _split_generators(self, dl_manager):
|
|
|
|
|
57 |
if self.config.name == "standard":
|
58 |
+
file_pattern = "Standard_version/*.json"
|
59 |
elif self.config.name == "stricter":
|
60 |
+
file_pattern = "Stricter_version/*.json"
|
61 |
elif self.config.name == "stricter_by_difficulty":
|
62 |
+
file_pattern = "Stricter_version_by_difficulty/**/*.json"
|
63 |
else:
|
64 |
+
file_pattern = "Standard_version/*.json"
|
65 |
+
|
66 |
+
# 파일들을 다운로드
|
67 |
+
try:
|
68 |
+
data_files = dl_manager.download_and_extract("")
|
69 |
+
if isinstance(data_files, str):
|
70 |
+
data_dir = data_files
|
71 |
+
else:
|
72 |
+
data_dir = data_files
|
73 |
+
|
74 |
+
except Exception as e:
|
75 |
+
if hasattr(dl_manager, 'manual_dir') and dl_manager.manual_dir:
|
76 |
+
data_dir = dl_manager.manual_dir
|
77 |
+
else:
|
78 |
+
raise e
|
79 |
|
80 |
return [
|
81 |
datasets.SplitGenerator(
|
82 |
name=datasets.Split.TRAIN,
|
83 |
gen_kwargs={
|
84 |
+
"data_dir": data_dir,
|
85 |
+
"config_name": self.config.name,
|
86 |
},
|
87 |
),
|
88 |
]
|
89 |
|
90 |
+
def _generate_examples(self, data_dir, config_name):
|
91 |
example_id = 0
|
92 |
|
93 |
+
if config_name == "standard":
|
94 |
+
search_path = os.path.join(data_dir, "Standard_version")
|
95 |
+
elif config_name == "stricter":
|
96 |
+
search_path = os.path.join(data_dir, "Stricter_version")
|
97 |
+
elif config_name == "stricter_by_difficulty":
|
98 |
+
search_path = os.path.join(data_dir, "Stricter_version_by_difficulty")
|
99 |
+
else:
|
100 |
+
search_path = os.path.join(data_dir, "Standard_version")
|
101 |
+
|
102 |
+
if not os.path.exists(search_path):
|
103 |
+
possible_paths = [
|
104 |
+
data_dir,
|
105 |
+
os.path.join(data_dir, "LEGAR_BENCH"),
|
106 |
+
os.path.join(data_dir, "LEGAR-BENCH")
|
107 |
+
]
|
108 |
+
|
109 |
+
found = False
|
110 |
+
for possible_path in possible_paths:
|
111 |
+
test_path = os.path.join(possible_path, search_path.split('/')[-1])
|
112 |
+
if os.path.exists(test_path):
|
113 |
+
search_path = test_path
|
114 |
+
found = True
|
115 |
+
break
|
116 |
+
|
117 |
+
if not found:
|
118 |
+
raise FileNotFoundError(f"Could not find data directory. Tried: {search_path}, data_dir: {data_dir}")
|
119 |
|
120 |
+
if config_name == "stricter_by_difficulty":
|
121 |
+
if os.path.exists(search_path):
|
122 |
+
for difficulty_folder in sorted(os.listdir(search_path)):
|
123 |
+
folder_path = os.path.join(search_path, difficulty_folder)
|
124 |
+
if os.path.isdir(folder_path):
|
125 |
+
for filename in sorted(os.listdir(folder_path)):
|
126 |
+
if filename.endswith('.json'):
|
127 |
+
filepath = os.path.join(folder_path, filename)
|
128 |
+
for item in self._load_json_file(filepath, difficulty=difficulty_folder):
|
129 |
+
yield example_id, item
|
130 |
+
example_id += 1
|
131 |
else:
|
132 |
+
if os.path.exists(search_path):
|
133 |
+
for filename in sorted(os.listdir(search_path)):
|
134 |
+
if filename.endswith('.json'):
|
135 |
+
filepath = os.path.join(search_path, filename)
|
136 |
+
for item in self._load_json_file(filepath, difficulty=""):
|
137 |
+
yield example_id, item
|
138 |
+
example_id += 1
|
139 |
|
140 |
def _load_json_file(self, filepath, difficulty=""):
|
141 |
with open(filepath, 'r', encoding='utf-8') as f:
|
142 |
data = json.load(f)
|
143 |
|
144 |
if isinstance(data, list):
|
145 |
+
for item in data:
|
146 |
+
yield self._process_item(item, difficulty)
|
147 |
else:
|
148 |
+
yield self._process_item(data, difficulty)
|
149 |
|
150 |
def _process_item(self, item, difficulty=""):
|
151 |
category = item.get("category", {})
|