Browse Source

update: enhance punctuation pairs check

master
Dnomd343 2 years ago
parent
commit
efc0861cf5
  1. 64
      src/punctuation/check.py

64
src/punctuation/check.py

@ -9,40 +9,36 @@ punctuationPairs = [
] ]
def isLeftPunctuation(char: str) -> bool: def pairsCheck(sentence: str) -> (bool, str):
for punctuationPair in punctuationPairs:
if char == punctuationPair[0]:
return True
return False
def isRightPunctuation(char: str) -> bool:
for punctuationPair in punctuationPairs:
if char == punctuationPair[1]:
return True
return False
def getLeftPunctuation(char: str) -> str:
for punctuationPair in punctuationPairs:
if char == punctuationPair[1]:
return punctuationPair[0]
return '' # no match
def pairsCheck(sentence: str) -> bool:
punctuationStack = [] punctuationStack = []
sentence = list(sentence) sentence = list(sentence)
def colorful(char: str, color: int) -> str: # string with color
return '\033[0;%dm%s\033[0;39m' % (color, char)
errorFlag = False
for i in range(0, len(sentence)): for i in range(0, len(sentence)):
char = sentence[i] for punctuationPair in punctuationPairs:
if isLeftPunctuation(char): if sentence[i] == punctuationPair[0]: # get left punctuation
punctuationStack.append(char) punctuationStack.append(punctuationPair)
elif isRightPunctuation(char): sentence[i] = colorful(sentence[i], 33) # mark punctuation
if punctuationStack.pop() != getLeftPunctuation(char): break
return False elif sentence[i] == punctuationPair[1]: # get right punctuation
return len(punctuationStack) == 0 if punctuationStack.pop()[1] != sentence[i]:
errorFlag = True
sentence[i] = colorful(sentence[i], 31) # match error case
print( else:
pairsCheck('测试“这个是OK的《2333》没错‘’”嗯嗯') sentence[i] = colorful(sentence[i], 33) # mark punctuation
) break
if len(punctuationStack) == 0 and not errorFlag:
return True, ''.join(sentence) # no error in sentence
for punctuation in reversed(punctuationStack):
sentence.append(colorful(punctuation[1], 35))
# print(punctuation)
return False, ''.join(sentence)
status, ret = pairsCheck('测试“这个是OK的《2333》没错‘233’’《嗯嗯')
print('ok' if status else 'error')
print(ret)

Loading…
Cancel
Save