rmdetect / aidetect /langid.py
reasonofmoon's picture
Upload folder using huggingface_hub
5a35b0b verified
Raw
History Blame Contribute Delete
800 Bytes
"""langid.py — lightweight language detection (ko vs en).
We only need to route to the right LM, so a fast Hangul-ratio heuristic is
enough and needs no dependency. Falls back to `langdetect` if installed and
the heuristic is ambiguous.
"""
from __future__ import annotations
import re
_HANGUL = re.compile(r"[\uac00-\ud7a3]")
_LATIN = re.compile(r"[A-Za-z]")
def detect_lang(text: str) -> str:
"""Return 'ko' or 'en'. Korean if Hangul chars dominate alphabetic chars."""
n_ko = len(_HANGUL.findall(text))
n_en = len(_LATIN.findall(text))
if n_ko == 0 and n_en == 0:
return "en"
if n_ko >= n_en:
return "ko"
if n_en > 0 and n_ko / (n_ko + n_en) > 0.15:
# mixed but meaningful Korean present -> treat as Korean
return "ko"
return "en"