#!/usr/bin/env python3
"""Quick check parsed data."""
import json

with open('/data/www/files/2026-yjs-lzy/parsed_all.json','r') as f:
    data = json.load(f)

matches = data['matches']

# Anomaly check
amounts = [(m['keyword'], m['amount'], m['file'], m['city'], m['county']) for m in matches if m['amount'] > 5000]
amounts.sort(key=lambda x: -x[1])
print(f'Amounts > 5000万元 (异常): {len(amounts)}')
for a in amounts[:20]:
    print(f'  {a[0]}: {a[1]:.2f}万元 | {a[4]} | {a[2][:50]}')

print(f'\nTotal matches: {len(matches)}')
print(f'Matches with amount > 0: {len([m for m in matches if m["amount"] > 0])}')
print(f'Matches with amount = 0: {len([m for m in matches if m["amount"] == 0])}')
print(f'Matches with amount > 5000 (anomaly): {len(amounts)}')

# City distribution with filtered amounts
cities = {}
for m in matches:
    c = m['city']
    if c not in cities:
        cities[c] = {'count': 0, 'valid_amount': 0.0, 'counties': set()}
    cities[c]['count'] += 1
    if m['amount'] <= 5000:
        cities[c]['valid_amount'] += m['amount']
    cities[c]['counties'].add(m['county'])

print('\n市州分布（过滤后）:')
for c, d in sorted(cities.items()):
    print(f'  {c}: {d["count"]}条, 有效金额{d["valid_amount"]:.2f}万元, {len(d["counties"])}个区县')

# Keyword distribution with filtered amounts
kw_dist = {}
for m in matches:
    kw = m['keyword']
    if kw not in kw_dist:
        kw_dist[kw] = {'count': 0, 'valid_amount': 0.0, 'anomaly_count': 0}
    kw_dist[kw]['count'] += 1
    if m['amount'] <= 5000:
        kw_dist[kw]['valid_amount'] += m['amount']
    else:
        kw_dist[kw]['anomaly_count'] += 1

print('\n关键字分布（过滤后）:')
for kw in ['网络培训','培训','能力提升','视频拍摄','党员教育片','课程制作','直播','视频','课件','系统','信息化','网络','数字','设备','平台','维护','软件']:
    if kw in kw_dist:
        d = kw_dist[kw]
        print(f'  {kw}: {d["count"]}次, 有效{d["valid_amount"]:.2f}万元, 异常{d["anomaly_count"]}条')

# Counties per city
print('\n各区县:')
for c in sorted(cities.keys()):
    print(f'  {c}: {sorted(cities[c]["counties"])}')
