python HW

This will use your HW3 answers. Email us for the HW3 solution if you were not able to solve it and are willing to accept your HW3 grade as you submitted it.
Write a program that loops over several search terms and writes the lines with positive matches to separate text files (one for each term).
This program will extend what you did for HW03 in two ways: reuse your search function from HW03 (leave it unchanged), but in your main program (readas: not in your function) you will count the number of results for each search term, and write out files with those results (one for each term).
For example, if you use three search terms: “oxford”, “cat”, and “London” then your program will end up creating three text files (oxford-results.txt, cat-results.txt, and London-results.txt), each containing the lines containing those words (that return True from your search function). Your program should also print out the counts.
The set of rules about a positive match remain the same from HW03 and those test cases should remain the same.
Key tip: you want your outer loop to be the search term and the inner loop to be the lines of text. Discussion question to complete AFTER completing the homework: Why does it need to be this way? What sort of things would go wrong if you flipped it around?
Step 1a: Open the file and be able to loop through the lines. The only goal for this step is just to get the file open and the lines isolated, and run those lines through you search function using a single search term. You can likely copy what you did for HW3. Read the file in with readlines() instead of looping over the infile. You need to repeatedly loop over the file context, which will not work with the “for line in infile…” pattern. Visually inspect that the function call is returning True or False appropriately for each line.
Step 1b: Once you have step 1a, adapt your code as follows:
create a list of search terms, eg [‘cat’, ‘London’, ‘horse’] or whatever you’d like
add a for loop over that list of terms, and move your for loop going over the lines into this for loop (this creates your nested loop)
Print out the positive matches to check that this is working. Just worry about visually inspecting it for now. We’ll add the other elements in next.
Step 2: Use a dictionary accumulator pattern (Links to an external site.) to count how many times each term has a positive result on each line. Print the final dictionary result for visual inspection. For example, my solution using the Three Years in Europe gives the following result: {‘oxford’: 11, ‘paris’: 48, ‘london’: 50}. Remember we are counting the number of lines, not the number of instances.
Step 3: Write out all the line results for each term to separate files. Name the files ‘[term]-results.txt’. You’ll need to do an outfile pattern somewhere in our loop structure. (outer? inner? that’s for you to figure out) For example, my solution would make oxford-results.txt, cat-results.txt, and london-results.txt. These files should automatically be created based on the search terms list you make at the beginning of the program. This means I should be able to add/remove terms to that search list and have the program automatically adjust without having to add extra outfile lines within the code.
You will need to upload one py file containing your code and one docx/pdf file for your narrative.

Last Completed Projects

topic title academic level Writer delivered