Home /
Expert Answers /
Computer Science /
8-6-lab-modify-dictionary-to-count-words-a-useful-application-for-a-dictionary-is-to-remember-or-pa474
(Solved):
8.6 LAB: Modify Dictionary to count words A useful application for a dictionary is to remember, or ...
8.6 LAB: Modify Dictionary to count words A useful application for a dictionary is to remember, or cache, previously obtained results so that they can be retrieved from the cache when they are requested again. We are going to modify the word_count.py program from LAB \( 8.5 \) so that the user can repeatedly enter files names. If the user enters the filename more than once, look up the answer from a dictionary instead of counting the words in the file again. We will have 2 Dictionaries - one from Lab \( 8.5 \) to hold the word count, counts and another new one, cache to hold the filenames. Ex: Sample output 1: Enter the name of a file (or quit He: 1 His: 1 I: 2 To: 1 Whose: 1 are: 1 fill: 1 here: 1 his: 1 house: 1 in: 1 is: 1 know.: 1 me: 1 not: 1 see: 1 snow.: 1 stopping: 1 the: 1 these: 1 think: 1 though;: 1 up: 1 village: 1 watch: 1 will: 1 with: 1 Enter the name of a file (or quit to quit): frost.txt
Load default template...
Traceback (most recent call last): main() File "main.py", line 5, in main filename = input('Enter file name: ') EOFError: EOF when reading a line Output differs. See highlights below. Special character legend Input frost.txt mary.txt frost.txt quit Enter file name: Word \( \rightarrow \rightarrow \) Count Whose \( \rightarrow \rightarrow 11 \) woods \( \rightarrow 1 \rightarrow 124 \) these \( \rightarrow \mid \rightarrow 1 \) are \( \rightarrow \mid \rightarrow 1 \) \( I \rightarrow 1 \rightarrow 1 \) think \( \rightarrow \rightarrow 1 \) know. \( \rightarrow \rightarrow 1 \) His \( \rightarrow \mid \rightarrow 1 \) house \( \rightarrow \rightarrow \mid 1 \) is \( \rightarrow 1 \rightarrow 1 \) in \( \rightarrow \rightarrow 1 \) the \( \rightarrow \rightarrow 1 \) village \( \rightarrow \mid \rightarrow 1 \) though; \( \rightarrow 1 \rightarrow 1 \) \( \mathrm{He} \rightarrow \mid \rightarrow 1 \) not \( \rightarrow 1 \rightarrow 1 \) see \( \rightarrow 1 \rightarrow 1 \) me \( \rightarrow \rightarrow 1 \) stopping \( \rightarrow 1 \rightarrow 1 \) here \( \rightarrow \rightarrow 11 \) To \( \rightarrow \rightarrow 1 \)