Datasets:
Tasks:
Text Generation
Modalities:
Text
Formats:
parquet
Languages:
Polish
Size:
10K - 100K
License:
Download src/test_build_fandom_pl.py from PiotrSty/fandom-pl: direct link, hf CLI and curl.
- Browser
- Download file 8.26 kB
-
https://huggingface.co/datasets/PiotrSty/fandom-pl/resolve/main/src/test_build_fandom_pl.py
- Command line
-
hf download hf://datasets/PiotrSty/fandom-pl/src/test_build_fandom_pl.py
-
curl -L -o test_build_fandom_pl.py https://huggingface.co/datasets/PiotrSty/fandom-pl/resolve/main/src/test_build_fandom_pl.py
8.26 kB
| import importlib.util | |
| from pathlib import Path | |
| SCRIPT = Path(__file__).with_name("build_fandom_pl.py") | |
| SPEC = importlib.util.spec_from_file_location("build_fandom_pl", SCRIPT) | |
| MODULE = importlib.util.module_from_spec(SPEC) | |
| SPEC.loader.exec_module(MODULE) | |
| def test_ns0_only(): | |
| assert all(w[0] for w in MODULE.WIKIS) | |
| assert len(MODULE.WIKIS) == 12 | |
| def test_normalize_repairs_hyphenation_and_page_numbers(): | |
| assert MODULE.normalize("Pierw-\nszy akapit.\n12\n\ndrugi wiersz") == "Pierwszy akapit.\n\ndrugi wiersz" | |
| def test_title_normalization_is_case_and_diacritic_insensitive(): | |
| assert MODULE.normalize_title("Dyskusja:Łódź (woj.)") == "dyskusja łodz woj" | |
| def test_boilerplate_lines_removed_only_when_frequent(): | |
| docs = [["Baner wikiprojektu", f"Prawdziwa dyskusja {i}."] for i in range(150)] | |
| lines = MODULE.strip_boilerplate(docs) | |
| assert "Baner wikiprojektu" in lines | |
| assert "Prawdziwa dyskusja 0." not in lines | |
| def test_boilerplate_long_lines_never_dropped(): | |
| docs = [["x" * 200] * 1 for _ in range(150)] | |
| assert MODULE.strip_boilerplate(docs) == set() | |
| def test_ip_redaction_patterns(): | |
| text = "Podpisano przez 83.4.55.1 dnia 2 maja" | |
| assert MODULE.IPV4_RE.search(text) | |
| assert not MODULE.IPV4_RE.search("wersja 1.2 wydana") | |
| def test_redaction_covers_compressed_ipv6_and_labelled_identifiers(): | |
| text = "Adres [2001:db8::1], ::ffff:192.0.2.1. PESEL: 00000000000 NIP 000-000-00-00 REGON: 000000000 Rachunek PL00 " + "0000 " * 6 | |
| cleaned, counts = MODULE.redact_pii(text) | |
| assert "2001:db8" not in cleaned and "192.0.2.1" not in cleaned | |
| assert cleaned.count("[REDACTED:IP]") == 2 | |
| assert cleaned.count("[REDACTED:ID]") == 3 | |
| assert "[REDACTED:ACCOUNT]" in cleaned | |
| assert counts["ipv6"] == 2 and counts["national_identifier"] == 3 | |
| assert MODULE.redact_pii(cleaned)[0] == cleaned | |
| def test_redaction_preserves_time_and_version_numbers(): | |
| text = "Wersja 1.2.3, godzina 12:30:45. Adres 2001:db8::2." | |
| cleaned, _ = MODULE.redact_pii(text) | |
| assert cleaned == "Wersja 1.2.3, godzina 12:30:45. Adres [REDACTED:IP]." | |
| def test_attribution_url_quotes_fragments_and_polish_characters(): | |
| from urllib.parse import unquote, urlsplit | |
| url = MODULE.page_url("Dyskusja:Łódź/C#", wiki="elderscrolls") | |
| assert url.isascii() | |
| assert not urlsplit(url).fragment | |
| assert unquote(urlsplit(url).path) == "/pl/wiki/Dyskusja:Łódź/C#" # Fandom Polish wikis use /pl/wiki/ | |
| def test_text_bound_and_license(): | |
| assert MODULE.MIN_TEXT_CHARS == 500 | |
| assert MODULE.LICENSE_SPDX == "CC-BY-SA-3.0" | |
| assert len(MODULE.WIKIS) == 12 | |
| def test_boilerplate_shingles_detected_on_subsample_and_filtered(): | |
| template = ["to jest szablon powitania bota " * 30] | |
| docs = [{"text": "\n".join(template + [f"unikalny tekst {i} " * 30])} for i in range(200)] | |
| hot, max_df, sampled = MODULE.boilerplate_shingles(docs, set(), stride=1, fraction=0.5) | |
| assert sampled == 200 and max_df == 100 | |
| template_sketch = MODULE.shingle_sketch(" ".join(template)) | |
| assert template_sketch & hot == template_sketch # whole template is boilerplate | |
| unique = MODULE.shingle_sketch(f"unikalny tekst 7 " * 30) | |
| assert unique - hot | |
| assert MODULE.filter_sketch(template_sketch | unique, hot) == unique - hot | |
| def test_indexed_dedup_matches_exhaustive_comparison(): | |
| import random | |
| rng = random.Random(42) | |
| sketches = [set(), {1}, {1}, set(range(10)), set(range(9)), set(range(8))] | |
| for _ in range(200): | |
| base = set(rng.sample(range(10000), rng.randint(10, 200))) | |
| sketches.extend([base, base | {10001}, set(sorted(base)[1:]), base | set(range(20))]) | |
| index = MODULE.NearDuplicateIndex() | |
| retained = [] | |
| for sketch in sketches: | |
| expected = (None, 0.0) | |
| for row_id, other in retained: | |
| score = len(sketch & other) / max(len(sketch | other), 1) | |
| if score >= 0.90: | |
| expected = (row_id, score) | |
| break | |
| assert index.find(sketch) == expected | |
| if expected[0] is None: | |
| row_id = str(len(retained)) | |
| retained.append((row_id, sketch)) | |
| index.add(row_id, sketch) | |
| def test_index_avoids_comparing_disjoint_sketches(): | |
| index = MODULE.NearDuplicateIndex() | |
| for i in range(1000): | |
| sketch = set(range(i * 100, (i + 1) * 100)) | |
| assert index.find(sketch) == (None, 0.0) | |
| index.add(str(i), sketch) | |
| assert index.comparisons == 0 | |
| assert index.find(set(range(90000, 90100))) == ("900", 1.0) | |
| assert index.comparisons == 1 | |
| def test_language_vote_reuses_identical_windows_without_changing_votes(): | |
| class Identifier: | |
| def __init__(self): | |
| self.calls = 0 | |
| def classify(self, text): | |
| self.calls += 1 | |
| return "pl", 0.99 | |
| identifier = Identifier() | |
| language, votes = MODULE.language_vote(identifier, "Przykładowy polski tekst.") | |
| assert language == "pl" and votes == [("pl", 0.99)] * 3 | |
| assert identifier.calls == 1 | |
| def write_test_source(tmp_path, texts): | |
| import gzip | |
| import json | |
| (tmp_path / "extracted").mkdir(parents=True, exist_ok=True) | |
| MODULE.save(tmp_path / "selection.json", {"wikis": [], "total_articles": 0, "observed_at": "2026-09-11T00:00:00Z", "files": [], "license_provenance": MODULE.FANDOM_LICENSE_URL}) | |
| MODULE.save(tmp_path / "acquisition.json", {"source_pages": len(texts), "observed_at": "2026-09-11T00:00:00Z"}) | |
| records = [{"pageid": str(index), "ns": "0", "title": f"NonArtykul:Test {index}", "timestamp": "2026-09-01", | |
| "wikitext_chars": len(text), "text": text, "wiki": "elderscrolls"} for index, text in enumerate(texts, 1)] | |
| with gzip.open(tmp_path / "extracted" / "source.jsonl.gz", "wt", encoding="utf-8") as f: | |
| for rec in records: | |
| f.write(json.dumps(rec, ensure_ascii=False) + "\n") | |
| MODULE.write_lines(tmp_path / "source_manifest.jsonl", records) | |
| def test_build_rechecks_length_after_redaction(tmp_path, monkeypatch): | |
| text = ("To jest przykładowa wypowiedź po polsku. " * 20)[:470] + " " + "kontakt" * 8 + "@example.org" | |
| write_test_source(tmp_path, [text]) | |
| monkeypatch.setattr(MODULE, "language_vote", lambda identifier, text: ("pl", [("pl", 1.0)])) | |
| MODULE.build(tmp_path) | |
| decision = MODULE.read_lines(tmp_path / "hf_repo/artifacts/decisions.jsonl")[0] | |
| assert not decision["selected"] | |
| assert decision["reason"] == "too_little_text_after_redaction" | |
| def test_build_verify_roundtrip_and_tamper_detection(tmp_path, monkeypatch): | |
| import pytest | |
| import pyarrow as pa | |
| import pyarrow.parquet as pq | |
| text = "To jest przykładowa wypowiedź po polsku dotycząca edycji encyklopedii. " * 15 | |
| write_test_source(tmp_path, [text, text, "Krótki tekst."]) | |
| monkeypatch.setattr(MODULE, "language_vote", lambda identifier, text: ("pl", [("pl", 1.0)])) | |
| MODULE.build(tmp_path) | |
| MODULE.verify(tmp_path) | |
| report = MODULE.load(tmp_path / "validation_report.json") | |
| assert report["verified"] | |
| assert report["texts_reconstructed_from_extracted_source"] == 1 | |
| assert report["rejection_counts"] == {"normalized_duplicate": 1, "too_little_text": 1} | |
| assert report["before_after_samples"] == 1 | |
| assert not report["publication_ready"] | |
| path = tmp_path / "hf_repo/data/train-00000-of-00001.parquet" | |
| table = pq.read_table(path) | |
| rows = table.to_pylist() | |
| rows[0]["token_count"] += 1 | |
| pq.write_table(pa.Table.from_pylist(rows, schema=table.schema), path) | |
| with pytest.raises(AssertionError): | |
| MODULE.verify(tmp_path) | |
| def test_api_fetch_stub(): | |
| import json | |
| MODULE.save(Path("C:\\Users\\Hipek\\CascadeProjects\\Datasets\\tmp_fandom_sel.json"), {"wikis": [{"name": "test", "api_url": "http://test"}], "total_articles": 0, "observed_at": "2026-09-11T00:00:00Z"}) | |
| sel = MODULE.load(Path("C:\\Users\\Hipek\\CascadeProjects\\Datasets\\tmp_fandom_sel.json")) | |
| assert "wikis" in sel | |
| Path("C:\\Users\\Hipek\\CascadeProjects\\Datasets\\tmp_fandom_sel.json").unlink() | |