Repository navigation
Preprocessing
This Python script processes PDFs, extracting both text and tabular data, and cleans it for easier analysis. It's designed for structured reports, particularly ESG (Environmental, Social, and Governance) documents. The code combines natural language processing (NLP) and table extraction.
-
PyPDF2: Extracts text from PDF pages.
-
nltk (Natural Language Toolkit): Tokenizes, removes stopwords, and lemmatizes text.
-
re (regular expressions): Cleans and formats text.
-
pandas: Structures extracted tables in DataFrames.
-
gmft.auto (CroppedTable, AutoTableDetector, AutoTableFormatter): Detects and formats tables.
-
gmft.pdf_bindings (PyPDFium2Document): Reads PDF data for table detection.
- Ensures NLTK data (tokenizers, stopwords, lemmatizers) can download without SSL issues.
- Uses PyPDF2 to read and extract text from PDF pages, storing it as a single string.
-
Removes extra spaces.
-
Preserves punctuation (e.g., periods, commas) and numbers.
-
Simplifies text for further processing.
-
Tokenization (tokenize_text): Splits text into sentences and words.
-
Stopword Removal (remove_stopwords): Filters out common, less meaningful words.
-
Lemmatization (lemmatize_tokens): Reduces words to their base forms for consistency.
- Combines cleaning, tokenization, stopword removal, and lemmatization into one function.
- Structures processed sentences for compatibility with AI models.
-
Uses gmft’s tools to detect tables with a minimum of two columns and two rows.
-
Extracts tables from each page of the PDF.
- Converts detected tables into DataFrames, fills missing values, and formats them as Markdown tables.
- Extracts and preprocesses text.
- Extracts and formats tables.
- Outputs cleaned text and formatted tables.
- Saves the cleaned text and tables as a .txt file in the ../txt_files directory.
Ideal for structured documents like ESG reports, combining narrative text and statistical tables. The output is suitable for machine learning tasks.
-
Preprocessed Text: Cleaned and structured text for AI analysis.
-
Extracted Tables: Detected tables with headers and rows formatted for readability.
================================================================================
Table 1
================================================================================
Column1 | Column2 | Column3
---------|----------|---------
123 | 45% | 2023
================================================================================
This script is similar but adds directory and SSL management.
-
PyPDF2: Reads and extracts text from PDFs.
-
nltk (Natural Language Toolkit): Provides tools for text tokenization, stopword removal, and lemmatization.
-
re (regular expressions): Cleans text.
-
ssl: Manages secure connections for NLTK downloads.
-
os: Manages files and directories.
- Ensures SSL downloads work for required NLTK corpora.
- Downloads essential NLTK tools (e.g., punkt, stopwords, wordnet).
- Reads and combines text from each PDF page.
- Replaces spaces and special characters to simplify text.
- Splits text into sentences and words.
- Filters out common, less meaningful words.
- Standardizes words to base forms.
- Combines cleaning, tokenization, stopword removal, and lemmatization.
- Identifies and structures tabular data in Markdown format.
- Structures preprocessed text and formatted tables for AI models.
- Extracts and preprocesses text and saves it as a Markdown .txt file.
Processes ESG or other reports with mixed text and table data for easy machine learning use.
The text file would contain clean, preprocessed text with tables:
Table header 1 | Table header 2 | Table header 3
---------------|----------------|---------------
Data1 | Data2 | Data3
More text follows here...
The structure ensures that the data is neatly organized and ready for analysis or modeling.