-
Notifications
You must be signed in to change notification settings - Fork 0
/
program 2 frequent word
38 lines (29 loc) · 1013 Bytes
/
program 2 frequent word
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
count = 0;
word = "";
maxCount = 0;
words = [];
#Opens a file in read mode
file = open("test.txt", "r")
#Gets each line till end of file is reached
for line in file:
#Splits each line into words
string = line.lower().replace(',','').replace('.','').split(" ");
#Adding all words generated in previous step into words
for s in string:
words.append(s);
#Determine the most repeated word in a file
for i in range(0, len(words)):
count = 1;
#Count each word in the file and store it in variable count
for j in range(i+1, len(words)):
if(words[i] == words[j]):
count = count + 1;
#If maxCount is less than count then store value of count in maxCount
#and corresponding word to variable word
if(count > maxCount):
maxCount = count;
word = words[i];
print("Most repeated word: " + word);
file.close();
output:
Most repeated word: python