-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy patheach-word-count.py
37 lines (28 loc) · 976 Bytes
/
each-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
"""
Count Words in a String - Counts the number of individual words in a string.
For added complexity read these strings in from a text file and generate a summary.
"""
class Words:
def each_word_count(self,text):
self.sentances = list()
self.words = list()
self.word_counts = dict()
self.lines = text.split('\n')
for line in self.lines:
self.sentances.extend(line.split("."))
for sentance in self.sentances:
self.words.extend(sentance.split(" "))
for word in self.words:
if word in self.word_counts.keys():
self.word_counts[word] += 1
else:
self.word_counts[word] = 1
return self.word_counts
# print(self.words)
x = Words()
filename = input("Enter file name : ")
if filename == '':
filename = "demo.txt"
file = open(filename,'r')
y = x.each_word_count(file.read())
print(y)