Part B: Programming with Dictionaries and Sets Consider the file named “lab11b.py”. That file contains the skeleton of a Python program to display information about the words in a document. Function “main” is complete. It handles the interaction with the user and calls other functions to perform the appropriate tasks. Function “print_word_index” is complete. It receives a dictionary, where each element is a word and aset of line numbers where that word appears in a document. It displays all of the words (in alphabeticorder), along with the lines numbers for each word (in ascending order). Function “build_word_index” is incomplete. It receives an input file and builds a dictionary containing the unique words which appear in the input file, along with the line numbers where each word appears. The first line of the input file should be considered to be line 1. a. Revise function “build_word_index” to accomplish the specified work. You may wish to review function “build_word_set” (above) for ideas about how to handle upper and lower case letters, as well as punctuation. b. Test the revised program using the sample documents. ============================= You can see the whole part of question at (http://www.cse.msu.edu/~cse231/Labs/Lab11/lab11.pdf) and I only need the answer of Part B. You can see more detail at (http://www.cse.msu.edu/~cse231/Labs/Lab11/) Solution import string def build_word_index( input_file ): word_map = {} line_no = 0 for i, line in enumerate(input_file): line_no+=1 word_lst = line.strip().split() word_lst = [w.lower().strip(string.punctuation) for w in word_lst] for word in word_lst: if word in word_map: word_map[word].add(line_no) elif word!=\"\": word_map[word]={line_no} return word_map def print_word_index( word_map ): index_lst = sorted(list(word_map.items())) for word, line_set in index_lst: line_lst = sorted(list(line_set)) line_str = str( line_lst[0] ) for line_no in line_lst[1:]: line_str += \", {}\".format( line_no ) print(\"{:14s}:\".format(word), line_str ) ## Alternative way to create the line_str ## line_str = \",\".join([str(i) for i in line_lst]) def main(): filename = input( \"Name of file to be processed: \" ) try: file = open( filename, \"r\" ) index = build_word_index( file ) print_word_index( index ) file.close() except IOError: print( \"Halting -- unable to open\", filename ) main() .