Browse Source

feat: chinese sentence check

master
Dnomd343 2 years ago
parent
commit
526820bf23
  1. 39
      src/character/chinese.py

39
src/character/chinese.py

@ -1,6 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- 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 def is_traditional(character: str) -> bool: # whether character is traditional chinese
character = character[0] character = character[0]
try: try:
@ -19,14 +27,29 @@ def is_simplified(character: str) -> bool: # whether character is simplified ch
return True return True
def chinese_check(character: str) -> None: def chinese_check(character: str) -> Chinese: # chinese character check
character = character[0] character = character[0]
print(character) if is_simplified(character): # simplified chinese case
print('is_simplified ->', is_simplified(character)) return Chinese.OK
print('is_traditional ->', is_traditional(character)) if is_traditional(character): # traditional chinese case
print() 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('') print(sentence_check('我們今天去吃飯了►►►太好吃了'))
chinese_check('') print(sentence_check('测试成功OK'))
chinese_check('')

Loading…
Cancel
Save