(python) Search documents To get a list of all the files in the current directory as a list, use the following imports in the your

Answered step by step
Verified Expert Solution
Question
35 users unlocked this solution today!

(python) Search documents

To get a list of all the files in the current directory as a list, use the following imports in the your python program along with the following code snippet.

from os import listdir from os.path import isfile, join mypath = "docs/" # path to the folder that contains all the text files allfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ] 

The dictionary must be structured in such a way that the key of the dictionary is a word and the value for that key is a list of all the lines that contain that word in any of the files in the dictionary. For example, if the key is the word longitude, then the value for that word is a list that contains the following lines from the corresponding files.

['or Longitude I've got to?" (Alice had no idea what Latitude was, ', 'or Longitude either, but thought they were nice grand words to ', 'Within me latitude widens, longitude lengthens, ', 'ever Tom was short in longitude he had made up in ', 'about fifteen degrees of longitude since we left St. ', 'meant that we was closing up on the longitude of ', 'longitude on the EARTH?" '] 

For every file in the list of files obtained above (in the allfiles variable), open the file and add each word in the file to a dictionary unless that word is already in the dictionary. To ensure that your program works well for upper and lower case versions (for example, person and Person should both be the same keyword), convert every word to lower case before adding it to the dictionary. For each word, add a list or append to the list all the lines in the file that contain that word. Then close the file and go on to the next file, until your program has read all the files. Hint: Test the dictionary on one file first and look for the key and test the value for the key. Make sure that you are getting a list of all the lines in that single file that contain the word. To further test whether the dictionary in your program is working correctly, open the text file in Sublime Text and look for that word and search for the same word in that text file to verify the results.

After having processed all the files in the docs folder, write out only those key-values pairs to a file for those keys that are greater than 6 characters in length. The name of the output file should be dictionary-summary.txt.

Ask the user for the search keyword. Now, if the keyword exists in the dictionary that contains all the words, then display the value for that key in the dictionary. If that keyword does not exist in the dictionary, then let the user know that the provided keyword does not exist in the dictionary. Write out the results of the search operation to a file called searchResults.txt. For example, if the key exists then write out the key and the value (list of lines that contain the word) to the file searchResults.txt. If the key does not exist, then write out an appropriate message the to the file searchResults.txt.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Link Copied!

Step: 1

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

100% Satisfaction Guaranteed-or Get a Refund!

Step: 2Unlock detailed examples and clear explanations to master concepts

blur-text-image_2

Step: 3Unlock to practice, ask and learn with real-world examples

blur-text-image_3

See step-by-step solutions with expert insights and AI powered tools for academic success

  • tick Icon Access 30 Million+ textbook solutions.
  • tick Icon Ask unlimited questions from AI Tutors.
  • tick Icon Order free textbooks.
  • tick Icon 100% Satisfaction Guaranteed-or Get a Refund!

Claim Your Hoodie Now!

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books
flashcard-anime

Study Smart with AI Flashcards

Access a vast library of flashcards, create your own, and experience a game-changing transformation in how you learn and retain knowledge

Explore Flashcards

Students Have Also Explored These Related Databases Questions!