"""
-----------------------------------
# Instructions for Code Execution
-----------------------------------

## Overview
This script is designed to count words from an ESG dictionary in text documents. It processes `.txt` format files, calculating the frequency of ESG dictionary words in each file.

## Folder Structure
- The script resides in a folder named 'Code'.
- 'Data' and 'Results' folders are siblings to the 'Code' folder and are located in the base directory.
- The 'Data' folder contains a sub-folder 'Firmfiles_cleaned', where the source `.txt` files are stored.
- The 'Results' folder is used to store the output of the script.

## Prerequisites
- Ensure all `.txt` files to be processed are placed in the 'Firmfiles_cleaned' sub-folder within the 'Data' folder.
- The 'ESG_Dictionary.csv' file must be present in the 'Data' folder.

## Processing Steps
1. **Word Counting and Dictionary Matching**: The script reads each `.txt` file in the 'Firmfiles_cleaned' folder and counts the occurrences of words found in the 'ESG_Dictionary.csv' file.
2. **Results Compilation**: The script then calculates the total word count and the count of dictionary words for each file.

## Output
- The results are saved in a CSV file named 'ESG_count.csv' in the 'Results' folder.

"""

import os
import csv
#------------ Set up logging ------------ 
import log_config
log_config.setup_logging()
import logging
logging.info("ESG File Word Count Block.py'")

#------------ Folder locations here ------------ 
# Source directory (User-specified)
source_folder = '../Data/Firmfiles_cleaned'
# ESG Dictionary CSV (User-specified)
esg_dic = '../Data/ESG_Dictionary.csv'

# Define path for generated results file
esg_count = '../Results/ESG_count.csv'


#------------ Functions to count dictionary words and process documents ------------ 
def count_dictionary_words(document, dictionary):
    word_count = 0
    dictionary_count = 0

    with open(document, 'r', encoding='utf-8') as file:
        text = file.read().lower().split()

        for word in text:
            word_count += 1
            if word in dictionary:
                dictionary_count += 1

    return dictionary_count, word_count


def process_documents(source_folder, esg_dic, esg_count):
    dictionary = set()

    with open(esg_dic, 'r', encoding='utf-8') as dict_file:
        reader = csv.reader(dict_file)
        for row in reader:
            dictionary.add(row[0].lower())

    with open(esg_count, 'w', newline='', encoding='utf-8') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(['File Name', 'Dictionary Word Count', 'Total Word Count'])

        for file in os.listdir(source_folder):
            if file.endswith('.txt'):
                document = os.path.join(source_folder, file)
                dict_count, word_count = count_dictionary_words(document, dictionary)
                writer.writerow([file, dict_count, word_count])



# Process the documents in the source folder
process_documents(source_folder, esg_dic, esg_count)

print(f"ESG word counts for each of the web-page files is completed and saved to 'ESG_count.csv' file")

#------------ Log completion of the script ------------
logging.info("Finished running 'ESG File Word Count Block.py'")