File size: 5,625 Bytes
efb213a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import argparse
import json
import sys
from collections import Counter
from pathlib import Path


def spans(tokens, labels):
    i = 0
    n = min(len(tokens), len(labels))
    while i < n:
        lab = labels[i]
        if not isinstance(lab, str) or not lab.startswith('B-'):
            i += 1
            continue
        ent = lab[2:]
        start = i
        i += 1
        while i < n and labels[i] == f'I-{ent}':
            i += 1
        end = i
        text = ''.join(tokens[start:end])
        yield ent, start, end, text


def normalize_season_text(text: str) -> str:
    t = (text or '').strip()
    if not t:
        return t
    return ' '.join(t.split())


def main() -> None:
    parser = argparse.ArgumentParser(description='Analyze season label distribution from JSONL char labels.')
    parser.add_argument('--input', required=True)
    parser.add_argument('--top-k', type=int, default=30)
    parser.add_argument('--sample-limit', type=int, default=8)
    parser.add_argument('--output', default=None)
    args = parser.parse_args()

    path = Path(args.input)
    season_counter = Counter()
    season_examples = {}
    weird_patterns = {
        'roman': 0,
        'ni_like': 0,
        'cjk_ord': 0,
        's_prefix': 0,
        'english_ord': 0,
        'digits_only': 0,
        'other': 0,
    }

    target_rows = {
        'kakuriyo_ni': [],
        'fire_force_nino': [],
    }

    rows = 0
    season_rows = 0

    with path.open('r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            rows += 1
            obj = json.loads(line)
            tokens = obj.get('tokens') or []
            labels = obj.get('labels') or []
            filename = obj.get('filename') or ''
            lower = filename.lower()

            if 'kakuriyo no yadomeshi ni' in lower and len(target_rows['kakuriyo_ni']) < args.sample_limit:
                target_rows['kakuriyo_ni'].append({
                    'filename': filename,
                    'season_field': obj.get('season'),
                    'title_field': obj.get('title'),
                })
            if (
                '炎炎' in filename
                and any(key in filename for key in ['弐ノ章', '貳ノ章', '貳之章'])
                and len(target_rows['fire_force_nino']) < args.sample_limit
            ):
                target_rows['fire_force_nino'].append({
                    'filename': filename,
                    'season_field': obj.get('season'),
                    'title_field': obj.get('title'),
                })

            found = False
            for ent, _s, _e, text in spans(tokens, labels):
                if ent != 'SEASON':
                    continue
                found = True
                season_rows += 1
                season = normalize_season_text(text)
                season_counter[season] += 1
                if season not in season_examples:
                    season_examples[season] = filename

                upper = season.upper()
                if upper in {'I', 'II', 'III', 'IV', 'V', 'VI'}:
                    weird_patterns['roman'] += 1
                elif season.lower().startswith('ni'):
                    weird_patterns['ni_like'] += 1
                elif any(ch in season for ch in ['第', '季', '章', '部', '期', '弐', '貳', '二']):
                    weird_patterns['cjk_ord'] += 1
                elif upper.startswith('S') and len(upper) >= 2 and upper[1].isdigit():
                    weird_patterns['s_prefix'] += 1
                elif any(x in season.lower() for x in ['season', 'st', 'nd', 'rd', 'th']):
                    weird_patterns['english_ord'] += 1
                elif season.isdigit():
                    weird_patterns['digits_only'] += 1
                else:
                    weird_patterns['other'] += 1

            if not found:
                pass

    result = {
        'input': str(path),
        'rows': rows,
        'season_span_count': int(sum(season_counter.values())),
        'unique_season_values': len(season_counter),
        'top_season_values': [
            {
                'season': season,
                'count': count,
                'percent_of_season_spans': (count / max(1, sum(season_counter.values()))) * 100.0,
                'example_filename': season_examples.get(season),
            }
            for season, count in season_counter.most_common(args.top_k)
        ],
        'focus_values': {
            key: season_counter.get(key, 0)
            for key in [
                'Ni',
                'ni',
                'NI',
                'II',
                '2',
                '02',
                'S2',
                'S02',
                'Season 2',
                '2nd',
                '第2季',
                '第二季',
                '弐ノ章',
                '貳ノ章',
                '貳之章',
                '貳',
            ]
        },
        'pattern_buckets': weird_patterns,
        'target_rows': target_rows,
    }

    text = json.dumps(result, ensure_ascii=False, indent=2)
    if args.output:
        out = Path(args.output)
        out.parent.mkdir(parents=True, exist_ok=True)
        out.write_text(text + '\n', encoding='utf-8')
        print(f'WROTE {out}')
    try:
        print(text)
    except UnicodeEncodeError:
        # Some Windows consoles still default to GBK. Emit UTF-8 bytes directly.
        sys.stdout.buffer.write((text + '\n').encode('utf-8', errors='replace'))


if __name__ == '__main__':
    main()