u4
This commit is contained in:
parent
0e1ca921d6
commit
0257627d83
@ -1,19 +1,11 @@
|
|||||||
def count_words(sentence):
|
def count_words(sentence):
|
||||||
dict = {}
|
counts = {}
|
||||||
separators = [".", ",", ":", "!", "?", "\n", "\t", "'", " ", '"']
|
start = 0
|
||||||
|
for i, ch in enumerate(sentence + " "):
|
||||||
|
if not (ch.isalnum() or ch == "'"):
|
||||||
|
word = sentence[start:i].lower().strip("'")
|
||||||
|
start = i + 1
|
||||||
|
if word:
|
||||||
|
counts[word] = counts.get(word, 0) + 1
|
||||||
|
|
||||||
previous_i = 0
|
return counts
|
||||||
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."""))
|
|
||||||
Loading…
Reference in New Issue
Block a user