Spaces:
Sleeping
Sleeping
File size: 47,833 Bytes
d660683 | 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 | """
HousingMind PDF Processing System
=================================
Comprehensive document processing pipeline for affordable housing policy documents.
Designed for PIH notices, CFR regulations, handbooks, and PHA guidance.
Features:
- Intelligent chunking that preserves regulatory structure
- Metadata extraction (PIH numbers, CFR citations, effective dates)
- Hybrid retrieval (semantic + BM25)
- Citation tracking with page numbers
- Query reformulation for housing terminology
"""
import re
import json
import hashlib
from pathlib import Path
from datetime import datetime
from typing import List, Dict, Optional, Tuple, Any
from dataclasses import dataclass, field, asdict
from enum import Enum
import logging
# PDF Processing
import fitz # pymupdf
# Text Processing
from rank_bm25 import BM25Okapi
import nltk
from nltk.tokenize import word_tokenize, sent_tokenize
# Vector/Embedding (assumes you have these installed)
try:
from sentence_transformers import SentenceTransformer
import numpy as np
EMBEDDINGS_AVAILABLE = True
except ImportError:
EMBEDDINGS_AVAILABLE = False
print("Warning: sentence-transformers not installed. Semantic search disabled.")
# Download required NLTK data
try:
nltk.data.find('tokenizers/punkt')
except LookupError:
nltk.download('punkt', quiet=True)
nltk.download('punkt_tab', quiet=True)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# =============================================================================
# DATA STRUCTURES
# =============================================================================
class DocumentType(Enum):
PIH_NOTICE = "pih_notice"
CFR_REGULATION = "cfr_regulation"
HANDBOOK = "handbook"
GUIDEBOOK = "guidebook"
FAQ = "faq"
ADMIN_PLAN = "admin_plan"
STATE_GUIDANCE = "state_guidance"
FORM = "form"
UNKNOWN = "unknown"
class HousingProgram(Enum):
HCV = "housing_choice_voucher"
PUBLIC_HOUSING = "public_housing"
RAD = "rental_assistance_demonstration"
PBRA = "project_based_rental_assistance"
HOPWA = "hopwa"
MIXED = "mixed"
UNKNOWN = "unknown"
@dataclass
class DocumentMetadata:
"""Comprehensive metadata for housing policy documents."""
source_path: str
document_id: str
title: str = ""
document_type: DocumentType = DocumentType.UNKNOWN
program: HousingProgram = HousingProgram.UNKNOWN
# Regulatory identifiers
pih_notice_number: Optional[str] = None
cfr_citations: List[str] = field(default_factory=list)
# Temporal information
effective_date: Optional[str] = None
expiration_date: Optional[str] = None
publication_date: Optional[str] = None
supersedes: Optional[str] = None
superseded_by: Optional[str] = None
# Geographic scope
region: Optional[str] = None # e.g., "Region VIII", "Colorado"
nationwide: bool = True
# Processing metadata
total_pages: int = 0
extraction_date: str = field(default_factory=lambda: datetime.now().isoformat())
extraction_quality: float = 1.0 # 0-1 score for OCR quality
def to_dict(self) -> Dict:
d = asdict(self)
d['document_type'] = self.document_type.value
d['program'] = self.program.value
return d
@dataclass
class DocumentChunk:
"""A chunk of document content with full provenance."""
chunk_id: str
document_id: str
content: str
# Location in source
page_numbers: List[int] = field(default_factory=list)
section_header: Optional[str] = None
subsection: Optional[str] = None
# Chunk context
chunk_index: int = 0
total_chunks: int = 0
# For retrieval
embedding: Optional[List[float]] = None
bm25_tokens: List[str] = field(default_factory=list)
# Inherited metadata (denormalized for retrieval)
metadata: Optional[DocumentMetadata] = None
def to_dict(self) -> Dict:
d = asdict(self)
d['metadata'] = self.metadata.to_dict() if self.metadata else None
# Don't serialize embeddings to JSON (too large)
d.pop('embedding', None)
return d
@dataclass
class RetrievalResult:
"""Result from hybrid retrieval with full citation info."""
chunk: DocumentChunk
semantic_score: float = 0.0
bm25_score: float = 0.0
combined_score: float = 0.0
def get_citation(self) -> str:
"""Generate a proper citation string."""
meta = self.chunk.metadata
parts = []
if meta.pih_notice_number:
parts.append(f"PIH {meta.pih_notice_number}")
if meta.title:
parts.append(meta.title)
if self.chunk.page_numbers:
pages = self.chunk.page_numbers
if len(pages) == 1:
parts.append(f"p. {pages[0]}")
else:
parts.append(f"pp. {min(pages)}-{max(pages)}")
if meta.effective_date:
parts.append(f"(effective {meta.effective_date})")
return ", ".join(parts) if parts else meta.source_path
# =============================================================================
# PDF EXTRACTION
# =============================================================================
class HousingPDFExtractor:
"""
Extracts text from housing policy PDFs with structure preservation.
Uses PyMuPDF (fitz) for robust handling of government documents.
"""
# Patterns for identifying document structure
SECTION_PATTERNS = [
r'^(\d+)\.\s+([A-Z][A-Za-z\s]+)', # "1. Purpose"
r'^([A-Z])\.\s+([A-Z][A-Za-z\s]+)', # "A. Background"
r'^Section\s+(\d+)[:\.]?\s*(.+)', # "Section 5: Eligibility"
r'^Chapter\s+(\d+)[:\.]?\s*(.+)', # "Chapter 3: Income"
r'^(\d+\.\d+)\s+([A-Z][A-Za-z\s]+)', # "5.1 Income Calculation"
r'^(?:PART|Part)\s+([IVX\d]+)[:\.]?\s*(.+)', # "PART II: Procedures"
]
# PIH Notice patterns
PIH_PATTERNS = [
r'PIH\s*[-–]?\s*(\d{4})\s*[-–]\s*(\d+)', # PIH 2024-15, PIH-2024-15
r'Notice\s+PIH\s*(\d{4})\s*[-–]\s*(\d+)',
r'PIH\s*(\d{4})\s*[-–]\s*(\d+)\s*\([A-Z]+\)', # PIH 2024-15 (HA)
]
# CFR citation patterns
CFR_PATTERNS = [
r'24\s*C\.?F\.?R\.?\s*§?\s*(\d+)\.(\d+)', # 24 CFR 982.201
r'24\s*C\.?F\.?R\.?\s*[Pp]art\s*(\d+)', # 24 CFR Part 982
r'§\s*(\d+)\.(\d+)', # §982.201 (when in CFR context)
]
# Date patterns
DATE_PATTERNS = [
r'[Ee]ffective\s*[Dd]ate[:\s]+(\w+\s+\d{1,2},?\s+\d{4})',
r'[Ee]ffective[:\s]+(\d{1,2}/\d{1,2}/\d{4})',
r'[Ee]xpires?[:\s]+(\w+\s+\d{1,2},?\s+\d{4})',
]
def __init__(self, ocr_fallback: bool = False):
self.ocr_fallback = ocr_fallback
def extract_document(self, pdf_path: Path) -> Tuple[str, DocumentMetadata, List[Dict]]:
"""
Extract text and metadata from a PDF.
Returns:
Tuple of (full_text, metadata, page_data)
where page_data is a list of {page_num, text, blocks} dicts
"""
pdf_path = Path(pdf_path)
try:
doc = fitz.open(str(pdf_path))
except Exception as e:
logger.error(f"Failed to open PDF {pdf_path}: {e}")
raise
# Generate document ID
doc_id = self._generate_doc_id(pdf_path)
# Initialize metadata
metadata = DocumentMetadata(
source_path=str(pdf_path),
document_id=doc_id,
total_pages=len(doc)
)
full_text = ""
page_data = []
all_text_for_metadata = []
for page_num, page in enumerate(doc, start=1):
# Extract text with layout preservation
text = page.get_text("text")
# Get text blocks for structure analysis
blocks = page.get_text("dict")["blocks"]
# Check extraction quality (detect potential OCR issues)
if not text.strip() and self.ocr_fallback:
# Could integrate OCR here if needed
logger.warning(f"Page {page_num} has no extractable text")
metadata.extraction_quality *= 0.9
page_data.append({
'page_num': page_num,
'text': text,
'blocks': blocks
})
full_text += f"\n\n--- Page {page_num} ---\n\n{text}"
all_text_for_metadata.append(text)
doc.close()
# Extract metadata from text
combined_text = "\n".join(all_text_for_metadata[:5]) # First 5 pages for metadata
self._extract_metadata(combined_text, metadata, pdf_path)
return full_text, metadata, page_data
def _generate_doc_id(self, pdf_path: Path) -> str:
"""Generate a unique document ID based on filename and content hash."""
with open(pdf_path, 'rb') as f:
content_hash = hashlib.md5(f.read()[:10000]).hexdigest()[:8]
return f"{pdf_path.stem}_{content_hash}"
def _extract_metadata(self, text: str, metadata: DocumentMetadata, pdf_path: Path):
"""Extract document metadata from text content."""
# Extract PIH notice number
for pattern in self.PIH_PATTERNS:
match = re.search(pattern, text, re.IGNORECASE)
if match:
groups = match.groups()
metadata.pih_notice_number = f"{groups[0]}-{groups[1]}"
metadata.document_type = DocumentType.PIH_NOTICE
break
# Extract CFR citations
for pattern in self.CFR_PATTERNS:
matches = re.findall(pattern, text)
for match in matches:
if isinstance(match, tuple):
cfr = f"24 CFR {match[0]}.{match[1]}" if len(match) > 1 else f"24 CFR Part {match[0]}"
else:
cfr = f"24 CFR {match}"
if cfr not in metadata.cfr_citations:
metadata.cfr_citations.append(cfr)
# Extract effective date
for pattern in self.DATE_PATTERNS:
match = re.search(pattern, text)
if match:
if 'effective' in pattern.lower():
metadata.effective_date = match.group(1)
elif 'expire' in pattern.lower():
metadata.expiration_date = match.group(1)
# Determine program type from content
metadata.program = self._detect_program(text)
# Determine document type if not already set
if metadata.document_type == DocumentType.UNKNOWN:
metadata.document_type = self._detect_document_type(text, pdf_path)
# Extract title from first meaningful line
metadata.title = self._extract_title(text, pdf_path)
def _detect_program(self, text: str) -> HousingProgram:
"""Detect which housing program the document relates to."""
text_lower = text.lower()
programs_found = []
if any(term in text_lower for term in ['housing choice voucher', 'section 8 voucher', 'hcv program', '24 cfr 982']):
programs_found.append(HousingProgram.HCV)
if any(term in text_lower for term in ['public housing', '24 cfr 960', '24 cfr 966', 'acc units']):
programs_found.append(HousingProgram.PUBLIC_HOUSING)
if any(term in text_lower for term in ['rental assistance demonstration', 'rad conversion', 'rad program']):
programs_found.append(HousingProgram.RAD)
if any(term in text_lower for term in ['project-based rental assistance', 'pbra', 'section 8 project']):
programs_found.append(HousingProgram.PBRA)
if len(programs_found) == 0:
return HousingProgram.UNKNOWN
elif len(programs_found) == 1:
return programs_found[0]
else:
return HousingProgram.MIXED
def _detect_document_type(self, text: str, pdf_path: Path) -> DocumentType:
"""Detect the type of document."""
text_lower = text.lower()
filename_lower = pdf_path.name.lower()
if 'handbook' in filename_lower or 'handbook' in text_lower[:1000]:
return DocumentType.HANDBOOK
if 'guidebook' in filename_lower or 'guidebook' in text_lower[:1000]:
return DocumentType.GUIDEBOOK
if 'faq' in filename_lower or 'frequently asked' in text_lower[:1000]:
return DocumentType.FAQ
if 'administrative plan' in text_lower[:2000] or 'admin plan' in filename_lower:
return DocumentType.ADMIN_PLAN
if any(term in text_lower[:500] for term in ['24 cfr', 'code of federal regulations']):
return DocumentType.CFR_REGULATION
return DocumentType.UNKNOWN
def _extract_title(self, text: str, pdf_path: Path) -> str:
"""Extract document title from text or filename."""
lines = text.split('\n')
# Look for title in first 20 non-empty lines
for line in lines[:20]:
line = line.strip()
# Skip very short lines or lines that look like headers/footers
if len(line) < 10 or len(line) > 200:
continue
if re.match(r'^(page\s*\d+|u\.s\.\s*department)', line, re.IGNORECASE):
continue
if re.match(r'^\d+$', line): # Just a number
continue
# This might be the title
return line
# Fall back to filename
return pdf_path.stem.replace('_', ' ').replace('-', ' ').title()
# =============================================================================
# INTELLIGENT CHUNKING
# =============================================================================
class HousingDocumentChunker:
"""
Chunks housing policy documents while preserving regulatory structure.
Key principles:
- Never break numbered lists or regulatory subsections
- Keep FAQ Q&As together
- Preserve section headers in each chunk
- Maintain paragraph boundaries where possible
"""
def __init__(
self,
chunk_size: int = 1000, # Target chunk size in tokens (approximate)
chunk_overlap: int = 100,
min_chunk_size: int = 200,
max_chunk_size: int = 2000
):
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
self.min_chunk_size = min_chunk_size
self.max_chunk_size = max_chunk_size
# Patterns for detecting structure
self.numbered_list_pattern = re.compile(
r'^[\s]*(\d+\.|[a-z]\.|[ivx]+\.|•|[-–])\s+',
re.MULTILINE
)
self.section_header_pattern = re.compile(
r'^(?:(?:\d+\.)+\s*|[A-Z]\.\s*|Section\s+\d+[:\.]?\s*|Chapter\s+\d+[:\.]?\s*)([A-Z][A-Za-z\s]+)',
re.MULTILINE
)
self.faq_pattern = re.compile(
r'^(?:Q\d*[:.]?\s*|Question\s*\d*[:.]?\s*)(.+?)(?=\n(?:A\d*[:.]?\s*|Answer[:.]?\s*))',
re.MULTILINE | re.DOTALL
)
def chunk_document(
self,
text: str,
metadata: DocumentMetadata,
page_data: List[Dict]
) -> List[DocumentChunk]:
"""
Chunk a document while preserving structure.
Returns list of DocumentChunk objects with full metadata.
"""
# First, segment by major sections
sections = self._segment_sections(text)
chunks = []
chunk_index = 0
for section_header, section_content in sections:
# Chunk within each section
section_chunks = self._chunk_section(
section_content,
section_header,
metadata,
page_data
)
for chunk_text, page_nums in section_chunks:
chunk_id = f"{metadata.document_id}_chunk_{chunk_index:04d}"
chunk = DocumentChunk(
chunk_id=chunk_id,
document_id=metadata.document_id,
content=chunk_text,
page_numbers=page_nums,
section_header=section_header,
chunk_index=chunk_index,
metadata=metadata,
bm25_tokens=self._tokenize_for_bm25(chunk_text)
)
chunks.append(chunk)
chunk_index += 1
# Update total chunks count
for chunk in chunks:
chunk.total_chunks = len(chunks)
return chunks
def _segment_sections(self, text: str) -> List[Tuple[Optional[str], str]]:
"""Split document into major sections."""
sections = []
# Find all section headers
header_matches = list(self.section_header_pattern.finditer(text))
if not header_matches:
# No clear sections, treat as one block
return [(None, text)]
# Extract sections
for i, match in enumerate(header_matches):
header = match.group(0).strip()
start = match.end()
end = header_matches[i + 1].start() if i + 1 < len(header_matches) else len(text)
section_content = text[start:end].strip()
if section_content:
sections.append((header, section_content))
# Don't forget content before first header
if header_matches[0].start() > 100:
preamble = text[:header_matches[0].start()].strip()
if preamble:
sections.insert(0, (None, preamble))
return sections
def _chunk_section(
self,
content: str,
section_header: Optional[str],
metadata: DocumentMetadata,
page_data: List[Dict]
) -> List[Tuple[str, List[int]]]:
"""
Chunk a section while preserving structural integrity.
Returns list of (chunk_text, page_numbers) tuples.
"""
# Check for special structures
if self._is_numbered_list(content):
return self._chunk_numbered_list(content, section_header, page_data)
if metadata.document_type == DocumentType.FAQ or self._contains_faq(content):
return self._chunk_faq(content, section_header, page_data)
# Standard paragraph-aware chunking
return self._chunk_paragraphs(content, section_header, page_data)
def _is_numbered_list(self, content: str) -> bool:
"""Check if content is primarily a numbered list."""
lines = content.split('\n')
numbered_lines = sum(1 for line in lines if self.numbered_list_pattern.match(line))
return numbered_lines > len(lines) * 0.3 # More than 30% numbered
def _contains_faq(self, content: str) -> bool:
"""Check if content contains FAQ structure."""
return bool(self.faq_pattern.search(content))
def _chunk_numbered_list(
self,
content: str,
section_header: Optional[str],
page_data: List[Dict]
) -> List[Tuple[str, List[int]]]:
"""Chunk numbered list content, keeping list items together."""
chunks = []
current_chunk_parts = []
current_size = 0
# Split into list items
items = self._split_list_items(content)
for item in items:
item_size = len(item.split())
if current_size + item_size > self.chunk_size and current_chunk_parts:
# Finish current chunk
chunk_text = self._assemble_chunk(current_chunk_parts, section_header)
page_nums = self._find_page_numbers(chunk_text, page_data)
chunks.append((chunk_text, page_nums))
# Start new chunk with overlap
current_chunk_parts = current_chunk_parts[-1:] if current_chunk_parts else []
current_size = sum(len(p.split()) for p in current_chunk_parts)
current_chunk_parts.append(item)
current_size += item_size
# Don't forget the last chunk
if current_chunk_parts:
chunk_text = self._assemble_chunk(current_chunk_parts, section_header)
page_nums = self._find_page_numbers(chunk_text, page_data)
chunks.append((chunk_text, page_nums))
return chunks
def _split_list_items(self, content: str) -> List[str]:
"""Split content into individual list items."""
items = []
current_item = []
for line in content.split('\n'):
if self.numbered_list_pattern.match(line) and current_item:
items.append('\n'.join(current_item))
current_item = [line]
else:
current_item.append(line)
if current_item:
items.append('\n'.join(current_item))
return items
def _chunk_faq(
self,
content: str,
section_header: Optional[str],
page_data: List[Dict]
) -> List[Tuple[str, List[int]]]:
"""Chunk FAQ content, keeping Q&A pairs together."""
chunks = []
# Find Q&A pairs
qa_pairs = self._extract_qa_pairs(content)
current_chunk_parts = []
current_size = 0
for q, a in qa_pairs:
qa_text = f"Q: {q}\nA: {a}"
qa_size = len(qa_text.split())
if current_size + qa_size > self.chunk_size and current_chunk_parts:
chunk_text = self._assemble_chunk(current_chunk_parts, section_header)
page_nums = self._find_page_numbers(chunk_text, page_data)
chunks.append((chunk_text, page_nums))
current_chunk_parts = []
current_size = 0
current_chunk_parts.append(qa_text)
current_size += qa_size
if current_chunk_parts:
chunk_text = self._assemble_chunk(current_chunk_parts, section_header)
page_nums = self._find_page_numbers(chunk_text, page_data)
chunks.append((chunk_text, page_nums))
return chunks
def _extract_qa_pairs(self, content: str) -> List[Tuple[str, str]]:
"""Extract question-answer pairs from FAQ content."""
pairs = []
# Simple pattern matching for Q&A
pattern = re.compile(
r'(?:Q\d*[:.]?\s*|Question\s*\d*[:.]?\s*)(.+?)\n\s*(?:A\d*[:.]?\s*|Answer[:.]?\s*)(.+?)(?=(?:\n\s*Q\d*[:.]|\n\s*Question|$))',
re.DOTALL | re.IGNORECASE
)
for match in pattern.finditer(content):
q = match.group(1).strip()
a = match.group(2).strip()
pairs.append((q, a))
# If no pairs found, fall back to treating as regular content
if not pairs:
return [(content, "")]
return pairs
def _chunk_paragraphs(
self,
content: str,
section_header: Optional[str],
page_data: List[Dict]
) -> List[Tuple[str, List[int]]]:
"""Standard paragraph-based chunking with sentence boundaries."""
chunks = []
# Split into paragraphs
paragraphs = re.split(r'\n\s*\n', content)
current_chunk_parts = []
current_size = 0
for para in paragraphs:
para = para.strip()
if not para:
continue
para_size = len(para.split())
# If single paragraph exceeds max, split by sentences
if para_size > self.max_chunk_size:
if current_chunk_parts:
chunk_text = self._assemble_chunk(current_chunk_parts, section_header)
page_nums = self._find_page_numbers(chunk_text, page_data)
chunks.append((chunk_text, page_nums))
current_chunk_parts = []
current_size = 0
# Split large paragraph
sentence_chunks = self._chunk_by_sentences(para, section_header, page_data)
chunks.extend(sentence_chunks)
continue
if current_size + para_size > self.chunk_size and current_chunk_parts:
chunk_text = self._assemble_chunk(current_chunk_parts, section_header)
page_nums = self._find_page_numbers(chunk_text, page_data)
chunks.append((chunk_text, page_nums))
# Overlap: keep last paragraph if small enough
if len(current_chunk_parts[-1].split()) < self.chunk_overlap:
current_chunk_parts = [current_chunk_parts[-1]]
current_size = len(current_chunk_parts[0].split())
else:
current_chunk_parts = []
current_size = 0
current_chunk_parts.append(para)
current_size += para_size
if current_chunk_parts:
chunk_text = self._assemble_chunk(current_chunk_parts, section_header)
page_nums = self._find_page_numbers(chunk_text, page_data)
chunks.append((chunk_text, page_nums))
return chunks
def _chunk_by_sentences(
self,
text: str,
section_header: Optional[str],
page_data: List[Dict]
) -> List[Tuple[str, List[int]]]:
"""Split large text block by sentence boundaries."""
chunks = []
sentences = sent_tokenize(text)
current_sentences = []
current_size = 0
for sent in sentences:
sent_size = len(sent.split())
if current_size + sent_size > self.chunk_size and current_sentences:
chunk_text = self._assemble_chunk([' '.join(current_sentences)], section_header)
page_nums = self._find_page_numbers(chunk_text, page_data)
chunks.append((chunk_text, page_nums))
# Overlap
overlap_sents = []
overlap_size = 0
for s in reversed(current_sentences):
if overlap_size + len(s.split()) <= self.chunk_overlap:
overlap_sents.insert(0, s)
overlap_size += len(s.split())
else:
break
current_sentences = overlap_sents
current_size = overlap_size
current_sentences.append(sent)
current_size += sent_size
if current_sentences:
chunk_text = self._assemble_chunk([' '.join(current_sentences)], section_header)
page_nums = self._find_page_numbers(chunk_text, page_data)
chunks.append((chunk_text, page_nums))
return chunks
def _assemble_chunk(self, parts: List[str], section_header: Optional[str]) -> str:
"""Assemble chunk parts with section header context."""
content = '\n\n'.join(parts)
if section_header:
return f"[Section: {section_header}]\n\n{content}"
return content
def _find_page_numbers(self, chunk_text: str, page_data: List[Dict]) -> List[int]:
"""Find which pages contain this chunk's content."""
pages = []
# Use first 100 chars of chunk for matching
search_text = chunk_text[:100].lower()
for page in page_data:
if search_text in page['text'].lower():
pages.append(page['page_num'])
# If no match found, try broader matching
if not pages:
words = search_text.split()[:5]
for page in page_data:
if all(w in page['text'].lower() for w in words):
pages.append(page['page_num'])
return sorted(set(pages)) if pages else [1]
def _tokenize_for_bm25(self, text: str) -> List[str]:
"""Tokenize text for BM25 indexing."""
# Lowercase and tokenize
tokens = word_tokenize(text.lower())
# Remove very short tokens and punctuation
tokens = [t for t in tokens if len(t) > 2 and t.isalnum()]
return tokens
# =============================================================================
# QUERY REFORMULATION
# =============================================================================
class HousingQueryReformulator:
"""
Reformulates queries using housing terminology synonyms and expansions.
PHA staff often use operational language that differs from regulatory language.
This bridges that gap.
"""
# Housing terminology mappings
SYNONYMS = {
# Program terms
'section 8': ['housing choice voucher', 'hcv', 'voucher program'],
'hcv': ['housing choice voucher', 'section 8', 'voucher'],
'voucher': ['housing choice voucher', 'hcv', 'section 8'],
'public housing': ['low rent housing', 'conventional public housing', 'acc'],
# Process terms
'terminate': ['end participation', 'termination', 'discontinue assistance'],
'termination': ['end of participation', 'terminate', 'discontinue'],
'denial': ['deny', 'rejection', 'ineligible'],
'port': ['portability', 'transfer', 'move'],
'portability': ['port', 'transfer', 'billing arrangement'],
# Income terms
'income': ['annual income', 'gross income', 'adjusted income'],
'rent': ['tenant rent', 'total tenant payment', 'ttp', 'family share'],
'ttp': ['total tenant payment', 'tenant rent', 'family share'],
'utility allowance': ['ua', 'utility reimbursement'],
# Eligibility terms
'criminal': ['criminal activity', 'criminal background', 'criminal history'],
'drugs': ['drug-related', 'illegal drug', 'controlled substance'],
'eviction': ['evict', 'lease termination', 'removal'],
# HQS terms
'inspection': ['hqs inspection', 'unit inspection', 'housing quality'],
'hqs': ['housing quality standards', 'inspection standards'],
'fail': ['failed inspection', 'deficiency', 'violation'],
'abatement': ['rent abatement', 'hap abatement', 'payment suspension'],
# VAWA terms
'vawa': ['violence against women', 'domestic violence', 'dating violence'],
'domestic violence': ['vawa', 'dv', 'family violence'],
# Other common terms
'briefing': ['voucher briefing', 'initial briefing', 'orientation'],
'recertification': ['reexamination', 'annual review', 'recert'],
'interim': ['interim reexamination', 'change in circumstances'],
'repayment agreement': ['repayment', 'overpayment', 'recovery'],
}
# CFR section mappings for common topics
TOPIC_TO_CFR = {
'eligibility': ['24 CFR 982.201', '24 CFR 982.202', '24 CFR 982.204'],
'income': ['24 CFR 5.609', '24 CFR 5.611', '24 CFR 982.516'],
'termination': ['24 CFR 982.552', '24 CFR 982.553', '24 CFR 982.555'],
'hqs': ['24 CFR 982.401', '24 CFR 982.404', '24 CFR 982.405'],
'portability': ['24 CFR 982.353', '24 CFR 982.354', '24 CFR 982.355'],
'rent': ['24 CFR 982.503', '24 CFR 982.505', '24 CFR 982.507'],
'owner': ['24 CFR 982.306', '24 CFR 982.307', '24 CFR 982.308'],
'vawa': ['24 CFR 5.2001', '24 CFR 5.2005', '24 CFR 5.2009'],
'reasonable accommodation': ['24 CFR 8.33', '24 CFR 100.204'],
}
def reformulate(self, query: str) -> Dict[str, Any]:
"""
Reformulate a query for better retrieval.
Returns:
Dict with 'original', 'expanded', 'synonyms_added', 'cfr_hints'
"""
query_lower = query.lower()
# Find synonyms to add
synonyms_to_add = set()
for term, synonyms in self.SYNONYMS.items():
if term in query_lower:
synonyms_to_add.update(synonyms)
# Find relevant CFR sections
cfr_hints = []
for topic, cfrs in self.TOPIC_TO_CFR.items():
if topic in query_lower:
cfr_hints.extend(cfrs)
# Build expanded query
expanded_parts = [query]
if synonyms_to_add:
expanded_parts.extend(list(synonyms_to_add)[:5]) # Limit expansion
expanded_query = ' '.join(expanded_parts)
return {
'original': query,
'expanded': expanded_query,
'synonyms_added': list(synonyms_to_add),
'cfr_hints': cfr_hints
}
# =============================================================================
# HYBRID RETRIEVAL
# =============================================================================
class HybridRetriever:
"""
Combines semantic search with BM25 for optimal housing policy retrieval.
Housing queries often need both:
- Semantic understanding ("Can I deny someone for past eviction?")
- Exact matches ("24 CFR 982.552(c)(1)(i)")
"""
def __init__(
self,
embedding_model: str = "BAAI/bge-large-en-v1.5",
semantic_weight: float = 0.6,
bm25_weight: float = 0.4
):
self.semantic_weight = semantic_weight
self.bm25_weight = bm25_weight
# Initialize embedding model
if EMBEDDINGS_AVAILABLE:
self.embedding_model = SentenceTransformer(embedding_model)
else:
self.embedding_model = None
# Will be populated when documents are indexed
self.chunks: List[DocumentChunk] = []
self.bm25_index: Optional[BM25Okapi] = None
self.embeddings: Optional[np.ndarray] = None
# Query reformulator
self.reformulator = HousingQueryReformulator()
def index_documents(self, chunks: List[DocumentChunk]):
"""Index chunks for hybrid retrieval."""
self.chunks = chunks
# Build BM25 index
corpus = [chunk.bm25_tokens for chunk in chunks]
self.bm25_index = BM25Okapi(corpus)
# Build embedding index
if self.embedding_model:
texts = [chunk.content for chunk in chunks]
self.embeddings = self.embedding_model.encode(texts, show_progress_bar=True)
# Store embeddings in chunks
for i, chunk in enumerate(chunks):
chunk.embedding = self.embeddings[i].tolist()
def search(
self,
query: str,
top_k: int = 10,
filters: Optional[Dict] = None,
use_reformulation: bool = True
) -> List[RetrievalResult]:
"""
Perform hybrid search with optional metadata filtering.
Args:
query: Search query
top_k: Number of results to return
filters: Optional metadata filters (e.g., {'program': 'hcv', 'document_type': 'pih_notice'})
use_reformulation: Whether to expand query with synonyms
"""
if not self.chunks:
raise ValueError("No documents indexed. Call index_documents first.")
# Reformulate query
if use_reformulation:
reformulated = self.reformulator.reformulate(query)
search_query = reformulated['expanded']
else:
search_query = query
# Apply metadata filters first
filtered_indices = self._apply_filters(filters) if filters else list(range(len(self.chunks)))
if not filtered_indices:
return []
# Get BM25 scores
query_tokens = word_tokenize(search_query.lower())
query_tokens = [t for t in query_tokens if len(t) > 2 and t.isalnum()]
bm25_scores = np.array(self.bm25_index.get_scores(query_tokens))
# Get semantic scores
if self.embedding_model and self.embeddings is not None:
query_embedding = self.embedding_model.encode([search_query])[0]
semantic_scores = np.dot(self.embeddings, query_embedding) / (
np.linalg.norm(self.embeddings, axis=1) * np.linalg.norm(query_embedding) + 1e-8
)
else:
semantic_scores = np.zeros(len(self.chunks))
# Normalize scores
if bm25_scores.max() > 0:
bm25_scores = bm25_scores / bm25_scores.max()
if semantic_scores.max() > 0:
semantic_scores = semantic_scores / semantic_scores.max()
# Combine scores (only for filtered indices)
results = []
for idx in filtered_indices:
combined = (
self.semantic_weight * semantic_scores[idx] +
self.bm25_weight * bm25_scores[idx]
)
results.append(RetrievalResult(
chunk=self.chunks[idx],
semantic_score=float(semantic_scores[idx]),
bm25_score=float(bm25_scores[idx]),
combined_score=float(combined)
))
# Sort by combined score and return top_k
results.sort(key=lambda x: x.combined_score, reverse=True)
return results[:top_k]
def _apply_filters(self, filters: Dict) -> List[int]:
"""Apply metadata filters to chunks."""
valid_indices = []
for i, chunk in enumerate(self.chunks):
if not chunk.metadata:
continue
matches = True
for key, value in filters.items():
if key == 'program':
if chunk.metadata.program.value != value and chunk.metadata.program != HousingProgram.MIXED:
matches = False
break
elif key == 'document_type':
if chunk.metadata.document_type.value != value:
matches = False
break
elif key == 'region':
if chunk.metadata.region != value and chunk.metadata.nationwide:
matches = False
break
elif key == 'current_only':
if value and chunk.metadata.superseded_by:
matches = False
break
if matches:
valid_indices.append(i)
return valid_indices
# =============================================================================
# RESPONSE VALIDATION
# =============================================================================
class ResponseValidator:
"""
Validates AI responses against retrieved chunks.
Ensures citations are accurate and flags uncertainty.
"""
def __init__(self, confidence_threshold: float = 0.7):
self.confidence_threshold = confidence_threshold
def validate_response(
self,
response: str,
retrieved_chunks: List[DocumentChunk],
query: str
) -> Dict[str, Any]:
"""
Validate an AI-generated response against source chunks.
Returns:
Dict with validation results and suggested citations
"""
validation = {
'is_supported': True,
'confidence': 1.0,
'citations': [],
'warnings': [],
'suggested_citations': []
}
# Check if response contains claims not in chunks
response_sentences = sent_tokenize(response)
chunk_texts = ' '.join([c.content.lower() for c in retrieved_chunks])
unsupported_claims = []
for sent in response_sentences:
# Skip meta sentences
if any(phrase in sent.lower() for phrase in [
'based on', 'according to', 'the regulation states',
'i recommend', 'you should consult', 'please note'
]):
continue
# Check if key terms appear in chunks
key_terms = [w for w in word_tokenize(sent.lower()) if len(w) > 4]
if key_terms:
term_coverage = sum(1 for t in key_terms if t in chunk_texts) / len(key_terms)
if term_coverage < 0.3:
unsupported_claims.append(sent)
if unsupported_claims:
validation['warnings'].append(
f"Potentially unsupported claims detected: {len(unsupported_claims)} sentences"
)
validation['confidence'] *= 0.8
# Generate suggested citations for each chunk used
for chunk in retrieved_chunks:
citation = self._generate_citation(chunk)
validation['suggested_citations'].append(citation)
validation['is_supported'] = validation['confidence'] >= self.confidence_threshold
return validation
def _generate_citation(self, chunk: DocumentChunk) -> str:
"""Generate a formatted citation for a chunk."""
meta = chunk.metadata
parts = []
if meta.pih_notice_number:
parts.append(f"PIH Notice {meta.pih_notice_number}")
elif meta.title:
parts.append(meta.title)
if chunk.section_header:
parts.append(f"Section: {chunk.section_header}")
if chunk.page_numbers:
if len(chunk.page_numbers) == 1:
parts.append(f"p. {chunk.page_numbers[0]}")
else:
parts.append(f"pp. {min(chunk.page_numbers)}-{max(chunk.page_numbers)}")
if meta.effective_date:
parts.append(f"(effective {meta.effective_date})")
return ", ".join(parts)
# =============================================================================
# MAIN PROCESSING PIPELINE
# =============================================================================
class HousingMindProcessor:
"""
Main pipeline for processing housing policy documents.
Combines extraction, chunking, and indexing.
"""
def __init__(
self,
embedding_model: str = "BAAI/bge-large-en-v1.5",
chunk_size: int = 1000
):
self.extractor = HousingPDFExtractor()
self.chunker = HousingDocumentChunker(chunk_size=chunk_size)
self.retriever = HybridRetriever(embedding_model=embedding_model)
self.validator = ResponseValidator()
self.processed_documents: Dict[str, DocumentMetadata] = {}
def process_pdf(self, pdf_path: Path) -> List[DocumentChunk]:
"""Process a single PDF and return chunks."""
logger.info(f"Processing: {pdf_path}")
# Extract
text, metadata, page_data = self.extractor.extract_document(pdf_path)
# Chunk
chunks = self.chunker.chunk_document(text, metadata, page_data)
# Store metadata
self.processed_documents[metadata.document_id] = metadata
logger.info(f" -> {len(chunks)} chunks, type: {metadata.document_type.value}")
return chunks
def process_directory(
self,
directory: Path,
recursive: bool = True
) -> List[DocumentChunk]:
"""Process all PDFs in a directory."""
directory = Path(directory)
if recursive:
pdf_files = list(directory.rglob("*.pdf"))
else:
pdf_files = list(directory.glob("*.pdf"))
logger.info(f"Found {len(pdf_files)} PDF files")
all_chunks = []
for pdf_path in pdf_files:
try:
chunks = self.process_pdf(pdf_path)
all_chunks.extend(chunks)
except Exception as e:
logger.error(f"Failed to process {pdf_path}: {e}")
continue
return all_chunks
def build_index(self, chunks: List[DocumentChunk]):
"""Build the hybrid search index."""
logger.info(f"Building index for {len(chunks)} chunks...")
self.retriever.index_documents(chunks)
logger.info("Index built successfully")
def search(
self,
query: str,
top_k: int = 5,
filters: Optional[Dict] = None
) -> List[RetrievalResult]:
"""Search the indexed documents."""
return self.retriever.search(query, top_k=top_k, filters=filters)
def save_index(self, output_path: Path):
"""Save processed chunks and metadata to disk."""
output_path = Path(output_path)
output_path.mkdir(parents=True, exist_ok=True)
# Save chunks (without embeddings for JSON)
chunks_data = [chunk.to_dict() for chunk in self.retriever.chunks]
with open(output_path / "chunks.json", 'w') as f:
json.dump(chunks_data, f, indent=2)
# Save embeddings separately as numpy
if self.retriever.embeddings is not None:
np.save(output_path / "embeddings.npy", self.retriever.embeddings)
# Save document metadata
meta_data = {k: v.to_dict() for k, v in self.processed_documents.items()}
with open(output_path / "metadata.json", 'w') as f:
json.dump(meta_data, f, indent=2)
logger.info(f"Index saved to {output_path}")
def load_index(self, index_path: Path):
"""Load a previously saved index."""
index_path = Path(index_path)
# Load chunks
with open(index_path / "chunks.json", 'r') as f:
chunks_data = json.load(f)
# Reconstruct chunks
chunks = []
for cd in chunks_data:
meta_dict = cd.pop('metadata', None)
if meta_dict:
meta_dict['document_type'] = DocumentType(meta_dict['document_type'])
meta_dict['program'] = HousingProgram(meta_dict['program'])
metadata = DocumentMetadata(**meta_dict)
else:
metadata = None
chunk = DocumentChunk(**cd)
chunk.metadata = metadata
chunks.append(chunk)
self.retriever.chunks = chunks
# Load embeddings
embeddings_path = index_path / "embeddings.npy"
if embeddings_path.exists():
self.retriever.embeddings = np.load(embeddings_path)
# Rebuild BM25 index
corpus = [chunk.bm25_tokens for chunk in chunks]
self.retriever.bm25_index = BM25Okapi(corpus)
# Load metadata
with open(index_path / "metadata.json", 'r') as f:
meta_data = json.load(f)
for doc_id, meta_dict in meta_data.items():
meta_dict['document_type'] = DocumentType(meta_dict['document_type'])
meta_dict['program'] = HousingProgram(meta_dict['program'])
self.processed_documents[doc_id] = DocumentMetadata(**meta_dict)
logger.info(f"Loaded index with {len(chunks)} chunks")
# =============================================================================
# USAGE EXAMPLE
# =============================================================================
def main():
"""Example usage of the HousingMind processor."""
# Initialize processor
processor = HousingMindProcessor(
embedding_model="BAAI/bge-large-en-v1.5",
chunk_size=800 # Slightly smaller for better precision
)
# Example: Process a directory of PDFs
# chunks = processor.process_directory(Path("./housing_docs"))
# Example: Process a single PDF
# chunks = processor.process_pdf(Path("./pih_2024-15.pdf"))
# Build the index
# processor.build_index(chunks)
# Save for later use
# processor.save_index(Path("./housingmind_index"))
# Example search
# results = processor.search(
# "Can we terminate for drug-related criminal activity?",
# top_k=5,
# filters={'program': 'housing_choice_voucher'}
# )
# for result in results:
# print(f"\n{'='*60}")
# print(f"Score: {result.combined_score:.3f} (Semantic: {result.semantic_score:.3f}, BM25: {result.bm25_score:.3f})")
# print(f"Citation: {result.get_citation()}")
# print(f"Content preview: {result.chunk.content[:300]}...")
print("HousingMind PDF Processor initialized successfully.")
print("\nExample usage:")
print(" processor = HousingMindProcessor()")
print(" chunks = processor.process_directory(Path('./housing_docs'))")
print(" processor.build_index(chunks)")
print(" results = processor.search('Can I terminate for criminal activity?')")
if __name__ == "__main__":
main()
|