Datasets:
Support non-streaming
Browse files- 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 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|