Datasets:
Download evaluation/io.py from DMU-ITREC/SailorEval: direct link, hf CLI and curl.
- Browser
- Download file 2.52 kB
-
https://huggingface.co/datasets/DMU-ITREC/SailorEval/resolve/main/evaluation/io.py
- Command line
-
hf download hf://datasets/DMU-ITREC/SailorEval/evaluation/io.py
-
curl -L -o io.py https://huggingface.co/datasets/DMU-ITREC/SailorEval/resolve/main/evaluation/io.py
2.52 kB
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| import re | |
| import unicodedata | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parents[1] | |
| DATA = ROOT / 'datasets' | |
| DATASET_VERSION = '1.4.0-navtex-en' | |
| def normalized_text(value): | |
| return ' '.join(unicodedata.normalize('NFKC',str(value or '')).casefold().split()) | |
| def comparable_text(value): | |
| """Ignore typographic quotes/terminal prose punctuation; retain math signs.""" | |
| value=normalized_text(value) | |
| value=''.join(ch for i,ch in enumerate(value) | |
| if ch not in '\"“”‘’\x27' or (i>0 and value[i-1].isdigit())) | |
| return ' '.join(value.split()).rstrip(' .;:') | |
| def mcq_fingerprint(row): | |
| return json.dumps([comparable_text(row['question']), | |
| sorted(comparable_text(v) for v in row['options'].values()), | |
| row.get('context'),row.get('media')],sort_keys=True,ensure_ascii=False) | |
| def load_json(path): | |
| return json.loads(Path(path).read_text(encoding='utf-8-sig')) | |
| def read_jsonl(path): | |
| result = [] | |
| for number, line in enumerate(Path(path).read_text(encoding='utf-8-sig').splitlines(), 1): | |
| if line.strip(): | |
| try: | |
| row = json.loads(line) | |
| if not isinstance(row, dict): | |
| raise ValueError('expected an object') | |
| result.append(row) | |
| except (ValueError, TypeError) as exc: | |
| raise ValueError(f'{path}:{number}: {exc}') from exc | |
| return result | |
| def write_json(path, value): | |
| path = Path(path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(json.dumps(value, ensure_ascii=False, indent=2, allow_nan=False)+'\n', encoding='utf-8') | |
| def write_jsonl(path, rows): | |
| path = Path(path) | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| with path.open('w', encoding='utf-8', newline='\n') as stream: | |
| for row in rows: | |
| stream.write(json.dumps(row, ensure_ascii=False, allow_nan=False)+'\n') | |
| def sha256(path): | |
| h = hashlib.sha256() | |
| with Path(path).open('rb') as stream: | |
| for block in iter(lambda: stream.read(1024*1024), b''): | |
| h.update(block) | |
| return h.hexdigest() | |
| def asset_path(relative, data=DATA): | |
| path = (data / relative).resolve() | |
| if not path.is_relative_to(data.resolve()): | |
| raise ValueError(f'Asset escapes dataset directory: {relative}') | |
| return path | |
| def task_specs(): | |
| return load_json(ROOT / 'evaluation/configs/tasks.json')['tasks'] | |