-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompare.py
More file actions
30 lines (23 loc) · 840 Bytes
/
compare.py
File metadata and controls
30 lines (23 loc) · 840 Bytes
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
#! -*- coding: utf-8 -*-
from bert4keras.snippets import longest_common_subsequence
def red_color(text):
return u'\033[1;31;40m%s\033[0m' % text
def green_color(text):
return u'\033[1;32;40m%s\033[0m' % text
def compare(source, target):
_, mapping = longest_common_subsequence(source, target)
source_idxs = set([i for i, j in mapping])
target_idxs = set([j for i, j in mapping])
colored_source, colored_target = u'', u''
for i, j in enumerate(source):
if i in source_idxs:
colored_source += green_color(j)
else:
colored_source += red_color(j)
for i, j in enumerate(target):
if i in target_idxs:
colored_target += green_color(j)
else:
colored_target += red_color(j)
print(colored_source)
print(colored_target)