Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

    count = 0;  
    word = "";  
    maxCount = 0;  
    words = [];  
       
    #Opens a file in read mode  
    file = open("data.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();  
Comment

PREVIOUS NEXT
Code Example
Python :: merge on row number python 
Python :: pandas change multiple column types 
Python :: python check for folder 
Python :: change selected option optionmenu tkinter 
Python :: pandas reorder columns by name 
Python :: add pip to path 
Python :: start virtualenv 
Python :: time until 2021 
Python :: double for in python 
Python :: remove outliers python dataframe 
Python :: dictionary python length 
Python :: dataframe fill none 
Python :: how to do date time formatting with strftime in python 
Python :: pdf to text python 
Python :: Substring in a django template? 
Python :: how to add column to np array 
Python :: pyqt5 button example 
Python :: django form set min and max value 
Python :: delete all files in a directory python 
Python :: give answer in 6 decimal python 
Python :: port 5432 failed: Connection timed out (0x0000274C/10060) Is the server running on that host and accepting TCP/IP connections? 
Python :: python check if string is int 
Python :: case in python 
Python :: make averages on python 
Python :: pathlib get extension 
Python :: pandas add column with constant value 
Python :: print textbox value in tkinter 
Python :: how to check if given number is binary in pytho 
Python :: remove spaces from string python 
Python :: load a Dictionary from File in Python Using the Load Function of the pickle Module 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =