-
Notifications
You must be signed in to change notification settings - Fork 2
/
Word Count.py
44 lines (43 loc) · 2.18 KB
/
Word Count.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def count_words(sentence):
res_dict = {}
sentence = sentence.lower()
sentence = sentence.strip("'")
sentence = sentence.split()
counter = 0
while counter < len(sentence):
if sentence[counter].isalnum() and "," not in sentence[counter]:
counter += 1
continue
if "\n" in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split("\n") + sentence[counter + 1:]
if "\t" in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split("\t") + sentence[counter + 1:]
if "," in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split(",") + sentence[counter + 1:]
if "." in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split(".") + sentence[counter + 1:]
if "_" in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split("_") + sentence[counter + 1:]
if "&" in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split("&") + sentence[counter + 1:]
if "!" in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split("!") + sentence[counter + 1:]
if "@" in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split("@") + sentence[counter + 1:]
if "%" in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split("%") + sentence[counter + 1:]
if "^" in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split("^") + sentence[counter + 1:]
if "$" in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split("$") + sentence[counter + 1:]
if ":" in sentence[counter]:
sentence = sentence[:counter] + sentence[counter].split(":") + sentence[counter + 1:]
sentence[counter] = sentence[counter].strip("'")
counter += 1
for word in sentence:
if word != "":
if word not in res_dict.keys():
res_dict[word] = 1
continue
res_dict[word] += 1
return res_dict