| 1 | 1 | from typing import Dict |
| 2 | import re |
| 2 | 3 | |
| 3 | 4 | def count_words(sentence: str) -> Dict[str, int]: |
| 4 | | pass |
| 5 | # Edge Case: Empty input string |
| 6 | if not sentence: |
| 7 | return {} |
| 8 | |
| 9 | # Convert to lowercase for case insensitivity |
| 10 | sentence = sentence.lower() |
| 11 | |
| 12 | # Find all sequences that qualify as words |
| 13 | # This includes alphanumeric sequences and contractions with apostrophes |
| 14 | words = re.findall(r"[a-z0-9]+(?:'[a-z0-9]+)*", sentence) |
| 15 | |
| 16 | # Count occurrences of each word |
| 17 | word_count = {} |
| 18 | for word in words: |
| 19 | # Edge Case: Words with leading/trailing apostrophes (should be stripped by regex pattern) |
| 20 | # Edge Case: Empty words (handled by regex pattern) |
| 21 | word_count[word] = word_count.get(word, 0) + 1 |
| 22 | |
| 23 | # Edge Case: Very long strings (handled by implementation) |
| 24 | # Edge Case: Multiple consecutive whitespace characters (handled by regex pattern) |
| 25 | # Edge Case: Multiple consecutive punctuation marks (handled by regex pattern) |
| 26 | # Edge Case: Numbers mixed with letters (handled as valid words) |
| 27 | # Edge Case: Mixed case sensitivity handling (converted to lowercase) |
| 28 | |
| 29 | return word_count |
| 30 | # Handled Edge Cases: empty input strings, multiple consecutive whitespace characters, multiple consecutive punctuation marks, mixed case sensitivity handling, numbers mixed with letters, very long strings |