Untitled

 avatar
unknown
plain_text
a year ago
1.5 kB
5
Indexable
#Write your code here
def correct_word(word):
  corrected_word = ""
  consecutive_count = 1
  for i in range(len(word)):
    if word[i].isalpha() or word[i].isspace():  # Include space characters
      if i > 0 and word[i] == word[i - 1]:
        consecutive_count += 1
        if consecutive_count >= 2:
          continue
      else:
        consecutive_count = 1
      corrected_word += word[i]
# Remove special characters from the end of the word
  while corrected_word and not corrected_word[-1].isalpha() and len(
      corrected_word) > 1:
    corrected_word = corrected_word[:-1]
  print(corrected_word, word, "returning")
  # Check if the word has only one character left after corrections
  if len(corrected_word) == 1:
    return ""


# Check if the word is misspelled
  # if len(corrected_word) > 1 and corrected_word[0].isupper():
  #   if corrected_word.lower() != "correct":
  #     return ""
  print(corrected_word, word, "returning")
  return corrected_word


def naive_spell_checker(sentence):
  corrected_sentence = ''
  for word in sentence.split():
    corrected_word = correct_word(word)
    if corrected_word:
      # Check if corrected word is not empty
      corrected_sentence += corrected_word + " "
    else:
      corrected_sentence += word + " "
  return corrected_sentence.strip()


# If word is empty, retain the original word
input_sentence = 'Thiiis is a missta%ke, please prrrint the corre$ct senten5ce'
corrected_sentence = naive_spell_checker(input_sentence)
print(corrected_sentence)
Editor is loading...
Leave a Comment