Datasets:
Download code/evaluation-code/v0.2-0c2690ae/np/proof_storage.py from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 3.25 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/np/proof_storage.py
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/v0.2-0c2690ae/np/proof_storage.py
-
curl -L -o proof_storage.py https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/np/proof_storage.py
3.25 kB
| """Lossless, row-local sharing of repeated proof identities and source arrays.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| SCHEMA = 'proof-dag/1' | |
| REF = '$proof' | |
| def compact(row): | |
| """Return a stored row without mutating the question or sharing across rows.""" | |
| proof = row.get('proof') | |
| if not proof or 'schema' in proof or 'values' in proof: | |
| return row | |
| values, indices = [], {} | |
| def visit(value): | |
| if isinstance(value, dict): | |
| if REF in value: | |
| raise ValueError('reserved proof reference key in inline proof') | |
| value = {k: visit(v) for k, v in value.items()} | |
| elif isinstance(value, (list, tuple)): | |
| value = [visit(v) for v in value] | |
| else: | |
| if not isinstance(value, str) or len(value) < 64: | |
| return value | |
| if isinstance(value, str) or isinstance(value, list) and len(value) >= 16: | |
| key = json.dumps(value, ensure_ascii=False) | |
| if key not in indices: | |
| indices[key] = len(values) | |
| values.append(value) | |
| return {REF: indices[key]} | |
| return value | |
| stored = visit(proof) | |
| if not values: | |
| return row | |
| stored.update(schema=SCHEMA, values=values) | |
| # Small proofs keep their original representation when sharing adds overhead. | |
| if len(json.dumps(stored, ensure_ascii=False)) >= len(json.dumps(proof, ensure_ascii=False)): | |
| return row | |
| return dict(row, proof=stored) | |
| def expand(row): | |
| """Restore every original proof field; reject broken or unknown DAG schemas.""" | |
| proof = row.get('proof') or {} | |
| if 'schema' not in proof: | |
| return row | |
| if proof['schema'] != SCHEMA: | |
| raise ValueError(f"unknown proof schema: {proof['schema']}") | |
| values = proof['values'] | |
| if not isinstance(values, list): | |
| raise ValueError('proof values must be a list') | |
| decoded = [] | |
| def visit(value, limit): | |
| if isinstance(value, dict): | |
| if REF in value: | |
| index = value[REF] | |
| if set(value) != {REF} or type(index) is not int or not 0 <= index < limit: | |
| raise ValueError('invalid proof reference') | |
| return decoded[index] | |
| return {k: visit(v, limit) for k, v in value.items()} | |
| if isinstance(value, list): | |
| return [visit(v, limit) for v in value] | |
| return value | |
| for value in values: | |
| decoded.append(visit(value, len(decoded))) | |
| restored = visit({k: v for k, v in proof.items() if k not in ('schema', 'values')}, len(decoded)) | |
| return dict(row, proof=restored) | |
| def main(argv=None): | |
| """Explicit conversion to ordinary, fully expanded question JSONL.""" | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--expand', type=Path, required=True) | |
| parser.add_argument('--out', type=Path, required=True) | |
| args = parser.parse_args(argv) | |
| with args.expand.open(encoding='utf-8') as source, args.out.open('x', encoding='utf-8') as dest: | |
| for line in source: | |
| if line.strip(): | |
| dest.write(json.dumps(expand(json.loads(line)), ensure_ascii=False) + '\n') | |
| if __name__ == '__main__': | |
| main() | |