Download code/export_and_mask.py from croguana/hr-curated-text: direct link, hf CLI and curl.
- Browser
- Download file 4.45 kB
-
https://huggingface.co/datasets/croguana/hr-curated-text/resolve/main/code/export_and_mask.py
- Command line
-
hf download hf://datasets/croguana/hr-curated-text/code/export_and_mask.py
-
curl -L -o export_and_mask.py https://huggingface.co/datasets/croguana/hr-curated-text/resolve/main/code/export_and_mask.py
4.45 kB
| """Wave 2: Shome/hr-curated-text — all curated Croatian prose except the non-commercial Riznica (PUBLISH-PLAN.md). | |
| python build_wave2.py -> hr-curated-text/data/<config>/<split>-NNNNN.parquet, hr-curated-text/stats.json | |
| - Configs: classla-web, wikipedia, wikisource, wikibooks (curated-v1 release), fineweb2, finepdfs, parlamint | |
| (curated-v2 release); splits train / validation (= release dev) / test. | |
| - Curated-v1 training documents flagged by the extended evaluation screen (curated-v2 work/v1-rescreen.json) are dropped. | |
| - Web configs (classla-web, fineweb2, finepdfs): e-mail addresses -> <EMAIL>, phone numbers -> <PHONE>; counts kept. | |
| - Columns: id, text, source, license. | |
| """ | |
| import gzip | |
| import json | |
| import re | |
| import sys | |
| from pathlib import Path | |
| import pandas as pd | |
| HERE = Path(__file__).resolve().parent | |
| DS = HERE.parent / 'datasets' | |
| V1, V2 = DS / 'minimind-pretrain-curated-v1/release', DS / 'minimind-pretrain-curated-v2/release' | |
| OUT = HERE / 'hr-curated-text' | |
| CONFIGS = { | |
| 'classla-web': (V1, 'hr-classla-web', 'CC0-1.0', True), | |
| 'fineweb2': (V2, 'hr-fineweb2', 'ODC-By-1.0', True), | |
| 'finepdfs': (V2, 'hr-finepdfs', 'ODC-By-1.0', True), | |
| 'wikipedia': (V1, 'hr-wikipedia', 'CC-BY-SA-4.0', False), | |
| 'wikisource': (V1, 'hr-wikisource', 'CC-BY-SA-4.0', False), | |
| 'wikibooks': (V1, 'hr-wikibooks', 'CC-BY-SA-4.0', False), | |
| 'parlamint': (V2, 'hr-parlamint', 'CC-BY-4.0', False), | |
| } | |
| SPLITS = {'train': 'train', 'validation': 'dev', 'test': 'test'} | |
| EMAIL = re.compile(r'[\w.+-]+ ?@ ?\w+(?:- ?\w+)*(?: ?\. ?\w+(?:- ?\w+)*)*(?: ?\. ?)(?:hr|com|net|org|eu|info|ba|si|rs|de|at|it|edu|gov|biz|me|io|uk|ch|fr|hu)\b', re.I) | |
| PHONE = re.compile(r'(?<![\w+/-])(?<!\d )(?<!\d\. )(?<!\d\.)(?:(?:\+|00)? ?385[\s./-]*(?:\(0\))?[\s./-]*[1-9]\d?|\(?0[1-9]\d?\)?)(?:[\s./–-]{1,3}\d{2,4}){2,3}(?![\w])') | |
| YEAR = re.compile(r'(?<!\d)(?:19|20)\d\d(?!\d)') | |
| DATE = re.compile(r'\d{1,2}\. ?\d{1,2}\. ?\d{2,4}\.?') | |
| KEY = re.compile(r'(broj|brojeve|brojevima|tel|telefon|mob|mobitel|gsm|fax|faks|kontakt|nazovite|nazvati|broj telefona|br\. tel|phone|call|☎|📞)[^\n]{0,40}$', re.I) | |
| def phone_sub(text): | |
| def phone(m): | |
| t = m.group(0) | |
| core = t.strip(' ()') | |
| d = re.sub(r'\D', '', t) | |
| if DATE.fullmatch(core) or len(d) < 8 or ('.' in core and YEAR.search(core)): | |
| return t | |
| intl = re.match(r'\(?(\+|00) ?385', core) | |
| mobile = d.startswith('09') and len(d) in (9, 10) | |
| keyed = KEY.search(text[max(0, m.start() - 60):m.start()]) | |
| return (' ' if t.startswith(' ') else '') + '<PHONE>' if intl or mobile or keyed else t | |
| return PHONE.sub(phone, text) | |
| SHARD_CHARS = 180_000_000 | |
| def mask(t, c): | |
| t, a = EMAIL.subn('<EMAIL>', t) | |
| n0 = t.count('<PHONE>') | |
| t = phone_sub(t) | |
| c['emails'] += a | |
| c['phones'] += t.count('<PHONE>') - n0 | |
| return t | |
| def main(): | |
| skip = {k: set(v) for k, v in json.loads((DS / 'minimind-pretrain-curated-v2/work/v1-rescreen.json').read_text()).items()} | |
| stats = {} | |
| for cfg, (root, src, lic, web) in CONFIGS.items(): | |
| for split, rel in SPLITS.items(): | |
| path = root / rel / f'{src}.jsonl.gz' | |
| if not path.exists(): | |
| continue | |
| c = dict(documents=0, words=0, emails=0, phones=0, dropped_screen=0) | |
| shard, chars, k = [], 0, 0 | |
| d = OUT / 'data' / cfg | |
| d.mkdir(parents=True, exist_ok=True) | |
| def flush(): | |
| nonlocal shard, chars, k | |
| if shard: | |
| pd.DataFrame(shard).to_parquet(d / f'{split}-{k:05d}.parquet', index=False) | |
| shard, chars, k = [], 0, k + 1 | |
| for line in gzip.open(path, 'rt'): | |
| r = json.loads(line) | |
| if split == 'train' and root == V1 and r['id'] in skip.get(src, ()): | |
| c['dropped_screen'] += 1 | |
| continue | |
| t = mask(r['text'], c) if web else r['text'] | |
| shard.append(dict(id=r['id'], text=t, source=cfg, license=lic)) | |
| c['documents'] += 1 | |
| c['words'] += len(t.split()) | |
| chars += len(t) | |
| if chars >= SHARD_CHARS: | |
| flush() | |
| flush() | |
| c['shards'] = k | |
| stats[f'{cfg}/{split}'] = c | |
| print(cfg, split, c, flush=True) | |
| (OUT / 'stats.json').write_text(json.dumps(stats, indent=1)) | |
| if __name__ == '__main__': | |
| main() | |