You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.0 KiB
Python
45 lines
1.0 KiB
Python
5 years ago
|
from .edit_distance import *
|
||
2 years ago
|
from rapidfuzz.distance import Levenshtein
|
||
5 years ago
|
|
||
|
|
||
|
def align(t1, t2):
|
||
|
"""Align text."""
|
||
4 years ago
|
s1 = list(grapheme_clusters(unicodedata.normalize("NFC", t1)))
|
||
|
s2 = list(grapheme_clusters(unicodedata.normalize("NFC", t2)))
|
||
5 years ago
|
return seq_align(s1, s2)
|
||
|
|
||
|
|
||
|
def seq_align(s1, s2):
|
||
|
"""Align general sequences."""
|
||
|
s1 = list(s1)
|
||
|
s2 = list(s2)
|
||
2 years ago
|
ops = Levenshtein.editops(s1, s2)
|
||
5 years ago
|
i = 0
|
||
|
j = 0
|
||
|
|
||
|
while i < len(s1) or j < len(s2):
|
||
|
o = None
|
||
|
try:
|
||
|
ot = ops[0]
|
||
|
if ot[1] == i and ot[2] == j:
|
||
2 years ago
|
del ops[0]
|
||
5 years ago
|
o = ot
|
||
|
except IndexError:
|
||
|
pass
|
||
|
|
||
|
if o:
|
||
4 years ago
|
if o[0] == "insert":
|
||
4 years ago
|
yield None, s2[j]
|
||
5 years ago
|
j += 1
|
||
4 years ago
|
elif o[0] == "delete":
|
||
4 years ago
|
yield s1[i], None
|
||
5 years ago
|
i += 1
|
||
4 years ago
|
elif o[0] == "replace":
|
||
4 years ago
|
yield s1[i], s2[j]
|
||
5 years ago
|
i += 1
|
||
|
j += 1
|
||
|
else:
|
||
4 years ago
|
yield s1[i], s2[j]
|
||
5 years ago
|
i += 1
|
||
|
j += 1
|