Spaces:
Sleeping
Sleeping
File size: 2,417 Bytes
5ff92b4 379b4ed 5ff92b4 | 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 | # from difflib import unified_diff
from difflib import SequenceMatcher
SEPERATOR = "\n\n\n=====以下為教案變更內容=====\n\n\n"
def compare_text(text1, text2):
"""
Compare two text strings and return their differences in a human-readable format.
Args:
text1 (str): Original text string
text2 (str): New text string
Returns:
str: Human-readable formatted differences
"""
if not text1:
return text2
if text1 == text2:
return text2
# Split the texts into lines
text1_lines = text1.splitlines()
text2_lines = text2.splitlines()
# Create a more readable diff output
output = []
# Add the full text2 content
output.append(text2)
# Add a separator line
output.append(SEPERATOR)
matcher = SequenceMatcher(None, text1_lines, text2_lines)
for op, i1, i2, j1, j2 in matcher.get_opcodes():
# if op == 'equal':
# # For unchanged sections, just add them without markers
# for line in text2_lines[j1:j2]:
# output.append(line)
if op == 'replace':
# Show both removed and added content
output.append("\n【修改前】")
for line in text1_lines[i1:i2]:
output.append(f"- {line}")
output.append("\n【修改後】")
for line in text2_lines[j1:j2]:
output.append(f"+ {line}")
elif op == 'delete':
# Show removed content
output.append("\n【已移除】")
for line in text1_lines[i1:i2]:
output.append(f"- {line}")
elif op == 'insert':
# Show added content
output.append("\n【新增內容】")
for line in text2_lines[j1:j2]:
output.append(f"+ {line}")
return "\n".join(output)
def extract_modified_sections(compared_text):
"""
Extract the part of the compared text that appears before the SEPERATOR line.
Args:
compared_text (str): The text returned by compare_text function
Returns:
str: The content before the SEPERATOR line
"""
if not compared_text:
return ""
# Split by the separator and return the first part
parts = compared_text.split(SEPERATOR, 1)
if len(parts) > 0:
return parts[0]
return "" |