File size: 9,283 Bytes
1367bac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""clean_hplt_v3.py — filtr Polish HPLT v3 bin-10 (WDS=10) -> kanoniczna schema DynaWord.

Adaptacja src/filter_european_hplt.py maintainera do SUROWEGO formatu HPLT v3 (jsonl.zst,
pobrany z data.hplt-project.org). Te same progi jakosci; mapowanie pol v3 -> pol v1.
Output: kanoniczna 8-kolumnowa schema (id/text/source/added/created/token_count/license/author)
+ stats.json (format DynaWord + drop-reasons + register/MT/domain) + karta .md.

Pola v3: text, lang[list], prob[list], u(url), doc_scores[list], web-register{dict}, c(mime), id.
"""
from __future__ import annotations
import argparse, json, os, re, sys, time
from pathlib import Path
from urllib.parse import urlparse
import zstandard as zstd
import pyarrow as pa, pyarrow.parquet as pq
import tiktoken

ADDED = "2026-07-14"
SOURCE = "european_hplt_v3_pl"
LICENSE = "CC0-1.0"

BOILERPLATE_RE = re.compile(
    r"(cookies?|privacy policy|terms of service|all rights reserved|"
    r"polityka prywatno\u015bci|regulamin|wszelkie prawa zastrze\u017cone|"
    r"zaloguj|rejestracja|newsletter)", re.I)
ADULT_SPAM_RE = re.compile(
    r"(porn|sex|escort|casino|viagra|cialis|bukmacher|hazard|"
    r"porno|seks|erotycz|kasyno|randki)", re.I)
LEGALISH_RE = re.compile(
    r"\b(ustawa|rozporz\u0105dzenie|dz\.u\.|sejm|senat|parlament|eur-lex|"
    r"trybuna\u0142|s\u0105d|wyrok|kodeks|komisja europejska)\b", re.I)
EXCLUDED_DOMAIN_PARTS = {".bip.", "bip.", "docplayer.pl", "mapa-kodow-pocztowych.pl",
                         "slideplayer.pl", "wikipedia.org", "wikisource.org", "wikibooks.org",
                         "wikiquote.org", "wikivoyage.org", "chomikuj.pl", "scribd.com",
                         "pdfcoffee.com", "dokumen.pub", "docer.pl", "ebookpoint", "wolnelektury.pl"}
# mojibake: polskie znaki UTF-8 zdekodowane jako CJK/inne (upstream HPLT extraction bug)
MOJIBAKE_RE = re.compile(r"[\u2e00-\u9fff\uff00-\uffef\ufffd]")
# naglowki serwisow wymiany plikow / PDF-ripow (ryzyko piractwa)
PDFHOST_RE = re.compile(r"(\d+\s*Pages?\s*[\u2022\u00b7]|Uploaded at|\d+\s*Words?\s*[\u2022\u00b7])", re.I)
MAX_CHARS = 120000  # ~40k tok: powyzej to ksiazki (copyright/OCR-risk), nie strony web
ALLOWED_REGISTERS = {"ID", "OP", "HI", "IN", "NA"}

CANON = pa.schema([("id", pa.string()), ("text", pa.string()), ("source", pa.string()),
                   ("added", pa.string()), ("created", pa.string()),
                   ("token_count", pa.int64()), ("license", pa.string()), ("author", pa.string())])

ENC = tiktoken.get_encoding("cl100k_base")

def domain(url):
    if not url:
        return ""
    h = urlparse(url).netloc.lower()
    return h[4:] if h.startswith("www.") else h

def top_register(reg):
    if not isinstance(reg, dict) or not reg:
        return "", 0.0
    k, v = max(reg.items(), key=lambda kv: kv[1] or 0.0)
    return str(k), float(v or 0.0)

def keep(o, min_chars, min_words, min_lang_prob, max_mt, dom_counts, max_per_domain):
    lang = o.get("lang")
    if not (isinstance(lang, list) and lang and lang[0] == "pol_Latn"):
        return None, "language"
    prob = o.get("prob")
    lp = float(prob[0]) if isinstance(prob, list) and prob else 0.0
    if lp < min_lang_prob:
        return None, "lang_prob"
    host = domain(o.get("u"))
    if any(p in host for p in EXCLUDED_DOMAIN_PARTS):
        return None, "domain"
    if max_per_domain and dom_counts.get(host, 0) >= max_per_domain:
        return None, "domain_cap"
    reg = o.get("web-register") or {}
    mt = float(reg.get("MT", 0.0)) if isinstance(reg, dict) else 0.0
    if mt >= max_mt:
        return None, "machine_translated"
    rtop, _ = top_register(reg)
    if ALLOWED_REGISTERS and rtop not in ALLOWED_REGISTERS:
        return None, "register"
    text = (o.get("text") or "").strip()
    if len(text) < min_chars:
        return None, "short"
    if len(text.split()) < min_words:
        return None, "few_words"
    if len(text) > MAX_CHARS:
        return None, "too_long"
    if PDFHOST_RE.search(text[:200]):
        return None, "filehost"
    if len(MOJIBAKE_RE.findall(text[:3000])) >= 3:
        return None, "mojibake"
    s = text[:5000]
    if len(BOILERPLATE_RE.findall(s)) >= 3:
        return None, "boilerplate"
    if ADULT_SPAM_RE.search(s):
        return None, "adult_spam"
    if LEGALISH_RE.search(s):
        return None, "legalish"
    return text, "kept"

def open_input(path):
    """Local file or URL (stream) -> binary readable context manager."""
    if str(path).startswith("http"):
        import urllib.request
        return urllib.request.urlopen(urllib.request.Request(path, headers={"User-Agent": "slayer-dsbench/1.0"}), timeout=120)
    return open(path, "rb")

def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--in", dest="inp", default=r"C:\ProjektyPublic\datasets\hplt_v3_pl\10_1.jsonl.zst")
    ap.add_argument("--out-dir", default=r"C:\ProjektyPublic\datasets\hplt_v3_pl\data\european_hplt_v3_pl")
    ap.add_argument("--source", default="european_hplt_v3_pl", help="wartosc kolumny source")
    ap.add_argument("--out-name", default="", help="basename plikow (domyslnie = source); id prefix")
    ap.add_argument("--bin-label", default="WDS=10")
    ap.add_argument("--min-chars", type=int, default=400)
    ap.add_argument("--min-words", type=int, default=80)
    ap.add_argument("--min-lang-prob", type=float, default=0.80)
    ap.add_argument("--max-mt-prob", type=float, default=0.20)
    ap.add_argument("--max-per-domain", type=int, default=250)
    ap.add_argument("--max-records", type=int, default=0)
    a = ap.parse_args()
    src = a.source; out = a.out_name or a.source
    outd = Path(a.out_dir); outd.mkdir(parents=True, exist_ok=True)
    t0 = time.time()

    drops = {}; regs = {}; doms = {}; dom_counts = {}
    ids, texts, tokens = [], [], []
    read = kept = chars = toks = 0
    mt_sum = 0.0
    writer = pq.ParquetWriter(outd / f"{out}.parquet", CANON, compression="zstd")

    def flush():
        nonlocal ids, texts, tokens
        if not ids:
            return
        n = len(ids)
        writer.write_table(pa.table({
            "id": ids, "text": texts, "source": [src]*n, "added": [ADDED]*n,
            "created": [""]*n, "token_count": tokens, "license": [LICENSE]*n, "author": [""]*n},
            schema=CANON))
        ids, texts, tokens = [], [], []

    with open_input(a.inp) as fh:
        reader = zstd.ZstdDecompressor().stream_reader(fh)
        buf = b""
        while True:
            chunk = reader.read(1 << 20)
            if not chunk:
                break
            buf += chunk
            while b"\n" in buf:
                line, buf = buf.split(b"\n", 1)
                if not line.strip():
                    continue
                try:
                    o = json.loads(line)
                except Exception:
                    continue
                read += 1
                text, reason = keep(o, a.min_chars, a.min_words, a.min_lang_prob,
                                    a.max_mt_prob, dom_counts, a.max_per_domain)
                if text is None:
                    drops[reason] = drops.get(reason, 0) + 1
                    continue
                host = domain(o.get("u")); dom_counts[host] = dom_counts.get(host, 0) + 1
                if len(doms) < 500:
                    doms[host] = doms.get(host, 0) + 1
                reg = o.get("web-register") or {}
                rtop, _ = top_register(reg); regs[rtop] = regs.get(rtop, 0) + 1
                mt_sum += float(reg.get("MT", 0.0)) if isinstance(reg, dict) else 0.0
                tk = len(ENC.encode(text, disallowed_special=()))
                ids.append(f"{out}_{kept}"); texts.append(text); tokens.append(tk)
                kept += 1; chars += len(text); toks += tk
                if len(ids) >= 1000:
                    flush()
                if read % 20000 == 0:
                    print(f"  read={read:,} kept={kept:,} tok={toks:,} ({time.time()-t0:.0f}s)", flush=True)
                if a.max_records and kept >= a.max_records:
                    buf = b""; break
            else:
                continue
            break
    flush(); writer.close()

    stats = {"read": read, "kept": kept, "drop_short": drops.get("short", 0),
             "drop_lang": drops.get("language", 0) + drops.get("lang_prob", 0),
             "drop_dup": 0, "drop_ocr": 0, "chars": chars, "tokens": toks,
             "licenses": {LICENSE: kept}, "authors_with_value": 0, "license": LICENSE,
             "stats_recomputed_from_parquet": True,
             "drop_detail": drops, "registers": regs,
             "mt_prob_mean_kept": round(mt_sum / max(1, kept), 3),
             "domains_top_sample": dict(sorted(doms.items(), key=lambda x: -x[1])[:30]),
             "secs": round(time.time()-t0, 1),
             "source_repo": f"HPLT/HPLT3.0 pol_Latn {a.bin_label} via {a.inp}"}
    (outd / f"{out}.stats.json").write_text(json.dumps(stats, ensure_ascii=False, indent=2)+"\n", encoding="utf-8")
    print("=== STATS ===")
    print(json.dumps({k: v for k, v in stats.items() if k not in ("domains_top_sample",)}, ensure_ascii=False, indent=2))
    print(f"parquet={outd/(out+'.parquet')} ({os.path.getsize(outd/(out+'.parquet')):,}B)")

if __name__ == "__main__":
    main()