Datasets:
File size: 7,209 Bytes
f15edbb | 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 | import importlib.util
from pathlib import Path
SCRIPT = Path(__file__).with_name("build_plwiki_talk_pl.py")
SPEC = importlib.util.spec_from_file_location("build_plwiki_talk_pl", SCRIPT)
MODULE = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(MODULE)
def test_talk_namespaces_exclude_user_talk_and_mainspace():
assert MODULE.TALK_NS == {"1", "5"}
assert "0" not in MODULE.TALK_NS and "3" not in MODULE.TALK_NS
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#")
assert url.isascii()
assert not urlsplit(url).fragment
assert unquote(urlsplit(url).path) == "/wiki/Dyskusja:Łódź/C#"
def test_text_bound_and_license():
assert MODULE.MIN_TEXT_CHARS == 500
assert MODULE.LICENSE_SPDX == "CC-BY-SA-4.0"
assert MODULE.DUMP_DATE.startswith("20")
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
MODULE.save(tmp_path / "selection.json", {"dump_date": "20260901", "files": [], "observed_at": "2026-09-11T00:00:00Z"})
MODULE.save(tmp_path / "acquisition.json", {"talk_pages": len(texts), "observed_at": "2026-09-11T00:00:00Z"})
records = [{"pageid": str(index), "ns": "1", "title": f"Dyskusja:Test {index}", "timestamp": "2026-09-01",
"wikitext_chars": len(text), "text": text} for index, text in enumerate(texts, 1)]
MODULE.write_lines(tmp_path / "source_manifest.jsonl", records)
(tmp_path / "extracted").mkdir()
with gzip.open(tmp_path / "extracted/test.jsonl.gz", "wt", encoding="utf-8") as handle:
for record in records:
handle.write(json.dumps(record) + "\n")
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_shard_filename_pattern_is_strict():
import re
pat = re.compile(r"plwiki-\d+-pages-meta-current\d+\.xml-p\d+p\d+\.bz2")
assert pat.fullmatch("plwiki-20260901-pages-meta-current1.xml-p1p187037.bz2")
assert not pat.fullmatch("plwiki-20260901-pages-articles.xml.bz2")
assert not pat.fullmatch("plwiki-20260901-pages-meta-current.xml.bz2")
|