This weeks assignment focuses on working with files lists lo
Solution
# import module for wildcard comparison
from fnmatch import *
# function for read file as text
def readFileAsText(infile):
# empty placeholder string for our file contents
file_text = \"\"
# open the file in \'rt\' mode: \'r\' for read and \'t\' for text.
with open(infile,\'rt\') as f:
# read and return entire file as a single string
file_text = f.read()
# return the now populated string
return file_text
# main function
def main():
print(\"This program searching for 5 occurrences of the keyword from a text file...\ \")
# define some variables for use below:
# allow inputs for testing purposes
# input_file = input(\"Type file path: \")
# keyword = input(\"Enter keyword: \")
input_file = \"hw4-sample-text.txt\"
keyword = \"Python\"
max_results = 5
inBodyCounter = 1
sentences=\'\'
# call our readFileAsText method, store result in `corpus`
corpus = readFileAsText(input_file)
# remove empty lines from text
corpus = [line for line in corpus.split(\'\ \') if line.strip()]
# join the paragraphs back together as a block of sentences
for i in range(len(corpus)):
sentences = sentences + corpus[i]
print(\'Searching for keyword \"{}\".\ \'.format(keyword))
# split the loaded text into period-delimited sentences.
sentences = sentences.split(\".\")
# iterate through sentence one at a time
for sentence in sentences:
# if counter is less than 6 continue iterating
if inBodyCounter <= max_results:
# split sentence into words to compare with keyword
words = sentence.split()
# iterate through each word in sentence before moving to next sentence
for word in words:
# checks 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 for loop on first keyword find
break
# if nothing found in first 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 for loop on first keyword find
break
main()
hw4-sample-text.txt
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 very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python\'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. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.
Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program doesn\'t catch the exception, the interpreter prints a stack trace. A source level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying to Python\'s introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.

