Skip to content

3 Automated ESG Metric Extraction and Processing Workflow

JIAOZIJUN edited this page Nov 17, 2024 · 40 revisions

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.

1. pdf_to_txt.py

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.

Overview

  • 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: Combines pdf_to_text, clean_text, and split_into_sentences to 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.

Output

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.

2. divide_quantity_qualitative.py

From the dictionary created, we divide metrics into qualitative and quantitative categories and save them to .json files.

Overview

  • A defaultdict is 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.

Output

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.

3. llm_model.py

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.

Overview

  • 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 an OpenLLMAPI instance 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: Calls process_esg_data to extract and save ESG metrics from the specified text file.

Output

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.

4. supplement_model.py

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.

Overview

In the load_supplement_model function:

  • We initialize a sentiment analysis pipeline using the distilbert-base-uncased-finetuned-sst-2-english model 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 1 to indicate the presence of the metric.

Output

A Qualitative_metrics CSV file contains fields for the metric, the value and the confidence score.

5. merge_similarity.py

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.

Overview

  • Data Merging: Reads the quantitative and qualitative files and merges them by rows.

  • Embedding Calculation: Uses BERT embeddings 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.

Output

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.

6. save_to_db.py

Save all the structured data into database which is called 'Structured_data' (click to get more details).

7. Summary

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.

Clone this wiki locally