File size: 2,177 Bytes
df7317f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)