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