albertvillanova HF Staff commited on
Commit
5ada2aa
·
verified ·
1 Parent(s): 887a7ab

Support non-streaming

Browse files
Files changed (1) hide show
  1. wikipedia.py +47 -22
wikipedia.py CHANGED
@@ -104,28 +104,53 @@ class Wikipedia(datasets.GeneratorBasedBuilder):
104
  return self.config.host + _URL_PATH.format(language=self.config.language.replace("-", "_"))
105
 
106
  def _split_generators(self, dl_manager):
107
- data_path = dl_manager.download(self._get_base_url())
108
- return [
109
- datasets.SplitGenerator(
110
- name=datasets.Split.TRAIN,
111
- gen_kwargs={
112
- "archive": dl_manager.iter_archive(data_path),
113
- },
114
- )
115
- ]
116
-
117
- def _generate_examples(self, archive):
118
- id_ = 0
119
- for path, file in archive:
120
- for line in file:
121
- document = json.loads(line)
122
- title = document["name"]
123
- url = document["url"]
124
- article = mwparserfromhtml.Article(document["article_body"]["html"])
125
- text = html_to_text(article)
126
- if text:
127
- yield id_, {"id": id_, "url": url, "title": title, "text": text}
128
- id_ += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
 
131
  def html_to_text(article: mwparserfromhtml.Article):
 
104
  return self.config.host + _URL_PATH.format(language=self.config.language.replace("-", "_"))
105
 
106
  def _split_generators(self, dl_manager):
107
+ if dl_manager.is_streaming:
108
+ data_path = dl_manager.download(self._get_base_url())
109
+ return [
110
+ datasets.SplitGenerator(
111
+ name=datasets.Split.TRAIN,
112
+ gen_kwargs={
113
+ "archive": dl_manager.iter_archive(data_path),
114
+ },
115
+ )
116
+ ]
117
+ else:
118
+ data_path = dl_manager.download_and_extract(self._get_base_url())
119
+ return [
120
+ datasets.SplitGenerator(
121
+ name=datasets.Split.TRAIN,
122
+ gen_kwargs={
123
+ "paths": list(dl_manager.iter_files(data_path)),
124
+ },
125
+ )
126
+ ]
127
+
128
+ def _generate_examples(self, archive=None, paths=None):
129
+ if archive:
130
+ id_ = 0
131
+ for path, file in archive:
132
+ for line in file:
133
+ document = json.loads(line)
134
+ title = document["name"]
135
+ url = document["url"]
136
+ article = mwparserfromhtml.Article(document["article_body"]["html"])
137
+ text = html_to_text(article)
138
+ if text:
139
+ yield id_, {"id": id_, "url": url, "title": title, "text": text}
140
+ id_ += 1
141
+ elif paths:
142
+ id_ = 0
143
+ for path in paths:
144
+ with open(path, "rb") as file:
145
+ for line in file:
146
+ document = json.loads(line)
147
+ title = document["name"]
148
+ url = document["url"]
149
+ article = mwparserfromhtml.Article(document["article_body"]["html"])
150
+ text = html_to_text(article)
151
+ if text:
152
+ yield id_, {"id": id_, "url": url, "title": title, "text": text}
153
+ id_ += 1
154
 
155
 
156
  def html_to_text(article: mwparserfromhtml.Article):