File size: 527 Bytes
bed01bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from functools import lru_cache
from typing import Literal


@lru_cache(maxsize=64)
def is_chinese(text):
    for char in text:
        if "\u4e00" <= char <= "\u9fff":
            return True
    return False


@lru_cache(maxsize=64)
def is_eng(text):
    for char in text:
        if "a" <= char.lower() <= "z":
            return True
    return False


@lru_cache(maxsize=64)
def guess_lang(text) -> Literal["zh", "en"]:
    if is_chinese(text):
        return "zh"
    if is_eng(text):
        return "en"
    return "zh"