u4_repair_2
This commit is contained in:
parent
f9b7921fa6
commit
584d9ac904
@ -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.'''))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user