19 lines
650 B
Python
19 lines
650 B
Python
def count_words(sentence):
|
|
dict = {}
|
|
separators = [".", ",", ":", "!", "?", "\n", "\t", "'", " ", '"']
|
|
|
|
previous_i = 0
|
|
for i, chr in enumerate(sentence):
|
|
if chr in separators:
|
|
word = sentence[previous_i:i]
|
|
previous_i = i + 1
|
|
word = word.lower().strip(" ")
|
|
if word in separators or word == "":
|
|
continue
|
|
if word in dict:
|
|
dict[word] = dict[word] + 1
|
|
else:
|
|
dict.update({word : 1})
|
|
return dict
|
|
|
|
print(count_words("""To je heslo: 'HESLO 123'!", zvolala postava zo seriálu Panelák.\nPotom som utiekol.""")) |