#!/usr/bin/env python3
import re

def filter_myanmar(input_file, output_file):
    with open(input_file, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    
    filtered = []
    for line in lines:
        stripped = line.rstrip('\n')
        # Keep lines that are page markers like [TRANG X] or TRANG X (có thể không có dấu ngoặc vuông)
        if re.match(r'^\[?TRANG\s+\d+\]?$', stripped.strip()):
            filtered.append(stripped)
        # Also keep lines that are purely Myanmar script (no Vietnamese)
        # But we need to remove lines containing '→' (dấu dịch) and the Vietnamese text after it.
        # Actually the pattern is: Myanmar text, maybe newline, then → Vietnamese.
        # In the file, each Myanmar line is followed by a line with → Vietnamese.
        # We'll skip lines that contain '→' and keep only lines that don't have '→' and are not empty.
        # But careful: Some lines might be empty or just whitespace.
        # Let's process line by line, but we need to know if a line contains →.
        # If line contains →, skip it.
        # Also skip lines that are purely Vietnamese (maybe without →) but we can rely on →.
        # Additionally, keep lines that are Myanmar script (any Unicode in Myanmar range) and not containing →.
        # However, there are also lines like "TRANG 1" without brackets.
        # Let's just implement simple: skip lines containing → and skip lines that are only Vietnamese (maybe we can't detect).
        # Since the file is structured: Myanmar line, then → Vietnamese line, repeat.
        # So we can skip lines that start with → or contain →.
        if '→' in stripped:
            continue
        # Also skip lines that are empty or only whitespace? Keep them to preserve spacing? Maybe remove blank lines.
        # For now, keep blank lines? Let's remove them to clean up.
        if stripped.strip() == '':
            continue
        # Keep all other lines (Myanmar text, page markers)
        filtered.append(stripped)
    
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write('\n'.join(filtered))

if __name__ == '__main__':
    filter_myanmar('Tam Bao.txt', 'Tam Bao - Myanmar.txt')
    print("Filtered file saved as 'Tam Bao - Myanmar.txt'")