Datasets:
Tasks:
Token Classification
Sub-tasks:
parsing
ArneBinder
commited on
Commit
•
e917db0
1
Parent(s):
70446e7
replace subdirecty_mapping by split_paths and allow list of files for values
Browse files
brat.py
CHANGED
@@ -2,7 +2,7 @@ import glob
|
|
2 |
import logging
|
3 |
from dataclasses import dataclass
|
4 |
from os import listdir, path
|
5 |
-
from typing import Dict, List, Optional
|
6 |
|
7 |
import datasets
|
8 |
from datasets import BuilderConfig, DatasetInfo, Features, Sequence, SplitGenerator, Value
|
@@ -18,8 +18,9 @@ class BratConfig(BuilderConfig):
|
|
18 |
description: Optional[str] = None
|
19 |
citation: Optional[str] = None
|
20 |
homepage: Optional[str] = None
|
21 |
-
|
22 |
-
|
|
|
23 |
file_name_blacklist: Optional[List[str]] = None
|
24 |
ann_file_extension: str = "ann"
|
25 |
txt_file_extension: str = "txt"
|
@@ -270,14 +271,15 @@ class Brat(datasets.GeneratorBasedBuilder):
|
|
270 |
)
|
271 |
return res
|
272 |
|
273 |
-
def _generate_examples(self, files=None, directory=None):
|
274 |
"""Read context (.txt) and annotation (.ann) files."""
|
275 |
if files is None:
|
276 |
-
|
277 |
-
directory
|
278 |
-
), "If files is None, directory has to be provided, but it is also None."
|
279 |
_files = glob.glob(f"{directory}/*.{self.config.ann_file_extension}")
|
280 |
files = sorted(path.splitext(fn)[0] for fn in _files)
|
|
|
|
|
281 |
|
282 |
for filename in files:
|
283 |
basename = path.basename(filename)
|
@@ -311,24 +313,36 @@ class Brat(datasets.GeneratorBasedBuilder):
|
|
311 |
|
312 |
data_dir = dl_manager.download_and_extract(self.config.url)
|
313 |
|
314 |
-
subdirectory_mapping = self.config.subdirectory_mapping
|
315 |
# if no subdirectory mapping is provided, ...
|
316 |
-
if
|
317 |
# ... use available subdirectories as split names ...
|
318 |
subdirs = [f for f in listdir(data_dir) if path.isdir(path.join(data_dir, f))]
|
319 |
if len(subdirs) > 0:
|
320 |
-
|
321 |
else:
|
322 |
# ... otherwise, default to a single train split with the base directory
|
323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
324 |
|
325 |
return [
|
326 |
SplitGenerator(
|
327 |
name=split,
|
328 |
# These kwargs will be passed to _generate_examples
|
329 |
gen_kwargs={
|
330 |
-
"
|
|
|
331 |
},
|
332 |
)
|
333 |
-
for
|
334 |
-
]
|
|
|
2 |
import logging
|
3 |
from dataclasses import dataclass
|
4 |
from os import listdir, path
|
5 |
+
from typing import Dict, List, Optional, Union
|
6 |
|
7 |
import datasets
|
8 |
from datasets import BuilderConfig, DatasetInfo, Features, Sequence, SplitGenerator, Value
|
|
|
18 |
description: Optional[str] = None
|
19 |
citation: Optional[str] = None
|
20 |
homepage: Optional[str] = None
|
21 |
+
|
22 |
+
# paths to directories or files per split (relative to url or data_dir)
|
23 |
+
split_paths: Optional[Dict[str, Union[str, List[str]]]] = None
|
24 |
file_name_blacklist: Optional[List[str]] = None
|
25 |
ann_file_extension: str = "ann"
|
26 |
txt_file_extension: str = "txt"
|
|
|
271 |
)
|
272 |
return res
|
273 |
|
274 |
+
def _generate_examples(self, base_dir: str, files: Optional[List[str]] = None, directory: Optional[str] = None):
|
275 |
"""Read context (.txt) and annotation (.ann) files."""
|
276 |
if files is None:
|
277 |
+
if directory is None:
|
278 |
+
raise ValueError("Either files or directory has to be provided.")
|
|
|
279 |
_files = glob.glob(f"{directory}/*.{self.config.ann_file_extension}")
|
280 |
files = sorted(path.splitext(fn)[0] for fn in _files)
|
281 |
+
elif directory is not None:
|
282 |
+
raise ValueError("Only one of files or directory can be provided.")
|
283 |
|
284 |
for filename in files:
|
285 |
basename = path.basename(filename)
|
|
|
313 |
|
314 |
data_dir = dl_manager.download_and_extract(self.config.url)
|
315 |
|
|
|
316 |
# if no subdirectory mapping is provided, ...
|
317 |
+
if self.config.split_paths is None:
|
318 |
# ... use available subdirectories as split names ...
|
319 |
subdirs = [f for f in listdir(data_dir) if path.isdir(path.join(data_dir, f))]
|
320 |
if len(subdirs) > 0:
|
321 |
+
split_paths = {subdir: {"directory": subdir} for subdir in subdirs}
|
322 |
else:
|
323 |
# ... otherwise, default to a single train split with the base directory
|
324 |
+
split_paths = {"train": {"directory": ""}}
|
325 |
+
else:
|
326 |
+
split_paths = {}
|
327 |
+
for split, paths in self.config.split_paths.items():
|
328 |
+
if isinstance(paths, str):
|
329 |
+
split_paths[split] = {"directory": paths}
|
330 |
+
elif isinstance(paths, list):
|
331 |
+
split_paths[split] = {"files": paths}
|
332 |
+
else:
|
333 |
+
raise ValueError(
|
334 |
+
f"split_paths must be a dict containing either a single path to a directory "
|
335 |
+
f"or a list of file paths, but found: {paths}"
|
336 |
+
)
|
337 |
|
338 |
return [
|
339 |
SplitGenerator(
|
340 |
name=split,
|
341 |
# These kwargs will be passed to _generate_examples
|
342 |
gen_kwargs={
|
343 |
+
"base_dir": data_dir,
|
344 |
+
**split_kwargs,
|
345 |
},
|
346 |
)
|
347 |
+
for split, split_kwargs in split_paths.items()
|
348 |
+
]
|