Question

!!!!!THIS NEEDS TO BE WRITTEN IN PYTHON 3!!!!!!! NOTE: self has access to the following: frequency_of(self,...

!!!!!THIS NEEDS TO BE WRITTEN IN PYTHON 3!!!!!!!

NOTE: self has access to the following:
frequency_of(self, word):
Returns the number of times word appears in the text.

line_count(self):
Returns the number of lines in the file

word_count(self):
Returns the number of words in the file.

vocabulary(self):
Returns a list of the unique words in the text, sorted in alphabetical order.

def percent_frequencies(self):
Returns a dictionary of the words in the text and the fraction of the text

!!!The function below is what I need help with. I cannot import any libraries ie. counter to ease the code.!!!
!!!!!THIS NEEDS TO BE WRITTEN IN PYTHON 3!!!!!!! THANKS IN ADVANCE

def most_common(self, n=1)
Returns a list of the most common n words in the text. By default, n is 1. The returned words should be in alphabetical order. There might be a case where multiple words have the same frequency, but you can only return some of them due to the n value.
In that case, return the ones that come first alphabetically.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program in python:

#class definition
class FileOperations:
    #specifying the file to be read
    fname = "ShakespeareSonets.txt"
    #function to find the number words in file
    def most_common(self,n):
        #reading the file
        f=open(self.fname, 'r')
        #declaring a dictionary
        dic={}
        #reading file line by line
        for line in f:
            #reading each words in a line
            for words in line.split():
                #if dictionary is empty add the current word
                if len(dic)==0:
                    dic[words]=1
                #check if the current word is present in the dictionary
                else:
                    flag=True
                    for key in dic:
                        #if word is present then increment the count
                        if key==words:
                            dic[key]=dic[key]+1
                            flag=False
                            break
                    #add the word to the dictionary
                    if flag==True:
                        dic[words]=1
        #sorting the dictionary based on values
        newdic=(sorted(dic.items(), key =lambda k:(k[1], k[0])))
        #declaring a list to store the occurence of common word
        ls=[]
        #iterating through the required number of common words
        for i in range(0,n):
            #adding the most common word into the list and removing it from dictionary
            ls.append(newdic.pop())
        #returning the list of most common words
        return ls
#creating the object of the class
obj=FileOperations()
#calling function to return most common words
print(obj.most_common(3))#change 3 to required number of most common words

Indentation:

Output:

Add a comment
Know the answer?
Add Answer to:
!!!!!THIS NEEDS TO BE WRITTEN IN PYTHON 3!!!!!!! NOTE: self has access to the following: frequency_of(self,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT