-1

I need to write a python spell checker that accepts a string and a list. it compares each word in the string to each word in the list, and returns the result if the word in the list is one character added, subtracted, or deleted. I cannot find out how to make my variable x different each time. Here is the code:

def spelling_corrector(q,r):
    def single_insert_or_delete(s1,s2):
        s1,s2 = s1.lower(), s2.lower()
        count = 0
        if s1 == s2:
            return 0
        elif len(s1) == len(s2):
            return 2
        elif len(s1) - len(s2) == -1:
            if s1 == s2[:-1]:
                return 1
            else:
                for i in range(len(s2)):
                    if s1 == s2[:i] + s2[i+1:]:
                        return 1
                    else:
                        return 2
        elif len(s1) - len(s2) == 1:
            if s1[:-1] == s2 or s1[1:] == s2:
                return 1  

        else:
            return 2
    for word in q:
        word.lower()
        for correct in q:
            correct.lower()
            if single_insert_or_delete(word, correct) == 1:
                x = q.replace(correct)

Any help on this?

3
  • Isn't q supposed to be a sentence? (This seems to be very similar to this question.) Commented Feb 28, 2016 at 4:34
  • yes, q is a sentence, and split() makes it a list of words Commented Feb 28, 2016 at 16:03
  • @Auston Hastings: your answer does not seem to work!! Commented Feb 28, 2016 at 16:26

1 Answer 1

0

I see a number of issues with the loop code at the bottom of your outer function.

To start with, you're looping over q, which I think is supposed to be a string containing any number of words. If you iterate directly on a string like this, you'll get individual characters, which I don't think you want. You probably should be using q.split() or something similar to break up the string into a list of word strings.

The next issue is that you're calling word.lower() (and later correct.lower()) but not doing anything with the return value. Strings in Python are immutable, so methods like lower return a new string with the requested changes, rather than modifying the existing string in place. You probably want word = word.lower() (and correct = correct.lower()). But this may have issues, as the changed capitalization may make the replace call later not work properly. A better approach may be to treat case as significant and just remove the lower calls.

A third issue is that your second loop is on q again, rather than being on r, which I think is supposed to be the list of correctly spelled words. (Note, you should also improve your variable names, so their meanings are obvious.) You probably want: for correct in r:

The final two (intertwined) issues are, I think, the ones you're actually asking about in the question. The first problem with your replace call in the last line is that you're only passing one argument. str.replace expects two (in addition to the instance it's being called on), so that won't work. Use replace(word, correct) to replace each occurrence of the substring word with correct (though more on why this might not be the right thing to do later).

The other issue is the variable x. You don't use x anywhere else in the code, so I don't know what you intend it to be used for. If you're just trying to make a new string with the replacements in it, I'd suggest overwriting the q variable with the return value of the replace call:

q = q.replace(word, correct)

Then just add return q at the bottom of the function.

Note, your function will still do the wrong thing in many cases, but fixing them will require a larger redesign. For one example of an error, if you have a string like "an" as q, and your dictionary contains ["and", "an"] (with the longer word before the smaller one), your code will assume the an word string is a misspelled version of and and replace it. You probably need to check first if the word is correctly spelled (perhaps with word in r) before checking if it's one character off any other words.

Another situation it will get wrong is when a misspelled word appears as a prefix in another word (which may or may not be spelled wrong itself). Try fixing "foo foobar" with a word list of ["food"] and you'll get "food foodbar", since the replace("foo", "food") call had no respect for word boundaries.

Sign up to request clarification or add additional context in comments.

1 Comment

it says that in line 26: object of type bool has no len.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.