Datasets:
Tasks:
Text Generation
Formats:
parquet
Sub-tasks:
language-modeling
Languages:
Danish
Size:
10M - 100M
ArXiv:
DOI:
License:
File size: 8,079 Bytes
4781621 9e230b3 4781621 9e230b3 4781621 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "datasets",
# "mediawiki-dump",
# "mwparserfromhell",
# "pandas",
# "requests",
# "tqdm",
# "transformers",
# "dynaword"
# ]
# [tool.uv.sources]
# dynaword = { git = "https://huggingface.co/datasets/danish-foundation-models/danish-dynaword", rev = "00e7f2aee7f7ad2da423419f77ecbb9c0536de0d" }
# ///
import bz2
import datetime
import json
import logging
import os
from pathlib import Path
import re
import subprocess
import sys
import threading
from mediawiki_dump.dumps import IteratorDump
from mediawiki_dump.reader import DumpReaderArticles
from mwparserfromhell import parse
import requests
from tqdm import tqdm
import pandas as pd
from datasets import Dataset
from typing import Tuple
from dynaword.process_dataset import (
add_token_count,
ensure_column_order,
remove_duplicate_text,
remove_empty_texts,
)
logger = logging.getLogger(__name__)
source = "wiki"
class WtfNodeBridge:
"""
Persistent Node bridge to wtf_wikipedia.
Call .parse(wikitext, lang=None) -> (text, is_redirect)
Remember to call .close() when done.
"""
def __init__(
self, node_script_path: str = "parser/wtf_bridge.js", node_cmd: str = "node"
):
self.proc = subprocess.Popen(
[node_cmd, node_script_path],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding="utf-8",
bufsize=1,
)
self._lock = threading.Lock()
# background thread to log stderr (helpful for debugging)
def _drain_stderr(p):
try:
for line in p.stderr:
logger.warning("wtf_node stderr: %s", line.rstrip())
except Exception:
pass
t = threading.Thread(target=_drain_stderr, args=(self.proc,), daemon=True)
t.start()
def parse(self, wikitext: str, lang: str | None = None) -> Tuple[str, bool]:
if self.proc.poll() is not None:
raise RuntimeError("Node bridge process has exited")
payload = {"wikitext": wikitext}
if lang:
payload["lang"] = lang
line = json.dumps(payload, ensure_ascii=False)
with self._lock:
# write and flush a single JSON line
try:
self.proc.stdin.write(line + "\n")
self.proc.stdin.flush()
except BrokenPipeError as e:
raise RuntimeError("Broken pipe writing to node bridge") from e
# read exactly one JSON line back
out_line = self.proc.stdout.readline()
if not out_line:
raise RuntimeError("No response from node bridge (it may have exited)")
res = json.loads(out_line)
if res.get("error"):
# choose to either raise or return empty text; here we raise
raise RuntimeError("Node bridge error: " + res["error"])
return res.get("text", ""), bool(res.get("isRedirect", False))
def close(self):
try:
if self.proc.stdin:
self.proc.stdin.close()
except Exception:
pass
try:
self.proc.terminate()
self.proc.wait(timeout=3)
except Exception:
try:
self.proc.kill()
except Exception:
pass
def download_wiki_dump(url: str, file_path: str):
"""
Downloads a file from a URL with a progress bar.
Args:
url (str): The URL of the file to download.
file_path (str): The local path to save the file.
"""
print(f"Downloading {url} to {file_path}...")
try:
with requests.get(url, stream=True) as r:
r.raise_for_status()
total_size = int(r.headers.get("content-length", 0))
block_size = 8192 # 8 Kibibytes
with (
open(file_path, "wb") as f,
tqdm(
total=total_size, unit="B", unit_scale=True, desc="Downloading"
) as pbar,
):
for chunk in r.iter_content(chunk_size=block_size):
if chunk: # filter out keep-alive chunks
f.write(chunk)
pbar.update(len(chunk))
logger.info("\nDownload complete.")
except requests.exceptions.RequestException as e:
logger.error(f"\nError downloading file: {e}")
sys.exit(1)
def get_content(file_name: str):
with bz2.open(file_name, mode="r") as fp:
yield from fp
def process_dump_to_parquet(bz2_file_path: str, parquet_file_path: str):
dump = IteratorDump(iterator=get_content(file_name=bz2_file_path))
pages = DumpReaderArticles().read(dump)
articles = []
today = datetime.datetime.now().strftime("%Y-%m-%d")
bridge = WtfNodeBridge("parser/wtf_bridge.js")
try:
for page in tqdm(pages):
try:
plain_text, is_redirect = bridge.parse(page.content, lang="da")
except Exception as exc:
logger.warning(
"wtf parse failed for page %s: %s -- falling back to mwparserfromhell",
getattr(page, "title", "<unknown>"),
exc,
)
plain_text = parse(page.content).strip_code().strip()
is_redirect = plain_text.startswith("REDIRECT")
if is_redirect:
continue
# Additional cleanup if you like (wtf.text() already removes a lot)
plain_text = re.sub(
r"thumb(?:\|(?:left|right|center|\d+px)*)*\|[^\n]*", "", plain_text
).strip()
if len(plain_text) == 0:
logger.warning("Skipping empty article")
continue
date = datetime.datetime.strptime(
page.timestamp, "%Y-%m-%dT%H:%M:%SZ"
).strftime("%Y-%m-%d")
articles.append(
{
"id": f"{source}_{page.page_id}",
"source": source,
"created": f"{date}, {date}",
"text": f"{page.title}\n{plain_text}",
"added": today,
}
)
finally:
bridge.close()
df = pd.DataFrame(articles)
ds = Dataset.from_pandas(df)
ds = add_token_count(ds)
ds = remove_empty_texts(ds)
ds = remove_duplicate_text(ds)
ds = ensure_column_order(ds)
ds.to_parquet(parquet_file_path, compression="snappy")
def main():
"""
Main function to orchestrate the download and processing.
"""
# --- Configuration ---
# URL for the latest Danish Wikipedia articles dump
WIKI_DUMP_URL = f"https://dumps.wikimedia.org/da{source}/latest/da{source}-latest-pages-articles.xml.bz2"
# Local file paths
DOWNLOADED_BZ2_FILE = f"tmp/da{source}-latest-pages-articles.xml.bz2"
OUTPUT_PARQUET_FILE = "wikipedia.parquet"
# --- Execution ---
# 1. Download the dump file
if not os.path.exists(DOWNLOADED_BZ2_FILE):
download_wiki_dump(WIKI_DUMP_URL, DOWNLOADED_BZ2_FILE)
else:
logger.info(f"File '{DOWNLOADED_BZ2_FILE}' already exists. Skipping download.")
# 2. Process the dump and save to Parquet
process_dump_to_parquet(DOWNLOADED_BZ2_FILE, OUTPUT_PARQUET_FILE)
# 3. (Optional) Clean up the downloaded file
# If you want to keep the bz2 file, comment out the next line.
logger.info(f"Cleaning up by removing '{DOWNLOADED_BZ2_FILE}'...")
os.remove(DOWNLOADED_BZ2_FILE)
logger.info("\nScript finished successfully.")
if __name__ == "__main__":
log_path = Path(__file__).parent / f"{source}.log"
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.StreamHandler(),
logging.FileHandler(log_path),
],
)
main()
|