This weeks assignment focuses on working with files lists lo

This week\'s assignment focuses on working with files, lists, loops, and strings in Python. Your goal is to write a simple Python program that will search for a specified keyword in a text file, and return the results as described below. For (up to) the first 5 occurrences of the keyword, print the entire sentence the keyword appears in. The end of a sentence is denoted by the \'.\' (full stop/period) character. You may ignore additional occurrences of the keyword in a sentence; we only require you to find and print the first. Copy the following into a separate text file and save it as a 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 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 out 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.\" Reference Materials Zelle text: Strings: Ch. 5 sec. 5.1 - 5.2, 5.5 - 5.6, 5.9 (pp121-128, 134-141, 152) Functions: Ch. 6 sec. 6.1 - 6.5.1 (pp 167-183) Flow Control: Ch. 7 sec. 7.1 - 7.4 (pp201-215) Official Python documentation: Strings, lists, slicing and indexing: [https://docs.python.org/3.4/tutorial/introduction.html#strings] Control flow: [https://docs.python.org/3.4/tutorial/controlflow.html]

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.

 This week\'s assignment focuses on working with files, lists, loops, and strings in Python. Your goal is to write a simple Python program that will search for
 This week\'s assignment focuses on working with files, lists, loops, and strings in Python. Your goal is to write a simple Python program that will search for

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site