File size: 4,114 Bytes
7f605ec |
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
import json
import difflib
def highlight_diff(a, b):
"""生成红色高亮 HTML,红底=不同部分"""
matcher = difflib.SequenceMatcher(None, a, b)
html_a, html_b = [], []
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
if tag == 'equal':
html_a.append(a[i1:i2])
html_b.append(b[j1:j2])
elif tag == 'replace' or tag == 'delete':
html_a.append(f'<span class="del">{a[i1:i2]}</span>')
elif tag == 'insert':
html_b.append(f'<span class="ins">{b[j1:j2]}</span>')
return ''.join(html_a), ''.join(html_b)
def generate_html(data, output_path="ocr_diff_viewer.html"):
"""生成带翻页功能的HTML"""
html_items = []
for i, item in enumerate(data):
# 修改这里
text = item.get("tiny_shuffled_content", "")
ocr = item.get("tiny_shuffled_content_ocr", "").replace("\n", "").replace(" ", "")
html_text, html_ocr = highlight_diff(text, ocr)
html_items.append(f"""
<div class="page" id="page-{i}" style="display: {'block' if i==0 else 'none'};">
<h2>样本 {i+1}/{len(data)}</h2>
<div class="block">
<h3>原文(text)</h3>
<div class="box">{html_text}</div>
</div>
<div class="block">
<h3>OCR 结果(ocr)</h3>
<div class="box">{html_ocr}</div>
</div>
</div>
""")
html_content = f"""
<html>
<head>
<meta charset="utf-8">
<title>OCR Diff Viewer</title>
<style>
body {{
font-family: "Courier New", monospace;
background-color: #f6f6f6;
padding: 20px;
}}
.block {{
margin-bottom: 30px;
}}
.box {{
background: #fff;
border: 1px solid #ccc;
padding: 15px;
white-space: pre-wrap;
font-size: 16px;
line-height: 1.5;
}}
.del {{ background-color: #ffcccc; }}
.ins {{ background-color: #ccffcc; }}
.nav {{
text-align: center;
margin-top: 30px;
}}
button {{
font-size: 16px;
padding: 6px 12px;
margin: 0 8px;
}}
</style>
</head>
<body>
<h1>📘 OCR 文本差异可视化</h1>
{"".join(html_items)}
<div class="nav">
<button onclick="prevPage()">上一条</button>
<button onclick="nextPage()">下一条</button>
<p id="page-indicator"></p>
</div>
<script>
let currentPage = 0;
const total = {len(data)};
const indicator = document.getElementById("page-indicator");
function showPage(i) {{
document.querySelectorAll('.page').forEach((el, idx) => {{
el.style.display = idx === i ? 'block' : 'none';
}});
indicator.innerText = `第 ${{i+1}} / ${{total}} 条`;
}}
function nextPage() {{
if (currentPage < total - 1) {{
currentPage++;
showPage(currentPage);
}}
}}
function prevPage() {{
if (currentPage > 0) {{
currentPage--;
showPage(currentPage);
}}
}}
showPage(currentPage);
</script>
</body>
</html>
"""
with open(output_path, "w", encoding="utf-8") as f:
f.write(html_content)
print(f"✅ 可视化结果已保存到 {output_path}")
if __name__ == "__main__":
# 示例:加载 JSON 文件
input_path = "/vol/zhaoy/ds-ocr/data/CCI3-Data/sample10_len1.0-1.2k/merged_ocr.json" # 你自己的路径
with open(input_path, "r", encoding="utf-8") as f:
data = json.load(f)
generate_html(data, "ocr_diff_viewer.html")
|