# -*- coding: utf-8 -*-
r"""B192: kiem chung 2 file bai hoc thang 10 + in danh sach tieu de (de gop, khu trung)."""
import io, os, re, sqlite3

RA = r'C:\Claw 2\QC\_baihoc_1010'
NG = {'LOC': os.path.join(RA, 'NGUON_LOC_T10.md'), 'TRI': os.path.join(RA, 'NGUON_TRI_T10.md')}
for ten, f in (('LOC', 'BAIHOC_LOC_T10.md'), ('TRI', 'BAIHOC_TRI_T10.md')):
    p = os.path.join(RA, f)
    if not os.path.exists(p):
        print('  %s KHONG CO' % f); continue
    t = io.open(p, encoding='utf-8').read()
    bh = re.findall(r'### Bài học (\d+)\s*[—\-–]\s*(.+)', t)
    so4 = len(re.findall(r'- Hiện tượng:', t)), len(re.findall(r'- Nguyên nhân gốc:', t)), \
           len(re.findall(r'- Bài học:', t)), len(re.findall(r'- Việc phải làm:', t))
    print('  %-18s %6d byte | %3d dòng | bài học: %d | 4 gạch (HT/NN/BH/VPL): %s | dấu **: %d | [nguồn không nêu: %d' % (
        f, len(t.encode()), t.count('\n') + 1, len(bh), so4, t.count('**'), t.count('[nguồn không nêu nguyên nhân gốc]')))
    print('     danh sach bai:')
    for no, td in bh:
        print('       %2s. %s' % (no, td.strip()[:96]))
    m = re.search(r'## Sự cố có nguyên nhân gốc rõ, chưa có việc phải làm(.+)', t, re.S)
    if m:
        ds = [l.strip('- ').strip() for l in m.group(1).strip().split('\n') if l.strip().startswith('-')]
        print('     muc cuoi: %d dong: %s' % (len(ds), (ds[0][:80] if ds else '')))
    # kiem chung so lieu: moi con so trong bai hoc phai co trong nguon
    ng = io.open(NG[ten], encoding='utf-8').read()
    so = set(re.findall(r'\d+(?:[.,]\d+)?', t))
    thieu = [x for x in so if x not in ng and len(x) >= 3]
    print('     kiem chung so lieu: %d con so dung | khong thay trong nguon: %d %s' % (len(so), len(thieu), thieu[:8]))
