Detect if text is primarily Chinese or English. Args: text: Input text to analyze. Returns: "chinese", "english", or "unknown".
(text: str)
| 5 | |
| 6 | |
| 7 | def 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 | |
| 32 | def is_mixed_language(question: str, answer: str) -> bool: |
no outgoing calls