pythonautocorrect

Find words with the same length [python 3 -enchant lib]


emphasized textI am creating an autocorrect program that finds misspelled words in a piece of text and then provides suggestions. Until now the code can find misspelled words but it can provide suggestions. One of the strategies I was thinking to use is to find words with similar length(have the same number of letters but +1 or -1 letter) then it can suggest it. An example for that will be if the user entered "teh lion is sleeping" meaning "the lion is sleeping" the program will search for words with the same length finding "the" and then suggesting it. this would have been very easy if I using a word list but I am using the library enchant since it has all of the English words but I can't find a way to use the library to find words with the same length. Additionally, I am aware it has a builtin feature to autocorrect but I am not allowed to use that. Thank you guys in advance. Also I tried to format my question as possible and sorry if the code is not documented since i am working on that.

wrong_words = []
while True:
  import enchant
  there_is_a_mistake = False
  punctuation = set("!#$%&()*+,./:;<=>?@[\]^_`{|}~"+'”' + '"' + '“')
  dictionary = enchant.Dict("en_US")
  keyboard_input = input()
  words_without_special_characters = "".join(x for x in keyboard_input if x not in punctuation) # https://www.youtube.com/watch?v=llf_pTNhYa4
  words_without_special_characters = words_without_special_characters.replace("-" and "—" and "-" and "–" and "-"," ")
  words_without_spaces_and_special_characters = words_without_special_characters.split()
  number_of_words = len(words_without_spaces_and_special_characters)
  for x in range(0,number_of_words):
    checked_words = dictionary.check(words_without_spaces_and_special_characters[x])
    while checked_words == False:
      read = open("saved_words.txt","r")#opens the file in reading mode.
      read_file = read.readlines()#reads from the file.
      read_file = [item.replace("\n","") for item in read_file]
      if words_without_spaces_and_special_characters[x] in read_file: # checks if the word is savedd in the clint word list
        break
      read.close()#closes the file reading for another operation.
      print(words_without_spaces_and_special_characters[x])
      words_without_spaces_and_special_characters[x].append(wrong_words)
      print("false")
      there_is_a_mistake = True
      break
  if there_is_a_mistake == True:
    keyboard_input_yes_or_no_to_save_words = input("if you want to save a word to your dictionary type :yes. (press enter if you dont want to)")
    if keyboard_input_yes_or_no_to_save_words == "yes":
      while True:
        keyboard_input_to_save_words = input("which words do you want to save? (type ignore if you dont want to)")
        keyboard_input_to_save_words = keyboard_input_to_save_words.split()
        check_if_word_was_orginally_written = set(keyboard_input_to_save_words) <= set(words_without_spaces_and_special_characters)
        if check_if_word_was_orginally_written == True:
          file = open("saved_words.txt","a+") #opens the file in the writing mode.
          for i in range(0,len(keyboard_input_to_save_words.split())):
            file.write(keyboard_input_to_save_words[i]) # writes information in the other file.
            file.write("\n") # creates new line.
          file.close()
          break
        if keyboard_input_to_save_words == "ignore":
          print("nope")
          break
        else:
          print("no words found. Try again. (type ignpre if you dont want to save any word)")
for i in range (len(wrong_words)):
  length_of_wrong_word = len(wrong_words)

Solution

  • I have little idea of what enchant lib does, but i'd do it this way in python3+

    let's say you have an string in a variable called word

    word = "hello"
    

    You can check the length of that word using len(str): len(word)

    print(len(word))
    >>>5
    

    You can then use a for loop to check every word in a list with a length matching the one of your word. let say you have a bunch of words in a list called listofwords

    for item in listofwords:
       if len(item) == len(word):
           print(item)
    

    in this for loop, it checks if any item in the list 'listofwords' has the same length as the string word (5) if it does, it prints back said item.