Datasets:
Download eval/tests/test_items.py from fishaudio/nanori: direct link, hf CLI and curl.
- Browser
- Download file 2.18 kB
-
https://huggingface.co/datasets/fishaudio/nanori/resolve/main/eval/tests/test_items.py
- Command line
-
hf download hf://datasets/fishaudio/nanori/eval/tests/test_items.py
-
curl -L -o test_items.py https://huggingface.co/datasets/fishaudio/nanori/resolve/main/eval/tests/test_items.py
2.18 kB
| import json | |
| import pytest | |
| from conftest import make_item | |
| from nanori_eval.items import load_items, normalise, top_stratum | |
| def test_top_stratum(): | |
| assert top_stratum('P-H/F2') == 'P-H' | |
| assert top_stratum('hard:kanji') == 'hard' | |
| assert top_stratum('D-E/kampo') == 'D-E' | |
| def test_flat_and_nested(tmp_path): | |
| flat = make_item('X-1', 'JA-PLACE', 'P-H/F1', weight=2.0, flags={'in_pilot': True}) | |
| a = normalise(flat) | |
| nested = {'id': 'X-1', 'set': 'JA-PLACE', 'stratum': 'P-H/F1', 'text': flat['text'], | |
| 'targets': [{'span': [4, 7], 'surface': flat['surface'], 'key': {'kana': 'ぽぽろ'}, 'accepted': ['ぽぽろ'], | |
| 'known_wrong': ['ぼぼろ']}], | |
| 'sampling': {'headline_weight': 2.0}, 'flags': {'in_pilot': True}} | |
| b = normalise(nested) | |
| assert a == b | |
| p = tmp_path / 'items.jsonl' | |
| p.write_text('\n'.join(json.dumps(r, ensure_ascii=False) for r in [flat, make_item('Y-1', 'JA-DRUG', 'D-E/x')])) | |
| items = load_items(str(p)) | |
| assert set(items['JA-PLACE']) == {'X-1'} and set(items['JA-DRUG']) == {'Y-1'} and items['JA-COMPANY'] == {} | |
| def test_span_must_match_surface(): | |
| r = make_item('X-1', 'JA-PLACE', 'P-H/F1') | |
| r['surface'] = 'ちがう' | |
| with pytest.raises(ValueError): | |
| normalise(r) | |
| def test_known_wrong_config_is_joined(tmp_path): | |
| item = make_item('X-1', 'JA-PLACE', 'P-H/F1') | |
| del item['known_wrong'] | |
| d = tmp_path / 'ds' | |
| (d / 'data' / 'places').mkdir(parents=True) | |
| (d / 'known_wrong').mkdir() | |
| (d / 'data' / 'places' / 'test.jsonl').write_text(json.dumps(item, ensure_ascii=False) + '\n') | |
| (d / 'known_wrong' / 'places.jsonl').write_text(json.dumps({'id': 'X-1', 'subset': 'JA-PLACE', | |
| 'known_wrong': ['ぼぼろ']}, ensure_ascii=False) + '\n') | |
| items = load_items(str(d), require_known_wrong=True) | |
| assert items['JA-PLACE']['X-1']['targets'][0]['known_wrong'] == ['ぼぼろ'] | |
| with pytest.raises(SystemExit): # items alone: the judges need the known-wrong rows | |
| load_items(str(d / 'data'), require_known_wrong=True) | |