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)
| 91 | |
| 92 | |
| 93 | def 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 | |
| 111 | def main(): |