【发布时间】: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)
【问题讨论】:
-
欢迎来到 SO,OP。请包含一个minimal, verifiable, complete 到目前为止您尝试过的代码示例,并通读asking guide
-
嘿,伙计!欢迎来到 SO。请看:How to create a Minimal, Complete, and Verifiable example
-
我们在这里吃代码,你赤手空拳...
-
各位,我编辑了,你能再看看吗
标签: python autocorrect