Ryan
Ryan

Reputation: 155

Creating a autocorrect and word suggestion program in python

def autocorrect(word):
    Break_Word   = sorted(word)
    Sorted_Word  = ''.join(Break_Word)
    return Sorted_Word

user_input = ""

while (user_input == ""):
 user_input = input("key in word you wish to enter: ")

user_word = autocorrect(user_input).replace(' ', '')




with open('big.txt') as myFile:
    for word in myFile:

        NewWord       = str(word.replace(' ', ''))
        Break_Word2  = sorted(NewWord.lower())
        Sorted_Word2 = ''.join(Break_Word2)
        if (Sorted_Word2 == user_word):
            print("The word",user_input,"exist in the dictionary")

Basically when I had a dictionary of correctly spelled word in "big.txt", if I get the similar from the user input and the dictionary, I will print out a line

I am comparing between two string, after I sort it out

However I am not able to execute the line

if (Sorted_Word2 == user_word):
            print("The word",user_input,"exist in the dictionary")

When I try hard code with other string like

  if ("a" == "a"):
            print("The word",user_input,"exist in the dictionary")

it worked. What wrong with my code? How can I compared two string from the file?

Upvotes: 0

Views: 2881

Answers (1)

user3556757
user3556757

Reputation: 3619

What does this mean? Does it throw an exception? Then if so, post that...

However I am not able to execute the line

if (Sorted_Word2 == user_word): print("The word",user_input,"exist in the dictionary")

because I can run a version of your program and the results are as expected.

def autocorrect(word):
    Break_Word   = sorted(word)
    Sorted_Word  = ''.join(Break_Word)
    return Sorted_Word

user_input = ""

#while (user_input == ""):
user_input = raw_input("key in word you wish to enter: ").lower()

user_word = autocorrect(user_input).replace(' ', '')
print ("user word '{0}'".format(user_word))


for word in ["mike", "matt", "bob", "philanderer"]:

    NewWord       = str(word.replace(' ', ''))
    Break_Word2  = sorted(NewWord.lower())
    Sorted_Word2 = ''.join(Break_Word2)
    if (Sorted_Word2 == user_word):
        print("The word",user_input,"exist in the dictionary")

key in word you wish to enter: druge user word 'degru' The word druge doesn't exist in the dictionary

key in word you wish to enter: Mike user word 'eikm' ('The word','mike', 'exist in the dictionary')

Moreover I don't know what all this "autocorrect" stuff is doing. All you appear to need to do is search a list of words for an instance of your search word. The "sorting" the characters inside the search word achieves nothing.

Upvotes: 1

Related Questions