The program takes as an input a text file. Your program should print all the unique words in the file and their frequencies in alphabetical order.

The program takes as an input a text file. Your program should print all the unique words in the file and their frequencies in alphabetical order


# Python code to find frequency of each word
def freq(string):
 
    # break the string into list of words 
    string = string.split()         
    str2 = []
 
    # loop till string values present in list str
    for i in string:             
 
        # checking for the duplicacy
        if i not in str2:
 
            # insert value in str2
            str2.append(i) 
             
    for i in range(0, len(str2)):
 
        # count the frequency of each word(present 
        # in str2) in str and print
        print('Frequency of', str2[i], 'is :', string.count(str2[i]))   
 
def main():
    string=''
    with  open("unique.txt",'r') as f:
        for line in f:
            string += line
   
    freq(string)                   
 
if __name__=="__main__":
    main()             # call main function Print this post

Comments

Popular posts from this blog

write a program that takes as an input a text file. Your program should print all the unique words in the file in alphabetical order.

Perform an in-depth evaluation of the following websites: Website 1: http://jbhifi.com.au Website 2: https://www.amazon.com

Global Bikes INC(GBI) Software Requirements Specifications (SRS)