MCPcopy Create free account
hub / github.com/Open-Bee/DataStudio / detect_language

Function detect_language

datastudio/utils/language.py:7–29  ·  view source on GitHub ↗

Detect if text is primarily Chinese or English. Args: text: Input text to analyze. Returns: "chinese", "english", or "unknown".

(text: str)

Source from the content-addressed store, hash-verified

5
6
7def detect_language(text: str) -> Literal["chinese", "english", "unknown"]:
8 """
9 Detect if text is primarily Chinese or English.
10
11 Args:
12 text: Input text to analyze.
13
14 Returns:
15 "chinese", "english", or "unknown".
16 """
17 # Remove punctuation and digits, keep only letters and Chinese characters
18 clean_text = re.sub(r"[^\u4e00-\u9fff\u3400-\u4dbfa-zA-Z]", "", text)
19
20 if not clean_text:
21 return "unknown"
22
23 # Count Chinese characters
24 chinese_chars = re.findall(r"[\u4e00-\u9fff\u3400-\u4dbf]", clean_text)
25
26 if len(chinese_chars) > 0:
27 return "chinese"
28 else:
29 return "english"
30
31
32def is_mixed_language(question: str, answer: str) -> bool:

Calls

no outgoing calls