Question: Question 1: Counting Words, Take 2 use python) In this question you will revisit Question 2 from Assignment 1. You are going to improve your
Question 1: Counting Words, Take 2 use python) In this question you will revisit Question 2 from Assignment 1. You are going to improve your program by using a dictionary to store the counts of all of the words of interest at the same time. The program output will appear the same as in assignment 1, but this time you are only allowed to traverse the story file once. As for assignment 1, begin by asking the user to enter two filenames. One file contains a list of words that you want to count (e.g. words.txt). The second file contains the text that you want to search for each word (e.g. story.txt). (When your program runs, the user could type a different name. Do not hardcode any filenames in your code.) Ask the user to enter the name of each file. Assume that the files are located in the same directory as your code file. Begin by building a dictionary with one item for each word in words.txt. Hint: The key should be the word, and the value will be the count initialize all counts to zero. Then, process story.txt once and count how many times each word occurs. You should process the file line by line, and NOT load the entire file in memory at once (Your user might not have enough memory to hold all of the contents of a large file!). Print a summary similar to the sample output below. Note 1: As for assignment 1, you should consider a word to be anything bounded by whitespace. You do not need to do any processing to account for punctuation or other special characters. For example, in the sample below, time. (with a period) is not considered the same as the word time (without a period). Capitalization will also matter. The will not be considered the same word as the. Note 2: You should test your program with small files of your own creation, until you are sure that the counting is correct. Your program will be tested with word files containing 10-20 words, and fairly large story files obtained from Project Gutenberg (https://www.gutenberg.org/). Download a book of your choice from Project Gutenberg, and verify that your program works with a large file. Sample input file, words.txt: a time Sample input file, story.txt: Once upon a time there was a dog. The dog ran around and around, and had a great time. The end. Sample program output (blue text is typed by the user): Enter the name of the file containing the words to count: words.txt Enter the name of the file containing the story: story.txt The word "a" occurs 3 times. The word "time" occurs 1 times. Program terminated normally.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
