avp26/u4/word_count.py
2026-10-05 22:11:05 +02:00

25 lines
640 B
Python

from collections import defaultdict
def count_words(sentance: str) -> dict:
words = defaultdict(int)
delims = [":", "?", "!", "\n", "\t"]
for delim in delims:
sentance = sentance.replace(delim, " ")
del_chars = ['"', ",", "."]
for del_ch in del_chars:
sentance = sentance.replace(del_ch, "")
sentance.replace("'", "^")
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.'''))