#!/usr/bin/env python3
import re
import sys
import difflib

def read_file(path):
    try:
        with open(path, 'r', encoding='utf-8') as f:
            return f.read()
    except Exception as e:
        print(f"Error reading {path}: {e}")
        return ""

def clean_myanmar_text(text):
    # Remove lines that contain non-Myanmar markers like "Trang", "###", "**" (but keep Myanmar punctuation and symbols)
    # Keep only characters in Myanmar Unicode block (U+1000–U+109F) and common punctuation: spaces, commas, periods, digits, etc.
    # Also keep Pali/sanskrit diacritics? Might be in Myanmar block.
    # We'll filter line by line, removing lines that are purely non-Myanmar.
    lines = text.splitlines()
    cleaned_lines = []
    for line in lines:
        # Check if line contains any Myanmar character
        if re.search(r'[\u1000-\u109F]', line):
            # Remove any non-Myanmar characters? We'll keep spaces, digits, punctuation.
            # But we want to keep the Myanmar text as is, including Pali diacritics.
            # So we just keep the line as is, but we can optionally strip leading/trailing whitespace.
            cleaned_lines.append(line.strip())
        # else: skip line (non-Myanmar headers/footers)
    # Join lines with newline
    cleaned = '\n'.join(cleaned_lines)
    # Optionally remove extra whitespace (multiple spaces, newlines)
    cleaned = re.sub(r'\s+', ' ', cleaned)  # collapse whitespace
    return cleaned.strip()

def similarity_ratio(text1, text2):
    # Use difflib SequenceMatcher
    return difflib.SequenceMatcher(None, text1, text2).ratio()

def main():
    standard_path = "tam_bao_011_020_standard.txt"
    gemini_path = "tam_bao_011_020.md"
    deepseek_path = "tam_bao_011_020_deepseek.md"
    
    print("Reading files...")
    standard_raw = read_file(standard_path)
    gemini_raw = read_file(gemini_path)
    deepseek_raw = read_file(deepseek_path)
    
    print("Cleaning texts (keeping only Myanmar content)...")
    standard = clean_myanmar_text(standard_raw)
    gemini = clean_myanmar_text(gemini_raw)
    deepseek = clean_myanmar_text(deepseek_raw)
    
    print(f"Standard length (chars): {len(standard)}")
    print(f"Gemini length (chars): {len(gemini)}")
    print(f"Deepseek length (chars): {len(deepseek)}")
    
    print("\nCalculating similarity ratios...")
    ratio_gemini = similarity_ratio(standard, gemini)
    ratio_deepseek = similarity_ratio(standard, deepseek)
    
    print(f"\nResults (compared to standard):")
    print(f"Gemini (tam_bao_011_020.md): {ratio_gemini*100:.2f}%")
    print(f"Deepseek (tam_bao_011_020_deepseek.md): {ratio_deepseek*100:.2f}%")
    
    # Also compute character-level accuracy? Use difflib's ratio as similarity.
    # For Levenshtein distance, try to import if available.
    try:
        import Levenshtein
        lev_gemini = Levenshtein.distance(standard, gemini)
        lev_deepseek = Levenshtein.distance(standard, deepseek)
        max_len = max(len(standard), len(gemini), len(deepseek))
        accuracy_gemini = (1 - lev_gemini / max_len) * 100 if max_len > 0 else 0
        accuracy_deepseek = (1 - lev_deepseek / max_len) * 100 if max_len > 0 else 0
        print(f"\nLevenshtein distance (lower is better):")
        print(f"Gemini: {lev_gemini}")
        print(f"Deepseek: {lev_deepseek}")
        print(f"\nApproximate character accuracy (based on Levenshtein):")
        print(f"Gemini: {accuracy_gemini:.2f}%")
        print(f"Deepseek: {accuracy_deepseek:.2f}%")
    except ImportError:
        print("\nLevenshtein module not installed. Skipping Levenshtein distance.")
    
    # Additional: compare gemini vs deepseek (they might be identical)
    ratio_gemini_deepseek = similarity_ratio(gemini, deepseek)
    print(f"\nSimilarity between Gemini and Deepseek: {ratio_gemini_deepseek*100:.2f}%")
    if ratio_gemini_deepseek > 0.99:
        print("Note: Gemini and Deepseek files are nearly identical.")
    
    # Output sample differences (first 500 chars)
    if len(standard) > 500 and len(gemini) > 500:
        print("\n--- First 500 chars of Standard ---")
        print(standard[:500])
        print("\n--- First 500 chars of Gemini ---")
        print(gemini[:500])
        print("\n--- First 500 chars of Deepseek ---")
        print(deepseek[:500])

if __name__ == "__main__":
    main()