diff --git a/u4/word_count.py b/u4/word_count.py index be07217..8413c16 100644 --- a/u4/word_count.py +++ b/u4/word_count.py @@ -1,25 +1,30 @@ from collections import defaultdict def count_words(sentance: str) -> dict: + sent_list = list(sentance) 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("'", "^") + forbidden = delims + del_chars + [" ", "'", ""] - for w in sentance.lower().split(" "): + for i, ch in enumerate(sent_list): + if(ch in delims): + sent_list[i] = " " + elif(ch in del_chars): + sent_list[i] = "" + if(ch == "'"): + if((sent_list[max(i-1, 0)] not in forbidden) and (sent_list[min(i+1, len(sent_list))] not in forbidden)): + sent_list[i] = '^' + else: + sent_list[i] = "" + + for w in ''.join(sent_list).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