#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import annotations

from pathlib import Path
from typing import List, Tuple


def _try_read_lines(path: Path, limit: int | None = None) -> List[str] | None:
    try:
        if not path.exists():
            return None
        with path.open("r", encoding="utf-8") as f:
            lines = [ln.strip() for ln in f.readlines()]
        lines = [ln for ln in lines if ln]
        return lines[:limit] if limit is not None else lines
    except Exception:
        return None


def load_parallel_corpus(limit: int | None = 3000) -> Tuple[List[str], List[str]]:
    """Load EN/FR sentence pairs or fallback toy data."""
    candidates = [
        (Path("Lab04/data/small_vocab_en.txt"), Path("Lab04/data/small_vocab_fr.txt")),
        (Path("人工智能实验4/data/small_vocab_en.txt"), Path("人工智能实验4/data/small_vocab_fr.txt")),
    ]
    for en_p, fr_p in candidates:
        en = _try_read_lines(en_p, limit)
        fr = _try_read_lines(fr_p, limit)
        if en and fr and len(en) == len(fr):
            return en, fr

    toy_en = [
        "he is a boy",
        "she is a girl",
        "i love you",
        "we are students",
        "they are friends",
        "this is a book",
        "that is a cat",
        "the sky is blue",
        "good morning",
        "how are you",
        "thank you",
        "see you later",
    ]
    toy_fr = [
        "il est un garcon",
        "elle est une fille",
        "je t aime",
        "nous sommes etudiants",
        "ils sont amis",
        "c est un livre",
        "c est un chat",
        "le ciel est bleu",
        "bonjour",
        "comment ca va",
        "merci",
        "a plus tard",
    ]
    return toy_en, toy_fr

