Browse Source

update: output punctuation pairs check directly

master
Dnomd343 2 years ago
parent
commit
1d870edbac
  1. 49
      src/punctuation/check.py

49
src/punctuation/check.py

@ -1,6 +1,8 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from itertools import product
punctuationPairs = [
('', ''),
('', ''),
@ -9,36 +11,31 @@ punctuationPairs = [
]
def pairsCheck(sentence: str) -> (bool, str):
def pairsCheck(sentence: str) -> bool:
errorFlag = False
punctuationStack = []
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 punctuationPair in punctuationPairs:
if sentence[i] == punctuationPair[0]: # get left punctuation
punctuationStack.append(punctuationPair)
for (i, punctuationPair) in product(range(0, len(sentence)), punctuationPairs):
if sentence[i] == punctuationPair[0]: # get left punctuation
punctuationStack.append(punctuationPair)
sentence[i] = colorful(sentence[i], 33) # mark punctuation
elif sentence[i] == punctuationPair[1]: # get right punctuation
if punctuationStack.pop()[1] != sentence[i]:
errorFlag = True
sentence[i] = colorful(sentence[i], 31) # match error case
else:
sentence[i] = colorful(sentence[i], 33) # mark punctuation
break
elif sentence[i] == punctuationPair[1]: # get right punctuation
if punctuationStack.pop()[1] != sentence[i]:
errorFlag = True
sentence[i] = colorful(sentence[i], 31) # match error case
else:
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)
if len(punctuationStack) != 0 or errorFlag: # something error in sentence
for punctuation in reversed(punctuationStack): # replenish missing punctuation
sentence.append(colorful(punctuation[1], 35))
print(''.join(sentence))
return False
return True # no error match in sentence
pairsCheck('测试“这个是OK的《2333》没错‘233’嗯嗯”')

Loading…
Cancel
Save