Question
python problem
def multi_search(pat_file : open, text_file : open) -> [(int,str,[int])]:
# Testing compare_files
==-->multi_search(open('pats1.txt'),open('texts1.txt'))-->[(1, 'See Spot.', [1, 2]), (2, 'See Snoopy', [1]), (3, ' run.', [2]), (5, 'Run dogs run.', [1, 2, 3]), (6, 'Dogs are great.', [1, 2, 3])]
4. (6 pts) Write a function named multi search, that takes two open files as arguments and returns a list of 3-tuples as a result (based on the information in both files). The multi search function reads the first file, interpreting each line in it as a regular expression pattern. Then it reads the second file, and for each line in the file, it produces a 3-tuple in the returned list, but only if the line matches (using re.search: meaning the match is not required to begin at the start of the line) one or more of the regular expression patterns read from the first file. Each 3-tuple contains the line number of the text matched in the second file (starting at line 1), the line of text itself from the second file, and a list of all the pattern line numbers (starting at line 1) from the first file that matched it: the list should show these numbers in ascending order (which can be done without sorting). Close both files right before the function finishes For example if the files patsl.txt and texts1.txt store the information shown at the bottom of this page, then calling the function as multi search (open (pats1.txt'), open (textsl.txt')) returns the following list: [(1, 'See Spot.', [1, 2]), (2, 'See Snoopy', [1]), (3, ' run. ', [2]), (5, Run dogs run.', [1, 2, 3]), (6, 'Dogs are great.', [1, 2, 3])] Hint: Call rstrip for each line read from each file; I used comprehensions, calls to enumerate, and the re.compile function (for efficiency, since the same patterns never change) patsl.txt textsI.txt A[A-Z] See Spot. See Snoopy run. (pause) Run dogs run. Dogs are great.
Explanation / Answer
import re
def multi_search(patternFile, textFile):
patterns = []
for pattern in patternFile:
patterns.append(pattern.rstrip())
i = 1
result = []
for line in textFile:
matching_pattern = []
line = line.rstrip()
for j in range(0, len(patterns)):
if re.search(patterns[j], line):
matching_pattern.append(j+1)
if len(matching_pattern) != 0:
result.append((i, line, matching_pattern))
i += 1
patternFile.close()
textFile.close()
return result
print(multi_search(open('pats1.txt'),open('texts1.txt')))
#-->[(1, 'See Spot.', [1, 2]), (2, 'See Snoopy', [1]), (3, ' run.', [2]), (5, 'Run dogs run.', [1, 2, 3]), (6, 'Dogs are great.', [1, 2, 3])]
# copy pastable code link: https://paste.ee/p/QeqpE