Spaces:
Build error
Build error
| import re | |
| import pdfplumber | |
| import tabula | |
| def extract_and_preprocess(pdf_path): | |
| """ | |
| Extrai o texto (via pdfplumber) e as tabelas (via tabula) de um arquivo PDF. | |
| Realiza pré-processamento do texto, removendo quebras de linha e espaços extras, | |
| e segmenta o texto em sentenças, atribuindo IDs. | |
| Retorna uma lista de dicionários com sentenças e uma lista de DataFrames (tabelas). | |
| """ | |
| text = "" | |
| with pdfplumber.open(pdf_path) as pdf: | |
| for page in pdf.pages: | |
| page_text = page.extract_text() or "" | |
| text += page_text + "\n" | |
| # Remove quebras de linha e espaços extras | |
| text = re.sub(r"\s+", " ", text).strip() | |
| raw_sentences = re.split(r'[.?!;]\s+', text) | |
| list_of_sentences = [] | |
| char_offset = 0 | |
| for i, sent in enumerate(raw_sentences): | |
| sent_id = f"S{i+1}" | |
| current_sentence = sent.strip() | |
| list_of_sentences.append({ | |
| "sentence_id": sent_id, | |
| "text": current_sentence, | |
| "global_start": char_offset, | |
| "global_end": char_offset + len(current_sentence) | |
| }) | |
| char_offset += len(current_sentence) + 2 | |
| tables = tabula.read_pdf(pdf_path, pages="all", multiple_tables=True) | |
| return list_of_sentences, tables | |
| def preprocess_text(text): | |
| """ | |
| Realiza o pré-processamento do texto: | |
| - Remove quebras de linha e espaços extras. | |
| Retorna o texto processado. | |
| """ | |
| text = text.strip() | |
| text = re.sub(r"\s+", " ", text) | |
| return text | |