Question
Write a Python program that given a keyword and a text file, will print out all the contexts where the keyword appears in the file.
Write a Python program that given a keyword and a text file, will print out all the contexts where the keyword appears in the file. We define a context of a keyword as the two words that precede it and the two words that immediately follow it, if they exist. For example, if the given keyword is "keyword" and the input file contains the description of this project, then its contexts should include
"given a keyword and a"
"where the keyword appears in"
"precede the keyword and the"
"the given keyword is "keyword""
Your program should take the file name as a command line argument. It should repeatedly ask for a new keyword from the user until 'Q' is entered to terminate the program. When a keyword is given, your program should output in the console, all of the contexts where this keyword appears in the input file, one per line.
You are required to use a dictionary that maps each keyword to the collection of its contexts.
To test your solution, create three small input files for which you know what the expected output should be. Once this is done successfully, test your program with the bible file that I post with this assignment to ensure that your program still works and that it is fast enough.
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