diff --git a/managers/TokenManager.py b/managers/TokenManager.py index 6f6fb66..3afa7f2 100644 --- a/managers/TokenManager.py +++ b/managers/TokenManager.py @@ -17,6 +17,7 @@ from horology import timed from diff_match_patch import diff +from click.core import fast_exit class TokenList(list): def __getitem__(self, key): @@ -264,6 +265,8 @@ def diff(self, tokens, oldText, newText): oldString = '' newString = '' sameStringLength = 0 + isDeleteShey = False + i = 0 for op, string in changes: if op == "-" or op == "+": if not sameStringLength: @@ -295,7 +298,7 @@ def diff(self, tokens, oldText, newText): start = -1 changePos = len(oldString) - # 當是加入 ། 時,換得+1,讓後面找 endNew 時不會用到現在加的 + # 當是加入 ། 時,換的位置+1,讓後面找 endNew 時不會用到現在加的 if string == '།': changePos += 1 else: @@ -311,6 +314,10 @@ def diff(self, tokens, oldText, newText): # op == "-" 時才找得到 changePos = oldString.find(string, sameStringLength - 1) + # 當是刪除 ། 時,且所在位置前一個非 ། 無,換的位置-1,讓後面找 endOld 時不會用到現在的 + if string == '།' and changes[i-1][1][-1] != '།': + isDeleteShey = True + if start == -1: # 改的地方前面沒有'།'或'\n' start = 0 @@ -319,6 +326,8 @@ def diff(self, tokens, oldText, newText): oldString = oldString + string newString = newString + string + i += 1 + # 往後 scan ,結束位置 endOld = oldString.find('།', changePos) endNew = newString.find('།', changePos) @@ -328,6 +337,9 @@ def diff(self, tokens, oldText, newText): #當 ། 有很多個同時在一起時 if endOld == endNew: endOld += 1 + if isDeleteShey: + endOld = endNew + 1 + # 在文章最後面加字 if (tokens[-1].end == changePos) or (changePos == -1): if (tokens[-1].end == changePos):