21 lines
401 B
Python
21 lines
401 B
Python
|
|
import re
|
||
|
|
from collections import defaultdict
|
||
|
|
|
||
|
|
|
||
|
|
def count_words_v1(sentence):
|
||
|
|
count = defaultdict(int)
|
||
|
|
for word in sentence.split():
|
||
|
|
count[word.lower()] += 1
|
||
|
|
return count
|
||
|
|
|
||
|
|
|
||
|
|
def count_words_v2(sentence):
|
||
|
|
regex = r"[\w|']+"
|
||
|
|
count = defaultdict(int)
|
||
|
|
for word in re.findall(regex, sentence):
|
||
|
|
count[word.lower()] += 1
|
||
|
|
return count
|
||
|
|
|
||
|
|
|
||
|
|
count_words = count_words_v2
|