diff --git a/utils/AlphabetUtils.py b/utils/AlphabetUtils.py index 045556a..547b1ed 100644 --- a/utils/AlphabetUtils.py +++ b/utils/AlphabetUtils.py @@ -2,7 +2,22 @@ LETTERS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'W', 'X', 'Y', 'Z'] +def is_letter_of_alphabet(letter: str): + """ + Checks if the given letter is a valid letter of the alphabet + :param letter: The letter to check + :return: If the letter is in the alphabet + """ + return letter.upper() in LETTERS + + def get_letter_at_index(idx: int, capital: bool = False): + """ + Returns the letter at the given index + :param idx: The index of the letter to return + :param capital: If the capitalized letter should be returned + :return: The letter + """ if idx < 0 or idx > 25: raise AttributeError @@ -10,6 +25,11 @@ def get_letter_at_index(idx: int, capital: bool = False): def get_index_of_letter(letter: str): + """ + Returns the index of the given letter + :param letter: The letter to return the index of + :return: The index of the letter + """ if letter.upper() not in LETTERS: raise AttributeError diff --git a/utils/CipherUtils.py b/utils/CipherUtils.py new file mode 100644 index 0000000..d48b766 --- /dev/null +++ b/utils/CipherUtils.py @@ -0,0 +1,31 @@ +import AlphabetUtils as au + + +def calculate_frequency(text: str, fancy_printing: bool = False): + """ + Calculates the frequency of every letter in the german alphabet for the given text + :param text: The text to calculate the letter frequency for + :return: A list of frequencies, where index 0 contains the frequency of a in percent and so on. + """ + occurrence_count = [0 for i in range(26)] + + for char in text: + if au.is_letter_of_alphabet(char): + char_index = au.get_index_of_letter(char) + occurrence_count[char_index] += 1 + + occurrence_frequency = [] + + for count in occurrence_count: + occurrence_frequency.append(count / len(text)) + + if fancy_printing: + for i in range(26): + print(f'{au.get_letter_at_index(i, True)}: {occurrence_frequency[i] * 100}%') + + return occurrence_frequency + + +if __name__ == '__main__': + print( + calculate_frequency('Hier den Text eingeben, für den die Wahrscheinlichkeiten berechnet werden sollen', True))