-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultimodal_utils.py
More file actions
75 lines (64 loc) · 2.33 KB
/
Copy pathmultimodal_utils.py
File metadata and controls
75 lines (64 loc) · 2.33 KB
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
# multimodal_utils.py
"""
Small human-friendly utilities for markdown/table/image normalization.
"""
import re
import unicodedata
from typing import Optional
def normalize_markdown(md: str) -> str:
if not md:
return ""
md = unicodedata.normalize("NFC", md).replace("\x00", "")
# ensure a space after markdown headings: "##Heading" -> "## Heading"
md = re.sub(r"^(#{1,6})(?!\s)", r"\1 ", md, flags=re.MULTILINE)
# collapse excessive blank lines
md = re.sub(r"\n{3,}", "\n\n", md)
md = "\n".join(line.rstrip() for line in md.splitlines())
return md.strip()
def sanitize_table_markdown(md: str) -> str:
if not md:
return ""
lines = md.splitlines()
if len(lines) < 2:
return md
# detect table-like spacing
if not any(re.search(r"\s{2,}", ln) for ln in lines[:10]):
return md
out = []
for ln in lines:
if re.search(r"\s{2,}", ln):
row = re.sub(r"[\t ]{2,}", " | ", ln).strip()
row = row.strip("| ")
out.append("| " + row + " |")
else:
out.append(ln)
# ensure header separator
if out and out[0].startswith("|") and (len(out) == 1 or not re.match(r"^\|\s*[-:]+\s*(\|\s*[-:]+\s*)+\|?$", out[1])):
cols = [c.strip() for c in out[0].strip("| ").split("|")]
sep = "| " + " | ".join("---" for _ in cols) + " |"
out.insert(1, sep)
return "\n".join(out)
def format_image_block(image_id: Optional[str], caption: Optional[str], ocr_text: Optional[str]) -> str:
parts = []
parts.append(f"" if image_id else "")
if caption:
c = caption.strip()
if c:
parts.append(f"**Caption:** {c}")
if ocr_text:
txt = re.sub(r"\n{2,}", "\n", ocr_text.strip())
parts.append(f"**OCR:** {txt[:300]}")
return "\n\n".join(parts).strip()
def safe_filename(name: str) -> str:
if not name:
return "file"
n = unicodedata.normalize("NFKD", name)
n = n.encode("ascii", "ignore").decode("ascii")
n = n.strip().replace(" ", "_")
n = re.sub(r"[^A-Za-z0-9_\-\.]", "", n).lower()
return n or "file"
def shorten_text_preview(text: str, limit: int = 200) -> str:
if not text:
return ""
s = re.sub(r"\s+", " ", text).strip()
return s if len(s) <= limit else s[:limit].rstrip() + "…"