Download recipe/kit-v3/check_normalized_splits.py from gowombat/tiny-voca-da: direct link, hf CLI and curl.
- Browser
- Download file 3.55 kB
-
https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/kit-v3/check_normalized_splits.py
- Command line
-
hf download hf://gowombat/tiny-voca-da/recipe/kit-v3/check_normalized_splits.py
-
curl -L -o check_normalized_splits.py https://huggingface.co/gowombat/tiny-voca-da/resolve/main/recipe/kit-v3/check_normalized_splits.py
3.55 kB
| #!/usr/bin/env python3 | |
| """Recheck frozen source splits after a frontend normalizer changes. | |
| No recording, split or historical text_id is modified. A newly equivalent text | |
| across splits is reported explicitly and exits nonzero for resolution. | |
| """ | |
| import argparse | |
| from collections import defaultdict | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| from prepare_coral import normalized_group_text | |
| from tiny_voca.config import Config | |
| from tiny_voca.text import normalize_text | |
| def check(source, config): | |
| normalized_cache = {} | |
| groups = defaultdict(list) | |
| changes, ids, waves = [], {}, {} | |
| counts = {} | |
| for split in ('train', 'validation', 'test'): | |
| rows = [json.loads(line) for line in (source / (split + '.jsonl')).read_text().split('\n') if line.strip()] | |
| counts[split] = len(rows) | |
| for row in rows: | |
| if row['split'] != split: | |
| raise ValueError('Source split field differs from its manifest') | |
| if row['id'] in ids: | |
| raise ValueError('Recording id occurs more than once') | |
| ids[row['id']] = split | |
| wave = row['waveform_sha256'] | |
| if wave in waves and waves[wave] != split: | |
| raise ValueError('Source waveform leaks across splits') | |
| waves[wave] = split | |
| text = row['text'] | |
| if text not in normalized_cache: | |
| normalized_cache[text] = normalized_group_text(normalize_text(text, config.profile)) | |
| group_text = normalized_cache[text] | |
| group = hashlib.sha256(group_text.encode()).hexdigest() | |
| identity = {'id': row['id'], 'source_id': row['source_id'], 'split': split, | |
| 'original_text': text, 'historical_text_id': row['text_id']} | |
| groups[group].append(identity) | |
| if group != row['text_id']: | |
| changes.append({**identity, 'current_text_id': group, 'current_group_text': group_text}) | |
| leaks = [{'current_text_id': group, 'splits': sorted({row['split'] for row in members}), 'records': members} | |
| for group, members in sorted(groups.items()) if len({row['split'] for row in members}) > 1] | |
| report = {'status': 'FAIL' if leaks else 'PASS', 'profile_digest': config.profile.digest, | |
| 'normalizer': config.profile.normalizer, 'splits': counts, 'records': len(ids), | |
| 'current_unique_text_groups': len(groups), 'changed_historical_group_ids': len(changes), | |
| 'cross_split_equivalence_groups': leaks, 'cross_split_waveform_leakage': 0, | |
| 'source_data_modified': False, 'grouping': 'current Danish normalize_text, then historical NFKC/case/punctuation/space grouping'} | |
| return report, changes | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--source', type=Path, required=True) | |
| parser.add_argument('--config', type=Path, required=True) | |
| parser.add_argument('--report', type=Path, required=True) | |
| args = parser.parse_args() | |
| config = Config.from_dict(json.loads(args.config.read_text())) | |
| report, changes = check(args.source, config) | |
| args.report.parent.mkdir(parents=True, exist_ok=True) | |
| args.report.write_text(json.dumps(report, ensure_ascii=False, indent=2) + '\n') | |
| args.report.with_suffix('.changed.jsonl').write_text(''.join(json.dumps(row, ensure_ascii=False) + '\n' for row in changes)) | |
| print(json.dumps(report, ensure_ascii=False, indent=2)) | |
| raise SystemExit(report['status'] != 'PASS') | |
| if __name__ == '__main__': | |
| main() | |