File size: 9,834 Bytes
4298e57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28c7ecb
4298e57
 
 
 
 
 
 
 
 
 
 
 
28c7ecb
4298e57
 
 
 
 
 
 
 
 
 
 
 
28c7ecb
4298e57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Load supported document formats and convert their content into RAG-ready chunks

The module extracts text and source metadata from PDF, DOCX, HTML, CSV, JSONL,
and plain-text files. Content is normalized, split into overlapping chunks,
and preserved with metadata for retrieval and citation

provides basic FAR section detection to improve regulatory source
identification and traceability
"""

from __future__ import annotations
import csv
import io
import re
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
import fitz
from bs4 import BeautifulSoup
from docx import Document
from config import DEFAULT_CHUNK_OVERLAP, DEFAULT_CHUNK_SIZE

@dataclass
class TextUnit:
    """ extracted text and its source metadata before chunking"""

    text: str
    source_name: str
    source_type: str
    page: int | None = None
    section: str | None = None
    url: str | None = None
    metadata: dict[str, Any] = field(default_factory=dict)


@dataclass
class Chunk:
    """searchable text chunk with source and citation metadata"""

    chunk_id: str
    text: str
    source_name: str
    source_type: str
    page: int | None = None
    section: str | None = None
    url: str | None = None
    metadata: dict[str, Any] = field(default_factory=dict)

    @property
    def citation(self) -> str:
        """readable citation from available source metadata"""
        bits = [self.source_name]

        if self.section:
            bits.append(self.section)

        if self.page is not None:
            bits.append(f"p. {self.page}")

        if self.chunk_id:
            bits.append(f"chunk {self.chunk_id}")

        return " · ".join(bits)


def _clean(text: str) -> str:
    """Normalize whitespace and remove null characters from extracted text"""
    text = text.replace("\x00", " ")
    text = re.sub(r"[ \t]+", " ", text)
    text = re.sub(r"\n{3,}", "\n\n", text)
    return text.strip()


def _split_text(text: str, chunk_size: int, overlap: int) -> list[str]:
    """Split text into overlapping chunks using natural breakpoints when possible"""
    text = _clean(text)

    if not text:
        return []

    if len(text) <= chunk_size:
        return [text]

    pieces: list[str] = []
    start = 0

    while start < len(text):
        end = min(start + chunk_size, len(text))

        # prefer paragraph or sentence boundaries instead of arbitrary cuts
        if end < len(text):
            preferred = max(
                text.rfind("\n\n", start, end),
                text.rfind(". ", start, end),
                text.rfind("; ", start, end),
            )

            if preferred > start + int(chunk_size * 0.55):
                end = preferred + 1

        part = text[start:end].strip()

        if part:
            pieces.append(part)

        if end >= len(text):
            break

        # keep overlap so nearby context is preserved between chunks
        start = max(end - overlap, start + 1)

    return pieces


def units_to_chunks(
    units: list[TextUnit],
    chunk_size: int = DEFAULT_CHUNK_SIZE,
    overlap: int = DEFAULT_CHUNK_OVERLAP,
) -> list[Chunk]:
    """Convert text units into numbered chunks while preserving source metadata"""
    chunks: list[Chunk] = []
    counter = 1

    for unit in units:
        for piece in _split_text(unit.text, chunk_size, overlap):
            chunks.append(
                Chunk(
                    chunk_id=str(counter),
                    text=piece,
                    source_name=unit.source_name,
                    source_type=unit.source_type,
                    page=unit.page,
                    section=unit.section,
                    url=unit.url,
                    metadata=dict(unit.metadata),
                )
            )
            counter += 1

    return chunks


def _load_pdf(path: Path) -> list[TextUnit]:
    """Extract text from each PDF page while preserving page numbers"""
    units: list[TextUnit] = []
    doc = fitz.open(path)

    for i, page in enumerate(doc):
        text = _clean(page.get_text("text"))

        if text:
            units.append(
                TextUnit(
                    text=text,
                    source_name=path.name,
                    source_type="PDF",
                    page=i + 1,
                )
            )

    doc.close()
    return units


def _load_docx(path: Path) -> list[TextUnit]:
    """Extract and combine non-empty paragraphs from a DOCX document"""
    doc = Document(path)
    paragraphs = [_clean(p.text) for p in doc.paragraphs if _clean(p.text)]

    return [
        TextUnit(
            text="\n\n".join(paragraphs),
            source_name=path.name,
            source_type="DOCX",
        )
    ]


def _load_html(path: Path) -> list[TextUnit]:
    """Extract readable text from HTML while removing non-content elements"""
    html = path.read_text(encoding="utf-8", errors="ignore")
    soup = BeautifulSoup(html, "html.parser")

    # removes elements that do not contribute meaningful document content
    for tag in soup(["script", "style", "noscript"]):
        tag.decompose()

    text = _clean(soup.get_text("\n"))

    return [
        TextUnit(
            text=text,
            source_name=path.name,
            source_type="HTML",
        )
    ]


def _load_csv(path: Path) -> list[TextUnit]:
    """Convert each populated CSV row into a metadata-aware text unit"""
    units: list[TextUnit] = []

    with path.open(
        "r",
        encoding="utf-8-sig",
        errors="ignore",
        newline="",
    ) as f:
        reader = csv.DictReader(f)

        for row_num, row in enumerate(reader, start=2):
            rendered = "\n".join(
                f"{k}: {v}"
                for k, v in row.items()
                if v not in (None, "")
            )

            if rendered.strip():
                units.append(
                    TextUnit(
                        text=rendered,
                        source_name=path.name,
                        source_type="CSV",
                        section=f"row {row_num}",
                    )
                )

    return units


def _load_jsonl(path: Path) -> list[TextUnit]:
    """Load JSONL records while preserving structured source metadata when available"""
    import json

    units: list[TextUnit] = []

    with path.open("r", encoding="utf-8", errors="ignore") as f:
        for row_num, line in enumerate(f, start=1):
            line = line.strip()

            if not line:
                continue

            try:
                obj = json.loads(line)

            # preserves malformed JSONL lines as plain text instead of discarding them
            except json.JSONDecodeError:
                units.append(
                    TextUnit(
                        text=line,
                        source_name=path.name,
                        source_type="JSONL",
                        section=f"line {row_num}",
                    )
                )
                continue

            # restore previously stored chunk/source metadata when present
            if isinstance(obj, dict) and obj.get("text"):
                units.append(
                    TextUnit(
                        text=str(obj["text"]),
                        source_name=str(obj.get("source_name") or path.name),
                        source_type=str(obj.get("source_type") or "JSONL"),
                        page=(
                            int(obj["page"])
                            if obj.get("page") not in (None, "")
                            else None
                        ),
                        section=obj.get("section"),
                        url=obj.get("url"),
                        metadata=obj.get("metadata") or {},
                    )
                )

            # serialize other JSON values into searchable text
            else:
                units.append(
                    TextUnit(
                        text=json.dumps(obj, ensure_ascii=False),
                        source_name=path.name,
                        source_type="JSONL",
                        section=f"line {row_num}",
                    )
                )

    return units


def load_file(path_like: str | Path) -> list[Chunk]:
    """Load a supported file type and return normalized, searchable chunks"""
    path = Path(path_like)
    suffix = path.suffix.lower()

    # route the file to the appropriate format-specific loader
    if suffix == ".pdf":
        units = _load_pdf(path)
    elif suffix == ".docx":
        units = _load_docx(path)
    elif suffix in {".html", ".htm"}:
        units = _load_html(path)
    elif suffix == ".csv":
        units = _load_csv(path)
    elif suffix == ".jsonl":
        units = _load_jsonl(path)
    elif suffix in {".txt", ".md", ".rst", ".json", ".yaml", ".yml"}:
        text = path.read_text(encoding="utf-8", errors="ignore")
        units = [
            TextUnit(
                text=text,
                source_name=path.name,
                source_type=suffix.lstrip(".").upper(),
            )
        ]
    else:
        raise ValueError(f"Unsupported file type: {suffix or 'unknown'}")

    return units_to_chunks(units)


def infer_far_section(text: str) -> str | None:
    """Detect common FAR section, subpart, or part references in text"""
    patterns = [
        r"\b(?:FAR\s+)?(\d{1,2}\.\d{3}(?:-\d+)?)\b",
        r"\b(Subpart\s+\d{1,2}\.\d+)\b",
        r"\b(Part\s+\d{1,2})\b",
    ]

    # return the first recognizable FAR reference
    for pattern in patterns:
        match = re.search(pattern, text, flags=re.IGNORECASE)

        if match:
            value = match.group(1)

            if re.match(r"^\d", value):
                return f"FAR {value}"

            return value

    return None