import math


def frequency_list(text):
    """
    Calculate the frequency of each character in the input text.

    Args:
        text (str): Input text.

    Returns:
        tuple: A dictionary of character frequencies and the total count of characters.
    """
    frequencies = {}
    for char in text:
        if char != '\n':  # Ignore newline characters
            frequencies[char] = frequencies.get(char, 0) + 1
    return frequencies, sum(frequencies.values())


def entropy(frequencies, total_count):
    """
    Calculate the entropy of the text based on character frequencies.

    Args:
        frequencies (dict): Dictionary of character frequencies.
        total_count (int): Total count of characters.

    Returns:
        float: The entropy of the text.
    """
    entropy = 0
    for count in frequencies.values():
        probability = count / total_count
        entropy += probability * math.log2(probability)
    return -entropy


def analyze_file(filename):
    """
    Analyze a text file to compute character frequencies and entropy.

    Args:
        filename (str): Path to the text file.
    """
    with open(filename, "r", encoding="utf-8") as file:
        text = file.read()

    frequencies, total_count = frequency_list(text)
    entropy_result = entropy(frequencies, total_count)

    print(f"File: {filename}")
    print(f"Entropy: {round(entropy_result, 2)}")


# Example analysis
# Please extract the files in the same folder where the Python script file resides.
# Otherwise, you should give the path of text files to the “analyze_file” function.
# For example, if files are in folder "Text files", you should run: analyze_file("Text files/faust.txt")
analyze_file("faust.txt")
analyze_file("wealth_of_nations.txt")
