import os import pdfplumber import json datadir = "data" pdffiles = [f for f in…
- User
import os import pdfplumber import json
data_dir = "data" pdf_files = [f for f in os.listdir(data_dir) if f.lower().endswith(".pdf")]
chunks = []
def split_text_into_chunks(text, file_name, page_number, max_chars=1200, overlap=30): start = 0 while start < len(text): end = start + max_chars chunk = text[start:end].strip() if chunk: chunks.append({ "type": "text", "page_content": chunk, "metadata": { "file": file_name, "page": page_number } }) start += max_chars - overlap
def add_table_chunk(table, file_name, page_number): if table and any(any(cell.strip() for cell in row if cell) for row in table): chunks.append({ "type": "table", "page_content": table, "metadata": { "file": file_name, "page": page_number } })
for pdf_file in pdf_files: pdf_path = os.path.join(data_dir, pdf_file)
with pdfplumber.open(pdf_path) as pdf: for i, page in enumerate(pdf.pages, start=1): text = page.extract_text() if text and text.strip(): split_text_into_chunks(text, pdf_file, i) tables = page.extract_tables() for table in tables: add_table_chunk(table, pdf_file, i)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.