Your goal is to write a simple Python program that will sear

Your goal is to write a simple Python program that will search for a specific keyword in a text file, and return the results as described below.

For (up to) the first 5 occurances of the keyword, print the entire sentence the keyword appears in. The end of a sentence is denoted by the \'.\' (period) character. You may ignore additional occurances of the keyword in a sentence; we only require you to find and print the first.

Copy the following into a text file and save as plain text document:

\"Python is an interpreted, object-oriented, high level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it a very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Pyton\'s simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code, reuse. Often programmers fall in love with Python because of the increased productivity it provides.\"

You should print something like:

Searching for keyword \"Python\":

Match 1: \"Python is an interpreted, object-oriented, high level programming language with dynamic semantics.\"

Match 2 : \"Python supports modules and packages, which encourages program modularity and code, reuse.\"

Match 3: \"Often programmers fall in love with Python because of the increased productivity it provides.\"

Note: In the paragraph, Pyton is meant to be mispelled.

Solution

from fnmatch import *

# !!!!!!!!!!!function for reading a text file!!!!!!!!!!!!
def readFileAsText(infile):
# empty the placeholder string
file_text = \"\"
# open the file in \'readtext\' mode i.e., \'rt\'mode
with open(infile,\'rt\') as file:
# read and return entire file as a single string
file_text = file.read()
# return the new generated string
return file_text

# main function
def main():
print(\"This program will search for 5 occurrences of the keyword from a text file...!!!!!!!!\ \")
  
inp_file = \"sample.txt\"
#sample.txt is our text file or you can also declare your file name in command line by inp_file = input(\"Type file path: \")
keyword = \"Python\" #this is the keyword which we have to search
max_result = 5
inBodyCounter = 1
sentences=\'\'

# call your readFileAsText method, store the result in `corpus`
corpus = readFileAsText(inp_file)
# remove the empty lines from the text
corpus = [line for line in corpus.split(\'\ \') if line.strip()]
# join the paragraphs as a block of sentences
for i in range(len(corpus)):
sentences = sentences + corpus[i]

print(\'Searching for the keyword \"{}\".\ \'.format(keyword))
# split loaded text into a period-delimited sentence.
sentences = sentences.split(\".\")
# iterate through sentence one at a time
for sentence in sentences:
# if counter is less than \'max_result+1\' i.e. here 6, continue iterating
if inBodyCounter <= max_result:
# split the sentence into words to compare with keyword
words = sentence.split()
# iterating through each word in sentence
for word in words:
# It checks for lower case word to lower case keyword.
if word.lower()==keyword.lower():
print(\"Match\", \'{}:\'.format(str(inBodyCounter)), \'\"{}.\"\'.format(sentence.lstrip()))
inBodyCounter+=1
# break words in words
break
# if nothing found in first sentence if compare by adding ?? wildcard char to word to capture Python\'s
elif fnmatch(word.lower(), str(keyword.lower()) + \'??\'):
print(\"Match\", \'{}:\'.format(str(inBodyCounter)), \'\"{}.\"\'.format(sentence.lstrip()))
inBodyCounter+=1
# break words in words
break
main()

Your goal is to write a simple Python program that will search for a specific keyword in a text file, and return the results as described below. For (up to) the
Your goal is to write a simple Python program that will search for a specific keyword in a text file, and return the results as described below. For (up to) the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site