| 1 | import re | |
| 2 | from collections import Counter | |
| 3 | ||
| 1 | 4 | def count_words(sentence): |
| 2 | pass | |
| 5 | # Edge Case: empty input string | |
| 6 | if not sentence: | |
| 7 | return {} | |
| 8 | ||
| 9 | # Convert to lowercase to handle 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 | # Edge Case: handling contractions with apostrophes as single words | |
| 15 | # Edge Case: treating numbers as words | |
| 16 | # Edge Case: handling mixed case words | |
| 17 | words = re.findall(r"[a-z0-9]+(?:'[a-z0-9]+)*", sentence) | |
| 18 | ||
| 19 | # Count occurrences of each word | |
| 20 | word_counts = Counter(words) | |
| 21 | ||
| 22 | # Convert Counter to regular dictionary | |
| 23 | return dict(word_counts) | |
| 24 | # Handled Edge Cases: empty input string, contractions with apostrophes, numbers as words, mixed case words |