Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A function called ordered_freq_distribution with two parameters: infile and orde

ID: 3671935 • Letter: A

Question

A function called ordered_freq_distribution with two parameters: infile and ordered_distfile. The first parameter, infile, is the name of the file for which we want to compute the frequency distribution. The second parameter, ordered_distfile is the name of the file into which the frequency distribution is to be written in sorted order of the frequency. That is, the words should be printed into the file in decreasing order of frequency. If several words have the same frequency, they should be listed in alphabetical order. Use string formatting to make sure that the output in ordered_distfile is neatly formatted.

Once again, in order to carry out this task, you must first create a dictionary to store the frequency distribution by calling the helper function freq_dictionary described in #(3) below. Remember to close all files used in the function.

This is the program used for this problem. Please use correct indentation and this is to be written in python 3.5.

For example, suppose poem.txt is the name of a file containing the following poem (courtesy of Dr. Seuss):

You have brains in your head.

You have feet in your shoes.

You can steer yourself in

any direction you choose.

After the function call ordered_freq_distribution("essay.txt", "ordfreq.txt"), a file called ordfreq.txt should contain the following:

you 4

in 3

have 2

your 2

any 1

brains 1

can 1

choose 1

direction 1

feet 1

head 1

shoes 1

steer 1

yourself 1

Explanation / Answer

Program:

FrequencyDistribution.py

import string

def freq_dictionary(infile):  
   a = open(infile, 'r')  
   b = {}

   # get the set of punctuations
   exclude = set(string.punctuation)

   for line in a:
       # remove all punctuations      
       line = ''.join(ch for ch in line if ch not in exclude)
      
       # convert line to lower case
       line = line.lower()
      
       words = line.split()

       for word in words:
           if word not in b:
               b[word] = 1
           else:
               b[word] += 1

   a.close()
   return b


def ordered_freq_distribution(infile, ordered_distfile):
   # get the dictionary of words
   b = freq_dictionary(infile)
  
   # sort the dictionary by value and make it as list
   c = sorted(b.items(), key=lambda x: (-x[1], x[0]))
      
   out = open(ordered_distfile, 'w')

   for pair in c:  
       out.write(pair[0])  
       out.write(' ')
       out.write(str(pair[1]))
       out.write(' ')      
  
   out.close()

def main():
   ordered_freq_distribution("essay.txt", "ordfreq.txt")
main()  

Output:

Input file: essay.txt

You have brains in your head.
You have feet in your shoes.
You can steer yourself in
any direction you choose.

Output file: ordfreq.txt

you 4
in 3
have 2
your 2
any 1
brains 1
can 1
choose 1
direction 1
feet 1
head 1
shoes 1
steer 1
yourself 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote