write a python program to find the most frequent word in text file

Solutions on MaxInterview for write a python program to find the most frequent word in text file by the best coders in the world

showing results for - "write a python program to find the most frequent word in text file"
Teo
22 Feb 2019
1    count = 0;  
2    word = "";  
3    maxCount = 0;  
4    words = [];  
5       
6    #Opens a file in read mode  
7    file = open("data.txt", "r")  
8          
9    #Gets each line till end of file is reached  
10    for line in file:  
11        #Splits each line into words  
12        string = line.lower().replace(',','').replace('.','').split(" ");  
13        #Adding all words generated in previous step into words  
14        for s in string:  
15            words.append(s);  
16       
17    #Determine the most repeated word in a file  
18    for i in range(0, len(words)):  
19        count = 1;  
20        #Count each word in the file and store it in variable count  
21        for j in range(i+1, len(words)):  
22            if(words[i] == words[j]):  
23                count = count + 1;  
24                  
25        #If maxCount is less than count then store value of count in maxCount  
26        #and corresponding word to variable word  
27        if(count > maxCount):  
28            maxCount = count;  
29            word = words[i];  
30              
31    print("Most repeated word: " + word);  
32    file.close();