add script
Browse files- README.md +23 -0
- wikipedia_html_enterprise.py +177 -0
README.md
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This is an helper script to load an html enterprise dataset into a datasets object
|
2 |
+
|
3 |
+
## How to use
|
4 |
+
|
5 |
+
1. Download a NS0 dump at https://dumps.wikimedia.org/other/enterprise_html/runs/20230220/
|
6 |
+
|
7 |
+
2. Untar it
|
8 |
+
|
9 |
+
For example with:
|
10 |
+
```
|
11 |
+
mkdir enwiki-NS6-20230220-ENTERPRISE-HTML
|
12 |
+
tar -I pigz -vxf enwiki-NS6-20230220-ENTERPRISE-HTML.json.tar.gz -C enwiki-NS6-20230220-ENTERPRISE-HTML
|
13 |
+
```
|
14 |
+
|
15 |
+
3. Load it:
|
16 |
+
```python
|
17 |
+
from datasets import load_dataset
|
18 |
+
|
19 |
+
local_path=... # Path to directory where you extracted the NS0 dump
|
20 |
+
shard_id=...
|
21 |
+
|
22 |
+
ds = load_dataset("SaulLu/wikipedia_html_enterprise", shard=shard_id, data_dir=local_path)
|
23 |
+
```
|
wikipedia_html_enterprise.py
ADDED
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 bz2
|
21 |
+
import codecs
|
22 |
+
import json
|
23 |
+
import re
|
24 |
+
import xml.etree.cElementTree as etree
|
25 |
+
from urllib.parse import quote
|
26 |
+
import mwparserfromhell
|
27 |
+
from multiprocess import Process, Manager
|
28 |
+
from tqdm import tqdm
|
29 |
+
import multiprocessing
|
30 |
+
import datasets
|
31 |
+
from functools import partial
|
32 |
+
from pathlib import Path
|
33 |
+
|
34 |
+
logger = datasets.logging.get_logger(__name__)
|
35 |
+
|
36 |
+
|
37 |
+
_CITATION = """"""
|
38 |
+
|
39 |
+
_DESCRIPTION = """"""
|
40 |
+
|
41 |
+
_LICENSE = (
|
42 |
+
"This work is licensed under the Creative Commons Attribution-ShareAlike "
|
43 |
+
"3.0 Unported License. To view a copy of this license, visit "
|
44 |
+
"http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to "
|
45 |
+
"Creative Commons, PO Box 1866, Mountain View, CA 94042, USA."
|
46 |
+
)
|
47 |
+
|
48 |
+
_INFO_FILE = "dumpstatus.json"
|
49 |
+
|
50 |
+
|
51 |
+
_VERSION = datasets.Version("2.0.0", "")
|
52 |
+
_NUM_SPLITS = 68
|
53 |
+
|
54 |
+
class WikipediaConfig(datasets.BuilderConfig):
|
55 |
+
"""BuilderConfig for Wikipedia."""
|
56 |
+
|
57 |
+
def __init__(self, shard=None, version=_VERSION, **kwargs):
|
58 |
+
"""BuilderConfig for Wikipedia.
|
59 |
+
|
60 |
+
Args:
|
61 |
+
split: int, split number.
|
62 |
+
**kwargs: keyword arguments forwarded to super.
|
63 |
+
"""
|
64 |
+
super().__init__(
|
65 |
+
name=f"shard_{shard}",
|
66 |
+
description=f"Wikipedia dataset for split {shard}",
|
67 |
+
version=version,
|
68 |
+
**kwargs,
|
69 |
+
)
|
70 |
+
self.shard = shard
|
71 |
+
print(f"Split: {self.shard}")
|
72 |
+
|
73 |
+
|
74 |
+
class Wikipedia(datasets.GeneratorBasedBuilder):
|
75 |
+
"""Wikipedia dataset."""
|
76 |
+
|
77 |
+
# Use mirror (your.org) to avoid download caps.
|
78 |
+
BUILDER_CONFIG_CLASS = WikipediaConfig
|
79 |
+
BUILDER_CONFIG = [WikipediaConfig(shard=str(id)) for id in range(_NUM_SPLITS)]
|
80 |
+
|
81 |
+
def _info(self):
|
82 |
+
return datasets.DatasetInfo(
|
83 |
+
description=_DESCRIPTION,
|
84 |
+
features=datasets.Features(
|
85 |
+
{
|
86 |
+
"identifier": datasets.Value("string"),
|
87 |
+
"name": datasets.Value("string"),
|
88 |
+
"namespace_name": datasets.Value("string"),
|
89 |
+
"namespace_identifier": datasets.Value("string"),
|
90 |
+
"categories": [
|
91 |
+
{
|
92 |
+
"name": datasets.Value("string"),
|
93 |
+
"url": datasets.Value("string"),
|
94 |
+
}
|
95 |
+
],
|
96 |
+
"date_modified": datasets.Value("string"),
|
97 |
+
"url": datasets.Value("string"),
|
98 |
+
"html": datasets.Value("string"),
|
99 |
+
"wikitext": datasets.Value("string"),
|
100 |
+
"in_language": datasets.Value("string"),
|
101 |
+
"main_entity": {
|
102 |
+
"identifier": datasets.Value("string"),
|
103 |
+
"url": datasets.Value("string"),
|
104 |
+
},
|
105 |
+
"is_part_of" : {
|
106 |
+
"name": datasets.Value("string"),
|
107 |
+
"identifier": datasets.Value("string"),
|
108 |
+
},
|
109 |
+
"license":[ {
|
110 |
+
"name": datasets.Value("string"),
|
111 |
+
"url": datasets.Value("string"),
|
112 |
+
"identifier": datasets.Value("string"),
|
113 |
+
}]
|
114 |
+
}
|
115 |
+
),
|
116 |
+
# No default supervised_keys.
|
117 |
+
supervised_keys=None,
|
118 |
+
homepage="https://dumps.wikimedia.org",
|
119 |
+
citation=_CITATION,
|
120 |
+
)
|
121 |
+
|
122 |
+
def _split_generators(self, dl_manager):
|
123 |
+
data_paths = [
|
124 |
+
Path(self.config.data_dir) / f"enwiki_{self.config.shard}.ndjson"
|
125 |
+
]
|
126 |
+
return [
|
127 |
+
datasets.SplitGenerator( # pylint:disable=g-complex-comprehension
|
128 |
+
name=datasets.Split.TRAIN, gen_kwargs={"filepaths": data_paths}
|
129 |
+
)
|
130 |
+
]
|
131 |
+
|
132 |
+
def _generate_examples(self, filepaths, ):
|
133 |
+
|
134 |
+
|
135 |
+
print("Parsing and cleaning Wikipedia examples")
|
136 |
+
|
137 |
+
for filepath in filepaths:
|
138 |
+
with open(filepath, 'r') as f:
|
139 |
+
for line in tqdm(f):
|
140 |
+
example = json.loads(line)
|
141 |
+
clean_example = {}
|
142 |
+
clean_example['name'] = example['name']
|
143 |
+
clean_example['identifier'] = example['identifier']
|
144 |
+
clean_example['date_modified'] = example['date_modified']
|
145 |
+
clean_example['namespace_name'] = example['namespace']["name"]
|
146 |
+
clean_example['namespace_identifier'] = example['namespace']["identifier"]
|
147 |
+
clean_example["categories"] = example.get("categories", None)
|
148 |
+
clean_example['url'] = example['url']
|
149 |
+
clean_example['html'] = f'{example["article_body"]["html"]}'
|
150 |
+
clean_example['wikitext'] = example['article_body']['wikitext']
|
151 |
+
clean_example['in_language'] = example['in_language']
|
152 |
+
clean_example['main_entity'] = example.get('main_entity', None)
|
153 |
+
clean_example['is_part_of'] = example['is_part_of']
|
154 |
+
clean_example['license'] = example['license']
|
155 |
+
yield clean_example['identifier'], clean_example
|
156 |
+
# num_processes = 16
|
157 |
+
# with multiprocessing.Pool(processes=num_processes) as pool:
|
158 |
+
|
159 |
+
# results = pool.imap_unordered(partial(parse_and_clean), filepaths)
|
160 |
+
# for result in results:
|
161 |
+
# for example in result:
|
162 |
+
# yield example
|
163 |
+
|
164 |
+
def parse_and_clean(filepath):
|
165 |
+
examples = []
|
166 |
+
with open(filepath, 'r') as f:
|
167 |
+
for line in tqdm(f):
|
168 |
+
example = json.loads(line)
|
169 |
+
clean_example = {}
|
170 |
+
clean_example['id'] = example['identifier']
|
171 |
+
clean_example['date_modified'] = example['date_modified']
|
172 |
+
clean_example['url'] = example['url']
|
173 |
+
clean_example['html'] = f'{example["article_body"]["html"]}'
|
174 |
+
clean_example['wikitext'] = example['article_body']['wikitext']
|
175 |
+
|
176 |
+
examples.append(clean_example)
|
177 |
+
return examples
|