lhoestq HF staff commited on
Commit
55b446b
1 Parent(s): 389ddfb

pass iter_archive

Browse files
Files changed (1) hide show
  1. openwebtext.py +4 -22
openwebtext.py CHANGED
@@ -14,9 +14,7 @@
14
  # limitations under the License.
15
  """The Open WebText Corpus"""
16
 
17
- import os
18
  import re
19
- import tarfile
20
 
21
  import datasets
22
 
@@ -38,23 +36,6 @@ _N_DATA_FILES = 21
38
  _DATA_FILES = ["subsets/urlsf_subset{:02d}.tar".format(i) for i in range(_N_DATA_FILES)]
39
 
40
 
41
- def _iter_tar(f):
42
- stream = tarfile.open(fileobj=f, mode="r|*")
43
- for tarinfo in stream:
44
- file_path = tarinfo.name
45
- if not tarinfo.isreg():
46
- continue
47
- if file_path is None:
48
- continue
49
- if os.path.basename(file_path).startswith((".", "__")):
50
- # skipping hidden files
51
- continue
52
- file_obj = stream.extractfile(tarinfo)
53
- yield file_path, file_obj
54
- stream.members = []
55
- del stream
56
-
57
-
58
  class Openwebtext(datasets.GeneratorBasedBuilder):
59
  """The Open WebText dataset."""
60
 
@@ -80,17 +61,18 @@ class Openwebtext(datasets.GeneratorBasedBuilder):
80
  datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={
81
  "archive_iterators": [
82
  dl_manager.iter_archive(archive) for archive in archives
83
- ]
 
84
  }),
85
  ]
86
 
87
- def _generate_examples(self, archive_iterators):
88
  """Yields examples."""
89
  for archive_iterator in archive_iterators:
90
  for xz_filepath, xz_f in archive_iterator:
91
  if not xz_filepath.endswith(".xz"):
92
  continue
93
- for txt_filepath, txt_f in _iter_tar(xz_f):
94
  if not txt_filepath.endswith(".txt"):
95
  continue
96
  idx = f"{xz_filepath}/{txt_filepath}"
 
14
  # limitations under the License.
15
  """The Open WebText Corpus"""
16
 
 
17
  import re
 
18
 
19
  import datasets
20
 
 
36
  _DATA_FILES = ["subsets/urlsf_subset{:02d}.tar".format(i) for i in range(_N_DATA_FILES)]
37
 
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  class Openwebtext(datasets.GeneratorBasedBuilder):
40
  """The Open WebText dataset."""
41
 
 
61
  datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={
62
  "archive_iterators": [
63
  dl_manager.iter_archive(archive) for archive in archives
64
+ ],
65
+ "iter_archive": dl_manager.iter_archive
66
  }),
67
  ]
68
 
69
+ def _generate_examples(self, archive_iterators, iter_archive):
70
  """Yields examples."""
71
  for archive_iterator in archive_iterators:
72
  for xz_filepath, xz_f in archive_iterator:
73
  if not xz_filepath.endswith(".xz"):
74
  continue
75
+ for txt_filepath, txt_f in iter_archive(xz_f):
76
  if not txt_filepath.endswith(".txt"):
77
  continue
78
  idx = f"{xz_filepath}/{txt_filepath}"