import csv
import re

# Myanmar range: \u1000-\u109F
# Also including common punctuation and spaces often found with Myanmar text
MYANMAR_ONLY_REGEX = re.compile(r'^[\u1000-\u109F\s\u1040-\u1049\u104A-\u104F,.?!;:\-]+$')
# Myanmar characters at the start of Column 2
MYANMAR_START_REGEX = re.compile(r'^([\u1000-\u109F\u1040-\u1049\u104A-\u104F]+)\s*(.*)$')

input_file = 'Tieng Myanmar - full.csv'
output_file = 'Tieng Myanmar - 501 to 2500.csv'

start_line = 501
end_line = 2500

with open(input_file, 'r', encoding='utf-8') as f:
    # Read all lines
    lines = f.readlines()

# Slice the lines (501 to 2500 inclusive, 1-indexed)
# line 501 is index 500
target_lines = lines[start_line-1:end_line]

output_data = []
stt = 1

for line in target_lines:
    line = line.strip()
    if not line:
        continue
    
    # Input CSV has 2 columns
    parts = line.split(',', 1)
    col1 = parts[0].strip()
    col2 = parts[1].strip() if len(parts) > 1 else ""
    
    # If Column 1 contains ONLY Myanmar characters (and punctuation/space), SKIP
    if MYANMAR_ONLY_REGEX.match(col1):
        continue
    
# Extract leading Myanmar characters from the start of Column 2 ("Tiếng Myanmar")
    # Literal transcription is mandatory.
    
    # We want to find leading Myanmar characters.
    # The Column 2 text often contains text then Myanmar then text.
    # Wait, the instruction says "Extract leading Myanmar characters FROM THE START OF Column 2".
    # Looking at the data:
    # 3: ဓာတ်မီး đát mi -> Leading Myanmar "ဓာတ်မီး"
    # 4: Khất thực. soan khàn... -> No leading Myanmar.
    
    myanmar_part = ""
    remaining_part = col2
    
    # Find all Myanmar characters in col2 and join them if they are at the start
    match = re.search(r'^[\u1000-\u109F\u1040-\u1049\u104A-\u104F\s]+', col2)
    if match:
        myanmar_part = match.group(0).strip()
        remaining_part = col2[match.end():].strip()
        
    output_data.append([stt, col1, myanmar_part, remaining_part])
    stt += 1

with open(output_file, 'w', encoding='utf-8', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(["STT", "Tiếng Việt", "Tiếng Myanmar", "Phát âm - ghi chú"])
    writer.writerows(output_data)

print(f"Processed {len(output_data)} lines.")
