Spaces:
Running
Running
File size: 23,773 Bytes
2e237ce |
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 |
#!/usr/bin/env python3
"""
Fixed PDF Data Extractor - Addresses key issues in comprehensive_extract.py
Key fixes:
1. Better table extraction and cleaning
2. Improved key-value pair extraction
3. More robust text processing
4. Enhanced vehicle registration extraction
5. Better date/number pattern recognition
"""
import json
import re
import pandas as pd
from typing import Dict, List, Any, Optional
import logging
from pathlib import Path
import sys
from datetime import datetime
try:
import pdfplumber
HAS_PDFPLUMBER = True
except ImportError:
HAS_PDFPLUMBER = False
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger("fixed_pdf_extractor")
class FixedPDFExtractor:
def __init__(self):
logger.info("๐ Initializing Fixed PDF Extractor")
def extract_everything(self, pdf_path: str) -> Dict[str, Any]:
if not HAS_PDFPLUMBER:
raise RuntimeError("pdfplumber is required. Install with: pip install pdfplumber")
logger.info(f"๐ Processing PDF: {pdf_path}")
result = {
"document_info": {
"filename": Path(pdf_path).name,
"total_pages": 0,
"extraction_timestamp": datetime.now().isoformat()
},
"extracted_data": {
"all_text_content": [],
"all_tables": [],
"key_value_pairs": {},
"audit_information": {},
"operator_information": {},
"vehicle_registrations": [],
"driver_records": [],
"compliance_summary": {},
"dates_and_numbers": {}
}
}
all_text_blocks, all_tables = [], []
with pdfplumber.open(pdf_path) as pdf:
result["document_info"]["total_pages"] = len(pdf.pages)
for page_num, page in enumerate(pdf.pages, 1):
logger.info(f"๐ Processing page {page_num}")
# Extract text with better handling
page_text = self._extract_page_text(page)
if page_text:
all_text_blocks.append({
"page": page_num,
"text": page_text,
"word_count": len(page_text.split())
})
# Extract tables with improved cleaning
tables = self._extract_page_tables(page, page_num)
all_tables.extend(tables)
result["extracted_data"]["all_text_content"] = all_text_blocks
result["extracted_data"]["all_tables"] = all_tables
# Process extracted data with improved methods
combined_text = "\n\n".join(b["text"] for b in all_text_blocks)
result["extracted_data"]["key_value_pairs"] = self._extract_key_value_pairs_improved(combined_text)
result["extracted_data"]["audit_information"] = self._extract_audit_info(combined_text, all_tables)
result["extracted_data"]["operator_information"] = self._extract_operator_info(combined_text, all_tables)
result["extracted_data"]["vehicle_registrations"] = self._extract_vehicle_registrations(all_tables)
result["extracted_data"]["driver_records"] = self._extract_driver_records(all_tables)
result["extracted_data"]["compliance_summary"] = self._extract_compliance_summary(combined_text, all_tables)
result["extracted_data"]["dates_and_numbers"] = self._extract_dates_and_numbers_improved(combined_text)
# Generate summary
result["extraction_summary"] = {
"text_blocks_found": len(all_text_blocks),
"tables_found": len(all_tables),
"key_value_pairs_found": len(result["extracted_data"]["key_value_pairs"]),
"vehicle_registrations_found": len(result["extracted_data"]["vehicle_registrations"]),
"driver_records_found": len(result["extracted_data"]["driver_records"]),
"total_characters": len(combined_text),
"processing_timestamp": datetime.now().isoformat()
}
logger.info("โ
Extraction completed!")
return result
def _extract_page_text(self, page) -> Optional[str]:
"""Extract text from page with better handling"""
try:
text = page.extract_text()
if text:
# Clean up text
text = re.sub(r'[ \t]+', ' ', text.strip())
text = re.sub(r'\n\s*\n', '\n', text)
return text
except Exception as e:
logger.warning(f"Failed to extract text from page: {e}")
return None
def _extract_page_tables(self, page, page_num: int) -> List[Dict]:
"""Extract tables with improved processing"""
tables = []
try:
raw_tables = page.extract_tables()
if raw_tables:
for table_idx, table in enumerate(raw_tables):
cleaned_table = self._clean_table_improved(table)
if cleaned_table and len(cleaned_table) > 0:
tables.append({
"page": page_num,
"table_index": table_idx + 1,
"headers": cleaned_table[0] if cleaned_table else [],
"data": cleaned_table[1:] if len(cleaned_table) > 1 else [],
"raw_data": cleaned_table,
"row_count": len(cleaned_table) - 1 if len(cleaned_table) > 1 else 0,
"column_count": len(cleaned_table[0]) if cleaned_table else 0
})
except Exception as e:
logger.warning(f"Failed to extract tables from page {page_num}: {e}")
return tables
def _clean_table_improved(self, table: List[List]) -> List[List[str]]:
"""Improved table cleaning with better cell processing"""
if not table:
return []
cleaned = []
for row in table:
cleaned_row = []
for cell in row:
if cell is None:
cleaned_cell = ""
else:
cleaned_cell = str(cell).strip()
cleaned_cell = re.sub(r'\s+', ' ', cleaned_cell)
cleaned_cell = re.sub(r'[\x00-\x1f\x7f-\x9f]', '', cleaned_cell)
cleaned_row.append(cleaned_cell)
if any(cell.strip() for cell in cleaned_row):
cleaned.append(cleaned_row)
# Optional: collapse single-column tables of empty strings
if cleaned and all(len(r) == len(cleaned[0]) for r in cleaned):
return cleaned
return cleaned
def _extract_key_value_pairs_improved(self, text: str) -> Dict[str, str]:
"""Improved key-value pair extraction with better cleaning"""
pairs: Dict[str, str] = {}
# Normalize text a bit for regex stability
t = text.replace('\r', '\n')
# Pattern 1: colon-separated pairs (key: value)
pattern1 = re.compile(
r'([A-Za-z][\w\s()/\-.]{2,80}?):\s*([^\n\r:][^\n\r]*)'
)
for key, val in pattern1.findall(t):
k = key.strip()
v = val.strip()
# Filter junk: very long values, pure separators, or obvious headers
if not v or len(v) > 200:
continue
if re.fullmatch(r'[-_/\.]+', v):
continue
# Avoid capturing the next key as value by trimming trailing key-like tokens
v = re.sub(r'\s+[A-Z][\w\s()/\-.]{2,40}:$', '', v).strip()
# Skip values that are just long digit runs (likely id lists without meaning)
if re.fullmatch(r'\d{6,}', v):
continue
pairs[k] = v
# Pattern 2: inline โKey โ Valueโ or โKey โ Valueโ
pattern2 = re.compile(r'([A-Za-z][\w\s()/\-.]{2,80}?)\s*[โโ-]\s*([^\n\r]+)')
for key, val in pattern2.findall(t):
k = key.strip()
v = val.strip()
if v and len(v) <= 200 and not re.fullmatch(r'\d{6,}', v):
pairs.setdefault(k, v)
return pairs
def _extract_audit_info(self, text: str, tables: List[Dict]) -> Dict[str, Any]:
"""Extract audit-specific information with better filtering"""
audit_info: Dict[str, Any] = {}
# Prefer tables
for table in tables:
headers = [str(h).lower() for h in table.get("headers", [])]
joined = ' '.join(headers)
if "audit information" in joined or "auditinformation" in joined:
data = table.get("data", [])
for row in data:
if len(row) >= 2 and row[0] and row[1]:
key = str(row[0]).strip()
value = str(row[1]).strip()
# Skip numbered list rows (e.g., "1.", "2)")
if re.match(r'^\s*\d+\s*[.)]\s*$', key):
continue
if key and value:
audit_info[key] = value
# Backup from text
candidates = {
"Date of Audit": r'Date\s+of\s+Audit[:\s]*([^\n\r]+)',
"Location of audit": r'Location\s+of\s+audit[:\s]*([^\n\r]+)',
"Auditor name": r'Auditor\s+name[:\s]*([^\n\r]+)',
"Audit Matrix Identifier (Name or Number)": r'Audit\s+Matrix\s+Identifier.*?[:\s]*([^\n\r]+)',
}
for k, pat in candidates.items():
if k not in audit_info:
m = re.search(pat, text, re.IGNORECASE)
if m:
audit_info[k] = m.group(1).strip()
return audit_info
def _extract_operator_info(self, text: str, tables: List[Dict]) -> Dict[str, Any]:
"""Extract operator information with better table parsing"""
operator_info: Dict[str, Any] = {}
# Look for operator information in tables first
for table in tables:
headers = [str(h).lower() for h in table.get("headers", [])]
if ("operatorinformation" in ' '.join(headers) or
"operator information" in ' '.join(headers) or
"operatorcontactdetails" in ' '.join(headers)):
data = table.get("data", [])
for row in data:
if len(row) >= 2 and row[0] and row[1]:
key = str(row[0]).strip()
value = str(row[1]).strip()
if key and value:
# Clean up key names
kl = key.lower()
if "operator name" in kl:
operator_info["operator_name"] = value
elif "trading name" in kl:
operator_info["trading_name"] = value
elif "company number" in kl:
if len(row) > 2:
company_parts = [str(r).strip() for r in row[1:] if str(r).strip()]
operator_info["company_number"] = "".join(company_parts)
else:
operator_info["company_number"] = value
elif "business address" in kl:
operator_info["business_address"] = value
elif "postal address" in kl:
operator_info["postal_address"] = value
elif "email" in kl:
operator_info["email"] = value
elif "telephone" in kl or "phone" in kl:
operator_info["phone"] = value
elif "nhvas accreditation" in kl:
operator_info["nhvas_accreditation"] = value
elif "nhvas manual" in kl:
operator_info["nhvas_manual"] = value
# Extract from text patterns as backup
patterns = {
'operator_name': r'Operator\s*name[:\s\(]*([^\n\r\)]+?)(?=\s*NHVAS|\s*Registered|$)',
'trading_name': r'Registered\s*trading\s*name[:\s\/]*([^\n\r]+?)(?=\s*Australian|$)',
'company_number': r'Australian\s*Company\s*Number[:\s]*([0-9\s]+?)(?=\s*NHVAS|$)',
'business_address': r'Operator\s*business\s*address[:\s]*([^\n\r]+?)(?=\s*Operator\s*Postal|$)',
'postal_address': r'Operator\s*Postal\s*address[:\s]*([^\n\r]+?)(?=\s*Email|$)',
'email': r'Email\s*address[:\s]*([^\s\n\r]+)',
'phone': r'Operator\s*Telephone\s*Number[:\s]*([^\s\n\r]+)',
'nhvas_accreditation': r'NHVAS\s*Accreditation\s*No\.[:\s\(]*([^\n\r\)]+)',
}
for key, pattern in patterns.items():
if key not in operator_info: # Only use text if not found in tables
match = re.search(pattern, text, re.IGNORECASE)
if match:
value = match.group(1).strip()
if value and len(value) < 200:
if key == 'company_number':
value = re.sub(r'\s+', '', value)
operator_info[key] = value
return operator_info
def _extract_vehicle_registrations(self, tables: List[Dict]) -> List[Dict]:
"""Extract vehicle registration information from tables"""
vehicles: List[Dict[str, Any]] = []
for table in tables:
headers = [str(h).lower() for h in table.get("headers", [])]
# Look for vehicle registration tables
if any(keyword in ' '.join(headers) for keyword in ['registration', 'vehicle', 'number']):
reg_col = None
for i, header in enumerate(headers):
if 'registration' in header and 'number' in header:
reg_col = i
break
if reg_col is not None:
data = table.get("data", [])
for row in data:
if len(row) > reg_col and row[reg_col]:
reg_num = str(row[reg_col]).strip()
# Validate registration format (letters/numbers)
if re.match(r'^[A-Z]{1,3}\s*\d{1,3}\s*[A-Z]{0,3}$', reg_num):
vehicle_info = {"registration_number": reg_num}
# Add other columns as additional info
for i, header in enumerate(table.get("headers", [])):
if i < len(row) and i != reg_col:
vehicle_info[str(header)] = str(row[i]).strip()
vehicles.append(vehicle_info)
return vehicles
def _extract_driver_records(self, tables: List[Dict]) -> List[Dict]:
"""Extract driver records from tables"""
drivers: List[Dict[str, Any]] = []
for table in tables:
headers = [str(h).lower() for h in table.get("headers", [])]
# Look for driver/scheduler tables
if any(keyword in ' '.join(headers) for keyword in ['driver', 'scheduler', 'name']):
name_col = None
for i, header in enumerate(headers):
if 'name' in header:
name_col = i
break
if name_col is not None:
data = table.get("data", [])
for row in data:
if len(row) > name_col and row[name_col]:
name = str(row[name_col]).strip()
# Basic name validation
if re.match(r'^[A-Za-z\s]{2,}$', name) and len(name.split()) >= 2:
driver_info = {"name": name}
# Add other columns
for i, header in enumerate(table.get("headers", [])):
if i < len(row) and i != name_col:
driver_info[str(header)] = str(row[i]).strip()
drivers.append(driver_info)
return drivers
def _extract_compliance_summary(self, text: str, tables: List[Dict]) -> Dict[str, Any]:
"""Extract compliance information"""
compliance = {
"standards_compliance": {},
"compliance_codes": {},
"audit_results": []
}
# Look for compliance tables
for table in tables:
headers = [str(h).lower() for h in table.get("headers", [])]
if any(keyword in ' '.join(headers) for keyword in ['compliance', 'standard', 'requirement']):
data = table.get("data", [])
for row in data:
if len(row) >= 2:
standard = str(row[0]).strip()
code = str(row[1]).strip()
if standard.startswith('Std') and code in ['V', 'NC', 'SFI', 'NAP', 'NA']:
compliance["standards_compliance"][standard] = code
# Extract compliance codes definitions
code_patterns = {
'V': r'\bV\b\s+([^\n\r]+)',
'NC': r'\bNC\b\s+([^\n\r]+)',
'SFI': r'\bSFI\b\s+([^\n\r]+)',
'NAP': r'\bNAP\b\s+([^\n\r]+)',
'NA': r'\bNA\b\s+([^\n\r]+)',
}
for code, pattern in code_patterns.items():
match = re.search(pattern, text, re.IGNORECASE)
if match:
compliance["compliance_codes"][code] = match.group(1).strip()
return compliance
def _extract_dates_and_numbers_improved(self, text: str) -> Dict[str, Any]:
"""Improved date and number extraction"""
result = {
"dates": [],
"registration_numbers": [],
"phone_numbers": [],
"email_addresses": [],
"reference_numbers": []
}
# Date patterns
date_patterns = [
r'\b(\d{1,2}(?:st|nd|rd|th)?\s+[A-Za-z]+\s+\d{4})\b',
r'\b(\d{1,2}/\d{1,2}/\d{4})\b',
r'\b(\d{1,2}-\d{1,2}-\d{4})\b',
r'\b(\d{1,2}\.\d{1,2}\.\d{4})\b',
]
for pattern in date_patterns:
result["dates"].extend(re.findall(pattern, text))
# Registration numbers (Australian format-ish)
reg_pattern = r'\b([A-Z]{1,3}\s*\d{1,3}\s*[A-Z]{0,3})\b'
result["registration_numbers"] = list(set(re.findall(reg_pattern, text)))
# Phone numbers (AU)
phone_pattern = r'\b((?:\+61|0)[2-9]\s?\d{4}\s?\d{4})\b'
result["phone_numbers"] = list(set(re.findall(phone_pattern, text)))
# Email addresses
email_pattern = r'\b([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})\b'
result["email_addresses"] = list(set(re.findall(email_pattern, text)))
# Reference numbers
ref_patterns = [
(r'RF(?:S)?\s*#?\s*(\d+)', 'RFS_Certifications'),
(r'NHVAS\s+Accreditation\s+No\.?\s*(\d+)', 'NHVAS_Numbers'),
(r'Registration\s+Number\s*#?\s*(\d+)', 'Registration_Numbers'),
]
for pattern, key in ref_patterns:
matches = re.findall(pattern, text, re.IGNORECASE)
if matches:
result["reference_numbers"].extend([f"{key}: {m}" for m in matches])
return result
@staticmethod
def save_results(results: Dict[str, Any], output_path: str):
"""Save results to JSON file"""
try:
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=2, ensure_ascii=False)
logger.info(f"๐พ Results saved to {output_path}")
except Exception as e:
logger.error(f"Failed to save results: {e}")
@staticmethod
def export_to_excel(results: Dict[str, Any], excel_path: str):
"""Export results to Excel with improved formatting"""
try:
with pd.ExcelWriter(excel_path, engine='openpyxl') as writer:
# Summary sheet
summary_data = []
extraction_summary = results.get("extraction_summary", {})
for key, value in extraction_summary.items():
summary_data.append({"Metric": key.replace("_", " ").title(), "Value": value})
pd.DataFrame(summary_data).to_excel(writer, sheet_name='Summary', index=False)
# Key-value pairs
kv_pairs = results.get("extracted_data", {}).get("key_value_pairs", {})
if kv_pairs:
kv_df = pd.DataFrame(list(kv_pairs.items()), columns=['Key', 'Value'])
kv_df.to_excel(writer, sheet_name='Key_Value_Pairs', index=False)
# Vehicle registrations
vehicles = results.get("extracted_data", {}).get("vehicle_registrations", [])
if vehicles:
pd.DataFrame(vehicles).to_excel(writer, sheet_name='Vehicle_Registrations', index=False)
# Driver records
drivers = results.get("extracted_data", {}).get("driver_records", [])
if drivers:
pd.DataFrame(drivers).to_excel(writer, sheet_name='Driver_Records', index=False)
# Compliance summary
compliance = results.get("extracted_data", {}).get("compliance_summary", {})
if compliance.get("standards_compliance"):
comp_df = pd.DataFrame(list(compliance["standards_compliance"].items()),
columns=['Standard', 'Compliance_Code'])
comp_df.to_excel(writer, sheet_name='Compliance_Standards', index=False)
logger.info(f"๐ Results exported to Excel: {excel_path}")
except Exception as e:
logger.error(f"Failed to export to Excel: {e}")
def main():
if len(sys.argv) < 2:
print("Usage: python fixed_pdf_extractor.py <pdf_path>")
sys.exit(1)
pdf_path = Path(sys.argv[1])
if not pdf_path.exists():
print(f"โ PDF not found: {pdf_path}")
sys.exit(1)
print("๐ Fixed PDF Data Extractor")
print("=" * 50)
extractor = FixedPDFExtractor()
results = extractor.extract_everything(str(pdf_path))
base = pdf_path.stem
output_dir = pdf_path.parent
# Save outputs
json_path = output_dir / f"{base}_comprehensive_data.json"
excel_path = output_dir / f"{base}_fixed_extraction.xlsx"
FixedPDFExtractor.save_results(results, str(json_path))
FixedPDFExtractor.export_to_excel(results, str(excel_path))
print("\n๐พ OUTPUT FILES:")
print(f" ๐ JSON Data: {json_path}")
print(f" ๐ Excel Data: {excel_path}")
print(f"\nโจ FIXED EXTRACTION COMPLETE!")
if __name__ == "__main__":
main()
|