def count_words(sentence): counts = {} start = 0 for i, ch in enumerate(sentence + " "): if not (ch.isalnum() or ch == "'"): word = sentence[start:i].lower().strip("'") start = i + 1 if word: counts[word] = counts.get(word, 0) + 1 return counts