Question

In: Computer Science

In this exercise, you will build a basic spell checker. Write a module called spell_checker.py. Your...

In this exercise, you will build a basic spell checker.

Write a module called spell_checker.py. Your module should contain the following functions:

  1. word_correct(string) -> string
    word_correct() takes in a string consisting of one word. It searches for the line in aspell.txt containing that word and replaces it with
    the first word present in the file. If the word is not in the file, it returns the same word back.

    For example,
    word_correct("cookie") = "cookie"
    word_correct("acommadate") = "accommodate"
    word_correct("basically") = "basically"

    Note that if the correction does not exist in aspell.txt, you will still have an incorrect word.
  2. line_correct( string) -> string
    line_correct() takes in a string consisting of a line of words. It goes through each word in the line and corrects it using word_correct().

    For example,
    line_correct("can you acommadate") = "can you accommodate"

  3. file_correct ( string) -> nothing
    line_correct() takes in a string that is the name of a file. It goes through each line in the file and corrects any spelling errors. It prints the results to a "corrected" txt file. For example, if the file's name is "file.txt", the corrected file should be called "file_corrected.txt"

You may use "blurb.txt" to test out your module.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#important notes

for the below code to work properly, you must ensure following things:

1) aspell.txt file must be present on the same directory
2) aspell.txt file must be valid (exactly in the format specified)
3) blurb.txt file must be present on the same directory
4) there should not be any punctuations in blurb.txt

failure to add aspell.txt and blurb.txt files will result in FileNotFoundException and that is not a mistake from my side..

#code

#spell_checker.py

def word_correct(word):
    # opening aspell.txt file, make sure file exist on the same directory
    file = open('aspell.txt', 'r')
    # looping through each line in file
    for line in file:
        # removing trailing newline and splitting line into words (by white space)
        words = line.strip().split(" ")
        # if word is in resultant list, closing file, returning first word on the list
        if word in words:
            file.close()
            return words[0]
    # if not found, closing file and returning word unchanged
    file.close()
    return word


def line_correct(line):
    corrected = ''
    # looping through each word in line
    for word in line.split():
        # appending corrected word to corrected string followed byt a space
        corrected += word_correct(word) + " "
    # returning corrected after removing stray " " at the end
    return corrected.rstrip(' ')


def file_correct(filename):
    # creating output file name. for example, if input file name is "data.txt", output file name
    # will be "data_corrected.txt"
    outputfilename = filename[0:filename.rfind('.')] + "_corrected" + filename[filename.rfind('.'):]
    # opening input file and output file
    inf = open(filename, 'r')
    outf = open(outputfilename, 'w')
    # looping through each line
    for line in inf:
        # correcting current line, writing to outf
        outf.write(line_correct(line.strip()) + "\n")
    # closing both file, saving changes
    inf.close()
    outf.close()


# code for testing with "blurb.txt" file
# if everything works correctly, "blurb_corrected.txt" file will be generated.
if __name__ == '__main__':
    file = 'blurb.txt'
    file_correct(file)

Related Solutions

Write a Class called Module with the following attributes: module code, module name, list of lecturers...
Write a Class called Module with the following attributes: module code, module name, list of lecturers for the module (some modules may have more than one lecturer – we only want to store their names), number of lecture hours, and module description. Create a parameterised (with parameters for all of the class attributes) and a non-parameterised constructor, and have the accessor and mutator methods for each attribute including a toString method. Write a class Student with the following attributes: student...
[10, 3, 15, 18] 4. Password Checker Write a fuction called passwordChecker() that asks the user...
[10, 3, 15, 18] 4. Password Checker Write a fuction called passwordChecker() that asks the user to input a string and checks if the string can be used as a password. Loop until the user inputs an appropriate password. A password must satisfy the following conditions: (a) Must contain at least 12 characters (b) Must contain one special symbol from string.punctuation [HINTS: use the string module; use the keyword in ] (c) Must contain at least one lower-case letter (d)
Your task in this exercise is to write a function called get_playlist_tracks(database_filename, which_playlist), which formulates a...
Your task in this exercise is to write a function called get_playlist_tracks(database_filename, which_playlist), which formulates a SELECT statement that returns track name, album title, genre name, artist name and track composer for all tracks in the database which are associated with a given playlist. This statement requires several nested INNER JOINs to pull together data from the "tracks", "albums", "genres", "artists", and "playlists" table. The default ordering of tracks should be used. The result of the function is a list...
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module.
Design and implement a C++ class called Module that handles information regarding your assignments for a specific module. Think of all the things you would want to do with such a class and write corresponding member functions for your Module class. Your class declaration should be well-documented so that users will know how to use it.Write a main program that does the following: Declare an array of all your modules. The elements of the array must be of type Module....
Overview and objective: Basic logic In this homework, you will exercise your understanding of boolean logic...
Overview and objective: Basic logic In this homework, you will exercise your understanding of boolean logic and the gates and circuits that embody it. A thorough understanding of boolean logic is extremely useful for almost all programming tasks and necessary to correctly implement complex systems. Additionally, greater familiarity with boolean logic should improve one’s ability to think critically in general as it forms the basis for all human reasoning. Technical Description and Instructions: 1. Consider the logical expression ( !x...
In this problem, we'll write a Python module that defines two things publicly. A namedtuple called...
In this problem, we'll write a Python module that defines two things publicly. A namedtuple called Student with two fields, scores and grade, intended to carry grade-related information about a student. (This will be part of the output of this problem.) So that we're all in agreement about what that will look like, here it is: Student = namedtuple('Student', ['scores', 'grade']) A function named build_grade_report, which takes two parameters: a Path object describing the path to a file containing the...
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
For this assignment, you can build on the 9th exercise that you made for the Assignment...
For this assignment, you can build on the 9th exercise that you made for the Assignment 1. So, you have a file named “band.html”. Create 2 more web pages besides band.html. All have to be in the same folder. You can redesign your band.html web page. Name each file as you wish (e.g., album names, tour, contact). They should also appear in the navigation bar. You can name your home page as Home in the navigation bar. Create an external...
CSC 466 Spring 2017 Assignment:: A6 Basic Type System --------------------------------------------------------------- Your assignment: Build the basic type...
CSC 466 Spring 2017 Assignment:: A6 Basic Type System --------------------------------------------------------------- Your assignment: Build the basic type system - type set at declaration - consult sym-tab on var usage - build type of expression based on parts - LHS = RHS types should match ** Also make sure vars are declared of course Note:: we started this in class -- start with the project you already have from /tmp/466T/ic0315.tar.gz that we worked on in class on Wed 3/15 ************************************************************* Kinds of...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT