-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomizer.py
More file actions
352 lines (300 loc) · 12.5 KB
/
atomizer.py
File metadata and controls
352 lines (300 loc) · 12.5 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from dataclasses import dataclass, field
from enum import Enum
from typing import List, Optional, Tuple
import re
class DocMode(str, Enum):
MARKDOWN = "markdown"
PLAIN = "plain"
class AtomType(str, Enum):
HEADING = "heading" # # / ## / ###
PSEUDO_HEADING = "pseudo_heading" # **Roadmap** on its own line, ALLCAPS, etc.
PARAGRAPH = "paragraph"
LIST_BLOCK = "list"
CODE_FENCE = "code_fence"
TABLE = "table"
HR = "hr" # --- / *** / ___
BLANK = "blank"
@dataclass
class Atom:
# Identity & location
idx: int
atom_type: AtomType
start_byte: int
end_byte: int
start_line: int # 0-based
end_line: int # inclusive, 0-based
# Content
text: str
# Weights (cheap now; token count can be added later)
weight_chars: int
weight_words: int
# Pseudo-tree metadata
depth: int = 0 # 0 = top; heading depth for markdown headings (1..6). For plain paragraphs usually 0.
section_path: Tuple[str, ...] = field(default_factory=tuple) # e.g., ("Roadmap", "First year")
section_path_ids: Tuple[int, ...] = field(default_factory=tuple)
section_node_id: Optional[int] = None
# Boundary info
can_cut_before: bool = False # true if "starting here" is a good cut point
boundary_strength: float = 0.0 # [0..1], heading-like boundaries high, paragraph low
# Extra features for later
keywords: Tuple[str, ...] = field(default_factory=tuple)
# -----------------------------
# Step 0: detect mode
# -----------------------------
_MD_HINTS = [
re.compile(r"^\s{0,3}#{1,6}\s+\S", re.M), # headings
re.compile(r"^\s{0,3}(```|~~~)", re.M), # fenced code
re.compile(r"^\s{0,3}([-*+]|(\d+\.))\s+\S", re.M), # lists
re.compile(r"^\s{0,3}>\s+\S", re.M), # blockquote
re.compile(r"\[[^\]]+\]\([^)]+\)"), # links
re.compile(r"^\s{0,3}(-{3,}|\*{3,}|_{3,})\s*$", re.M) # hr
]
def detect_mode(text: str) -> DocMode:
"""Heuristic: if there are multiple markdown signals, treat as markdown."""
hits = 0
for pat in _MD_HINTS:
if pat.search(text):
hits += 1
# If it shows at least 2 markdown characteristics, call it markdown.
return DocMode.MARKDOWN if hits >= 2 else DocMode.PLAIN
# -----------------------------
# Step 1: atomization
# -----------------------------
_RE_HEADING = re.compile(r"^\s{0,3}(#{1,6})\s+(.*?)\s*$")
_RE_HR = re.compile(r"^\s{0,3}(-{3,}|\*{3,}|_{3,})\s*$")
_RE_FENCE = re.compile(r"^\s{0,3}(```|~~~)\s*(\S+)?\s*$") # ```lang
_RE_LIST = re.compile(r"^\s{0,3}([-*+])\s+\S|^\s{0,3}\d+\.\s+\S")
_RE_TABLE_SEP = re.compile(r"^\s*\|?(\s*:?-+:?\s*\|)+\s*:?-+:?\s*\|?\s*$")
_RE_TABLE_ROW = re.compile(r"^\s*\|.*\|\s*$")
def _is_standalone_bold_heading(line: str) -> Optional[str]:
"""
Detect lines like:
**Roadmap**
on their own line (optionally surrounded by whitespace).
Returns extracted title if match, else None.
"""
s = line.strip()
m = re.fullmatch(r"\*\*(.+?)\*\*", s)
if m:
title = m.group(1).strip()
# Avoid matching "**bold** in a sentence" (must be whole line)
if title:
return title
return None
def _is_allcaps_heading(line: str) -> Optional[str]:
s = line.strip()
if not s:
return None
# Short-ish, mostly letters/spaces, and many caps
if len(s) <= 80 and re.fullmatch(r"[A-Z0-9][A-Z0-9 \-:,'\".()]+", s):
# Require at least one letter and majority uppercase
letters = [c for c in s if c.isalpha()]
if letters and sum(c.isupper() for c in letters) / len(letters) > 0.8:
return s
return None
def _count_words(s: str) -> int:
return len(re.findall(r"\S+", s))
def atomize(text: str, mode: Optional[DocMode] = None) -> tuple[list[Atom], dict[int, int]]:
"""
Convert a document to a linear atom stream with rich metadata.
- Preserves content (atoms reference slices).
- Avoids splitting inside code fences, list blocks, tables.
- Adds pseudo-tree info via heading stack when detected.
"""
if mode is None:
mode = detect_mode(text)
# Work line-by-line but keep byte offsets
lines = text.splitlines(keepends=True)
# Precompute byte offsets per line (start byte of each line)
line_start_byte: List[int] = []
b = 0
for ln in lines:
line_start_byte.append(b)
b += len(ln.encode("utf-8")) if False else len(ln) # assume Python str is fine for offsets in this context
atoms: List[Atom] = []
idx = 0
# Pseudo-tree heading stack: list of (depth, node_id, title)
heading_stack: List[Tuple[int, int, str]] = []
next_node_id = 1
section_registry: dict[int, int] = {} # node_id -> atom_idx
def current_section_path_titles() -> Tuple[str, ...]:
return tuple(title for _, _, title in heading_stack)
def current_section_path_ids() -> Tuple[int, ...]:
return tuple(node_id for _, node_id, _ in heading_stack)
def current_section_node_id() -> Optional[int]:
return heading_stack[-1][1] if heading_stack else None
def push_heading(depth: int, title: str) -> None:
nonlocal next_node_id
while heading_stack and heading_stack[-1][0] >= depth:
heading_stack.pop()
heading_stack.append((depth, next_node_id, title))
next_node_id += 1
def emit(atom_type: AtomType, start_line: int, end_line: int,
depth: int = 0, can_cut_before: bool = False, boundary_strength: float = 0.0) -> None:
nonlocal idx
start_byte = line_start_byte[start_line] if start_line < len(line_start_byte) else len(text)
# end_byte = start of line after end_line, or end of text
if end_line + 1 < len(line_start_byte):
end_byte = line_start_byte[end_line + 1]
else:
end_byte = len(text)
chunk = "".join(lines[start_line:end_line + 1])
atoms.append(Atom(
idx=idx,
atom_type=atom_type,
start_byte=start_byte,
end_byte=end_byte,
start_line=start_line,
end_line=end_line,
text=chunk,
weight_chars=len(chunk),
weight_words=_count_words(chunk),
depth=depth,
section_path=current_section_path_titles(),
section_path_ids=current_section_path_ids(),
section_node_id=current_section_node_id(),
can_cut_before=can_cut_before,
boundary_strength=boundary_strength,
))
idx += 1
i = 0
while i < len(lines):
raw = lines[i]
line = raw.rstrip("\n")
# Blank line
if line.strip() == "":
emit(AtomType.BLANK, i, i, can_cut_before=False, boundary_strength=0.0)
i += 1
continue
# Horizontal rule
if _RE_HR.match(line):
emit(AtomType.HR, i, i, can_cut_before=True, boundary_strength=0.9)
i += 1
continue
# Fenced code block
m_f = _RE_FENCE.match(line)
if m_f:
fence = m_f.group(1)
start = i
i += 1
while i < len(lines):
if re.match(rf"^\s{{0,3}}{re.escape(fence)}\s*$", lines[i].rstrip("\n")):
i += 1
break
i += 1
emit(AtomType.CODE_FENCE, start, i - 1, can_cut_before=True, boundary_strength=0.6)
continue
# Markdown heading (# ...)
if mode == DocMode.MARKDOWN:
m_h = _RE_HEADING.match(line)
if m_h:
depth = len(m_h.group(1))
title = m_h.group(2).strip()
push_heading(depth, title)
emit(AtomType.HEADING, i, i, depth=depth, can_cut_before=True, boundary_strength=1.0)
section_registry[current_section_node_id()] = idx - 1
i += 1
continue
# Pseudo heading: standalone **Title** or ALLCAPS line
title = _is_standalone_bold_heading(line) or _is_allcaps_heading(line)
if title:
parent_depth = heading_stack[-1][0] if heading_stack else 0
pseudo_depth = min(parent_depth + 1, 6) if parent_depth > 0 else 1
push_heading(pseudo_depth, title)
emit(AtomType.PSEUDO_HEADING, i, i, depth=pseudo_depth, can_cut_before=True, boundary_strength=0.95)
section_registry[current_section_node_id()] = idx - 1
i += 1
continue
# Table block (simple heuristic)
# Detect a table header row followed by separator line; or consecutive |...| lines
if mode == DocMode.MARKDOWN and _RE_TABLE_ROW.match(line):
# If next line looks like separator, treat as a table block
if i + 1 < len(lines) and _RE_TABLE_SEP.match(lines[i + 1].rstrip("\n")):
start = i
i += 2
while i < len(lines) and _RE_TABLE_ROW.match(lines[i].rstrip("\n")):
i += 1
emit(AtomType.TABLE, start, i - 1, can_cut_before=True, boundary_strength=0.6)
continue
# List block
if _RE_LIST.match(line):
start = i
i += 1
# Continue while lines are list-ish or indented continuation lines
while i < len(lines):
nxt = lines[i].rstrip("\n")
if nxt.strip() == "":
# stop before blank line; blank becomes its own atom
break
if _RE_LIST.match(nxt):
i += 1
continue
# continuation: indented line (common in lists)
if re.match(r"^\s{2,}\S", nxt):
i += 1
continue
break
emit(AtomType.LIST_BLOCK, start, i - 1, can_cut_before=True, boundary_strength=0.5)
continue
# Paragraph: consume until blank line or a strong boundary starter
start = i
i += 1
while i < len(lines):
nxt_raw = lines[i]
nxt = nxt_raw.rstrip("\n")
if nxt.strip() == "":
break
if _RE_HR.match(nxt):
break
if _RE_FENCE.match(nxt):
break
if mode == DocMode.MARKDOWN and _RE_HEADING.match(nxt):
break
if _is_standalone_bold_heading(nxt) or _is_allcaps_heading(nxt):
break
if _RE_LIST.match(nxt):
break
# Table start checks
if mode == DocMode.MARKDOWN and _RE_TABLE_ROW.match(nxt):
if i + 1 < len(lines) and _RE_TABLE_SEP.match(lines[i + 1].rstrip("\n")):
break
i += 1
emit(AtomType.PARAGRAPH, start, i - 1, can_cut_before=False, boundary_strength=0.1)
continue
# Post-pass: mark "can_cut_before" for paragraphs that follow a blank or are large (fallback boundaries)
# (Optional) You can keep this off initially.
# for j in range(1, len(atoms)):
# if atoms[j].atom_type == AtomType.PARAGRAPH and atoms[j-1].atom_type in {AtomType.BLANK, AtomType.HR}:
# atoms[j].can_cut_before = True
# atoms[j].boundary_strength = max(atoms[j].boundary_strength, 0.2)
# -----------------------------
# Sanity checks (debug / dev)
# -----------------------------
for node_id, atom_idx in section_registry.items():
assert 0 <= atom_idx < len(atoms), (
f"section_registry points to invalid atom_idx {atom_idx}"
)
a = atoms[atom_idx]
assert a.section_node_id == node_id, (
f"Mismatch: registry node_id={node_id}, "
f"atom.section_node_id={a.section_node_id}, atom.idx={a.idx}"
)
assert a.atom_type in {AtomType.HEADING, AtomType.PSEUDO_HEADING}, (
f"Registry points to non-heading atom type {a.atom_type}"
)
return atoms, section_registry
# -----------------------------
# Tiny debug helper
# -----------------------------
def summarize_atoms(atoms: List[Atom], max_preview: int = 60) -> str:
rows = []
for a in atoms:
preview = a.text.replace("\n", "\\n")
if len(preview) > max_preview:
preview = preview[:max_preview] + "…"
rows.append(
f"[{a.idx:03d}] {a.atom_type:14s} lines {a.start_line}-{a.end_line} "
f"w={a.weight_words:4d} depth={a.depth} cut={int(a.can_cut_before)} "
f"path={'/'.join(a.section_path) if a.section_path else '-'} :: {preview}"
)
return "\n".join(rows)