【问题标题】:Find words with the same length [python 3 -enchant lib]查找长度相同的单词[python 3 -enchant lib]
【发布时间】:2018-12-06 18:25:50
【问题描述】:

强调文本我正在创建一个自动更正程序,它可以在一段文本中找到拼写错误的单词,然后提供建议。到目前为止,代码可以找到拼写错误的单词,但它可以提供建议。我正在考虑使用的策略之一是找到具有相似长度的单词(具有相同数量的字母但 +1 或 -1 字母)然后它可以建议它。一个例子是,如果用户输入“teh lion is sleep”,意思是“狮子在睡觉”,程序将搜索具有相同长度的单词,找到“the”,然后建议它。如果我使用单词列表,这将非常容易,但我正在使用图书馆附魔,因为它包含所有英文单词,但我找不到使用图书馆查找相同长度的单词的方法。此外,我知道它具有自动更正的内置功能,但我不允许使用它。提前谢谢你们。此外,我尝试尽可能格式化我的问题,如果由于我正在处理代码没有记录代码,我很抱歉。

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)

【问题讨论】:

标签: python autocorrect


【解决方案1】:

我不知道 enchant lib 做了什么,但我会在 python3+ 中这样做

假设你在一个名为 word 的变量中有一个字符串

word = "hello"

您可以使用 len(str) 检查该单词的长度:len(word)

print(len(word))
>>>5

然后,您可以使用 for 循环检查列表中长度与您的单词匹配的每个单词。假设您在一个名为 listofwords 的列表中有一堆单词

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

在这个 for 循环中,它检查列表 'listofwords' 中的任何项目是否与字符串 word (5) 具有相同的长度,如果是,它会打印回所述项目。

【讨论】:

  • 谢谢你,我编辑了它,你能再看看吗?问题是我使用的不是列表而是库。如果你能帮助我,那将非常有帮助
  • 我会检查 enchant lib 的基础知识,稍后再为您提供更具体的答案
猜你喜欢
  • 2021-04-27
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 2019-07-19
  • 2021-12-29
相关资源
最近更新 更多