avp26/u4/word_count.py

9 lines
376 B
Python

from collections import Counter
import re
def count_words(sentence):
pattern = r"[a-zA-ZáčďéíĺľňóôŕšťúýžÁČĎÉÍĹĽŇÓÔŔŠŤÚÝŽ0-9]+(?:'[a-zA-ZáčďéíĺľňóôŕšťúýžÁČĎÉÍĹĽŇÓÔŔŠŤÚÝŽ0-9]+)?"
clean_sentence = sentence.lower()
found_words = re.findall(pattern, clean_sentence)
return dict(Counter(found_words))