import re

def refine_line(content):
    # Take Vietnamese part if multi-language
    if '→' in content:
        parts = content.split('→')
        content = parts[-1].strip()
    return content

def process_full_file():
    with open('Tam Bao.txt', 'r', encoding='utf-8') as f:
        full_content = f.read()
    
    pages = re.split(r'(TRANG \d+)', full_content)
    final_output = []
    
    for i in range(1, len(pages), 2):
        page_label = pages[i]
        page_content = pages[i+1].strip()
        
        lines = page_content.split('\n')
        processed_page = [page_label]
        
        for line in lines:
            line = line.strip()
            if not line: continue
            if line.startswith('→'):
                raw_text = line[1:].strip()
                refined = refine_line(raw_text)
                processed_page.append(f"→ {refined}")
            else:
                processed_page.append(line)
        
        final_output.append('\n'.join(processed_page))
    
    with open('Tam Bao - Zen.txt', 'w', encoding='utf-8') as f:
        f.write('\n\n'.join(final_output))

if __name__ == "__main__":
    process_full_file()
