From 526820bf23e0eea1d48c9286de2c549fde565661 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 12 Dec 2022 17:26:39 +0800 Subject: [PATCH] feat: chinese sentence check --- src/character/chinese.py | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/character/chinese.py b/src/character/chinese.py index b56f1ae..e7d843d 100755 --- a/src/character/chinese.py +++ b/src/character/chinese.py @@ -1,6 +1,14 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +from enum import Enum + +class Chinese: + OK = 'simplified' # simplified chinese + WARN = 'traditional' # traditional chinese + ERROR = 'unknown' # unknown character + + def is_traditional(character: str) -> bool: # whether character is traditional chinese character = character[0] try: @@ -19,14 +27,29 @@ def is_simplified(character: str) -> bool: # whether character is simplified ch return True -def chinese_check(character: str) -> None: +def chinese_check(character: str) -> Chinese: # chinese character check character = character[0] - print(character) - print('is_simplified ->', is_simplified(character)) - print('is_traditional ->', is_traditional(character)) - print() + if is_simplified(character): # simplified chinese case + return Chinese.OK + if is_traditional(character): # traditional chinese case + return Chinese.WARN + return Chinese.ERROR # unknown case + + +def sentence_check(sentence: str) -> (bool, str): # chinese sentence check + flag = False + characters = [] + for character in sentence: + if chinese_check(character) == Chinese.OK: # normal case + characters.append(character) + elif chinese_check(character) == Chinese.WARN: # warning case + flag = True + characters.append('\033[0;33m%s\033[0;39m' % character) + else: + flag = True + characters.append('\033[0;31m%s\033[0;39m' % character) # error case + return not flag, ''.join(characters) -chinese_check('我') -chinese_check('飯') -chinese_check('们') +print(sentence_check('我們今天去吃飯了►►►太好吃了')) +print(sentence_check('测试成功OK'))