diff --git a/u4/word_count.py b/u4/word_count.py index 276deda..be07217 100644 --- a/u4/word_count.py +++ b/u4/word_count.py @@ -1,17 +1,25 @@ from collections import defaultdict def count_words(sentance: str) -> dict: - words = defaultdict(0) + words = defaultdict(int) delims = [":", "?", "!", "\n", "\t"] for delim in delims: - sentance = sentance.replace(delim, "") + sentance = sentance.replace(delim, " ") - for w in sentance.split(" "): - words[w] += 1 + del_chars = ['"', ",", "."] + for del_ch in del_chars: + sentance = sentance.replace(del_ch, "") - return words + sentance.replace("'", "^") -print(count_words(""""To je heslo: 'HESLO 123'!", zvolala postava zo seriálu Panelák.\nPotom som utiekol.""")) + for w in sentance.lower().split(" "): + if w == "": + continue + words[w.replace("^", "'")] += 1 + + return dict(words) + +print(count_words('''"To don't je heslo: 'HESLO 123'!", zvolala postava zo seriálu Panelák.\nPotom som utiekol.''')) \ No newline at end of file