This commit is contained in:
2025-12-08 12:46:30 +08:00
parent 01a35075d2
commit 7bcf9f0c87
3 changed files with 29 additions and 27 deletions

View File

@@ -1,7 +1,7 @@
[metadata] [metadata]
# replace with your username: # replace with your username:
name = guan name = guan
version = 0.1.191 version = 0.1.192
author = guanjihuan author = guanjihuan
author_email = guanjihuan@163.com author_email = guanjihuan@163.com
description = An open source python package description = An open source python package

View File

@@ -1,6 +1,6 @@
Metadata-Version: 2.4 Metadata-Version: 2.4
Name: guan Name: guan
Version: 0.1.191 Version: 0.1.192
Summary: An open source python package Summary: An open source python package
Home-page: https://py.guanjihuan.com Home-page: https://py.guanjihuan.com
Author: guanjihuan Author: guanjihuan

View File

@@ -83,24 +83,25 @@ def loop_calculation_with_three_parameters(function_name, parameter_array_1, par
# 文本对比 # 文本对比
def word_diff(a, b, print_show=1): def word_diff(a, b, print_show=1):
import difflib import difflib
import re import jieba
import guan import logging
a_words = guan.divide_text_into_words(a) jieba.setLogLevel(logging.ERROR)
b_words = guan.divide_text_into_words(b) a_words = jieba.lcut(a)
b_words = jieba.lcut(b)
sm = difflib.SequenceMatcher(None, a_words, b_words) sm = difflib.SequenceMatcher(None, a_words, b_words)
result = [] result = []
for tag, i1, i2, j1, j2 in sm.get_opcodes(): for tag, i1, i2, j1, j2 in sm.get_opcodes():
if tag == "equal": if tag == "equal":
result.extend(a_words[i1:i2]) result.extend(a_words[i1:i2])
elif tag == "delete": elif tag == "delete":
result.append("\033[91m" + " ".join(a_words[i1:i2]) + "\033[0m") result.append("\033[9;91m" + "".join(a_words[i1:i2]) + "\033[0m")
elif tag == "insert": elif tag == "insert":
result.append("\033[92m" + " ".join(b_words[j1:j2]) + "\033[0m") result.append("\033[92m" + "".join(b_words[j1:j2]) + "\033[0m")
elif tag == "replace": elif tag == "replace":
result.append("\033[91m" + " ".join(a_words[i1:i2]) + "\033[0m") result.append("\033[9;91m" + "".join(a_words[i1:i2]) + "\033[0m")
result.append("\033[92m" + " ".join(b_words[j1:j2]) + "\033[0m") result.append(" ")
diff_result = " ".join(result) result.append("\033[92m" + "".join(b_words[j1:j2]) + "\033[0m")
diff_result = re.sub(r' +', ' ', diff_result) diff_result = "".join(result)
if print_show: if print_show:
print(diff_result) print(diff_result)
return diff_result return diff_result
@@ -109,33 +110,34 @@ def word_diff(a, b, print_show=1):
def word_diff_to_html(a, b, filename='diff_result', write_file=1): def word_diff_to_html(a, b, filename='diff_result', write_file=1):
import difflib import difflib
from html import escape from html import escape
import re import jieba
import guan import logging
a_words = guan.divide_text_into_words(a) jieba.setLogLevel(logging.ERROR)
b_words = guan.divide_text_into_words(b) a_words = jieba.lcut(a)
b_words = jieba.lcut(b)
sm = difflib.SequenceMatcher(None, a_words, b_words) sm = difflib.SequenceMatcher(None, a_words, b_words)
html_parts = [] html_parts = []
for tag, i1, i2, j1, j2 in sm.get_opcodes(): for tag, i1, i2, j1, j2 in sm.get_opcodes():
if tag == "equal": if tag == "equal":
html_parts.append(" ".join(map(escape, a_words[i1:i2]))) html_parts.append("".join(map(escape, a_words[i1:i2])))
elif tag == "delete": elif tag == "delete":
html_parts.append(f"<span style='background:#e74c3c;color:white;padding:1px 2px;border-radius:2px'>" html_parts.append(f"<span style='background:#e74c3c;color:white;padding:1px 2px;border-radius:2px;text-decoration:line-through;'>"
+ " ".join(map(escape, a_words[i1:i2])) + "".join(map(escape, a_words[i1:i2]))
+ "</span>") + "</span>")
elif tag == "insert": elif tag == "insert":
html_parts.append(f"<span style='background:#2ecc71;color:white;padding:1px 2px;border-radius:2px'>" html_parts.append(f"<span style='background:#2ecc71;color:white;padding:1px 2px;border-radius:2px;'>"
+ " ".join(map(escape, b_words[j1:j2])) + "".join(map(escape, b_words[j1:j2]))
+ "</span>") + "</span>")
elif tag == "replace": elif tag == "replace":
html_parts.append(f"<span style='background:#e74c3c;color:white;padding:1px 2px;border-radius:2px'>" html_parts.append(f"<span style='background:#e74c3c;color:white;padding:1px 2px;border-radius:2px;text-decoration:line-through;'>"
+ " ".join(map(escape, a_words[i1:i2])) + "".join(map(escape, a_words[i1:i2]))
+ "</span>") + "</span>")
html_parts.append(f"<span style='background:#2ecc71;color:white;padding:1px 2px;border-radius:2px'>" html_parts.append(" ")
+ " ".join(map(escape, b_words[j1:j2])) html_parts.append(f"<span style='background:#2ecc71;color:white;padding:1px 2px;border-radius:2px;'>"
+ "".join(map(escape, b_words[j1:j2]))
+ "</span>") + "</span>")
diff_result = " ".join(html_parts) diff_result = "".join(html_parts)
diff_result = diff_result.replace("\n", "<br>") diff_result = diff_result.replace("\n", "<br>")
diff_result = re.sub(r' +', ' ', diff_result)
if write_file: if write_file:
with open(filename+'.html', 'w', encoding='UTF-8') as f: with open(filename+'.html', 'w', encoding='UTF-8') as f:
f.write(diff_result) f.write(diff_result)