-
Notifications
You must be signed in to change notification settings - Fork 21
/
utils.py
86 lines (74 loc) · 2.19 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
def transform(op1, op2):
func = {0:del_del, 1:del_ins, 2:ins_del, 3:ins_ins}
op2[1:] = func[op1[0]*2+op2[0]](op1[1:], op2[1:])
return op2
def del_del(op1, op2):
if op1[0] >= op2[0]+op2[1]:
return op2
if op1[0]+op1[1] <= op2[0]:
op2[0] -= op1[1]
return op2
if op1[0] <= op2[0]:
op2[1] = op2[0]+op2[1]-op1[0]-op1[1]
if op2[1]<0:
op2[1] = 0
op2[0] = op1[0]+op1[1]
return op2
if op2[0]+op2[1] >= op1[0]+op1[1]:
op2[1] -= op1[1]
return op2
op2[1] = op1[0]-op2[0]
return op2
def del_ins(op1, op2):
if op1[0] >= op2[0]:
return op2
if op1[0]+op1[1] <= op2[0]:
op2[0] -= op1[1]
return op2
op2[0] = op1[0]
return op2
def ins_del(op1, op2):
if op1[0] <= op2[0]:
op2[0] += len(op1[1])
return op2
if op2[0] > op1[0]+len(op1[1]):
return op2
op2[1] += len(op1[1])
return op2
def ins_ins(op1, op2):
if op1[0] >= op2[0]:
return op2
op2[0] += len(op1[1])
return op2
def text_patch(text, patchs):
for patch in patchs:
if patch[0] == 1:
text = text[0:patch[1]]+patch[2]+text[patch[1]:]
if patch[0] == 0:
text = text[0:patch[1]]+text[(patch[1]+patch[2]):]
return text
def forward(pre_patches, patch):
for pre in pre_patches:
for i in xrange(len(patch)):
patch[i] = transform(pre, patch[i])
for i in xrange(len(patch)):
for j in xrange(i+1, len(patch)):
patch[j] = transform(patch[i], patch[j])
return patch
######## unittest ################
import unittest
class TestUtils(unittest.TestCase):
def test(self):
origin = 'hello world'
p = forward([], [[1, 0, 'aa'], [0, 6, 1]])
text = text_patch(origin, p)
self.assertEqual(text, 'aahello orld')
p2 = forward([], [[1, 1, 'bb'], [0, 6, 1]])
text = text_patch(text, p2)
self.assertEqual(text, 'abbahell orld')
p2 = forward(p, [[1, 1, 'bb'], [0, 6, 1]])
text = text_patch(origin, p+p2)
self.assertEqual(text, 'aahbbello orld')
if __name__ == '__main__':
unittest.main()