Question
import re def read(filename): with open(filename, 'r', encoding='utf-8') as file: return file.read() def parse(text): words = re.findall(r'bw+b', text.lower()) return words def analyze(list1,
import re\ \ def read(filename):\ with open(filename, 'r', encoding='utf-8') as file:\ return file.read()\ \ def parse(text):\ words = re.findall(r'\\\\b\\\\w+\\\\b', text.lower())\ return words\ \ def analyze(list1, list2, stopwords):\ set1 = set([word for word in list1 if word not in stopwords])\ set2 = set([word for word in list2 if word not in stopwords])\ common_words = sorted(set1.intersection(set2))\ return common_words\ \ if __name__ == '__main__':\ text1 = read("text1.txt")\ text2 = read("text2.txt")\ text3 = read("text3.txt")\ \ list1 = parse(text1)\ list2 = parse(text2)\ list3 = parse(text3)\ \ stopwords = {'a', 'an', 'as', 'and', 'all', 'are', 'at', 'also',\ 'be', 'by',\ 'even',\ 'from',\ 'in', 'is', 'it', 'its',\ 'may', 'most', 'many',\ 'on', 'of', 'off', 'or',\ 'some',\ 'to', 'the', 'this', 'these', 'that', 'their', 'them', 'they', 'then',\ 'was', 'were', 'which'\ }\ \ common1 = analyze(list1, list2, stopwords)\ common2 = analyze(list1, list3, stopwords)\ \ print(f'The common words in text1.txt and text2.txt are:')\ for word in common1:\ print(word)\ \ print(f'\ The common words in text1.txt and text3.txt are:')\ for word in common2:\ print(word)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started