khulnasoft
commited on
Commit
•
e49978e
1
Parent(s):
415423a
Create banglawiki.py
Browse files- banglawiki.py +142 -0
banglawiki.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
# Lint as: python3
|
17 |
+
"""Wikipedia dataset containing cleaned articles of all languages."""
|
18 |
+
|
19 |
+
|
20 |
+
import json
|
21 |
+
|
22 |
+
import datasets
|
23 |
+
import pyarrow.parquet as pq
|
24 |
+
|
25 |
+
from .lang_def import WIKIPEDIA_LANGUAGES
|
26 |
+
|
27 |
+
logger = datasets.logging.get_logger(__name__)
|
28 |
+
|
29 |
+
|
30 |
+
_CITATION = """\
|
31 |
+
@ONLINE {wikidump,
|
32 |
+
author = {Wikimedia Foundation},
|
33 |
+
title = {Wikimedia Downloads},
|
34 |
+
url = {https://dumps.wikimedia.org}
|
35 |
+
}
|
36 |
+
"""
|
37 |
+
|
38 |
+
_DESCRIPTION = """\
|
39 |
+
Wikipedia dataset containing cleaned articles of all languages.
|
40 |
+
The datasets are built from the Wikipedia dump
|
41 |
+
(https://dumps.wikimedia.org/) with one split per language. Each example
|
42 |
+
contains the content of one full Wikipedia article with cleaning to strip
|
43 |
+
markdown and unwanted sections (references, etc.).
|
44 |
+
"""
|
45 |
+
|
46 |
+
_LICENSE = (
|
47 |
+
"This work is licensed under the Creative Commons Attribution-ShareAlike "
|
48 |
+
"3.0 Unported License. To view a copy of this license, visit "
|
49 |
+
"http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to "
|
50 |
+
"Creative Commons, PO Box 1866, Mountain View, CA 94042, USA."
|
51 |
+
)
|
52 |
+
|
53 |
+
_BASE_URL_TMPL = "data/{date}/{lang}"
|
54 |
+
|
55 |
+
_VERSION = datasets.Version("1.2.0", "")
|
56 |
+
|
57 |
+
|
58 |
+
class WikipediaConfig(datasets.BuilderConfig):
|
59 |
+
"""BuilderConfig for Wikipedia."""
|
60 |
+
|
61 |
+
def __init__(self, language=None, date=None, version=_VERSION, **kwargs):
|
62 |
+
"""BuilderConfig for Wikipedia.
|
63 |
+
Args:
|
64 |
+
language: string, the language code for the Wikipedia dump to use.
|
65 |
+
date: string, date of the Wikipedia dump in YYYYMMDD format. A list of
|
66 |
+
available dates can be found at https://dumps.wikimedia.org/enwiki/.
|
67 |
+
**kwargs: keyword arguments forwarded to super.
|
68 |
+
"""
|
69 |
+
super().__init__(
|
70 |
+
name=f"{date}.{language}",
|
71 |
+
description=f"Wikipedia dataset for {language}, parsed from {date} dump.",
|
72 |
+
version=version,
|
73 |
+
**kwargs,
|
74 |
+
)
|
75 |
+
self.date = date
|
76 |
+
self.language = language
|
77 |
+
|
78 |
+
|
79 |
+
class Wikipedia(datasets.ArrowBasedBuilder):
|
80 |
+
"""Wikipedia dataset."""
|
81 |
+
|
82 |
+
# Use mirror (your.org) to avoid download caps.
|
83 |
+
BUILDER_CONFIG_CLASS = WikipediaConfig
|
84 |
+
BUILDER_CONFIGS = [
|
85 |
+
WikipediaConfig(
|
86 |
+
language=lang,
|
87 |
+
date=date,
|
88 |
+
) # pylint:disable=g-complex-comprehension
|
89 |
+
for date in WIKIPEDIA_LANGUAGES.keys()
|
90 |
+
for lang in WIKIPEDIA_LANGUAGES[date]
|
91 |
+
]
|
92 |
+
|
93 |
+
def _info(self):
|
94 |
+
return datasets.DatasetInfo(
|
95 |
+
description=_DESCRIPTION,
|
96 |
+
features=datasets.Features(
|
97 |
+
{
|
98 |
+
"id": datasets.Value("string"),
|
99 |
+
"url": datasets.Value("string"),
|
100 |
+
"title": datasets.Value("string"),
|
101 |
+
"text": datasets.Value("string"),
|
102 |
+
}
|
103 |
+
),
|
104 |
+
# No default supervised_keys.
|
105 |
+
supervised_keys=None,
|
106 |
+
homepage="https://dumps.wikimedia.org",
|
107 |
+
citation=_CITATION,
|
108 |
+
)
|
109 |
+
|
110 |
+
def _split_generators(self, dl_manager):
|
111 |
+
def _base_url(lang):
|
112 |
+
return _BASE_URL_TMPL.format(lang=lang, date=self.config.date)
|
113 |
+
|
114 |
+
lang = self.config.language
|
115 |
+
|
116 |
+
info_file_url = _base_url(lang) + "/info.json"
|
117 |
+
downloaded_info_file = dl_manager.download_and_extract({"info": info_file_url})
|
118 |
+
with open(downloaded_info_file["info"], "r") as f:
|
119 |
+
info = json.load(f)
|
120 |
+
|
121 |
+
shard_urls = [
|
122 |
+
_base_url(lang) + "/" + shard_name for shard_name in info["shards"]
|
123 |
+
]
|
124 |
+
downloaded_files = dl_manager.download({"train": shard_urls})
|
125 |
+
logger.info("downloaded_files = %s", downloaded_files)
|
126 |
+
|
127 |
+
return [
|
128 |
+
datasets.SplitGenerator(
|
129 |
+
name=datasets.Split.TRAIN,
|
130 |
+
gen_kwargs={"filepaths": downloaded_files["train"]},
|
131 |
+
),
|
132 |
+
]
|
133 |
+
|
134 |
+
def _generate_tables(self, filepaths):
|
135 |
+
"""This function returns the examples in the raw (text) form."""
|
136 |
+
|
137 |
+
for filepath in filepaths:
|
138 |
+
with open(filepath, "rb") as f:
|
139 |
+
pf = pq.ParquetFile(f)
|
140 |
+
for group_i in range(pf.num_row_groups):
|
141 |
+
tbl = pf.read_row_group(group_i)
|
142 |
+
yield group_i, tbl
|