Advertisement
BERKYT

search for typos in words

Nov 29th, 2023
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. words = [
  2.     'tim',
  3.     'for',
  4.     'print',
  5.     '1234567890',
  6.     '321',
  7.     '000',
  8. ]
  9.  
  10.  
  11. def check_word(operator: str):
  12.     probability_map = {}
  13.  
  14.     for word in words:
  15.         coincidence = 0
  16.         len_word = len(word)
  17.         len_operator = len(operator)
  18.  
  19.         if len_word == len_operator:
  20.             for op_symbol, w_symbol in zip(operator, word):
  21.                 if w_symbol == op_symbol:
  22.                     coincidence += 1
  23.         else:
  24.             continue
  25.  
  26.         if coincidence == 0:
  27.             continue
  28.  
  29.         probability_map[word] = 100 / len_word * coincidence, coincidence
  30.  
  31.     if probability_map:
  32.         possible_word = sorted(probability_map)[0]
  33.  
  34.         percent_probability, coincidence = probability_map[possible_word]
  35.  
  36.         if percent_probability > 75 or len(possible_word) - coincidence <= 2:
  37.             return possible_word
  38.     else:
  39.         return
  40.  
  41.  
  42. if __name__ == '__main__':
  43.     for word in ['ti2', 'fi2', '123', '1234567990', 'prinr', 'Ti2', 'ris', 'qqq']:
  44.         possible_word = check_word(word)
  45.         if possible_word is not None:
  46.             print(f'Вы написали: "{word}", возможно Вы имели ввиду: {possible_word}')
  47.         else:
  48.             print(f'Слова {word} нет в словаре!')
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement