MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / analyze_text

Function analyze_text

maths/entropy.py:93–108  ·  view source on GitHub ↗

Convert text input into two dicts of counts. The first dictionary stores the frequency of single character strings. The second dictionary stores the frequency of two character strings.

(text: str)

Source from the content-addressed store, hash-verified

91
92
93def analyze_text(text: str) -> tuple[dict, dict]:
94 """
95 Convert text input into two dicts of counts.
96 The first dictionary stores the frequency of single character strings.
97 The second dictionary stores the frequency of two character strings.
98 """
99 single_char_strings = Counter() # type: ignore[var-annotated]
100 two_char_strings = Counter() # type: ignore[var-annotated]
101 single_char_strings[text[-1]] += 1
102
103 # first case when we have space at start.
104 two_char_strings[" " + text[0]] += 1
105 for i in range(len(text) - 1):
106 single_char_strings[text[i]] += 1
107 two_char_strings[text[i : i + 2]] += 1
108 return single_char_strings, two_char_strings
109
110
111def main():

Callers 1

calculate_probFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected