Spaces:
Sleeping
Sleeping
File size: 1,488 Bytes
22e1b62 |
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 |
from difflib import SequenceMatcher
def highlight_overlap_by_word_to_list(text1, text2):
"""
trả về:
- list of words in text1
- list of words in text2
- list of index of hight words in text 1
- list of index of hight words in text 2
"""
# Tách chuỗi thành các từ (word) dựa vào khoảng trắng
words1 = text1.split()
words2 = text2.split()
index1 = []
index2 = []
# Sử dụng SequenceMatcher để tìm các đoạn trùng lặp giữa danh sách các từ
matcher = SequenceMatcher(None, words1, words2)
highlighted_text1 = []
highlighted_text2 = []
# Theo dõi vị trí hiện tại trong words1 và words2
current_pos1 = 0
current_pos2 = 0
# Lặp qua các đoạn so khớp
for match in matcher.get_matching_blocks():
start1, start2, length = match
# Thêm các từ không trùng lặp vào (giữ nguyên)
highlighted_text1.extend(words1[current_pos1:start1])
highlighted_text2.extend(words2[current_pos2:start2])
if length > 0:
for i in range(start1, start1 + length):
index1.append(i)
for i in range(start2, start2 + length):
index2.append(i)
# Cập nhật vị trí hiện tại
current_pos1 = start1 + length
current_pos2 = start2 + length
return words1, words2, index1, index2
if __name__ == "__main__":
pass
|