Repository navigation
3 Automated ESG Metric Extraction and Processing Workflow
This workflow is designed to automatically extract Environmental, Social and Governance (ESG) metrics from PDF reports, structure and clean the data, and perform similarity matching on the extracted metrics to standardise the data. The process includes the following steps:

| Python file names | Basic Description |
|---|---|
| pdf_to_txt.py | Extract texts from PDF reports and clean. |
| divide_quantity_qualitative.py | Separate qualitative and quantitative metrics. |
| llm_model.py | Construct a LLM model to extract targeted ESG information from unstructured reports. |
| supplement_model.py | Qualitative metric extraction function and sentiment analysis. |
| merge_similarity.py | Use BERT embeddings to match metrics, filter the data on similarity thresholds. |
| save_to_db.py | Save the processed output files into database. |
We convert the ESG reports to texts by using regularization and provide a pipeline to convert PDF files into clean, structured text files, particularly useful for processing documents for further text analysis.
-
clean_text: We apply regularization with regex to remove unnecessary elements, such as extra whitespace, non-ASCII characters, page headers, footers, and page numbers. -
pdf_to_clean_text: Combinespdf_to_text,clean_text, andsplit_into_sentencesto create a full pipeline: Extracts text from the PDF. Cleans and splits the text into sentences. Saves the cleaned, sentence-split text to a specified .txt output file.
The result is a clean, segmented text file with extraneous information removed. This text will be passed on to the next step to extract specific ESG metrics.
From the dictionary created, we divide metrics into qualitative and quantitative categories and save them to .json files.
-
A
defaultdictis used to group keywords by their corresponding metric, creating a dictionary where each metric is associated with a list of keywords. -
qualitative_metrics_keywords: Contains metrics that match the "Exposure" category. -
quantity_metrics_keywords: Contains all other metrics not in the qualitative category.
A qualitative_metrics_keywords JSON file and a quantitative_metrics_keywords JSON file. The quantitative_metrics_keywords will be fed into a large language model to help extract structured numerical information from text, while qualitative_metrics_keywords will be applied to DistilBERT to obtain sentiment information.
We define a pipeline for extracting ESG-related metrics from semi-structured text using OpenAI's language model, specifically configured for environmental, social, and governance data. It provides utilities to retrieve ESG indicators, values, units, and confidence scores from raw text and then saves the results in CSV format.
-
Class
OpenLLMAPI: A custom class that interfaces with the OpenAI API for language model completion. It formats prompts and handles model response retrieval. -
create_open_llm: Initialises anOpenLLMAPIinstance with the specified model name. -
extract_json: Parses JSON structures from model responses. This function uses regular expressions to find JSON-like text and then attempts to parse it into Python dictionaries. -
format_confidence: Ensures confidence scores are formatted as decimals from 0 to 1 with the actual precision level produced by the model, indicating how certain the model is about the accuracy of this extraction from the semi-structured data. -
process_esg_data: Sets up the prompt for ESG data extraction, reading text lines from a file. For each line, it sends a prompt to the OpenAI model and extracts JSON-based ESG metrics (indicator, value, unit, and confidence score). -
load_llm_model: Callsprocess_esg_datato extract and save ESG metrics from the specified text file.
The results include metrics, values, units, and their respective confidence scores, compiled into a CSV file to ensure the extracted data is readily accessible for further analysis.
We load a sentiment analysis model to process qualitative metrics based on a JSON file of metric keywords. The function searches for keyword matches within the text file and splits matched sentences into shorter sub-sentences for analysis. Using it, we evaluate each sub-sentence to obtian sentiment and confidence score. For each detected metric, we assign a value of 1, store it in a new DataFrame alongside its confidence score and drop duplicates.
In the load_supplement_model function:
-
We initialize a sentiment analysis pipeline using the
distilbert-base-uncased-finetuned-sst-2-englishmodel to assess sentiment. -
For each metric in
qualitative_metrics_keywords, the function searches for sentences containing keywords associated with that metric. -
When a match is found, the sentence is then split into sub-sentences (each with a maximum of 512 characters), and each sub-sentence will be analyzed and get its sentiment label and confidence score.
-
We finally extract the metric and confidence score from the results, setting the value to
1to indicate the presence of the metric.
A Qualitative_metrics CSV file contains fields for the metric, the value and the confidence score.
We merge the quantitative and qualitative metrics from separate files and process these metrics with a BERT model to identify semantic similarities to the target labels. The outcomes are saved based on the similarity thresholds.
-
Data Merging: Reads the quantitative and qualitative files and merges them by rows.
-
Embedding Calculation: Uses
BERTembeddings for both the metrics and the target labels to calculate pairwise cosine similarity. -
Data Filtering: Filters metrics by similarity threshold and removes duplicates to obtain the unique and most-similar metrics.
-
Data Cleaning and Final Processing: Ensures that qualitative metrics match the predefined keywords and has cleaned the non-numeric entries.
The output is a merged CSV file containing both quantitative and qualitative metrics. Key columns include metrics, values, units and similarity scores, where similarity highlights the relevance between metrics and target labels. The output has unique entries retained from each standard metric , based on the highest similarity and confidence values.
Save all the structured data into database which is called 'Structured_data' (click to get more details).
Through these steps, the workflow achieves a complete process from PDF report extraction to standardized ESG metric output. The final structured data enables automated ESG report analysis and comparison, simplifying ESG data collection and standardization for organizations.