V4ldeLund's picture
Publish Norwegian dyna-instruct v0.1.0 with five curated subsets
faaa741 verified
Raw History Blame Contribute Delete
3.54 kB
"""Build the training split of pere/reasoning_norwegian in chat form.
Source: pere/reasoning_norwegian
Revision: 214ce4bd6dc06156b7f930a8992cedd5a1cf3e1e
"""
from __future__ import annotations
import json
import html
import unicodedata
from collections.abc import Iterator
from pathlib import Path
from datasets import load_dataset
from dynainst.build_utils import build_dataset
SOURCE = "reasoning-norwegian"
HF_DATASET = "pere/reasoning_norwegian"
REVISION = "214ce4bd6dc06156b7f930a8992cedd5a1cf3e1e"
ADDED = "2026-09-03"
CREATED = "2025-01-01, 2025-02-28"
SAVE_DIR = Path(__file__).parent
PROMPT = """Gjennomgå og korriger følgende tekst for feil i tegnsetting (feilplasserte, manglende eller overflødige tegn) samt bruk av store og små bokstaver. Du skal aldri endre stavemåten eller legge til eller fjerne ord.
Når du svarer, skal du kun returnere den korrigerte teksten, uten ekstra forklaring. Hvis setningen allerede er korrekt, returner originalteksten.
Her er teksten du skal analysere:
{corrupt}"""
def lexical_content(text: str) -> str:
"""Ignore punctuation, whitespace, and case while retaining letters/digits."""
normalized = unicodedata.normalize("NFKC", html.unescape(text)).casefold()
return "".join(character for character in normalized if character.isalnum())
def convert(raw) -> Iterator[dict]:
for index, row in enumerate(raw):
# Some released `text` serializations disagree with the separate verified
# answer field. Rebuild the documented task from explicit fields and omit
# rows for which no verified final correction was retained.
answer = row["text_result"].strip()
if not answer:
continue
if lexical_content(row["corrupt"]) != lexical_content(answer):
continue
yield {
"id": f"{SOURCE}_{index:05d}",
"messages": [
{
"role": "user",
"content": PROMPT.format(corrupt=row["corrupt"].strip()),
},
{"role": "assistant", "content": answer},
],
"source": SOURCE,
"added": ADDED,
"created": CREATED,
"language": ["nob"],
"task": "instruction_following",
"token_count": -1,
}
def write_attribution(raw, dataset) -> None:
output_path = SAVE_DIR / "attribution.jsonl"
prefix = f"{SOURCE}_"
with output_path.open("w", encoding="utf-8") as output:
for sample_id in dataset["id"]:
source_index = int(sample_id.removeprefix(prefix))
source_row = raw[source_index]
record = {
"id": sample_id,
"article_url": source_row["url"],
"paragraph_number": source_row["paragraph_number"],
"reasoning_dataset": HF_DATASET,
"reasoning_dataset_revision": REVISION,
"wikipedia_page_revision_id": None,
"wikipedia_page_revision_known": False,
}
output.write(json.dumps(record, ensure_ascii=False, sort_keys=True) + "\n")
def main() -> None:
# Validation and test remain held out by construction.
raw = load_dataset(HF_DATASET, revision=REVISION, split="train")
output_path = SAVE_DIR / f"{SOURCE}.parquet"
dataset = build_dataset(convert(raw), output_path=output_path)
write_attribution(raw, dataset)
print(f"Wrote {len(dataset):,} samples to {output_path}")
if __name__ == "__main__":
main()