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

write in Python “A file concordance tracks the unique words in a file and their

ID: 3708405 • Letter: W

Question

write in Python

“A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order.”

ADDITIONAL REQUIREMENTS:

Your program should allow the user to enter the name of the text file to process for the concordance.

Before you start writing your program, (re)read section 6.2 on Problem Solving with Top-Down Design, because I want you to actually design your program. Split your program up into well-defined functions by doing a top-down design and developing a structure chart.

When you write your program, be sure to use:

meaningful variable names with good style (i.e., useCamelCase or use_underscores)

docstring comments at the start of the program and after the “def”inition of each function which

describing what they do

a main function (see lab4 Part C) located at the top of program with a call to it at the bottom of the file

to start execution

global constants where appropriate with good style (ALL_CAPS_AND_UNDERSCORES).

Explanation / Answer

CODE:

import string

import sys

filename=input('Enter the file name: ')

punctuations = set(string.punctuation)

d_words={}

try:

    with open(filename,'r') as f:

        for line in f:

            words=line.split()

            for word in words:             

             

                word=word.lower()

                if len(word)==0:

                    continue

             

                word=word.strip()

                word=''.join(word.split())             

             

                word = ''.join(c for c in word if c not in punctuations)

                if word not in d_words:

                    d_words[word]=1

                else:

                    d_words[word]+=1

except FileNotFoundError as e:

    print('Given file not found ')

    sys.exit()

print('Frequency of Words in the %s' % filename)

l=[]

for k,v in sorted(d_words.items()):

    if len(k)!=0:

             

     

        l.append(k+' '+str(v))

# displaying the word frequency

for i in l:

  r=i.split()

    print(r[0],r[1])